From f5dcb1a3e1b0bd8f455bf83dbb40b775def3c73f Mon Sep 17 00:00:00 2001
From: Luke Warlow
The Name
production is defined in XML.
+
The AttValue
,
+ Name
, Char
, and
+ PubidChar
productions are defined in XML.
XML
This specification also references the
Attr
interfaceCharacterData
interfaceCharacterData
interface, and its data attributeComment
interfaceCDATASection
interfaceDOMImplementation
interfaceDocument
interface and its
+ documentElement
concept and
doctype
attribute
DocumentOrShadowRoot
interfaceDocumentFragment
interfaceDocumentType
interfaceDocumentType
interface, and its name, publicId and systemId attributesChildNode
interfaceElement
interfaceattachShadow()
method.Node
interfaceNodeList
interfaceProcessingInstruction
interfaceProcessingInstruction
interface, and its target attributeShadowRoot
interfaceText
interfaceRange
interfaceCharacterData
node and its
replace data algorithmkeypress
". The terms
"name" and "type" for events are synonymous.
The following features are defined in DOM Parsing and Serialization: - DOMPARSING
- -XML serialization
The following features are defined in Selection API: SELECTION
Return the XML serialization of node - given require well-formed.
Return the result of XML fragment serialization algorithm with + node and require well-formed.
The fragment parsing algorithm steps, given an Element
@@ -113465,6 +113466,57 @@ enum DOMParserSupportedType {
+
XMLSerializer
interfaceThe XMLSerializer
interface has a number of outstanding issues in the
+ DOM Parsing and Serialization issue tracker, documenting various problems
+ with its specification.
xmlserializer = new XMLSerializer()
Constructs a new XMLSerializer
object.
string = xmlserializer.serializeToString(root)
Returns the result of serializing root to XML.
+ +Throws a TypeError
exception if root is not a
+ valid Node
type.
Throws an "InvalidStateError
" DOMException
if root
+ cannot be serialized to XML.
The design of XMLSerializer
, as a class that needs to be constructed
+ and then have its serializeToString()
+ method called, is an unfortunate historical artifact. If we were designing this functionality
+ today it would be a standalone function.
[Exposed=Window]
+interface XMLSerializer {
+ constructor();
+
+ DOMString serializeToString(Node
root);
+};
+
+ The new XMLSerializer()
+ constructor steps are to do nothing.
The serializeToString(root)
+ method steps are:
Return the result of invoking XML fragment serialization algorithm with + root and false.
The The XML fragment serialization
- algorithm for a The following steps form the XML
+ fragment serialization algorithm. The algorithm takes as input a DOM For Let namespace be null. For Let prefix map be a new namespace prefix map. In both cases, the string returned must be XML namespace-well-formed and must be an isomorphic
- serialization of all of that node's relevant child nodes, in tree order.
- User agents may adjust prefixes and namespace declarations in the serialization (and indeed might
- be forced to do so in some cases to obtain namespace-well-formed XML). User agents may use a
- combination of regular text and character references to represent Add the XML namespace with prefix value
+ " A node's relevant child nodes are those that apply given the following rules: Let prefix index be a generated namespace prefix index with value
+ 1. The generated namespace prefix index is used to generate a new unique prefix value
+ when no suitable existing namespace prefix is available to serializae a node's namespaceURI (or
+ the namespaceURI of one of node's attributes). See the
+ generate a prefix algorithm. Return the result of running Node XML serialization steps passing
+ node, namespace, prefix map, prefix index, and
+ require well-formed. The Node XML serialization steps, given a For The Element XML serialization steps, given an If require well-formed is true, and node's local name contains the character " For the purposes of this section, an internal general parsed entity is considered XML
- namespace-well-formed if a document consisting of an element with no namespace declarations whose
- contents are the internal general parsed entity would itself be XML namespace-well-formed. Let markup be the string " If any of the following error cases are found in the DOM subtree being serialized, then the
- algorithm must throw an " Let qualified name be the empty string. Let skip end tag be false. Let ignore namespace definition attribute be false. Let map be the result of Copying prefix
+ map. Let local prefixes map be an empty map. The map has unique This map is local to each element. It is used to ensure there are no conflicting
+ prefixes if a new namespace prefix attribute needs to be generated. It is also used to enable skipping of duplicate prefix definitions
+ when writing an element's attributes: the map allows the algorithm to distinguish between a
+ prefix in the namespace prefix map that might be locally-defined (to the current
+ Let local default namespace be the result of recording the namespace
+ information for node given map and local prefixes map. The above step will update map with any found namespace prefix
+ definitions, add the found prefix definitions to the local prefixes map and return a
+ local default namespace value defined by a default namespace attribute if one
+ exists. Otherwise it returns null. Let inherited ns be a copy of namespace. Let ns be the value of node's namespace If inherited ns is equal to ns, then: If local default namespace is not null, then set ignore namespace
+ definition attribute to true. If ns is the XML namespace, then append to qualified
+ name the concatenation of the string " Otherwise, append to qualified name the value of node's local name. The
+ node's prefix if it exists, is dropped. Append the qualified name to markup. Otherwise: Let prefix be the value of node's namespace prefix. Let candidate prefix be the result of retrieving a
+ preffered prefix string prefix from map given ns. The above can return null if no namespace key ns exists in
+ map. If the value of prefix matches " If require well-formed is true, then throw an
+ " Let candidate prefix be prefix. Found a suitable namespace prefix: if candidate prefix is not
+ null, then: The following can serialize a different prefix than the Append the concatenation of candidate prefix, " If the local default namespace is not null ( there exists a
+ locally-defined default namespace declaration attribute) and its value is not the XML
+ namespace, then let inherited ns be the value of local default
+ namespace unless the local default namespace is the empty string in which
+ case let it be null (the context namespace is changed to the declared default, rather than
+ this node's own namespace). Any default namespace definitions or namespace prefixes that define the
+ XML namespace are omitted when serializing this node's attributes. Append the value of qualified name to markup. Otherwise, if prefix is not null, then: By this step, there is no namespace or prefix mapping declaration in this
+ node (or any parent node visited by this algorithm) that defines
+ prefix otherwise the step labelled Found a suitable namespace prefix
+ would have been followed. The sub-steps that follow will create a new namespace prefix
+ declaration for prefix and ensure that prefix does not conflict with an
+ existing namespace prefix declaration of the same localName in node's
+ attribute list. If the local prefixes map contains a key matching prefix, then
+ let prefix be the result of generating a
+ prefix providing as input map, ns and prefix
+ index. Add prefix to map given
+ ns. Append to qualified name the concatenation of prefix, " Append the following to markup, in the order listed: The following serializes a namespace prefix declaration for prefix
+ which was just added to the map. " The string " prefix " The result of serializing an attribute given
+ ns and require well-formed; " If local default namespace is not null, then let inherited ns
+ be local default namespace unless local default namespace is the
+ empty string in which case let it get null. Otherwise, if local default namespace is null or local default
+ namespace is not null and it's value is not equal to ns, then: At this point, the namespace for this node still needs to be serialized, but
+ there's no prefix (or candidate prefix) available; the following uses the default
+ namespace declaration to define the namespace--optionally replacing an existing default
+ declaration if present. Set ignore namespace definition attribute to true. Append to qualified name the value of node's local name. Let the value of inherited ns be ns. The new default namespace will be used in the serialization to define this
+ node's namespace and act as the context namespace for its children. Append the value of qualified name to markup. Append the following to markup, in the order listed: The following serializes new (or replacement) default namespace
+ definition. " The string " " The result of serializaing an attribute value given
+ ns and require well-formed; " Otherwise, the node has a local default namespace that matches
+ ns. Append to qualified name the value of node's local name, let the value of inherited
+ ns be ns, and append the value of qualified name to
+ markup All of the combinations where ns is not equal to inherited
+ ns are handled above such that node will be serialized preserving its
+ original namespace. Append to markup the result of the XML
+ serialization of node's attributes given map, prefix
+ index, local prefixes map, ignore namespace definition attribute and
+ require well-formed. If ns is the HTML namespace, and node's list of children is empty, and node's local name matches any one of the following
+ void elements: "area", "base", "basefont", "bgsound", "br", "col", "embed",
+ "frame", "hr", "img", "input", "keygen", "link", "menuitem", "meta", "param", "source", "track",
+ "wbr"; then append the following to markup, in the order listed: " " and set skip end tag to true. If ns is not the HTML namespace, and node's list of
+ children is empty, then append "/" (U+002F SOLIDUS) to
+ markup and set skip end tag to true. Append " If skip end tag is true, then return markup. If ns is the HTML namespace, and node's local name is "template", then this is a
+ This allows template contents to round-trip, given the rules for
+ parsing XHTML documents. Otherwise, append to markup the result of running the XML
+ serialization algorithm on each of node's children, in tree order, providing inherited ns,
+ map, prefix index and require well-formed. Append the following to markup, in the order listed: " qualified name; " Return markup. The Document XML serialization steps, given a If require well-formed is true, and node's document element is null, then throw an
+ " Let serialized document be the empty string. For each child of node, in tree order: Let childMarkup be the result of the Node XML serialization
+ steps passing child, namespace, prefix map, prefix
+ index, and require well-formed. This will serialize any number of Append result to serialized document. Return serialized document. The Comment node XML serialization steps, given a If require well-formed is true, and node's data contains characters that are not matched by the XML
+ Char production or contains " Otherwise return the concatenation of " The CDATASection node XML serialization steps, given a Let markup be the concatenation of " Return markup. The Text node XML serialization steps, given a If require well-formed is true, and node's data contains characters that are not matched by the XML
+ Char production, then throw an
+ " These are the only ways to make a DOM unserialisable. The DOM enforces all the
- other XML constraints; for example, trying to append two elements to a Let markup be the value of node's data. Replace any occurances of " Replace any occurances of " Replace any occurances of " Return markup The DocumentFragment XML serialization steps, given a Let markup be the empty string. For each child of node, in tree order: Let childMarkup be the result of the Node XML serialization
+ steps passing child, namespace, prefix map,
+ prefix index, and require well-formed. Append result to markup. Return markup. The DocumentType node XML serialization steps, given a If require well-formed is true, and node's publicId attribute contains characters that are not
+ matched by the XML PubidChar production, then throw an
+ " If require well-formed is true, and node's systemId attribute contains characters that are not
+ matched by the XML Char production or that contains both a " Let markup be the empty string. Append the string " Append a " Append the value of node's name
+ attribute to markup. If the value of node's publicId attribute is not the empty string then append
+ the following, in the order listed, to markup: " The string " " " The value of node's publicId
+ attribute; " If the value of node's systemId attribute is not the empty string and the
+ value of node's publicId attribute is
+ the empty string, then append the following, in the order listed, to markup: " The string " If the value of node's systemId attribute is not the empty string, then
+ append the following, in the order listed, to markup: " " The value of node's systemId
+ attribute; " Append " Return markup. The ProcessingInstruction node XML serialization steps, given a
+ If require well-formed is true, and node's target attribute contains a " If require well-formed is true, and node's data contains characters that are not matched by the XML
+ Char production or contains the string " Let markup be the empty string. Append the following, in the order listed, to markup: " The value of node's target attribute; " The value of node's data
+ attribute; " Return markup. The Element Attributes XML serialization steps, given an Let result be the empty string. Let localname set be a new empty namespace localname set. This localname
+ set will contain typles of unique attribute namespace and localname pairs, and is populated as each attribute
+ is processed. This set is used to optionally enforce the well-formed
+ constraint that an element cannot have two attributes with the same namespace and localname.
+ This can occur when two otherwise indentical attributes on the same element differ only by their
+ prefix values. Loop: For each attr in
+ element's attribute list: If require well-formed is true, and the localname set contains a
+ tuple whose values match those of a new tuple consisting of attr's namespace and local name, then throw an
+ " Create a new tuple consisting of attr's namespace and local name, and add it to the localname
+ set. Let attribute namespace be attr's namespace. Let candidate prefix be null. If attribute namespace is not null, then: Let candidate prefix be the result of retrieving a preferred prefix string. from map given
+ attribute namespace and attr's prefix If the value of attribute namespace is the XMLNS namespace,
+ then: If any of the following are true, then stop running these steps and goto Loop to visit the next attribute: attr's value is the
+ XML namespace; The XML namespace cannot be redeclared and survive
+ round-tripping (unless it defines the prefix " attr's prefix
+ is null and ignore namespace definition attribute is true (the
+ Element's default namespace attribute should be skipped); attr's prefix
+ is not null and either attrs's local
+ name is not a key contained in the local prefixes map, or attrs's local
+ name is present in the local prefixes map but the value of the key
+ does not match attr's value and furthermore the attrs's local name (as the prefix to find) is
+ found in the namespace prefix map given
+ the namespace consisting of attr's value (the current namespace prefix definition
+ was exactly defined previously--on an ancestor element not the current element whose
+ attributes are being processed). If require well-formed is true, and the attr's value matches the XMLNS namespace,
+ then throw an " If require well-formed is true, and the attr's value is the empty string, then throw an
+ " If attr's prefix is " Otherwise: Let candidate prefix be the result of generating a prefix with map, attribute namespace,
+ and prefix index. Append the following, in the order listed, to result: " The string " candidate prefix; " The result of serializing an
+ attribute value given attribute namespace and require
+ well-formed; " Append a " If candidate prefix is not null, then append the concatenation of
+ candidate prefix with " If require well-formed is true, and the localname set contains
+ the the character " Append the following, in the order listed, to result: The value of attrs's local
+ name; " The result of serializing an attribute
+ value given attr's value and
+ require well-formed; " Return result. To serialize an attribute value given attribute value and a boolean
+ require well-formed: If require well-formed is true, and attribute value contains
+ characters that not matched by the XML XML Char production, then
+ throw an " If attribute value is null, then return the empty string. Let markup be attribute value. Replace any occurances of " Replace any occurances of " Replace any occurances of " Replace any occurances of " Replace any occurances of U+0009 CHARACTER TABULATION (tab) in markup with
+ " Replace any occurances of U+000A LINE FEED (LF) in markup with " Replace any occurances of U+000D CARRIAGE RETURN (CR) in markup with " This matches behavior present in browsers, and goes above and beyond the grammar
+ requirement in the XML specification's AttValue production. Return markup A namespace prefix map is a map that associates namespaceURI and namespace
+ prefix lists, where namespaceURI values are the map's unique keys (which can include the
+ null value representing no namespace), and ordered lists of associated prefix values are the
+ map's key values. The namespace prefix map will be populated by previously seen namespaceURIs and
+ all their previously encountered prefix associations for a given node and its ancestors. To copy a namespace prefix map given map means to copy the
+ map's keys into a new empty namespace prefix map, and to copy each of the
+ values in the namespace prefix list associated with each keys' value into a new list
+ which should be associated with the respective key in the new map. To retrieve a preferred prefix string preferred prefix from the
+ namespace prefix map map given a namespace ns: Let candidates list be the result of retrieving a list from map where there exists a key in map that
+ matches the value of ns or if there is no such key, then return null. For each prefix in candidate list, iterating from beginning to
+ end: There will always be at least one prefix value in the list. If prefix matches preferred prefix, then return
+ prefix. If prefix is the last item in candidates list, then return
+ prefix To check if a prefix string prefix is found in a
+ namespace prefix map given a namespace ns: Let candidates list be the result of retrieving a list from map where there exists a key in map that
+ matches the value of ns or if there is no such key, return false. If the value of prefix occurs at least once in candidates list,
+ return true, otherwise return false. To add a prefix string prefix to the namespace
+ prefix map map given a namespace ns: Let candidates list be null. If there exists a key in map that matches the value of ns, set
+ candidates list to the result of retrieving a list from map with ns. If candidates list is null, then create a new list with prefix as the only item in the list, and associate that list
+ with a new key ns in map. The steps in retrieve a preferred prefix string use the list to
+ track the most recently used (MRU) prefix associated with a given namespace, which
+ will be the prefix at the end of the list. This list can contain duplicates of the
+ same prefix value seen earlier (and that's OK). To generate a prefix given a namespace prefix map map, a
+ string new namespace, and a reference to a generated namespace prefix
+ index prefix index: Let generated prefix be the concatenation of the string " Let the value of prefix index be incremented by one. Add to map the generated prefix
+ given new namespace. Return generated prefix.setTimeout()
and
-
Serializing XML fragments
+ Serializing XML fragments
- Document
or Element
node either returns a fragment
- of XML that represents that node or throws an exception.Node
+ node and a boolean require well-formed, and returns a string.Document
s, the algorithm must return a string in the form of a document entity, if none of the error cases
- below apply.
+
+
+ Element
s, the algorithm must return a string in the form of an internal general parsed entity, if none of the
- error cases below apply.Text
nodes in the
- DOM.xml
" to prefix map.
-
template
elementsNode
node,
+ namespace, prefix map, prefix index, and a boolean require
+ well-formed, are:
+
- Element
Document
template
- element's template contents, if any.Comment
CDATASection
Text
DocumentFragment
DocumentType
ProcessingInstruction
Attr
objectTypeError
.Element
s, if any of the elements in the serialization are in no namespace, the
- default namespace in scope for those elements must be explicitly declared as the empty string. (This doesn't apply in the Document
case.) XML
- XMLNSElement
node,
+ namespace, prefix map, prefix index, and a boolean require
+ well-formed, are:
+
+
+
+ :
" (U+003A COLON) or does not match the XML Name production, then throw an
+ "InvalidStateError
" DOMException
.<
" (U+003C LESS-THAN
+ SIGN).InvalidStateError
" DOMException
- instead of returning a string:
-
+ Document
node with no child element nodes.DocumentType
node that has an external subset public identifier that contains
- characters that are not matched by the XML PubidChar
production. XMLDocumentType
node that has an external subset system identifier that contains
- both a U+0022 QUOTATION MARK (") and a U+0027 APOSTROPHE (') or that contains characters that are
- not matched by the XML Char
production. XMLNode
+ prefix strings as its keys, with corresponding namespaceURI Node
values as the
+ map's key values (in this map, the null namespace is represented by the empty string).Name
production. XMLElement
) and one that is not.Attr
node with no namespace whose local name is the lowercase string "xmlns
". XMLNSElement
node with two or more attributes with the same local name and
- namespace.Attr
node, Text
node, Comment
node, or
- ProcessingInstruction
node whose data contains characters that are not matched by
- the XML Char
production. XMLComment
node whose data contains two adjacent U+002D HYPHEN-MINUS characters
- (-) or ends with such a character.ProcessingInstruction
node whose target name is an ASCII
- case-insensitive match for the string "xml
".
+
ProcessingInstruction
node whose target name contains a U+003A COLON (:).xml:
" and the value of
+ node's local name.ProcessingInstruction
node whose data contains the string "?>
".
+
+ xmlns
", then:
+
+ InvalidStateError
" DOMException
. An Element
with namespace prefix "xmlns
" will not legally round-trip in a conforming XML
+ parser.Element
's
+ existing prefix if it already had one. However, the retrieving a preferred prefix string algorithm already tried to match the
+ existing prefix if possible.
+
+ :
"
+ (U+003A COLON), and node's local
+ name. There exists on this node or the
+ node's ancestry a namespace prefix definition that defines the node's
+ namespace.
+
+ :
" (U+003A COLON), and node's local name.
+
+
" (U+0020 SPACE);xmlns:
";="
" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);"
" (U+0022 QUOTATION MARK).
+
+
+
+
" (U+0020 SPACE);xmlns
";=
" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);"
" (U+0022 QUOTATION MARK).
+
+
+
" (U+0020 SPACE);/
" (U+002F SOLIDUS).>
" (U+003E GREATER-THAN SIGN) to
+ markup.template
element. Append to markup the result of XML serializing a DocumentFragment
node given
+ node's template contents, providing inherited ns,
+ map, prefix index and require well-formed.
+
+ </
" (U+003C LESS-THAN SIGN, U+002F SOLIDUS);>
" (U+003D GREATER-THAN SIGN).Document
+ node, namespace, prefix map, prefix index, and a
+ boolean require well-formed, are:
+
+
+ InvalidStateError
" DOMException
.
+
+
+ ProcessingInstruction
and
+ Comment
nodes both before and after the Document
's document element node, including at most one
+ DocumentType
node. (Text
nodes are not allowed as children of the
+ Document
.)Comment
+ node, and a boolean require well-formed, are:
+
+
+ --
" (two
+ adjacent U+002D HYPHEN-MINUS characters) or that ends with a "-
" (U+002D
+ HYPHEN-MINUS) character, then throw an "InvalidStateError
"
+ DOMException
.<!--
", node's
+ data, and "-->
".CDATASection
+ node, are:
+
+
+ <![CDATA[
",
+ node's data, and ">]]
".Text
node,
+ and a boolean require well-formed, are:
+
+
+ InvalidStateError
" DOMException
.Document
node
- will throw a "HierarchyRequestError
" DOMException
.&
" (U+0026 AMPERSAND) in
+ markup with "&
".<
" (U+003C LESS-THAN SIGN) in
+ markup with "<
".>
" (U+003D GREATER-THAN SIGN) in
+ markup with ">
".DocumentFragment
+ node, namespace, prefix map, prefix index, and a
+ boolean require well-formed, are:
+
+
+
+
+ DocumentType
+ node, and a boolean require well-formed, are:
+
+
+ InvalidStateError
" DOMException
."
" (U+0022 QUOTATION MARK) and a "'
" (U+0027
+ APOSTROPHE), then throw an "InvalidStateError
"
+ DOMException
.<!DOCTYPE
" to markup.
" (U+0020 SPACE) to markup.
+
+
" (U+0020 SPACE);PUBLIC
";
" (U+0020 SPACE);"
" (U+0022 QUOTATION MARK);"
" (U+0022 QUOTATION MARK);
+
+
" (U+0020 SPACE);SYSTEM
";
+
+
" (U+0020 SPACE);"
" (U+0022 QUOTATION MARK);"
" (U+0022 QUOTATION MARK);>
" (U+003E GREATER-THAN SIGN) to
+ markupProcessingInstruction
node, and a boolean require well-formed,
+ are:
+
+
+ :
" (U+003A COLON) character or is an ASCII case-insensitive match
+ for the string "xml
", then throw an
+ "InvalidStateError
" DOMException
.?>
" (U+003F QUESTION MARK, U+003E GREATER-THAN SIGN), then throw an
+ "InvalidStateError
" DOMException
.
+
+ <?
" (U+003C LESS-THAN SIGN, U+003F QUESTION MARK);
" (U+0020 SPACE);?>
" (U+003F QUESTION MARK, U+003E GREATER-THAN
+ SIGN);Element
+ element, a namespace prefix map map, a generated
+ namespace prefix index prefix index reference, a boolean ignore namespace
+ definition attribute, and a boolean require well-formed, are:
+
+
+
+
+ InvalidStateError
" DOMException
.
+
+
+
+
+
+ xml
"). To avoid
+ this problem, this algorithm always prefixes elements in the XML namespace
+ with "xml
" and drops any related definitions as seen in the above
+ condition.
+
+
+ InvalidStateError
"
+ DOMException
.InvalidStateError
" DOMException
.xmlns
", then set candiate prefix to "xmlns
".
+
+
+
+
" (U+0020 SPACE);xmlns:
";="
" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);"
" (U+0022 QUOTATION MARK).
" (U+0020 SPACE) to result.:
" (U+003A COLON) to candidate
+ prefix.:
" (U+003A COLON), or equals "xmlns
" and attribute namespace is null, or does not match the XML
+ Name production, then throw an
+ "InvalidStateError
" DOMException
.
+
+ ="
" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);"
" (U+0022 QUOTATION MARK).
+
+
+ InvalidStateError
" DOMException
.&
" (U+0026 AMPERSAND) in
+ markup with "&
"."
" (U+0022 QUOTATION MARK) in
+ markup with ""
".<
" (U+003C LESS-THAN SIGN) in
+ markup with "<
".>
" (U+003D GREATER-THAN SIGN) in
+ markup with ">
".	
".
".
".
+
+
+
+
+
+
+
+
+
+
+
+
ns
" and the current numerical value of prefix index.Parsing XML fragments