While developing a tool (which I don't consider important detailing here, on the question, given that I was able to develop the MCVE's below), I noticed that, at least in the Chrome and Firefox versions that I have on my desktop, the string I get from the innerHTML
attribute is not equal to the original source code I wrote statically on the HTML file.
console.log(document.querySelector("div").innerHTML);
/*
<table>
<tbody><tr>
<td>Hello</td>
<td>World</td>
</tr>
</tbody></table>
*/
<div>
<table>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</table>
</div>
As you may have noticed, a spontaneous <tbody>
tag (which I have not added to my HTML source!) came out, aparently due to preprocessing some time in between the page download and the page onload event. In this particular case, for my application purposes, this modification doesn't generate an error and could thus be ignored.
Turns out that, in certain cases, this sort of alteration can be catastrophic, specially when all the markup is removed, like in the example below.
console.log(document.querySelector("div").innerHTML);
/*
Hello
World
*/
<div>
<td>Hello</td>
<td>World</td>
</div>
Obviously, in this case the original markup has issues, but in my application, "misuses" (like a <td>
inside a <div>
) are accepted. What is not accepted is the innerHTML
being left with no HTML markup at all, which leads to the main question: how can I get the original, statically coded HTML markup for the <div>
element?
Also, if possible, it would also be nice to know why and how this phenomenon occurs, because I'm curious :D