This is a snapshot of an early working draft and has therefore been superseded by the HTML standard.
This document will not be further updated.
The following steps form the HTML fragment
serialisation algorithm. The algorithm takes as input a DOM
Document, referred to as the node, and either returns a string or raises an
This algorithm serialises the children of the node being serialised, not the node itself.
Let s be a string, and initialise it to the empty string.
For each child node child of the node, in tree order, append the appropriate string from the following list to s:
Append a U+003C LESS-THAN SIGN (
character, followed by the element's tag name. (For nodes created by
the HTML parser,
Document.renameNode(), the tag name will be
For each attribute that the element has, append a U+0020 SPACE
character, the attribute's name (which, for attributes set by the HTML parser or by
Element.setAttribute(), will be lowercase), a U+003D
EQUALS SIGN (
=) character, a U+0022 QUOTATION
") character, the attribute's value,
described below, and a second U+0022 QUOTATION MARK (
While the exact order of attributes is UA-defined, and may depend on factors such as the order that the attributes were given in the original markup, the sort order must be stable, such that consecutive invocations of this algorithm serialise an element's attributes in the same order.
Append a U+003E GREATER-THAN SIGN (
If the child node is an
wbr element, then continue on to the next child node at
If the child node is a
textarea element, append a U+000A LINE FEED (LF)
Append the value of running the HTML
fragment serialisation algorithm on the child
element (thus recursing into this algorithm for that element),
followed by a U+003C LESS-THAN SIGN (
character, a U+002F SOLIDUS (
/) character, the
element's tag name again, and finally a U+003E GREATER-THAN SIGN
If one of the ancestors of the child node is a
plaintext element, then append the value of the child node's
data DOM attribute
Otherwise, append the value of the child node's
data DOM attribute, escaped as described below.
Append the literal string
<!-- (U+003C LESS-THAN
SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS, U+002D
HYPHEN-MINUS), followed by the value of the child
data DOM attribute, followed by the
--> (U+002D HYPHEN-MINUS, U+002D
HYPHEN-MINUS, U+003E GREATER-THAN SIGN).
Append the literal string
LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+0044 LATIN CAPITAL LETTER
D, U+004F LATIN CAPITAL LETTER O, U+0043 LATIN CAPITAL LETTER C,
U+0054 LATIN CAPITAL LETTER T, U+0059 LATIN CAPITAL LETTER Y, U+0050
LATIN CAPITAL LETTER P, U+0045 LATIN CAPITAL LETTER E), followed by a
space (U+0020 SPACE), followed by the value of the child node's
name DOM attribute,
followed by the literal string
> (U+003E GREATER-THAN
Other nodes types (e.g.
Attr) cannot occur as
children of elements. If they do, this algorithm must raise an
The result of the algorithm is the string s.
Escaping a string (for the purposes of the
algorithm above) consists of replacing any occurances of the "
&" character by the string "
&", any occurances of the "
<" character by the string "
<", any occurances of the "
>" character by the string "
>", and any occurances of the "
"" character by the string "
Entity reference nodes are assumed to be expanded by the user agent, and are therefore not covered in the algorithm above.
It is possible that the output of this algorithm, if parsed
with an HTML parser, will not return the original
tree structure. For instance, if a
textarea element to which
Comment node has been appended is serialised and
the output is then reparsed, the comment will end up being displayed in
the text field. Similarly, if, as a result of DOM manipulation, an element
contains a comment that contains the literal string "
-->", then when the result of serialising the element
is parsed, the comment will be truncated at that point and the rest of the
comment will be interpreted as markup. More examples would be making a
script element contain a text node
with the text string "
</script>", or having a
p element that contains a
ul element (as the
element's start tag would imply the
end tag for the