Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it normal that JavaScript can create otherwise invalid DOM?

Somewhat by accident, I found out that a span inserted directly inside a tbody stays in place when done with JavaScript (insertBefore), where such invalid DOM would if created with literal HTML lead to the span being placed before the entire table.

I expected either the same behaviour as with literal HTML or some DOM Exception being thrown.

E.g. this HTML

<table>
    <thead><tr><th>Table Header</th></td></thead>
    <tbody>
        <span>from HTML &rarr; goes up</span>
        <tr><td>Table Contents</td></tr>
    </tbody>
</table>

with this JavaScript:

var span = document.createElement('span'),
    tbody = document.querySelector('tbody');
span.innerHTML = 'Created with JS &rarr; stays in place';
tbody.insertBefore(span, tbody.querySelector('tr'));

renders "Created with JS → stays in place" between the header and the first row; the original, literal, span moves outside of the table.

Is this normal, and can/should I count on this? (It behaves the same in FF, Chrome, Opera, IE >= 9 (not tested below)).

Also, is there a way to query the DOM whether content of a certain type would (under normal circumstances) be valid at a certain point in the DOM? This is actually what I wanted to do when I found out about this quirk (which it is, imho).

The fiddle is here: http://jsfiddle.net/xr37g9kw/2/

like image 684
pft Avatar asked Nov 09 '15 16:11

pft


People also ask

Does JavaScript manipulate the DOM?

The DOM stands for Document Object Model. It can simply be understood as a tree of nodes created by the browser. Each of these nodes has its own properties and methods which can be manipulated using JavaScript. The ability to manipulate the DOM is one of the most unique and useful abilities of JavaScript.

Is DOM important in JavaScript?

It allows a language (JavaScript) to manipulate, structure, and style your website. After the browser reads your HTML document, it creates a representational tree called the Document Object Model and defines how that tree can be accessed.

Is DOM a JavaScript object?

So yes : in the browser's context, DOM objects are JS objects, this is not reciprocal of course. But DOM API is not exclusive to Javascript, it defines interfaces which can be implemented in any languages, for instance Python has a DOM API too and in this case, DOM objects are Python objects.

Is DOM API part of JavaScript?

DOM API is not part of the JavaScript language. They are separate entities. The DOM is just a set of functionalities that browser exposes via global object ( window ) to the scripts which are being executed in the browser environment. In other environments - such as node, the DOM API is not necessarily available.


1 Answers

As for "is this normal, and can/should I count on this?" Sadly, yes. But mostly you should be aware of the node types you are working with. NB, in case of table, there are a handful of not so well known DOM methods (HTMLTableElement.rows. InsertRow() and so on).

As for "is there a way to query the DOM whether content of a certain type would (under normal circumstances) be valid at a certain point in the DOM?" nothing built-in for this exact purpose, but you could exploit one native feature of JavaScript -> DOM API: you can let browser to re-parse HTML chunk in the "literal way". Yes, I am speaking about innerHTML.

In your fiddle, adding**tbody.outerHTML = tbody.outerHTML** "fixes" the structure, so you could hypothetically take some DOM node, look at its DOM tree, clone, "re-eval" it and compare with original.

like image 84
myf Avatar answered Sep 19 '22 14:09

myf