W3C

HTML 5

A vocabulary and associated APIs for HTML and XHTML

← 8.2.3. 8.2.4 TokenisationTable of contents8.4. 8.4 Serialising Serializing HTML fragments →

8.2.4. 8.2.5 Tree construction

The input to the tree construction stage is a sequence of tokens from the tokenisation stage. The tree construction stage is associated with a DOM Document object when a parser is created. The "output" of this stage consists of dynamically modifying or extending that document's DOM tree.

Tree construction passes through several phases. Initially, UAs must act according to the steps described as being those of the initial phase . This specification does not define when an interactive user agent has to render the Document so that it is available to the user, or when it has to begin accepting user input.

As each token is emitted from the tokeniser, the user agent must process the token according to the rules given in the section corresponding to the current insertion mode .

When the steps below require the UA to append insert a character to into a node, if that node has a child immediately before where the UA must collect it character is to be inserted, and all subsequent consecutive characters that would child is a Text node, then the character must be appended to that node, and insert one Text node; otherwise, a new Text node whose data is just that character must be inserted in the concatenation of all those characters. appropriate place.

DOM mutation events must not fire for changes caused by the UA parsing the document. (Conceptually, the parser is not mutating the DOM, it is constructing it.) This includes the parsing of any content inserted using document.write() and document.writeln() calls. [DOM3EVENTS]

Not all of the tag names mentioned below are conformant tag names in this specification; many are included to handle legacy content. They still form part of the algorithm that implementations are required to implement to claim conformance.

The algorithm described below places no limit on the depth of the DOM tree generated, or on the length of tag names, attribute names, attribute values, text nodes, etc. While implementators implementors are encouraged to avoid arbitrary limits, it is recognised recognized that practical concerns will likely force user agents to impose nesting depths.

8.2.4.1. 8.2.5.1. Creating and inserting elements

When the steps below require the UA to create an element for a token in a particular namespace, the UA must create a node implementing the interface appropriate for the element type corresponding to the tag name of the token in the given namespace (as given in the specification that defines that element, e.g. for an a element in the HTML namespace ,this specification defines it to be the HTMLAnchorElement interface), with the tag name being the name of that element, with the node being in the given namespace, and with the attributes on the node being those given in the given token.

The initial phase interface appropriate for an element in the HTML namespace that is not defined in this specification is HTMLElement .The interface appropriate for an element in another namespace that is not defined by that namespace's specification is Element .

When the steps below require the UA to insert an HTML element for a token, the UA must first create an element for the token in the HTML namespace ,and then append this node to the current node ,and push it onto the stack of open elements so that it is the new current node .

The steps below may also require that the UA insert an HTML element in a particular place, in which case the UA must follow the same steps except that it must insert or append the new node in the location specified instead of appending it to the current node .(This happens in particular during the parsing of tables with invalid content.)

When the steps below require the UA to insert a foreign element for a token, the UA must first create an element for the token in the given namespace, and then append this node to the current node ,and push it onto the stack of open elements so that it is the new current node .If the newly created element has an xmlns attribute in the XMLNS namespace whose value is not exactly the same as the element's namespace, that is a parse error .

When the steps below require the user agent to adjust foreign attributes for a token, then, if any of the attributes on the token match the strings given in the first column of the following table, let the attribute be a namespaced attribute, with the prefix being the string given in the corresponding cell in the second column, the local name being the string given in the corresponding cell in the third column, and the namespace being the namespace given in the corresponding cell in the fourth column. (This fixes the use of namespaced attributes, in particular xml:lang .)

Attribute name Prefix Local name Namespace
xlink:actuate xlink actuate XLink namespace
xlink:arcrole xlink arcrole XLink namespace
xlink:href xlink href XLink namespace
xlink:role xlink role XLink namespace
xlink:show xlink show XLink namespace
xlink:title xlink title XLink namespace
xlink:type xlink type XLink namespace
xml:base xml base XML namespace
xml:lang xml lang XML namespace
xml:space xml space XML namespace
xmlns (none) xmlns XMLNS namespace
xmlns:xlink xmlns xlink XMLNS namespace

The generic CDATA parsing algorithm and the generic RCDATA parsing algorithm consist of the following steps. These algorithms are always invoked in response to a start tag token.

  1. Create an element for the token in the HTML namespace .

  2. Append the new element to the current node .

  3. If the algorithm that was invoked is the generic CDATA parsing algorithm ,switch the tokeniser's content model flag to the CDATA state; otherwise the algorithm invoked was the generic RCDATA parsing algorithm ,switch the tokeniser's content model flag to the RCDATA state.

  4. Then, collect all the character tokens that the tokeniser returns until it returns a token that is not a character token, or until it stops tokenising.

  5. If this process resulted in a collection of character tokens, append a single Text node, whose contents is the concatenation of all those tokens' characters, to the new element node.

  6. The tokeniser's content model flag will have switched back to the PCDATA state.

  7. If the next token is an end tag token with the same tag name as the start tag token, ignore it. Otherwise, it's an end-of-file token, and this is a parse error .

8.2.5.2. Closing elements that have implied end tags

Initially, When the tree construction stage steps below require the UA to generate implied end tags ,then, while the current node is a dd element, a dt element, an li element, an option element, an optgroup element, a p element, an rp element, or an rt element, the UA must handle each token emitted pop the current node off the stack of open elements .

If a step requires the UA to generate implied end tags but lists an element to exclude from the tokenisation process, then the UA must perform the above steps as if that element was not in the above list.

8.2.5.3. Foster parenting

Foster parenting happens when content is misnested in tables.

When a node node is to be foster parented ,the node node must be inserted into the foster parent element stage , and the current table must be marked as tainted .(Once the current table has been tainted ,whitespace characters are inserted into the foster parent element instead of the current node .)

The foster parent element is the parent element of the last table element in the stack of open elements ,if there is a table element and it has such a parent element. If there is no table element in the stack of open elements ( fragment case ), then the foster parent element is the first element in the stack of open elements (the html element). Otherwise, if there is a table element in the stack of open elements ,but the last table element in the stack of open elements has no parent, or its parent node is not an element, then the foster parent element is the element before the last table element in the stack of open elements .

If the foster parent element is the parent element of the last table element in the stack of open elements ,then node must be inserted immediately before the last table element in the stack of open elements in the foster parent element ; otherwise, node must be appended to the foster parent element .

8.2.5.4. The " initial " insertion mode

When the insertion mode is " initial ", tokens must be handled as follows:

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Ignore the token.

A comment token

Append a Comment node to the Document object with the data attribute set to the data given in the comment token.

A DOCTYPE token

If the DOCTYPE token's name does not case-insensitively match the string " HTML ", or if the token's public identifier is not missing, or if the token's system identifier is not missing, then there is a parse error . Conformance checkers may, instead of reporting this error, switch to a conformance checking mode for another language (e.g. based on the DOCTYPE token a conformance checker could recognise recognize that the document is an HTML4-era document, and defer to an HTML4 conformance checker.)

Append a DocumentType node to the Document node, with the name attribute set to the name given in the DOCTYPE token; the publicId attribute set to the public identifier given in the DOCTYPE token, or the empty string if the public identifier was not set; missing; the systemId attribute set to the system identifier given in the DOCTYPE token, or the empty string if the system identifier was not set; missing; and the other attributes specific to DocumentType objects set to null and empty lists as appropriate. Associate the DocumentType node with the Document object so that it is returned as the value of the doctype attribute of the Document object.

Then, if the DOCTYPE token matches one of the conditions in the following list, then set the document to quirks mode :

Otherwise, if the DOCTYPE token matches one of the conditions in the following list, then set the document to limited quirks mode :

The name, system identifier, and public identifier strings must be compared to the values given in the lists above in a case-insensitive manner. A system identifier whose value is the empty string is not considered missing for the purposes of the conditions above.

Then, switch to the root element phase insertion mode to " before html of the tree construction stage. ".

A start tag token An end tag token A character token that is not one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE An end-of-file token Anything else

Parse error .

Set the document to quirks mode .

Then, switch to Switch the root element phase insertion mode to " before html of the tree construction stage and ", then reprocess the current token.

8.2.4.2. 8.2.5.5. The root element phase " before html " insertion mode

After When the initial phase , as each token insertion mode is emitted from the tokenisation " before html stage, it ", tokens must be processed handled as described in this section. follows:

A DOCTYPE token

Parse error . Ignore the token.

A comment token

Append a Comment node to the Document object with the data attribute set to the data given in the comment token.

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Ignore the token.

A character token that is not one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE A start tag token An end whose tag token An end-of-file token name is "html"
If

Create an element for the token is a start tag token with in the tag name "html", and HTML namespace .Append it to the Document object. Put this element in the stack of open elements .

If the token has an attribute "application", "manifest", then run the application cache selection algorithm with the value of that attribute as the manifest URI. URI, resolving relative URIs according to section 5.1 of RFC3986 as if there was no base URI embedded in content (i.e. relative to the base URI from the encapsulating entity or the URI used to retrieve the entity). Otherwise, run the application cache selection algorithm with no manifest. [RFC3986]

Switch the insertion mode to " before head ".

Anything else

Create an HTMLElement node with the tag name html , in the HTML namespace . Append it to the Document object. Put this element in the stack of open elements .

Run the application cache selection algorithm with no manifest.

Switch to the main phase insertion mode to " before head and ", then reprocess the current token.

Should probably make end tags be ignored, so that "</head> <!-- --> <html> " puts the comment before the root node (or should we?)

The root element can end up being removed from the Document object, e.g. by scripts; nothing in particular happens in such cases, content continues being appended to the nodes as described in the next section. 8.2.4.3. The main phase After the root element phase , each token emitted from the tokenisation stage must be processed as described in this section. This is by far the most involved part of parsing an HTML document. The tree construction stage in this phase has several pieces of state: a stack of open elements , a list of active formatting elements , a head element pointer , a form element pointer , and an insertion mode . We could just fold insertion modes and phases into one concept (and duplicate the two rules common to all insertion modes into all of them). 8.2.4.3.1. The stack of open elements Initially the stack of open elements contains just the html root element node created in the last phase before switching to this phase (or, in the fragment case , the html element created as part of that algorithm ). That's the topmost node of the stack. It never gets popped off the stack. (This stack grows downwards.) The current node is the bottommost node in this stack. Elements in the stack fall into the following categories: Special The following HTML elements have varying levels of special parsing rules: address , area , base , basefont , bgsound , blockquote , body , br , center , col , colgroup , dd , dir , div , dl , dt , embed , fieldset , form , frame , frameset , h1 , h2 , h3 , h4 , h5 , h6 , head , hr , iframe , image , img , input , isindex , li , link , listing , menu , meta , noembed , noframes , noscript , ol , optgroup , option , p , param , plaintext , pre , script , select , spacer , style , tbody , textarea , tfoot , thead , title , tr , ul , and wbr . Scoping The following HTML elements introduce new scopes for various parts of the parsing: button , caption , html , marquee , object , table , td and th . Formatting The following HTML elements are those that end up in the list of active formatting elements : a , b , big , em , font , i , nobr , s , small , strike , strong , tt , and u . Phrasing All other elements found while parsing an HTML document. Still need to add these new elements to the lists: event-source , section , nav , article , aside , header , footer , datagrid , command The stack of open elements is said to have an element in scope or have an element in table scope when the following algorithm terminates in a match state: Initialise node to be the current node (the bottommost node of the stack). If node is the target node, terminate in a match state. Otherwise, if node is a table element, terminate in a failure state. Otherwise, if the algorithm is the "has an element in scope" variant (rather than the "has an element in table scope" variant), and node is one of the following, terminate in a failure state: caption td th button marquee object Otherwise, if node is an html element, terminate in a failure state. (This can only happen if the node is the topmost node of the stack of open elements , and prevents the next step from being invoked if there are no more elements in the stack.) Otherwise, set node to the previous entry in the stack of open elements and return to step 2. (This will never fail, since the loop will always terminate in the previous step if the top of the stack is reached.) Nothing happens if at any time any of the elements in the stack of open elements are moved to a new location in, or removed from, the Document tree. In particular, the stack is not changed in this situation. This can cause, amongst other strange effects, content to be appended to nodes that are no longer in the DOM. In some cases (namely, when closing misnested formatting elements ), the stack is manipulated in a random-access fashion.

8.2.4.3.2. 8.2.5.6. The list of active formatting elements Initially the list of active formatting elements is empty. It is used to handle mis-nested formatting element tags . The list contains elements in the formatting category, and scope markers. The scope markers are inserted when entering buttons, object elements, marquees, table cells, and table captions, and are used to prevent formatting from "leaking" into tables, buttons, object elements, and marquees. When the steps below require the UA to reconstruct the active formatting elements , the UA must perform the following steps: If there are no entries in the list of active formatting elements , then there is nothing to reconstruct; stop this algorithm. If the last (most recently added) entry in the list of active formatting elements is a marker, or if it is an element that is in the stack of open elements , then there is nothing to reconstruct; stop this algorithm. Let entry be the last (most recently added) element in the list of active formatting elements . If there are no entries " before entry in the list of active formatting elements , then jump to step 8. Let entry be the entry one earlier than entry in the list of active formatting elements . If entry is neither a marker nor an element that is also in the stack of open elements , go to step 4. Let entry be the element one later than entry in the list of active formatting elements . Perform a shallow clone of the element entry to obtain clone . [DOM3CORE] Append clone to the current node and push it onto the stack of open elements so that it is the new current node . Replace the entry for entry in the list with an entry for clone . If the entry for clone in the list of active formatting elements is not the last entry in the list, return to step 7. This has the effect of reopening all the formatting elements that were opened in the current body, cell, or caption (whichever is youngest) that haven't been explicitly closed. The way this specification is written, the list of active formatting elements always consists of elements in chronological order with the least recently added element first and the most recently added element last (except for while steps 8 to 11 of the above algorithm are being executed, of course). When the steps below require the UA to clear the list of active formatting elements up to the last marker , the UA must perform the following steps: Let entry be the last (most recently added) entry in the list of active formatting elements . Remove entry from the list of active formatting elements . If entry was a marker, then stop the algorithm at this point. The list has been cleared up to the last marker. Go to step 1. 8.2.4.3.3. Creating and inserting HTML elements When the steps below require the UA to create an element for a token , the UA must create a node implementing the interface appropriate for the element type corresponding to the tag name of the token (as given in the section of this specification that defines that element, e.g. for an a element it would be the HTMLAnchorElement interface), with the tag name being the name of that element, with the node being in the HTML namespace , and with the attributes on the node being those given in the given token. When the steps below require the UA to insert an HTML element for a token, the UA must first create an element for the token , and then append this node to the current node , and push it onto the stack of open elements so that it is the new current node . The steps below may also require that the UA insert an HTML element in a particular place, in which case the UA must create an element for the token and then insert or append the new node in the location specified. (This happens in particular during the parsing of tables with invalid content.) The interface appropriate for an element that is not defined in this specification is HTMLElement . The generic CDATA parsing algorithm and the generic RCDATA parsing algorithm consist of the following steps. These algorithms are always invoked in response to a start tag token, and are always passed a context node , typically the current node , which is used as the place to insert the resulting element node. Create an element for the token . Append the new element to the given context node . If the algorithm that was invoked is the generic CDATA parsing algorithm , switch the tokeniser's content model flag to the CDATA state; otherwise the algorithm invoked was the generic RCDATA parsing algorithm , switch the tokeniser's content model flag to the RCDATA state. Then, collect all the character tokens that the tokeniser returns until it returns a token that is not a character token, or until it stops tokenising. If this process resulted in a collection of character tokens, append a single Text node, whose contents is the concatenation of all those tokens' characters, to the new element node. The tokeniser's content model flag will have switched back to the PCDATA state. If the next token is an end tag token with the same tag name as the start tag token, ignore it. Otherwise, this is a parse error . 8.2.4.3.4. Closing elements that have implied end tags When the steps below require the UA to generate implied end tags , then, if the current node is a dd element, a dt element, an li element, a p element, a tbody element, a td element, a tfoot element, a th element, a thead element, a tr element, the UA must act as if an end tag with the respective tag name had been seen and then generate implied end tags again. The step that requires the UA to generate implied end tags but lists an element to exclude from the process, then the UA must perform the above steps as if that element was not in the above list. 8.2.4.3.5. The element pointers Initially the head element pointer and the form element pointer are both null. Once a head element has been parsed (whether implicitly or explicitly) the head element pointer gets set to point to this node. The form element pointer points to the last form element that was opened and whose end tag has not yet been seen. It is used to make form controls associate with forms in the face of dramatically bad markup, for historical reasons. 8.2.4.3.6. The insertion mode Initially the " insertion mode is " before head ". It can change to " in head ", " in head noscript ", " after head ", " in body ", " in table ", " in caption ", " in column group ", " in table body ", " in row ", " in cell ", " in select ", " after body ", " in frameset ", and " after frameset " during the course of the parsing, as described below. It affects how certain tokens are processed. If the tree construction stage is switched from the main phase to the trailing end phase and back again, the various pieces of state are not reset; the UA must act as if the state was maintained.

When the steps below require the UA to reset the insertion mode appropriately , it means the UA must follow these steps: Let last be false. Let node be the last node in the stack of open elements . If node is the first node in the stack of open elements, then set last to true. If the context element of the HTML fragment parsing algorithm is neither a td element nor a th element, then set node to the context element. ( fragment case ) If node is a select element, then switch the insertion mode to " in select " and abort these steps. ( fragment case ) If node is a td or th element, then switch the insertion mode to " in cell " and abort these steps. If node is a tr element, then switch the insertion mode to " in row " and abort these steps. If node is a tbody , thead , or tfoot element, then switch the insertion mode to " in table body " and abort these steps. If node is a caption element, then switch the insertion mode to " in caption " and abort these steps. If node is a colgroup element, then switch the insertion mode to " in column group " and abort these steps. ( fragment case ) If node is a table element, then switch the insertion mode to " in table " and abort these steps. If node is a head element, then switch the insertion mode to " in body " (" in body "! not " in head " !) and abort these steps. ( fragment case ) If node is a body element, then switch the insertion mode to " in body " and abort these steps. If node is a frameset element, then switch the insertion mode to " in frameset " and abort these steps. ( fragment case ) If node is an html element, then: if the head element pointer is null, switch the insertion mode to " before head ", otherwise, switch the insertion mode to " after head ". In either case, abort these steps. ( fragment case ) If last is true, then set the insertion mode to " in body " and abort these steps. ( fragment case ) Let node now be the node before node in the stack of open elements . Return to step 3. 8.2.4.3.7. How to handle tokens in the main phase Tokens in the main phase must be handled as follows:

A DOCTYPE token Parse error . Ignore the token. A start tag whose tag name is "html" If this start tag token was not the first start tag token, then it is a parse error . For each attribute on the token, check to see if the attribute is already present on the top element of the stack of open elements . If it is not, add the attribute and its corresponding value to that element. An end-of-file token Generate implied end tags. If there are more than two nodes on the stack of open elements , or if there are two nodes but the second node is not a body node, this is a parse error . Otherwise, if the parser was originally created as part of the HTML fragment parsing algorithm , and there's more than one element in the stack of open elements , and the second node on the stack of open elements is not a body node, then this is a parse error . ( fragment case ) Stop parsing. This fails because it doesn't imply HEAD and BODY tags. We should probably expand out the insertion modes and merge them with phases and then put the three things here into each insertion mode instead of trying to factor them out so carefully. Anything else Depends on the insertion mode : If the insertion mode is " before head " Handle the token as follows: A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Append the character to Ignore the current node . token.

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "head" "html"

Create an element for Process the token . Set the head element pointer to this new element node. Append the new element to using the current node rules for and push it onto the stack of open elements . Change the insertion mode to " in head body ". " insertion mode .

A start tag token whose tag name is one of: "base", "link", "meta", "script", "style", "title" "head"

Act as if a start tag token with the tag name "head" and no attributes had been seen, then reprocess Insert an HTML element for the current token.

This will result in a

Set the head element pointer to the newly created head element being generated, and with the current token being reprocessed in element.

Switch the insertion mode to " in head " insertion mode . ".

An end tag whose tag name is one of: "head", "body", "html", "p", "br"

Act as if a start tag token with the tag name "head" and no attributes had been seen, then reprocess the current token.

Any other end tag

Parse error . Ignore the token.

Do we really want to ignore end tags here? A character token that is not one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE
Any other start tag token Anything else

Act as if a start tag token with the tag name "head" and no attributes had been seen, then reprocess the current token.

This will result in an empty head element being generated, with the current token being reprocessed in the " after head " insertion mode . .

If the insertion mode is
8.2.5.7. The " in head " insertion mode

Handle When the token insertion mode is " in head ", tokens must be handled as follows. follows:

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Append Insert the character to into the current node .

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "html"

Process the token using the rules for the " in body " insertion mode .

A start tag whose tag name is one of: "base", "command", "event-source", "link"

Insert an HTML element for the token. Immediately pop the current node off the stack of open elements .

Acknowledge the token's self-closing flag ,if it is set.

A start tag whose tag name is "meta"

Insert an HTML element for the token. Immediately pop the current node off the stack of open elements .

Acknowledge the token's self-closing flag ,if it is set.

If the element has a charset attribute, and its value is a supported encoding, and the confidence is currently tentative , then change the encoding to the encoding given by the value of the charset attribute.

Otherwise, if the element has a content attribute, and applying the algorithm to extract for extracting an encoding from a Content-Type to its value returns a supported encoding encoding , and the confidence is currently tentative , then change the encoding to the encoding encoding .

A start tag whose tag name is "title"

Follow the generic RCDATA parsing algorithm , with the head element pointer as the context node , unless that's null, in which case use the current node ( fragment cose ). .

A start tag whose tag name is "noscript", if the scripting flag is enabled :
A start tag whose tag name is one of: "noframes", "style"

Follow the generic CDATA parsing algorithm , with the current node as the context node . .

A start tag whose tag name is "noscript", if the scripting flag is disabled :

Insert a noscript an HTML element for the token.

Change Switch the insertion mode to " in head noscript ".

A start tag whose tag name is "script"

Create an element for the token in the HTML namespace .

Mark the element as being "parser-inserted" . This ensures that, if the script is external, any document.write() calls in the script will execute in-line, instead of blowing the document away, as would happen in most other cases.

Switch the tokeniser's content model flag to the CDATA state.

Then, collect all the character tokens that the tokeniser returns until it returns a token that is not a character token, or until it stops tokenising.

If this process resulted in a collection of character tokens, append a single Text node to the script element node whose contents is the concatenation of all those tokens' characters.

The tokeniser's content model flag will have switched back to the PCDATA state.

If the next token is not an end tag token with the tag name "script", then this is a parse error ; mark the script element as "already executed" . Otherwise, the token is the script element's end tag, so ignore it.

If the parser was originally created for the HTML fragment parsing algorithm , then mark the script element as "already executed" , and skip the rest of the processing described for this token (including the part below where " scripts that will execute as soon as the parser resumes " are executed). ( fragment case )

Marking the script element as "already executed" prevents it from executing when it is inserted into the document a few paragraphs below. Thus, scripts missing their end tags and scripts that were inserted using innerHTML aren't executed.

Let the old insertion point have the same value as the current insertion point . Let the insertion point be just before the next input character .

Append the new element to the current node . Special processing occurs when a script element is inserted into a document that might cause some script to execute, which might cause new characters to be inserted into the tokeniser .

Let the insertion point have the value of the old insertion point . (In other words, restore the insertion point to the value it had before the previous paragraph. This value might be the "undefined" value.)

At this stage, if there is a script that will execute as soon as the parser resumes , then:

If the tree construction stage is being called reentrantly , say from a call to document.write() :

Abort the processing of any nested invokations invocations of the tokeniser, yielding control back to the caller. (Tokenisation will resume when the caller returns to the "outer" tree construction stage.)

Otherwise:

Follow these steps:

  1. Let the script be the script that will execute as soon as the parser resumes . There is no longer a script that will execute as soon as the parser resumes .

  2. Pause until the script has completed loading .

  3. Let the insertion point be just before the next input character .

  4. Execute the script .

  5. Let the insertion point be undefined again.

  6. If there is once again a script that will execute as soon as the parser resumes , then repeat these steps from step 1.

An end tag whose tag name is "head"

Pop the current node (which will be the head element) off the stack of open elements .

Change Switch the insertion mode to " after head ".

An end tag whose tag name is one of: "body", "html", "p", "br"

Act as described in the "anything else" entry below.

A start tag whose tag name is "head"
Any other end tag

Parse error . Ignore the token.

Anything else

Act as if an end tag token with the tag name "head" had been seen, and reprocess the current token.

In certain UAs, some elements don't trigger the "in body" mode straight away, but instead get put into the head. Do we want to copy that?

If
8.2.5.8. The " in head noscript " insertion mode

When the insertion mode is " in head noscript " ", tokens must be handled as follows:

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "html"

Process the token using the rules for the " in body " insertion mode .

An end tag whose tag name is "noscript"

Pop the current node (which will be a noscript element) from the stack of open elements ; the new current node will be a head element.

Switch the insertion mode to " in head ".

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE
A comment token
A start tag whose tag name is one of: "link", "meta", "noframes", "style"

Process the token as if using the insertion mode rules for had been the " in head ". " insertion mode .

An end tag whose tag name is one of: "p", "br"

Act as described in the "anything else" entry below.

A start tag whose tag name is one of: "head", "noscript"
Any other end tag

Parse error . Ignore the token.

Anything else

Parse error . Act as if an end tag with the tag name "noscript" had been seen and reprocess the current token.

If the insertion mode is
8.2.5.9. The " after head " insertion mode

Handle When the token insertion mode is " after head ", tokens must be handled as follows:

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Append Insert the character to into the current node .

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "html"

Process the token using the rules for the " in body " insertion mode .

A start tag whose tag name is "body"

Insert a body an HTML element for the token.

Change Switch the insertion mode to " in body ".

A start tag whose tag name is "frameset"

Insert a frameset an HTML element for the token.

Change Switch the insertion mode to " in frameset ".

A start tag token whose tag name is one of: "base", "link", "meta", "noframes", "script", "style", "title"

Parse error .

Push the node pointed to by the head element pointer onto the stack of open elements .

Process the token as if using the insertion mode rules for had been the " in head ". " insertion mode .

Pop the current node (which will be the node pointed to by the head element pointer ) off the stack of open elements .

A start tag whose tag name is "head"
Any other end tag

Parse error .Ignore the token.

Anything else

Act as if a start tag token with the tag name "body" and no attributes had been seen, and then reprocess the current token.

If the insertion mode is
8.2.5.10. The " in body " insertion mode

Handle When the token insertion mode is " in body ", tokens must be handled as follows:

A character token

Reconstruct the active formatting elements , if any.

Append Insert the token's character to into the current node .

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag token whose tag name is one of: "base", "link", "meta", "script", "style" "html"

Process Parse error .For each attribute on the token as token, check to see if the insertion mode had been " in head ". attribute is already present on the top element of the stack of open elements .If it is not, add the attribute and its corresponding value to that element.

A start tag token whose tag name is one of: "base", "command", "event-source", "link", "meta", "noframes", "script", "style", "title"

Parse error . Process the token as if using the insertion mode rules for had been the " in head ". " insertion mode .

A start tag whose tag name is "body"

Parse error .

If the second element on the stack of open elements is not a body element, or, if the stack of open elements has only one node on it, then ignore the token. ( fragment case )

Otherwise, for each attribute on the token, check to see if the attribute is already present on the body element (the second element) on the stack of open elements . If it is not, add the attribute and its corresponding value to that element.

An end tag whose tag name is "body" end-of-file token

If the second element there is a node in the stack of open elements that is not either a dd element, a dt element, an li element, a p element, a tbody element, a td element, a tfoot element, a th element, a thead element, a tr element, the body element, or the html element, then this is a parse error . Ignore

Stop parsing .

An end tag whose tag name is "body"

If the token. ( fragment case stack of open elements ) does not have a body element in scope ,this is a parse error ; ignore the token.

Otherwise, if there is a node in the stack of open elements that is not either a dd element, a dt element, an li element, a p element, a tbody element, a td element, a tfoot element, a th element, a thead element, a tr element, the body element, or the html element, then this is a parse error .

Change Switch the insertion mode to " after body ". Otherwise, ignore the token.

An end tag whose tag name is "html"

Act as if an end tag with tag name "body" had been seen, then, if that token wasn't ignored, reprocess the current token.

The fake end tag token here can only be ignored in the fragment case .

A start tag whose tag name is one of: "address", "article", "aside", "blockquote", "center", "datagrid", "dialog", "dir", "div", "dl", "fieldset", "listing", "footer", "h1", "h2", "h3", "h4", "h5", "h6", "header", "menu", "nav", "ol", "p", "section", "ul"
This doesn't match browsers.

If the stack of open elements has a p element in scope , then act as if an end tag with the tag name p had been seen.

Insert an HTML element for the token.

A start tag whose tag name is "pre" one of: "pre", "listing"

If the stack of open elements has a p element in scope , then act as if an end tag with the tag name p had been seen.

Insert an HTML element for the token.

If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move on to the next one. (Newlines at the start of pre blocks are ignored as an authoring convenience.)

A start tag whose tag name is "form"

If the form element pointer is not null, ignore the token with a parse error .

Otherwise:

If the stack of open elements has a p element in scope , then act as if an end tag with the tag name p had been seen.

Insert an HTML element for the token, and set the form element pointer to point to the element created.

A start tag whose tag name is "li"

If the stack of open elements has a p element in scope , then act as if an end tag with the tag name p had been seen. Run the following algorithm:

  1. Initialise node to be the current node (the bottommost node of the stack).

  2. If node is an li element, then pop all the nodes from act as if an end tag with the current node tag name li up to node , including node , then stop this algorithm. If more than one node is popped, had been seen, then this is a parse error . jump to the last step.

  3. If node is not in the formatting category, and is not in the phrasing category, and is not an address or div element, then stop this algorithm. jump to the last step.

  4. Otherwise, set node to the previous entry in the stack of open elements and return to step 2. Finally, insert an li element . A start tag whose tag name is one of: "dd", "dt"

  5. If the stack of open elements has a p element in scope , then act as if an end tag with the tag name p had been seen.

    Finally, insert an HTML element for the token.

A start tag whose tag name is one of: "dd", "dt"

Run the following algorithm:

  1. Initialise node to be the current node (the bottommost node of the stack).

  2. If node is a dd or dt element, then pop all the nodes from act as if an end tag with the current node up to node , including same tag name as node , then stop this algorithm. If more than one node is popped, had been seen, then this is a parse error . jump to the last step.

  3. If node is not in the formatting category, and is not in the phrasing category, and is not an address or div element, then stop this algorithm. jump to the last step.

  4. Otherwise, set node to the previous entry in the stack of open elements and return to step 2.

  5. If the stack of open elements has a p element in scope ,then act as if an end tag with the tag name p had been seen.

    Finally, insert an HTML element with the same tag name as for the token's. token.

A start tag whose tag name is "plaintext"

If the stack of open elements has a p element in scope , then act as if an end tag with the tag name p had been seen.

Insert an HTML element for the token.

Switch the content model flag to the PLAINTEXT state.

Once a start tag with the tag name "plaintext" has been seen, that will be the last token ever seen other than character tokens (and the end-of-file token), because there is no way to switch the content model flag out of the PLAINTEXT state.

An end tag whose tag name is one of: "address", "article", "aside", "blockquote", "center", "datagrid", "dialog", "dir", "div", "dl", "fieldset", "footer", "header", "listing", "menu", "nav", "ol", "pre", "section", "ul"

If the stack of open elements has does not have an element in scope with the same tag name as that of the token, then this is a parse error .

Otherwise, run these steps:

  1. generate Generate implied end tags .

  2. Now, if If the current node is not an element with the same tag name as that of the token, then this is a parse error .

  3. If Pop elements from the stack of open elements has until an element in scope with the same tag name as that of the token, then pop elements from this stack until an element with that tag name token has been popped from the stack.

An end tag whose tag name is "form"

Set the form element pointer to null.

If the stack of open elements has does not have an element in scope with the same tag name as that of the token, then this is a parse error .

Otherwise, run these steps:

  1. generate Generate implied end tags .

  2. Now, if If the current node is not an element with the same tag name as that of the token, then this is a parse error .

  3. Otherwise, if Pop elements from the current node stack of open elements is until an element with the same tag name as that of the token pop that element has been popped from the stack. In any case, set the form element pointer to null.

An end tag whose tag name is "p"

If the stack of open elements does not has a p have an element in scope , with the same tag name as that of the token, then generate implied end tags , except for this is a parse error ; act as if a start tag with the tag name p elements. had been seen, then reprocess the current token.

Otherwise, run these steps:

  1. Generate implied end tags ,except for elements with the same tag name as the token.

  2. If the current node is not a p element, an element with the same tag name as that of the token, then this is a parse error .

  3. If Pop elements from the stack of open elements has a p element in scope , then pop elements from this stack until the stack no longer has a p an element in scope . Otherwise, act as if a start tag with the same tag name p had as the token has been seen, then reprocess popped from the current token. stack.

An end tag whose tag name is one of: "dd", "dt", "li"

If the stack of open elements has does not have an element in scope whose tag name matches with the same tag name as that of the token, then this is a parse error .

Otherwise, run these steps:

  1. generate Generate implied end tags , except for elements with the same tag name as the token.

  2. If the current node is not an element with the same tag name as that of the token, then this is a parse error .

  3. If Pop elements from the stack of open elements has an element in scope whose tag name matches the tag name of the token, then pop elements from this stack until an element with that the same tag name as the token has been popped from the stack. A start tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6" If the stack of open elements has a p element in scope , then act as if an end tag with the tag name p had been seen. Insert an HTML element for the token.

An end tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6"

If the stack of open elements does not has have an element in scope an element whose tag name is one of "h1", "h2", "h3", "h4", "h5", or "h6", then this is a parse error .

Otherwise, run these steps:

  1. generate Generate implied end tags .

  2. Now, if If the current node is not an element with the same tag name as that of the token, then this is a parse error .

  3. If Pop elements from the stack of open elements has in scope until an element whose tag name is one of "h1", "h2", "h3", "h4", "h5", or "h6", then pop elements from the stack until an element with one of those tag names "h6" has been popped from the stack.

An end tag whose tag name is "sarcasm"

Take a deep breath, then act as described in the "any other end tag" entry below.

A start tag whose tag name is "a"

If the list of active formatting elements contains an element whose tag name is "a" between the end of the list and the last marker on the list (or the start of the list if there is no marker on the list), then this is a parse error ; act as if an end tag with the tag name "a" had been seen, then remove that element from the list of active formatting elements and the stack of open elements if the end tag didn't already remove it (it might not have if the element is not in table scope ).

In the non-conforming stream <a href="a"> a<table> <a href="b"> b</table> x , the first a element would be closed upon seeing the second one, and the "x" character would be inside a link to "b", not to "a". This is despite the fact that the outer a element is not in table scope (meaning that a regular </a> end tag at the start of the table wouldn't close the outer a element).

Reconstruct the active formatting elements , if any.

Insert an HTML element for the token. Add that element to the list of active formatting elements .

A start tag whose tag name is one of: "b", "big", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u"

Reconstruct the active formatting elements , if any.

Insert an HTML element for the token. Add that element to the list of active formatting elements .

A start tag whose tag name is "nobr"

Reconstruct the active formatting elements , if any.

If the stack of open elements has a nobr element in scope , then this is a parse error . Act as if an end tag with the tag name nobr had been seen, then once again reconstruct the active formatting elements , if any.

Insert an HTML element for the token. Add that element to the list of active formatting elements .

An end tag whose tag name is one of: "a", "b", "big", "em", "font", "i", "nobr", "s", "small", "strike", "strong", "tt", "u"

Follow these steps:

  1. Let the formatting element be the last element in the list of active formatting elements that:

    • is between the end of the list and the last scope marker in the list, if any, or the start of the list otherwise, and
    • has the same tag name as the token.

    If there is no such node, or, if that node is also in the stack of open elements but the element is not in scope , then this is a parse error . Abort these steps. The token is ignored.

    Otherwise, if there is such a node, but that node is not in the stack of open elements , then this is a parse error ; remove the element from the list, and abort these steps.

    Otherwise, there is a formatting element and that element is in the stack and is in scope . If the element is not the current node , this is a parse error . In any case, proceed with the algorithm as written in the following steps.

  2. Let the furthest block be the topmost node in the stack of open elements that is lower in the stack than the formatting element , and is not an element in the phrasing or formatting categories. There might not be one.

  3. If there is no furthest block , then the UA must skip the subsequent steps and instead just pop all the nodes from the bottom of the stack of open elements , from the current node up to and including the formatting element , and remove the formatting element from the list of active formatting elements .

  4. Let the common ancestor be the element immediately above the formatting element in the stack of open elements .

  5. If the furthest block has a parent node, then remove the furthest block from its parent node.

  6. Let a bookmark note the position of the formatting element in the list of active formatting elements relative to the elements on either side of it in the list.

  7. Let node and last node be the furthest block . Follow these steps:

    1. Let node be the element immediately prior to above node in the stack of open elements .
    2. If node is not in the list of active formatting elements , then remove node from the stack of open elements and then go back to step 1.
    3. Otherwise, if node is the formatting element , then go to the next step in the overall algorithm.
    4. Otherwise, if last node is the furthest block , then move the aforementioned bookmark to be immediately after the node in the list of active formatting elements .
    5. If node has any children, perform a shallow clone of node , replace the entry for node in the list of active formatting elements with an entry for the clone, replace the entry for node in the stack of open elements with an entry for the clone, and let node be the clone.
    6. Insert last node into node , first removing it from its previous parent node if any.
    7. Let last node be node .
    8. Return to step 1 of this inner set of steps.
  8. Insert If the common ancestor node is a table ,tbody ,tfoot ,thead ,or tr element, then, foster parent whatever last node ended up being in the previous step.

    Otherwise, append whatever last node ended up being in the previous step into to the common ancestor node, first removing it from its previous parent node if any.

  9. Perform a shallow clone of the formatting element .

  10. Take all of the child nodes of the furthest block and append them to the clone created in the last step.

  11. Append that clone to the furthest block .

  12. Remove the formatting element from the list of active formatting elements , and insert the clone into the list of active formatting elements at the position of the aforementioned bookmark.

  13. Remove the formatting element from the stack of open elements , and insert the clone into the stack of open elements immediately after (i.e. in a more deeply nested position than) below the position of the furthest block in that stack.

  14. Jump back to step 1 in this series of steps.

The way these steps are defined, only elements in the formatting category ever get cloned by this algorithm.

Because of the way this algorithm causes elements to change parents, it has been dubbed the "adoption agency algorithm" (in contrast with other possibly algorithms for dealing with misnested content, which included the "incest algorithm", the "secret affair algorithm", and the "Heisenberg algorithm").

A start tag whose tag name is "button"

If the stack of open elements has a button element in scope , then this is a parse error ; act as if an end tag with the tag name "button" had been seen, then reprocess the token.

Otherwise:

Reconstruct the active formatting elements , if any.

Insert an HTML element for the token.

If the form element pointer is not null, then associate the button element with the form element pointed to by the form element pointer .

Insert a marker at the end of the list of active formatting elements .

A start tag token whose tag name is one of: "applet", "marquee", "object"

Reconstruct the active formatting elements , if any.

Insert an HTML element for the token.

Insert a marker at the end of the list of active formatting elements .

An end tag token whose tag name is one of: "applet", "button", "marquee", "object"

If the stack of open elements does not has have an element in scope an element whose tag name is with the same as the tag name as that of the token, then this is a parse error .

Otherwise, run these steps:

  1. generate Generate implied end tags .

  2. Now, if If the current node is not an element with the same tag name as that of the token, then this is a parse error .

  3. Now, if Pop elements from the stack of open elements has until an element in scope whose tag name matches with the same tag name of the token, then pop elements from as the stack until that element token has been popped from the stack, and clear stack.

  4. Clear the list of active formatting elements up to the last marker .
A start tag whose tag name is "xmp"

Reconstruct the active formatting elements , if any.

Follow the generic CDATA parsing algorithm , with the current node as the context node . .

A start tag whose tag name is "table"

If the stack of open elements has a p element in scope , then act as if an end tag with the tag name p had been seen.

Insert an HTML element for the token.

Change Switch the insertion mode to " in table ".

A start tag whose tag name is one of: "area", "basefont", "bgsound", "br", "embed", "img", "param", "spacer", "wbr"

Reconstruct the active formatting elements , if any.

Insert an HTML element for the token. Immediately pop the current node off the stack of open elements .

Acknowledge the token's self-closing flag ,if it is set.

A start tag whose tag name is one of: "param", "source"

Insert an HTML element for the token. Immediately pop the current node off the stack of open elements .

Acknowledge the token's self-closing flag ,if it is set.

A start tag whose tag name is "hr"

If the stack of open elements has a p element in scope , then act as if an end tag with the tag name p had been seen.

Insert an HTML element for the token. Immediately pop the current node off the stack of open elements .

Acknowledge the token's self-closing flag ,if it is set.

A start tag whose tag name is "image"

Parse error . Change the token's tag name to "img" and reprocess it. (Don't ask.)

A start tag whose tag name is "input"

Reconstruct the active formatting elements , if any.

Insert an input HTML element for the token. Immediately pop the current node off the stack of open elements .

Acknowledge the token's self-closing flag ,if it is set.

If the form element pointer is not null, then associate the newly created input element with the form element pointed to by the form element pointer .

Pop that input element off the stack of open elements .
A start tag whose tag name is "isindex"

Parse error .

If the form element pointer is not null, then ignore the token.

Otherwise:

Acknowledge the token's self-closing flag ,if it is set.

Act as if a start tag token with the tag name "form" had been seen.

If the token has an attribute called "action", set the action attribute on the resulting form element to the value of the "action" attribute of the token.

Act as if a start tag token with the tag name "hr" had been seen.

Act as if a start tag token with the tag name "p" had been seen.

Act as if a start tag token with the tag name "label" had been seen.

Act as if a stream of character tokens had been seen (see below for what they should say).

Act as if a start tag token with the tag name "input" had been seen, with all the attributes from the "isindex" token except "name", "action", and "prompt". Set the name attribute of the resulting input element to the value " isindex ".

Act as if a stream of character tokens had been seen (see below for what they should say).

Act as if an end tag token with the tag name "label" had been seen.

Act as if an end tag token with the tag name "p" had been seen.

Act as if a start tag token with the tag name "hr" had been seen.

Act as if an end tag token with the tag name "form" had been seen.

If the token has an attribute with the name "prompt", then the first stream of characters must be the same string as given in that attribute, and the second stream of characters must be empty. Otherwise, the two streams of character tokens together should, together with the input element, express the equivalent of "This is a searchable index. Insert your search keywords here: (input field)" in the user's preferred language.

Then need to specify that if the form submission causes just a single form control, whose name is "isindex", to be submitted, then we submit just the value part, not the "isindex=" part.

A start tag whose tag name is "textarea"

Create an element for the token in the HTML namespace .Append the new element to the current node .

If the form element pointer is not null, then associate the newly created textarea element with the form element pointed to by the form element pointer .

Append the new element to the current node . Switch the tokeniser's content model flag to the RCDATA state.

If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move on to the next one. (Newlines at the start of textarea elements are ignored as an authoring convenience.)

Then, collect all the character tokens that the tokeniser returns until it returns a token that is not a character token, or until it stops tokenising.

If this process resulted in a collection of character tokens, append a single Text node, whose contents is the concatenation of all those tokens' characters, to the new element node.

The tokeniser's content model flag will have switched back to the PCDATA state.

If the next token is an end tag token with the tag name "textarea", ignore it. Otherwise, this is a parse error .

A start tag whose tag name is one of: "iframe", "noembed", "noframes" "noembed"
A start tag whose tag name is "noscript", if the scripting flag is enabled :

Follow the generic CDATA parsing algorithm , with the current node as the context node . .

A start tag whose tag name is "select"

Reconstruct the active formatting elements , if any.

Insert an HTML element for the token.

Change If the form element pointer is not null, then associate the select element with the form element pointed to by the form element pointer .

If the insertion mode is one of in table ", " in caption ", " in column group ", " in table body ", " in row ", or " in cell ", then switch the insertion mode to " in select in table ". Otherwise, switch the insertion mode to " in select ".

A start tag whose tag name is one of: "rp", "rt"

If the stack of open elements has a ruby element in scope ,then generate implied end tags .If the current node is not then a ruby element, this is a parse error ; pop all the nodes from the current node up to the node immediately before the bottommost ruby element on the stack of open elements .

Insert an HTML element for the token.

An end tag whose tag name is "br"

Parse error . Act as if a start tag token with the tag name "br" had been seen. Ignore the end tag token.

A start or end tag whose tag name is "math"

Reconstruct the active formatting elements ,if any.

Adjust foreign attributes for the token. (This fixes the use of namespaced attributes, in particular XLink.)

Insert a foreign element for the token, in the MathML namespace .

If the token has its self-closing flag set, pop the current node off the stack of open elements and acknowledge the token's self-closing flag .

Otherwise, let the secondary insertion mode be the current insertion mode ,and then switch the insertion mode to " in foreign content ".

A start tag whose tag name is one of: "caption", "col", "colgroup", "frame", "frameset", "head", "option", "optgroup", "tbody", "td", "tfoot", "th", "thead", "tr" An end tag whose tag name is one of: "area", "basefont", "bgsound", "br", "embed", "hr", "iframe", "image", "img", "input", "isindex", "noembed", "noframes", "param", "select", "spacer", "table", "textarea", "wbr"
An end tag whose tag name is "noscript", if scripting is enabled :

Parse error . Ignore the token.

A start or end tag whose tag name is one of: "event-source", "section", "nav", "article", "aside", "header", "footer", "datagrid", "command" Work in progress! A Any other start tag token not covered by the previous entries

Reconstruct the active formatting elements , if any.

Insert an HTML element for the token.

This element will be a phrasing element.

An Any other end tag token not covered by the previous entries

Run the following algorithm:

  1. Initialise node to be the current node (the bottommost node of the stack).

  2. If node has the same tag name as the end tag token, then:

    1. Generate implied end tags .

    2. If the tag name of the end tag token does not match the tag name of the current node , this is a parse error .

    3. Pop all the nodes from the current node up to node , including node , then stop this algorithm.

  3. Otherwise, if node is in neither the formatting category nor the phrasing category, then this is a parse error . Stop this algorithm. The end tag token is ignored.

  4. Set node to the previous entry in the stack of open elements .

  5. Return to step 2.

If
8.2.5.11. The " in table " insertion mode

When the insertion mode is " in table " ", tokens must be handled as follows:

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Append If the current table is tainted ,then act as described in the "anything else" entry below.

Otherwise, insert the character to into the current node .

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "caption"

Clear the stack back to a table context . (See below.)

Insert a marker at the end of the list of active formatting elements .

Insert an HTML element for the token, then switch the insertion mode to " in caption ".

A start tag whose tag name is "colgroup"

Clear the stack back to a table context . (See below.)

Insert an HTML element for the token, then switch the insertion mode to " in column group ".

A start tag whose tag name is "col"

Act as if a start tag token with the tag name "colgroup" had been seen, then reprocess the current token.

A start tag whose tag name is one of: "tbody", "tfoot", "thead"

Clear the stack back to a table context . (See below.)

Insert an HTML element for the token, then switch the insertion mode to " in table body ".

A start tag whose tag name is one of: "td", "th", "tr"

Act as if a start tag token with the tag name "tbody" had been seen, then reprocess the current token.

A start tag whose tag name is "table"

Parse error . Act as if an end tag token with the tag name "table" had been seen, then, if that token wasn't ignored, reprocess the current token.

The fake end tag token here can only be ignored in the fragment case .

An end tag whose tag name is "table"

If the stack of open elements does not have an element in table scope with the same tag name as the token, this is a parse error . Ignore the token. ( fragment case )

Otherwise:

Generate implied end tags . Now, if the current node is not a table element, then this is a parse error . Pop elements from this stack until a table element has been popped from the stack.

Reset the insertion mode appropriately .

An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"

Parse error . Ignore the token.

Anything else A start tag whose tag name is one of: "style", "script"

Parse error . Process If the token current table is tainted then act as if described in the insertion mode "anything else" entry below.

Otherwise, process the token using the rules for was the " in body head ", with the following exception: " insertion mode .

A start tag whose tag name is "input"

If the current node is a table , tbody , tfoot , thead , token does not have an attribute with the name "type", or tr element, then, whenever if it does, but that attribute's value is not a node would be inserted into case-insensitive match for the string "hidden", or, if the current node table is tainted , it must instead be inserted into then: act as described in the "anything else" entry below.

Otherwise:

foster parent Parse error .

Insert an HTML element . for the token.

The foster parent If the form element pointer is not null, then associate the parent input element of with the last table form element in pointed to by the stack of open elements , if there is a table form element and it has such a parent element. If there is no pointer .

Pop that table input element in off the stack of open elements ( fragment case ), then .

An end-of-file token

If the foster parent element current node is not the first element in the stack of open elements (the root html element). Otherwise, if there element, then this is a table parse error. .

It can only be the current node element in the stack of open elements , but the last fragment case .

table Stop parsing. element in

Anything else

Parse error .Process the stack of open elements has no parent, or its parent node is not an element, then token using the foster parent element rules for is the element before " in body " insertion mode ,except that if the last current node is a table element in the stack of open elements . If the , foster parent element tbody is the parent element of the last , table tfoot element in the stack of open elements , then the new node must be inserted immediately before the last , table thead element in the stack of open elements in the foster parent element , or tr ; otherwise, element, then, whenever a node would be inserted into the new current node , it must instead be appended to the foster parent element parented .

When the steps above require the UA to clear the stack back to a table context , it means that the UA must, while the current node is not a table element or an html element, pop elements from the stack of open elements . If this causes any elements to be popped from the stack, then this is a parse error .

The current node being an html element after this process is a fragment case . If

8.2.5.12. The " in caption " insertion mode

When the insertion mode is " in caption " ", tokens must be handled as follows:

An end tag whose tag name is "caption"

If the stack of open elements does not have an element in table scope with the same tag name as the token, this is a parse error . Ignore the token. ( fragment case )

Otherwise:

Generate implied end tags .

Now, if the current node is not a caption element, then this is a parse error .

Pop elements from this stack until a caption element has been popped from the stack.

Clear the list of active formatting elements up to the last marker .

Switch the insertion mode to " in table ".

A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"
An end tag whose tag name is "table"

Parse error . Act as if an end tag with the tag name "caption" had been seen, then, if that token wasn't ignored, reprocess the current token.

The fake end tag token here can only be ignored in the fragment case .

An end tag whose tag name is one of: "body", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"

Parse error . Ignore the token.

Anything else

Process the token as if using the insertion mode rules for was the " in body ". " insertion mode .

If
8.2.5.13. The " in column group " insertion mode

When the insertion mode is " in column group " ", tokens must be handled as follows:

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Append Insert the character to into the current node .

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "html"

Process the token using the rules for the " in body " insertion mode .

A start tag whose tag name is "col"

Insert a col an HTML element for the token. Immediately pop the current node off the stack of open elements .

Acknowledge the token's self-closing flag ,if it is set.

An end tag whose tag name is "colgroup"

If the current node is the root html element, then this is a parse error , ignore the token. ( fragment case )

Otherwise, pop the current node (which will be a colgroup element) from the stack of open elements . Switch the insertion mode to " in table ".

An end tag whose tag name is "col"

Parse error . Ignore the token.

An end-of-file token

If the current node is the root html element, then stop parsing .( fragment case )

Otherwise, act as described in the "anything else" entry below.

Anything else

Act as if an end tag with the tag name "colgroup" had been seen, and then, if that token wasn't ignored, reprocess the current token.

The fake end tag token here can only be ignored in the fragment case .

If
8.2.5.14. The " in table body " insertion mode

When the insertion mode is " in table body " ", tokens must be handled as follows:

A start tag whose tag name is "tr"

Clear the stack back to a table body context . (See below.)

Insert a tr an HTML element for the token, then switch the insertion mode to " in row ".

A start tag whose tag name is one of: "th", "td"

Parse error . Act as if a start tag with the tag name "tr" had been seen, then reprocess the current token.

An end tag whose tag name is one of: "tbody", "tfoot", "thead"

If the stack of open elements does not have an element in table scope with the same tag name as the token, this is a parse error . Ignore the token.

Otherwise:

Clear the stack back to a table body context . (See below.)

Pop the current node from the stack of open elements . Switch the insertion mode to " in table ".

A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "tfoot", "thead"
An end tag whose tag name is "table"

If the stack of open elements does not have a tbody , thead , or tfoot element in table scope , this is a parse error . Ignore the token. ( fragment case )

Otherwise:

Clear the stack back to a table body context . (See below.)

Act as if an end tag with the same tag name as the current node ("tbody", "tfoot", or "thead") had been seen, then reprocess the current token.

An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "td", "th", "tr"

Parse error . Ignore the token.

Anything else

Process the token as if using the insertion mode rules for was the " in table ". " insertion mode .

When the steps above require the UA to clear the stack back to a table body context , it means that the UA must, while the current node is not a tbody , tfoot , thead , or html element, pop elements from the stack of open elements . If this causes any elements to be popped from the stack, then this is a parse error .

The current node being an html element after this process is a fragment case . If

8.2.5.15. The " in row " insertion mode

When the insertion mode is " in row " ", tokens must be handled as follows:

A start tag whose tag name is one of: "th", "td"

Clear the stack back to a table row context . (See below.)

Insert an HTML element for the token, then switch the insertion mode to " in cell ".

Insert a marker at the end of the list of active formatting elements .

An end tag whose tag name is "tr"

If the stack of open elements does not have an element in table scope with the same tag name as the token, this is a parse error . Ignore the token. ( fragment case )

Otherwise:

Clear the stack back to a table row context . (See below.)

Pop the current node (which will be a tr element) from the stack of open elements . Switch the insertion mode to " in table body ".

A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr"
An end tag whose tag name is "table"

Act as if an end tag with the tag name "tr" had been seen, then, if that token wasn't ignored, reprocess the current token.

The fake end tag token here can only be ignored in the fragment case .

An end tag whose tag name is one of: "tbody", "tfoot", "thead"

If the stack of open elements does not have an element in table scope with the same tag name as the token, this is a parse error . Ignore the token.

Otherwise, act as if an end tag with the tag name "tr" had been seen, then reprocess the current token.

An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "td", "th"

Parse error . Ignore the token.

Anything else

Process the token as if using the insertion mode rules for was the " in table ". " insertion mode .

When the steps above require the UA to clear the stack back to a table row context , it means that the UA must, while the current node is not a tr element or an html element, pop elements from the stack of open elements . If this causes any elements to be popped from the stack, then this is a parse error .

The current node being an html element after this process is a fragment case . If

8.2.5.16. The " in cell " insertion mode

When the insertion mode is " in cell " ", tokens must be handled as follows:

An end tag whose tag name is one of: "td", "th"

If the stack of open elements does not have an element in table scope with the same tag name as that of the token, then this is a parse error and the token must be ignored.

Otherwise:

Generate implied end tags , except for elements with the same tag name as the token. .

Now, if the current node is not an element with the same tag name as the token, then this is a parse error .

Pop elements from this stack until an element with the same tag name as the token has been popped from the stack.

Clear the list of active formatting elements up to the last marker .

Switch the insertion mode to " in row ". (The current node will be a tr element at this point.)

A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"

If the stack of open elements does not have a td or th element in table scope , then this is a parse error ; ignore the token. ( fragment case )

Otherwise, close the cell (see below) and reprocess the current token.

An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html"

Parse error . Ignore the token.

An end tag whose tag name is one of: "table", "tbody", "tfoot", "thead", "tr"

If the stack of open elements does not have an element in table scope with the same tag name as that of the token (which can only happen for "tbody", "tfoot" and "thead", or, in the fragment case ), then this is a parse error and the token must be ignored.

Otherwise, close the cell (see below) and reprocess the current token.

Anything else

Process the token as if using the insertion mode rules for was the " in body ". " insertion mode .

Where the steps above say to close the cell , they mean to run the following algorithm:

  1. If the stack of open elements has a td element in table scope , then act as if an end tag token with the tag name "td" had been seen.

  2. Otherwise, the stack of open elements will have a th element in table scope ; act as if an end tag token with the tag name "th" had been seen.

The stack of open elements cannot have both a td and a th element in table scope at the same time, nor can it have neither when the insertion mode is " in cell ". If the insertion mode is

8.2.5.17. The " in select " insertion mode

Handle When the token insertion mode is " in select ", tokens must be handled as follows:

A character token

Append Insert the token's character to into the current node .

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "html"

Process the token using the rules for the " in body " insertion mode .

A start tag whose tag name is "option"

If the current node is an option element, act as if an end tag with the tag name "option" had been seen.

Insert an HTML element for the token.

A start tag whose tag name is "optgroup"

If the current node is an option element, act as if an end tag with the tag name "option" had been seen.

If the current node is an optgroup element, act as if an end tag with the tag name "optgroup" had been seen.

Insert an HTML element for the token.

An end tag whose tag name is "optgroup"

First, if the current node is an option element, and the node immediately before it in the stack of open elements is an optgroup element, then act as if an end tag with the tag name "option" had been seen.

If the current node is an optgroup element, then pop that node from the stack of open elements . Otherwise, this is a parse error , ignore the token.

An end tag whose tag name is "option"

If the current node is an option element, then pop that node from the stack of open elements . Otherwise, this is a parse error , ignore the token.

An end tag whose tag name is "select"

If the stack of open elements does not have an element in table scope with the same tag name as the token, this is a parse error . Ignore the token. ( fragment case )

Otherwise:

Pop elements from the stack of open elements until a select element has been popped from the stack.

Reset the insertion mode appropriately .

A start tag whose tag name is "select"

Parse error . Act as if the token had been an end tag with the tag name "select" instead.

A start tag whose tag name is one of: "input", "textarea"

Parse error .Act as if an end tag with the tag name "select" had been seen, and reprocess the token.

An end-of-file token

If the current node is not the root html element, then this is a parse error. .

It can only be the current node in the fragment case .

Stop parsing.

Anything else

Parse error .Ignore the token.

8.2.5.18. The " in select in table " insertion mode

When the insertion mode is " in select in table ", tokens must be handled as follows:

A start tag whose tag name is one of: "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th"

Parse error .Act as if an end tag with the tag name "select" had been seen, and reprocess the token.

An end tag whose tag name is one of: "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th"

Parse error .

If the stack of open elements has an element in table scope with the same tag name as that of the token, then act as if an end tag with the tag name "select" had been seen, and reprocess the token. Otherwise, ignore the token.

Anything else

Process the token using the rules for the " in select " insertion mode .

8.2.5.19. The " in foreign content " insertion mode

When the insertion mode is " in foreign content ", tokens must be handled as follows:

A character token

Insert the token's character into the current node .

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error . Ignore the token.

If
A start tag whose tag name is neither "mglyph" nor "malignmark", if the current node is an mi element in the MathML namespace .
A start tag whose tag name is neither "mglyph" nor "malignmark", if the current node is an mo element in the MathML namespace .
A start tag whose tag name is neither "mglyph" nor "malignmark", if the current node is an mn element in the MathML namespace .
A start tag whose tag name is neither "mglyph" nor "malignmark", if the current node is an ms element in the MathML namespace .
A start tag whose tag name is neither "mglyph" nor "malignmark", if the current node is an mtext element in the MathML namespace .
A start tag, if the current node is an element in the HTML namespace .
An end tag

Process the token using the rules for the secondary insertion mode .

If, after doing so, the insertion mode is still " in foreign content ", but there is no element in scope that has a namespace other than the HTML namespace ,switch the insertion mode to the secondary insertion mode .

A start tag whose tag name is one of: "b", "big", "blockquote", "body", "br", "center", "code", "dd", "div", "dl", "dt" , "em", "embed", "font", "h1", "h2", "h3", "h4", "h5", "h6" , "hr", "i", "img", "li", "listing" , "menu", "meta", "nobr", "ol" , "p", "pre", "ruby", "s", "small", "span", "strong", "strike" , "sub", "sup", "table", "tt", "u", "ul", "var"
An end-of-file token

Parse error .

Pop elements from the stack of open elements until the current node is in the HTML namespace .

Switch the insertion mode to the secondary insertion mode ,and reprocess the token.

Any other start tag

Adjust foreign attributes for the token. (This fixes the use of namespaced attributes, in particular XLink in SVG.)

Insert a foreign element for the token, in the same namespace as the current node .

If the token has its self-closing flag set, pop the current node off the stack of open elements and acknowledge the token's self-closing flag .

8.2.5.20. The " after body " insertion mode

Handle When the token insertion mode is " after body ", tokens must be handled as follows:

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Process the token as it would be processed if using the insertion mode rules for was the " in body ". " insertion mode .

A comment token

Append a Comment node to the first element in the stack of open elements (the html element), with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "html"

Process the token using the rules for the " in body " insertion mode .

An end tag whose tag name is "html"

If the parser was originally created as part of the HTML fragment parsing algorithm , this is a parse error ; ignore the token. (The element will be an html element in this case.) ( fragment case )

Otherwise, switch to the trailing end phase . insertion mode to " after after body ".

An end-of-file token

Stop parsing.

Anything else

Parse error . Set Switch the insertion mode to " in body " and reprocess the token.

If the insertion mode is
8.2.5.21. The " in frameset " insertion mode

Handle When the token insertion mode is " in frameset ", tokens must be handled as follows:

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Append Insert the character to into the current node .

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "html"

Process the token using the rules for the " in body " insertion mode .

A start tag whose tag name is "frameset"

Insert a frameset an HTML element for the token.

An end tag whose tag name is "frameset"

If the current node is the root html element, then this is a parse error ; ignore the token. ( fragment case )

Otherwise, pop the current node from the stack of open elements .

If the parser was not originally created as part of the HTML fragment parsing algorithm ( fragment case ), and the current node is no longer a frameset element, then change switch the insertion mode to " after frameset ".

A start tag whose tag name is "frame"

Insert an HTML element for the token. Immediately pop the current node off the stack of open elements .

Acknowledge the token's self-closing flag ,if it is set.

A start tag whose tag name is "noframes"

Process the token as if using the rules for the " in head " insertion mode .

An end-of-file token

If the current node is not the root html element, then this is a parse error. .

It can only be the current node had been " in body the fragment case .

Stop parsing. ".

Anything else

Parse error . Ignore the token.

If the insertion mode is
8.2.5.22. The " after frameset " insertion mode

Handle When the token insertion mode is " after frameset ", tokens must be handled as follows:

A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE

Append Insert the character to into the current node .

A comment token

Append a Comment node to the current node with the data attribute set to the data given in the comment token.

A DOCTYPE token

Parse error .Ignore the token.

A start tag whose tag name is "html"

Process the token using the rules for the " in body " insertion mode .

An end tag whose tag name is "html"

Switch to the trailing end phase . insertion mode to " after after frameset ".

A start tag whose tag name is "noframes"

Process the token as if using the insertion mode rules for had been the " in body head ". " insertion mode .

An end-of-file token

Stop parsing.

Anything else

Parse error . Ignore the token.

This doesn't handle UAs that don't support frames, or that do support frames but want to show the NOFRAMES content. Supporting the former is easy; supporting the latter is harder.

8.2.4.4. 8.2.5.23. The trailing end phase " after after body " insertion mode

After When the main phase , as each token insertion mode is emitted from the tokenisation " after after body stage, it ", tokens must be processed handled as described in this section. follows:

A DOCTYPE token Parse error . Ignore the token. A comment token

Append a Comment node to the Document object with the data attribute set to the data given in the comment token.

A DOCTYPE token
A character token that is one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE
A start tag whose tag name is "html"

Process the token as it would using the rules for the " in body " insertion mode .

An end-of-file token

Stop parsing .

Anything else

Parse error .Switch the insertion mode to " in body " and reprocess the token.

8.2.5.24. The " after after frameset " insertion mode

When the insertion mode is " after after frameset ", tokens must be processed handled as follows:

A comment token

Append a Comment node to the Document object with the data attribute set to the data given in the main phase . comment token.

A DOCTYPE token
A character token that is not one of one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF), or U+0020 SPACE
A start tag token An end whose tag token name is "html"

Parse error . Switch back to Process the main phase token using the rules for and reprocess the token. " in body " insertion mode .

An end-of-file token

Stop parsing .

Anything else

Parse error .Switch the insertion mode to " in frameset " and reprocess the token.

8.2.5. 8.2.6 The End end

Once the user agent stops parsing the document, the user agent must follow the steps in this section.

First, the current document readiness must be set to "interactive".

Then, the rules for when a script completes loading start applying (script execution is no longer managed by the parser).

If any of the scripts in the list of scripts that will execute as soon as possible have completed loading , or if the list of scripts that will execute asynchronously is not empty and the first script in that list has completed loading , then the user agent must act as if those scripts just completed loading, following the rules given for that in the script element definition.

Then, if the list of scripts that will execute when the document has finished parsing is not empty, and the first item in this list has already completed loading , then the user agent must act as if that script just finished loading.

By this point, there will be no scripts that have loaded but have not yet been executed.

The user agent must then fire a simple event called DOMContentLoaded at the Document .

Once everything that delays the load event has completed, the user agent must set the current document readiness to "complete", and then fire a load event at the body element .

delaying the load event for things like image loads allows for intranet port scans (even without javascript!). Should we really encode that into the spec?

8.3. 8.3 Namespaces

The HTML namespace is: http://www.w3.org/1999/xhtml

The MathML namespace is: http://www.w3.org/1998/Math/MathML

The SVG namespace is: http://www.w3.org/2000/svg

The XLink namespace is: http://www.w3.org/1999/xlink

The XML namespace is: http://www.w3.org/XML/1998/namespace

The XMLNS namespace is: http://www.w3.org/2000/xmlns/