rzymek
rzymek

Reputation: 876

How does DOM API affect HTML parsing?

During reading how the DOM tree is created HTML5 spec: 8.2.1 Overview of the parsing model, a question came into my mind and I could not find the answer.

According to the spec using document.write is bad and apart from other issues, it will pause parsing and supply new characters into tokenizer input.

The question is how does it look like when one use DOM API, eg. appendChild. When will it be executed?

As far as I understand, this will add already created Node so the parsing and tokenization is not needed. However does the new node will be attached immediately or after constructing the "first/original" DOM tree?

Upvotes: 2

Views: 52

Answers (1)

Alohci
Alohci

Reputation: 83126

It happens immediately, to the DOM in what ever state it is in at the time. The DOM document always forms a consistent tree, so it can always be added to safely.

Appending elements has always been pretty harmless in that respect, but removing elements from the DOM document while parsing was ongoing, used to be a good way to crash older IE versions. In fact, that problem continued to some degree right up until IE implemented the HTML5 parsing algorithm in IE10.

Upvotes: 3

Related Questions