Get rid of most innerHTML and use documentFragment for some tasks

This patch takes care of sanitising innerHTML as per issue https://bugzilla.mozilla.org/show_bug.cgi?id=837740. It makes use of `textContent` and `DocumentFragment` in some cases, which also improves the speed of these operations. Some other small fixes in code and comments have been made.