The following steps form the HTML fragment
parsing algorithm. The algorithm takes as input a DOM
Element, referred to as context, which
gives the context for the parser, as well as input, a
string to parse, and returns a list of zero or more nodes.
Parts marked fragment case in algorithms in the parser section are parts that only occur if the parser was created for the purposes of this algorithm. The algorithms have been annotated with such markings for informational purposes only; such markings have no normative weight. If it is possible for a condition described as a fragment case to occur even when the parser wasn't created for the purposes of handling this algorithm, then that is an error in the specification.
Create a new
Document node, and mark it as being an HTML document.
Create a new HTML parser, and associate it with
the just created
Set the HTML parser's tokenisation stage's content model flag according to the context element, as follows:
Switch the HTML parser's tree construction stage to the main phase.
Let root be a new
html element with no attributes.
Append the element root to the
Document node created above.
Set up the parser's stack of open elements so that it contains just the single element root.
Reset the parser's insertion mode appropriately.
The parser will reference the context node as part of that algorithm.
Set the parser's
pointer to the nearest node to the context that
form element (going straight up the ancestor chain,
and including the element itself, if it is a
or, if there is no such
form element, to null.
Place into the input stream for the HTML parser just created the input.
Start the parser and let it run until it has consumed all the characters just inserted into the input stream.
Return all the child nodes of root, preserving the document order.