index.html
author travil@microsoft.com
Thu, 20 Mar 2014 17:39:08 -0700
changeset 37 c80b719c8211
parent 36 393c42a44e7f
child 38 fb9edcfb8f5f
permissions -rw-r--r--
Correction to the namespace prefix matching sub-steps--they must only consider real XMLNS attributes.
<!doctype html>
<html>
<head>
    <meta charset=UTF-8>
    <title>DOM Parsing and Serialization</title>
    <style>
        /* Make these stand-out more... */
        .externalDFN {
            font-style: italic;
            background-color: #fff9d6;
        }
        /* Switch statement */
        dl.switch dt::before {
            content: "↪ ";
            margin-left: 1em;
        }
        /* Better spacing around various lists (implied paragraph children) */
        ol > li, section:not(#toc) ul > li, section dl > dt {
            margin: 1em 0;
        }
        /* domintro styling */
        dl.domintro {
            background-color: rgb(221, 255, 221);
            padding: 1em 0.5em 1em 2em;
            clear: both;
        }
        dl.domintro dt {
            color: black;
        }
        dl.domintro > dd {
            color: green;
        }
        dl.domintro::before {
            float: right;
            background-color: white;
            display: block;
            border: 2px solid black;
            color: green;
            margin-top: -20px;
            padding: 2px;
            content: "This box is non-normative. Implementation requirements are given below this box.";
        }
        /* Fancy table stuff */
        table {
            border-collapse: collapse;
        }
        thead tr {
            border-bottom: 2px solid black;
        }
        tbody tr:not(:last-child) {
            border-bottom: 1px solid black;
        }
        td {
            border-left: 1px solid black;
            padding: 4px;
        }
        /* Extra IDL :-) */
        .extraidl {
            line-height: 120%;
            padding: 1em;
            border-top: 1px solid #90b8de;
            border-bottom: 1px solid #90b8de;
        }
        .extraidl:before {
            width: 150px;
            color: #fff;
            padding: 3px;
            font-weight: bold;
            font-family: initial;
            margin: -1em 0 1em -1em;
            display: block;
            content: "WebIDL";
            background-color: rgb(144, 184, 222);
        }
    </style>
    <script type="text/javascript" src='https://www.w3.org/Tools/respec/respec-w3c-common' class='remove' async></script>
    <script type="text/javascript" src="respecConfig.js" class='remove'></script>
</head>
<body>

    <section id="abstract">
       <p>This specification defines various APIs for programmatic access to 
          HTML and generic XML parsers by web applications for use in parsing 
          and serializing DOM nodes.</p>
    </section>

    <section id="sotd">
        <p>This specification is based on the original work of the <a href="http://domparsing.spec.whatwg.org/">
            DOM Parsing and Serialization</a> Living Specification, though it has diverged in terms of 
            supported features, normative requirements, and algorithm specificity. As appropriate, 
            relevant fixes from the living specification are incorporated into this document. 
    </section>


    <section id="issues" class="introductory">
        <h1>Issues</h1>
        
        <p class=issue>Open issues that appear throughout the remainder of this 
        document will be highlighted like this.</p>
        
        <!-- I don't believe this is a point of contention anymore...
        <p class="issue">This specification currently requires using the XML 
        Parser for some APIs, when in an XML document. It is unclear whether 
        consensus can be found for this approach.</p>
        -->
    </section>

    <section id="conformance">
        <p>Requirements phrased in the imperative as part of algorithms
        (such as "strip any leading space characters" or "return false and
        terminate these steps") are to be interpreted with the meaning of the
        key word ("must", "should", "may", etc) used in introducing the
        algorithm.</p>

        <p>Conformance requirements phrased as algorithms or specific steps
        may be implemented in any manner, so long as the end result is
        equivalent. (In particular, the algorithms defined in this
        specification are intended to be easy to follow, and not intended to
        be performant.)</p>

        <p id="hardwareLimitations">User agents may impose
        implementation-specific limits on otherwise unconstrained inputs,
        e.g. to prevent denial of service attacks, to guard against running
        out of memory, or to work around platform-specific limitations.</p>

        <p>When a method or an attribute is said to call another method or 
        attribute, the user agent must invoke its internal API for that 
        attribute or method so that e.g. the author can't change the behavior 
        by overriding attributes or methods with custom properties or functions 
        in ECMAScript.</p>

        <p>Unless otherwise stated, string comparisons are done in a <a
        data-spec="DOM4" class="externalDFN" title="case-sensitive">
        case-sensitive</a> manner.</p>

        <p>If an algorithm calls into another algorithm, any exception that is 
        thrown by the latter (unless it is explicitly caught), must cause the 
        former to terminate, and the exception to be propagated up to 
        <em>its</em> caller.</p>
    
        <section>
            <h2>Dependencies</h2>

            <p>The IDL fragments in this specification must be interpreted as
            required for conforming IDL fragments, as described in the Web IDL
            specification. [[!WEBIDL]]</p>

            <p>Some of the terms used in this specification are defined in
            [[DOM4]], [[HTML5]], and [[XML10]].
        </section>

        <section>
            <h2>Extensibility</h2>

            <p>Vendor-specific proprietary extensions to this specification are
            strongly discouraged. Authors must not use such extensions, as
            doing so reduces interoperability and fragments the user base,
            allowing only users of specific user agents to access the content in
            question.</p>

            <p>If vendor-specific extensions are needed, the members should be
            prefixed by vendor-specific strings to prevent clashes with future
            versions of this specification. Extensions must be defined so that
            the use of extensions neither contradicts nor causes the
            non-conformance of functionality defined in the specification.</p>

            <p>When vendor-neutral extensions to this specification are needed,
            either this specification can be updated accordingly, or an
            extension specification can be written that overrides the
            requirements in this specification. When someone applying this
            specification to their activities decides that they will recognise
            the requirements of such an extension specification, it becomes an
            <dfn title="other applicable specifications">applicable
            specification</dfn> for the purposes of conformance requirements in
            this specification.</p>
            <!-- http://www.w3.org/mid/17E341CD-E790-422C-9F9A-69347EE01CEB@iki.fi -->
        </section>
    </section>

    <section>
        <h2>Terminology</h2>

        <p>The term <dfn>context object</dfn> means the object on which the method or
        attribute being discussed was called.
    </section>


    <section>
        <h1>Parsing and serializing <a title="node" data-spec="DOM4" class="externalDFN">Node</a>s</h1>

        <section>
            <h2>Parsing</h2>

            <p>The following steps form the
            <dfn title="concept-parse-fragment">fragment parsing algorithm</dfn>, whose
            arguments are a <var>markup</var> string and a
            <var>context element</var>.

            <ol>
              <li>
                <p>If the <var>context element</var>'s
                <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
                is an <a title="html-document" data-spec="DOM4" class="externalDFN">HTML document</a>: let
                <var>algorithm</var> be the
                <a title="html-fragment-parsing-algorithm" data-spec="HTML5" class="externalDFN">HTML 
                fragment parsing algorithm</a>.</p>

                <p>If the <var>context element</var>'s
                <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
                is an <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>: let
                <var>algorithm</var> be the
                <a title="xml-fragment-parsing-algorithm" data-spec="HTML5" class="externalDFN">XML 
                fragment parsing algorithm</a>.</p>
              </li>

              <li>Invoke <var>algorithm</var> with <var>markup</var> as
              the <var>input</var>, and <var>context element</var> as the
              <var><a data-spec="HTML5" title="concept-frag-parse-context" class="externalDFN">context</a></var>
              element.</li>

              <li>Let <var>new children</var> be the nodes returned.</li>

              <li>Let <var>fragment</var> be a new
              <code><a title="documentfragment" data-spec="DOM4" class="externalDFN">DocumentFragment</a></code> whose
              <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
              is <var>context element</var>'s
              <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>.

              <li><a data-spec="DOM4" title="concept-node-append" class="externalDFN">Append</a>
              each <a data-spec="DOM4" title="concept-node" class="externalDFN">node</a> in
              <var>new children</var> to <var>fragment</var> (in order).

              <p class=note>This ensures the
              <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
              for the new <a data-spec="DOM4" title="concept-node" class="externalDFN">nodes</a> is correct.

              <li>Return <var>fragment</var>.
            </ol>
    </section>

    <section>
        <h2>Serializing</h2>
        <p>To <dfn title="concept-serialize">serialize</dfn> a
        <a title="node" data-spec="DOM4" class="externalDFN">Node</a> <var>node</var>, the user agent
        must run the following steps:

        <ol>
            <li>Let <var>document</var> be <var>node</var>'s
                <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>.
            <li>If <var>document</var> is an
                <a title="html-document" data-spec="DOM4" class="externalDFN">HTML document</a>, 
                return an <a title="concept-serialize-html">HTML serialization</a> of <var>node</var>.
            <li>Otherwise, <var>document</var> is an
                <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>.
            <li>Let <var>context namespace</var> be <code>null</code>.
            <li>Let <var>prefix list</var> be an empty list. The <var>prefix list</var> will 
                contain strings that represent a history of namespace prefixes [[XML-NAMES]]
                that have been serialized by the <a title="concept-serialize-xml">XML serialization</a>
                algorithm for a subtree.
            <li>Return an <a title="concept-serialize-xml">XML serialization</a> of <var>node</var>
                providing to the algorithm <var>context namespace</var> as the <var>namespace</var>
                and <var>prefix list</var> as <var>prefixes</var>.
        </ol>

        <p>To produce an <dfn title="concept-serialize-html">HTML serialization</dfn> of a
        <a title="node" data-spec="DOM4" class="externalDFN">Node</a> <var>node</var>, the user agent
        must run the
        <a data-spec="HTML5" title="html-fragment-serialization-algorithm" class="externalDFN">HTML 
        fragment serialization algorithm</a> [[!HTML5]] on <var>node</var> and return the string produced.

        <p class=note>The following steps for serializing a <var>node</var> belonging to an
            <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a> are 
            designed to produce a serialization that is compatible with the
            <a title="html-parser" data-spec="HTML5" class="externalDFN">HTML parser</a>. For example,
            elements in the XHTML namespace that contain no child nodes are serialized with
            an explicit begin and end tag rather than using the XML self-closing syntax. Exceptions 
            to this rule occur when an XHTML element's equivalent HTML element is a 
            <a title="void-elements" data-spec="HTML5" class="externalDFN">void element</a> that 
            would be auto-closed by the 
            <a title="html-parser" data-spec="HTML5" class="externalDFN">HTML parser</a>.
        </p>
           
        An <a title="concept-serialize-xml">XML serialization</a> differs from an 
        <a title="concept-serialize-html">HTML serialization</a> in the following ways:
        <ul>
            <li><a title="element" data-spec="DOM4" class="externalDFN">Element</a>s will always be
                serialized with an explicitly defined namespace.
            <li><a title="concept-attribute" data-spec="DOM4" class="externalDFN">Attribute</a>s with
                non-null
                <a title="dom-attr-namespaceuri" data-spec="DOM4" class="externalDFN">namespaceURI</a>s
                other than those in the <code>http://www.w3.org/XML/1998/namespace</code> (XML) or 
                <code>http://www.w3.org/2000/xmlns/</code> (XMLNS) namespaces are explicitly
                serialized with their namespaces defined. (An 
                <a title="concept-serialize-html">HTML serialization</a> will drop these namespaces.)
        </ul>

        <p>To produce an <dfn title="concept-serialize-xml">XML serialization</dfn> of a
            <a title="node" data-spec="DOM4" class="externalDFN">Node</a> <var>node</var> given a
            context namespace <var>namespace</var> and prefix list <var>prefixes</var>, the user 
            agent must run the appropriate steps, depending on <var>node</var>'s interface:</p>
                
        <dl class=switch>
            <dt><code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code>
            <dd>
                <p>Run the following algorithm:
                <ol>
                    <!-- "namespace" was passed via the caller -->
                    <li>Let <var>markup</var> be an empty string.
                    <li>Let <var>qualified name</var> be an empty string.
                    <li>Let <var>list</var> be a copy of the <var>prefixes</var> array.
                    <li>Let <var>prefix</var> be the value of <var>node</var>'s 
                        <code><a title="dom-element-prefix" data-spec="DOM4" class="externalDFN">prefix</a></code>
                        attribute.
                    <li>Let <var>ns</var> be the value of <var>node</var>'s 
                        <code><a title="dom-element-namespaceuri" data-spec="DOM4" class="externalDFN">namespaceURI</a></code>
                        attribute.
                    <li>Let a <var>skip end tag</var> flag have the value <code>false</code>.
                    <li>Append "<code>&lt;</code>" (U+003C LESS-THAN SIGN) to <var>markup</var>.
                    <li>If <var>ns</var> is equal to <code>http://www.w3.org/XML/1998/namespace</code> (the XML namespace),
                        then append the following to <var>qualified name</var>:
                        <ol>
                            <li>The string "<code>xml</code>";
                            <li>"<code>:</code>" (U+003A COLON).
                        </ol>
                        <p class="note">The prefix "xml" will always be serialized when the 
                            <a title="element" data-spec="DOM4" class="externalDFN">Element</a>'s namespace is 
                            <code>http://www.w3.org/XML/1998/namespace</code> regardless of the value of <var>node</var>'s 
                            actual <var>prefix</var>.</p>
                    <li>Otherwise, if <var>prefix</var> is not <code>null</code> then append the following to
                        <var>qualified name</var>:
                        <ol>
                            <li>The value of <var>prefix</var>;
                            <li>"<code>:</code>" (U+003A COLON).
                        </ol>
                    <li>Append the value of <var>node</var>'s
                        <code><a title="dom-element-localname" data-spec="DOM4" class="externalDFN">localName</a></code>
                        attribute to <var>qualified name</var>.
                    <li>Append the value of <var>qualified name</var> to <var>markup</var>.
                    <!-- Tried ":" in names, but this doesn't trick the serializer--it just produces 
                        a serialization that is not round-trippable
                        <p class="issue">escaping / throwing -->
                    <p class=note>The following conditional steps determine whether an attribute need be 
                        serialized in order to define the node's namespace prefix:
                    <li>If <var>namespace</var> is not equal to <var>ns</var> (the <var>node</var>'s
                        own namespace is different from its parent), and <var>ns</var> is not equal to 
                        <code>http://www.w3.org/XML/1998/namespace</code> (the XML namespace), and 
                        <var>prefix</var> is not <code>null</code>, then run these sub-steps:
                        <ol>
                            <li>If <var>list</var> contains the value of <var>prefix</var>, then
                                abort these sub-steps. This namespace prefix
                                was already serialized.
                            <li>Add the value of <var>prefix</var> to <var>list</var>.
                            <li>If <var>node</var> has an attribute whose 
                                <a title="dom-attribute-prefix" data-spec="DOM4" class="externalDFN">prefix</a>
                                attribute value is equal to "<code>xmlns</code>", and whose 
                                <a title="dom-attribute-localname" data-spec="DOM4" class="externalDFN">localName</a>
                                attribute value is equal to <var>prefix</var>, then
                                abort these sub-steps. The <var>prefix</var> namespace definition will be 
                                serialized later as part of the <a title="concept-serialize-xml-attributes">XML 
                                serialization of <var>node</var>'s attributes</a>. <span class="note">CAUTION: 
                                this step only considers attributes whose namespace is the XMLNS namespace and that 
                                define a namespace prefix (e.g., <code>xmlns:prefix-definition="namespace"</code>). It is 
                                possible to create (and serialize) attributes that appear to define namespace prefixes, 
                                but which will not according to this step. For example, 
                                <code><a title="dom-element-setattribute" data-spec="DOM4" class="externalDFN">setAttribute</a>(<em>"xmlns:p"</em>, <em>"mynamespace"</em>)</code>
                                will create an attribute with a <code>null</code>
                                <a title="dom-attribute-prefix" data-spec="DOM4" class="externalDFN">prefix</a>, 
                                <a title="dom-attribute-localname" data-spec="DOM4" class="externalDFN">localName</a> of
                                <code>"xmlns:p"</code>, and <code>null</code>
                                <a title="dom-attribute-namespaceuri" data-spec="DOM4" class="externalDFN">namespaceURI</a>.
                                If such an attribute were created on an element in the <code>"mynamespace"</code>
                                namespace with <code>"p"</code> prefix (e.g., via 
                                <code><a title="dom-document-createelementns" data-spec="DOM4" class="externalDFN">createElementNS</a>(<em>"namespace"</em>, <em>"p:elem"</em>)</code>,
                                then a serialization of this element would produce a mal-formed XML fragment with 
                                duplicate attribute names: 
                                <code>"&lt;p:elem xmlns:p="namespace" xmlns:p="namespace"/></code>.
                                </span>
                            <li>Append the following to <var>markup</var>, in order:
                            <ol>
                                <li>"<code> </code>" (U+0020 SPACE);
                                <li>The string "<code>xmlns:</code>";
                                <li>The value of <var>prefix</var>;
                                <li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
                                <li>The value of <var>ns</var>;
                                <li>"<code>"</code>" (U+0022 QUOTATION MARK);
                            </ol>
                        </ol>
                    <p class=note>The following conditional steps determine whether a default namespace is 
                        serialized for this node:
                    <li>If <var>namespace</var> is not equal to <var>ns</var>, and <var>ns</var> is not equal to 
                        <code>http://www.w3.org/XML/1998/namespace</code> (the XML namespace), and <var>prefix</var>
                        is <code>null</code>, then run these sub-steps:
                        <ol>
                            <li>If <var>node</var> has an attribute whose 
                                <a title="concept-attribute-name" data-spec="DOM4" class="externalDFN">name</a>
                                attribute value is equal to "<code>xmlns</code>", abort 
                                these sub-steps. The default namespace will be 
                                serialized later as part of the <a title="concept-serialize-xml-attributes">XML 
                                serialization of <var>node</var>'s attributes</a>.
                            <li>Append the following to <var>markup</var>, in order:
                            <ol>
                                <li>"<code> </code>" (U+0020 SPACE);
                                <li>The string "<code>xmlns</code>";
                                <li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
                                <li>The value of <var>ns</var>;
                                <li>"<code>"</code>" (U+0022 QUOTATION MARK);
                            </ol>
                        </ol>
                    <li>Append to <var>markup</var> the result of the 
                        <a title="concept-serialize-xml-attributes">XML 
                        serialization of <var>node</var>'s attributes</a>, passing <var>list</var>
                        as the <var>prefixes</var>.
                    <li>If the value of <var>ns</var> is the string "<code>http://www.w3.org/1999/xhtml</code>",
                        and the <var>node</var>'s list of 
                        <a title="concept-tree-child" data-spec="DOM4" class="externalDFN">children</a>
                        is empty, and the <var>node</var>'s
                        <code><a title="dom-element-tagname" data-spec="DOM4" class="externalDFN">tagName</a></code>
                        matches any one of the following 
                        <a title="void-elements" data-spec="HTML5" class="externalDFN">void elements</a>:
                        "<code>area</code>",
                        "<code>base</code>",
                        "<code>br</code>",
                        "<code>col</code>",
                        "<code>embed</code>",
                        "<code>hr</code>",
                        "<code>img</code>",
                        "<code>input</code>",
                        "<code>keygen</code>",
                        "<code>link</code>",
                        "<code>menuitem</code>",
                        "<code>meta</code>",
                        "<code>param</code>",
                        "<code>source</code>",
                        "<code>track</code>",
                        "<code>wbr</code>";
                        then append the following to <var>markup</var>, in order:
                        <ol>
                            <li>"<code> </code>" (U+0020 SPACE);
                            <li>"<code>/</code>" (U+002F SOLIDUS);
                        </ol>
                        and set the <var>skip end tag</var> flag to <code>true</code>.
                    <li>If the value of <var>ns</var> is not the string "<code>http://www.w3.org/1999/xhtml</code>",
                        and the <var>node</var>'s list of 
                        <a title="concept-tree-child" data-spec="DOM4" class="externalDFN">children</a>
                        is empty, then append "<code>/</code>" (U+002F SOLIDUS) to <var>markup</var>
                        and set the <var>skip end tag</var> flag to <code>true</code>.
                    <li>Append "<code>&gt;</code>" (U+003E GREATER-THAN SIGN) to <var>markup</var>.
                    <li>If the value of <var>skip end tag</var> is <code>true</code>, then return 
                        the value of <var>markup</var> and skip the remaining steps. The 
                        <var>node</var> is a leaf-node.
                    <li>Append to <var>markup</var> the result of performing an 
                        <a title="concept-serialize-xml">XML serialization</a> of each of 
                        <var>node</var>'s
                        <a title="concept-tree-child" data-spec="DOM4" class="externalDFN">children</a>,
                        in order, providing the value of <var>ns</var> for the <var>namespace</var>
                        and <var>list</var> for the <var>prefixes</var>.
                    <li>Append the following to <var>markup</var>, in order:
                        <ol>
                            <li>"<code>&lt;/</code>" (U+003C LESS-THAN SIGN, U+002F SOLIDUS);
                            <li>The value of <var>qualified name</var>;
                            <li>"<code>&gt;</code>" (U+003E GREATER-THAN SIGN).
                        </ol>
                    <li>Return the value of <var>markup</var>.
                </ol>

            <dt><code><a title="document" data-spec="DOM4" class="externalDFN">Document</a></code>
            <dd>
                <p>Return the result of concatenating the following, in order:
                <ol>
                    <li>The string produced by running the steps to 
                        <a title="concept-serialize-doctype">produce a DocumentType serialization</a>
                        of <var>node</var>'s 
                        <a title="dom-document-doctype" data-spec="HTML5" class="externalDFN">doctype</a>
                        attribute;
                    <li>The string produced by an <a title="concept-serialize-xml">XML serialization</a>
                        of <var>node</var>'s 
                        <a title="dom-document-documentelement" data-spec="HTML5" class="externalDFN">documentElement</a>
                        attribute, providing <code>null</code> as the <var>namespace</var> and an 
                        empty list as <var>prefixes</var>. 
                </ol>

            <dt><code><a title="comment" data-spec="DOM4" class="externalDFN">Comment</a></code>
            <dd>
                <p>Return the concatenation of "<code>&lt;!--</code>", <var>node</var>'s
                <code><a title="dom-characterdata-data" data-spec="DOM4" class="externalDFN">data</a></code>, and
                "<code>--></code>".

            <dt><code><a title="cdata" data-spec="DOML2" class="externalDFN">CDATASection</a></code>
            <dd><ol>
                <li>Let <var>markup</var> be the concatenation of "<code>&lt;![CDATA[</code>", 
                <var>node</var>'s
                <code><a title="dom-characterdata-data" data-spec="DOM4" class="externalDFN">data</a></code>, 
                and "<code>]]></code>".

                <!-- This does not actually appear to be the case in implementations today.
                <p>If <var>data</var> doesn't match the
                  <code><a title="NT-CData" data-spec="XML10" class="externalDFN">CDSect</a></code> production, throw a
                  <code><a title="domexception" data-spec="DOM4" class="externalDFN">DOMException</a></code>
                  with name <code>InvalidStateError</code> and terminate the entire algorithm.
                -->
                
                <li>Return <var>markup</var>.
              </ol>

              <p class=note>CDATASection objects may be created by the historical 
            <code>document.createCDATASection</code> API, or as a result of parsing an
            <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>.

            <dt><code><a title="text" data-spec="DOM4" class="externalDFN">Text</a></code>
            <dd><ol>
                <li>Let <var>markup</var> be <var>node</var>'s
                <code><a title="dom-characterdata-data" data-spec="DOM4" class="externalDFN">data</a></code>.

                <li>Replace any occurrences of "<code>&amp;</code>" in <var>markup</var> by
                "<code>&amp;amp;</code>".
                
                <li>Replace any occurrences of "<code>&lt;</code>" in <var>markup</var> by
                "<code>&amp;lt;</code>".
                
                <li>Replace any occurrences of "<code>&gt;</code>" in <var>markup</var> by
                "<code>&amp;gt;</code>".
                
                <li>Return <var>data</var>.
              </ol>
            <dt><code><a title="documentfragment" data-spec="DOM4" class="externalDFN">DocumentFragment</a></code>
            <dd><ol>
                <li>Let <var>markup</var> the empty string.

                <li>For each
                <a title="concept-tree-child" data-spec="DOM4" class="externalDFN">child</a> of
                <var>node</var>, in order,
                <a title="concept-serialize-xml">produce an XML serialization</a>
                of the child and concatenate the result to <var>markup</var>.

                <li>Return <var>markup</var>.
              </ol>
            <dt><code><a title="documenttype" data-spec="DOM4" class="externalDFN">DocumentType</a></code>
            <dd>Run the steps to <a title="concept-serialize-doctype">produce a DocumentType 
                serialization</a> of <var>node</var>
                and return the string this produced.           
            
            <dt><code><a title="processinginstruction" data-spec="DOM4" class="externalDFN">ProcessingInstruction</a></code>
            <dd><ol>
                <li>Let <var>markup</var> be the concatenation of "<code>&lt;?</code>", 
                <var>node</var>'s
                <code><a title="dom-characterdata-data" data-spec="DOM4" class="externalDFN">data</a></code>, 
                and "<code>?></code>".

                <!-- This does not actually appear to be the case in implementations today.
                <p>If <var>data</var> doesn't match the
                  <code><a title="NT-PI" data-spec="XML10" class="externalDFN">PI</a></code> production, throw a
                  <code><a title="domexception" data-spec="DOM4" class="externalDFN">DOMException</a></code>
                  with name <code>InvalidStateError</code> and terminate the entire algorithm.
                -->
                
                <li>Return <var>markup</var>.
              </ol>
            <p class=note>ProcessingInstruction objects may be created by the historical 
            <code>document.createProcessingInstruction</code> API, or as a result of parsing an
            <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>.
         
        </dl>

        <p>To <dfn title="concept-serialize-doctype">produce a DocumentType serialization</dfn> of a
        <a title="node" data-spec="DOM4" class="externalDFN">Node</a> <var>node</var>, the user agent
        must return the result of the following algorithm:</p>
                
        <ol>
            <li>Let <var>markup</var> be an empty string.
            <li>Append the string "<code>&lt;!DOCTYPE</code>" to <var>markup</var>.
            <li>Append "<code> </code>" (U+0020 SPACE) to <var>markup</var>.
            <li>Append the value of the <var>node</var>'s 
                <code><a title="dom-documenttype-name" data-spec="DOM4" class="externalDFN">name</a></code>
                attribute to <var>markup</var>. For a <var>node</var> belonging to an
                <a title="html-document" data-spec="DOM4" class="externalDFN">HTML document</a>,
                the value will be all lowercase.
            <li>If the <var>node</var>'s 
                <code><a title="dom-documenttype-publicid" data-spec="DOM4" class="externalDFN">publicId</a></code>
                is not the empty string then append the following, in order, to <var>markup</var>:
                <ol>
                    <li>"<code> </code>" (U+0020 SPACE);
                    <li>The string "<code>PUBLIC</code>";
                    <li>"<code> </code>" (U+0020 SPACE);
                    <li>"<code>"</code>" (U+0022 QUOTATION MARK);
                    <li>The value of the <var>node</var>'s 
                        <code><a title="dom-documenttype-publicid" data-spec="DOM4" class="externalDFN">publicId</a></code>
                        attribute;
                    <li>"<code>"</code>" (U+0022 QUOTATION MARK);
                </ol>
            </li>
            <li>If the <var>node</var>'s 
                <code><a title="dom-documenttype-systemid" data-spec="DOM4" class="externalDFN">systemId</a></code>
                is not the empty string and the <var>node</var>'s 
                <code><a title="dom-documenttype-publicid" data-spec="DOM4" class="externalDFN">publicId</a></code>
                is set to the empty string, then append the following, in order, to <var>markup</var>:
                <ol>
                    <li>"<code> </code>" (U+0020 SPACE);
                    <li>The string "<code>SYSTEM</code>";
                </ol>
            </li>
            <li>If the <var>node</var>'s 
                <code><a title="dom-documenttype-systemid" data-spec="DOM4" class="externalDFN">systemId</a></code>
                is not the empty string then append the following, in order, to <var>markup</var>:
                <ol>
                    <li>"<code> </code>" (U+0020 SPACE);
                    <li>"<code>"</code>" (U+0022 QUOTATION MARK);
                    <li>The value of the <var>node</var>'s 
                        <code><a title="dom-documenttype-systemid" data-spec="DOM4" class="externalDFN">systemId</a></code>
                        attribute;
                    <li>"<code>"</code>" (U+0022 QUOTATION MARK);
                </ol>
            </li>
            <li>Optional: if the <var>node</var> has an (historical) 
                <code><a title="ID-Core-DocType-internalSubset" data-spec="DOM3" class="externalDFN">internalSubset</a></code>
                and the
                <code><a title="ID-Core-DocType-internalSubset" data-spec="DOM3" class="externalDFN">internalSubset</a></code>
                attribute's value is a non-empty string, then append the following,
                in order, to <var>markup</var>:
                <ol>
                    <li>"<code> </code>" (U+0020 SPACE);
                    <li>"<code>[</code>" (U+005B LEFT SQUARE BRACKET);
                    <li>The value of the <var>node</var>'s 
                        <code><a title="ID-Core-DocType-internalSubset" data-spec="DOM3" class="externalDFN">internalSubset</a></code>
                        attribute;
                    <li>"<code>]</code>" (U+005D RIGHT SQUARE BRACKET);
                </ol>
                <p class=note>A <var>node</var> belonging to an
                   <a title="html-document" data-spec="DOM4" class="externalDFN">HTML document</a>
                   will never have an 
                   <code><a title="ID-Core-DocType-internalSubset" data-spec="DOM3" class="externalDFN">internalSubset</a></code>
                   because any <code><a title="ID-Core-DocType-internalSubset" data-spec="DOM3" class="externalDFN">internalSubset</a></code>
                   markup is ignored by the parser.</p>
            </li>
            <li>Append "<code>&gt;</code>" (U+003E GREATER-THAN SIGN) to <var>markup</var>.
        </ol>

        
        <p>The <dfn title="concept-serialize-xml-attributes">XML serialization of the attributes</dfn>
        of an <a title="concept-element" data-spec="DOM4" class="externalDFN">element</a>
        <var>element</var> together with a prefix list <var>prefixes</var> is the result of the 
        following algorithm:

        <ol>
            <li>Let <var>result</var> be the empty string.
            <li>For each
                <a title="concept-attribute" data-spec="DOM4" class="externalDFN">attribute</a>
                <var>attr</var> in <var>element</var>'s
                <a title="concept-element-attribute" data-spec="DOM4" class="externalDFN">attributes</a>,
                in order:
                <ol>
                    <li>Let <var>attribute namespace</var> be the <var>attr</var>'s  
                        <a title="dom-attribute-namespaceuri" data-spec="DOM4" class="externalDFN">namespaceURI</a>
                        value.
                    <li>Append the following strings to <var>result</var>:
                        <ol>
                            <li>"<code> </code>" (U+0020 SPACE);
                            <li><var>attr</var>'s
                            <a title="concept-attribute-name" data-spec="DOM4" class="externalDFN">name</a>;
                            <!--<p class="issue">escaping / throwing  NO issues here to my knowledge -->
                            <li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
                            <li><var>attr</var>'s
                            <a title="concept-attribute-value" data-spec="DOM4" class="externalDFN">value</a>,
                            replacing any occurrences of the following:
                            <ol>
                                <li>"<code>&quot;</code>" with "<code>&amp;quot;</code>"
                                <li>"<code>&amp;</code>" with "<code>&amp;amp;</code>"
                                <li>"<code>&lt;</code>" with "<code>&amp;lt;</code>"
                                <li>"<code>&gt;</code>" with "<code>&amp;gt;</code>"
                            </ol>
                            <p class=note>This matches behavior present in browsers, and goes above
                            and beyond the grammar requirement in the XML specification's AttValue
                            production [[XML10]] by also replacing "<code>&gt;</code>" characters.</p>
                            <li>"<code>"</code>" (U+0022 QUOTATION MARK).
                        </ol>
                    <p class="note">The following conditional steps add any namespace prefix declarations
                        into the <var>prefixes</var> list. Only attributes in the XMLNS namespace are
                        collected (e.g., attributes made to look like namespace declarations via 
                        <code><a title="dom-element-setattribute" data-spec="DOM4" class="externalDFN">setAttribute</a>(<em>"xmlns:name"</em>,
                        <em>"value"</em>)</code> are not included).
                    <li>If the value of <var>attribute namespace</var> is equal to 
                        <code>http://www.w3.org/2000/xmlns/</code> (the XMLNS namespace), then:
                        <ol>
                            <li>Let <var>prefix definition</var> be the value of <var>attr</var>'s 
                                <a title="dom-attribute-localname" data-spec="DOM4" class="externalDFN">localName</a>.
                            <li>Add the value of <var>prefix definition</var> to <var>prefixes</var>. 
                                Since this namespace prefix definition has been serialized, it is
                                unnecessary to serialize it again if subsequently encountered in 
                                <var>element</var>'s children.
                        </ol>
                </ol>
            <li>Return <var>result</var>.
        </ol>
    </section>
</section>

<section>
    <h1>The <code>DOMParser</code> interface</h1>
    
<pre class=extraidl>enum <span class=idlInterfaceID>SupportedType</span> {
    "text/html",
    "text/xml",
    "application/xml",
    "application/xhtml+xml",
    "image/svg+xml"
};</pre>

    <p>The <dfn title="dom-domparser"><code>DOMParser()</code></dfn> constructor
    must return a new <code>DOMParser</code> object.

    <dl class="idl" title="[Constructor] interface DOMParser">
        <dt>Document parseFromString(DOMString str, SupportedType type)</dt>
        <dd>
            <p>The
            <dfn title="dom-domparser-parsefromstring"><code>parseFromString(<var>str</var>, <var>type</var>)</code></dfn>
            method must run these steps, depending on <var>type</var>:

            <dl class=switch>
                <dt>"<code>text/html</code>"
                <dd>
                <p>Parse <var>str</var> with an
                <code><a data-spec="HTML5" title="html-parser" class="externalDFN">HTML parser</a></code>, and return the newly
                created <a title="concept-document" data-spec="DOM4" class="externalDFN">document</a>.

                <p>The <a data-spec="HTML5" title="scripting-flag" class="externalDFN">scripting flag</a> must be set to
                "disabled".

                <p class=note><code><a data-spec="HTML5" title="meta" class="externalDFN">meta</a></code> elements are not
                taken into account for the encoding used, as a Unicode stream is passed into
                the parser.

                <p class=note><code><a data-spec="HTML5" title="script" class="externalDFN">script</a></code> elements get marked
                unexecutable and the contents of <code><a data-spec="HTML5" title="noscript" class="externalDFN">noscript</a></code>
                get parsed as markup.

                <dt>"<code>text/xml</code>"
                <dt>"<code>application/xml</code>"
                <dt>"<code>application/xhtml+xml</code>"
                <dt>"<code>image/svg+xml</code>"
                <dd>
                <ol>
                    <li>Parse <var>str</var> with a namespace-enabled
                    <code><a data-spec="HTML5" title="xml-parser" class="externalDFN">XML parser</a></code>.

                    <li>If the previous step didn't return an error, return the newly
                    created <a title="concept-document" data-spec="DOM4" class="externalDFN">document</a>
                    and terminate these steps.

                    <li>Otherwise, throw a 
                    <a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> 
                    with name <code>SyntaxError</code>.
                    
                    <p class=note>Some UAs do not throw an exception, but rather return a minimal 
                    well-formed XML document that describes the error. In these cases, the error 
                    document's root element will be named <code>parsererror</code> and its namespace
                    will be set to "<code>http://www.mozilla.org/newlayout/xml/parsererror.xml</code>".
                    
                    <!--
                    It does not make sense to pave the cow paths by standardizing the 
                    mozilla error handling approach, when there is clear objection to that approach
                    from web developers who expect success to load the document they want, rather
                    than an error document. Browsers are reluctant to change behavior due to web
                    compatibility, but IE has thrown in these cases for multiple releases now without
                    any significant compatibility impact, and thus it seems feasable to spec the 
                    simpler and developer-expected behavior here.
                    appropriate behavior here.
                    
                    <li>Let <var>document</var> be a newly-created
                    <code><a data-spec="DOM4" title="xmldocument" class="externalDFN">XMLDocument</a></code>.

                    <li>
                    <p>Let <var>root</var> be a new
                    <code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code>, with its
                    <a title="concept-element-local-name" data-spec="DOM4" class="externalDFN">local name</a>
                    set to "<code>parsererror</code>" and its
                    <a title="concept-element-namespace" data-spec="DOM4" class="externalDFN">namespace</a>
                    set to
                    "<code>http://www.mozilla.org/newlayout/xml/parsererror.xml</code>".
                    <!-- see https://bugzilla.mozilla.org/show_bug.cgi?id=45566 --><!--
                    <p>At this point user agents may
                    <a data-spec="DOM4" title="concept-node-append" class="externalDFN">append</a> nodes
                    to <var>root</var>, for example to describe the nature of the
                    error.

                    <li><a data-spec="DOM4" title="concept-node-append" class="externalDFN">Append</a>
                    <var>root</var> to <var>document</var>.

                    <li>Return <var>document</var>.
                    -->
                </ol>
            </dl>

            <p>In any case, the returned
            <a title="concept-document" data-spec="DOM4" class="externalDFN">document</a>'s
            <a title="concept-document"-content-type data-spec="DOM4" class="externalDFN">content type</a>
            must be the <var>type</var> argument. Additionally, the 
            <a title="concept-document" data-spec="DOM4" class="externalDFN">document</a> must have a
            <a title="concept-document"-url data-spec="DOM4" class="externalDFN">URL</a> value equal to 
            the URL of the 
            <a title="active-document" data-spec="HTML5" class="externalDFN">active document</a>, a 
            <a title="location" data-spec="HTML5" class="externalDFN">location</a> value of <code>null</code>.

            <p class=note>The returned
            <a title="concept-document" data-spec="DOM4" class="externalDFN">document</a>'s
            <a title="concept-document"-encoding data-spec="DOM4" class="externalDFN">encoding</a> is
            the default, UTF-8.
        </dd>
    </dl>
</section>

<section>
    <h1>The <code>XMLSerializer</code> interface</h1>

    <p>The <dfn title="dom-xmlserializer"><code>XMLSerializer()</code></dfn>
    constructor must return a new <code>XMLSerializer</code> object.

    <dl class="idl" title="[Constructor] interface XMLSerializer">
        <dt>DOMString serializeToString(Node root)</dt>

        <dd>The <code>serializeToString(<var>root</var>)</code>
        method must <a title="concept-serialize-xml">produce an XML serialization</a> of <var>root</var> and return the result.</dd>
    </dl>
</section>

<section>
    <h1>Extensions to the <code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code> interface</h1>

    <dl class="idl" title="partial interface Element">
        <dt>[TreatNullAs=EmptyString] attribute DOMString innerHTML</dt>
        <dd>
            <p>The <dfn title="dom-element-innerhtml"><code>innerHTML</code></dfn> IDL
            attribute represents the markup of the
            <code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code>'s contents.

            <dl class=domintro>
                <!--doc.ih
                <dt><var>document</var> . <code title="dom-document-innerhtml">innerHTML</code> [ = <var>value</var> ]
                <dd>
                <p>Returns a fragment of HTML or XML that represents the
                <code><a title="document" data-spec="DOM4" class="externalDFN">Document</a></code>.

                <p>Can be set, to replace the
                <code><a title="document" data-spec="DOM4" class="externalDFN">Document</a></code>'s contents with the result of
                parsing the given string.

                <p>In the case of an <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>,
                will throw an
                <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>InvalidStateError</code>
                if the <code><a title="document" data-spec="DOM4" class="externalDFN">Document</a></code> cannot be serialized
                to XML, and a
                <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>SyntaxError</code>
                if the given string is not well-formed.
                -->

                <dt><var>element</var> . <code title="dom-element-innerhtml">innerHTML</code> [ = <var>value</var> ]
                <dd>
                <p>Returns a fragment of HTML or XML that represents the element's
                contents.

                <p>Can be set, to replace the contents of the element with nodes
                parsed from the given string.

                <p>In the case of an <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>,
                will throw a
                <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>InvalidStateError</code>
                if the <code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code> cannot be serialized
                to XML, and a
                <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>SyntaxError</code>
                if the given string is not well-formed.
            </dl>

            <p>On getting, if the <a title="context object">context object</a>'s
            <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
            is an <a title="html-document" data-spec="DOM4" class="externalDFN">HTML document</a>, then the attribute
            must return the result of running the
            <a data-spec="HTML5" title="html-fragment-serialization-algorithm" class="externalDFN">HTML fragment serialization algorithm</a> on the
            <a title="context object">context object</a>; otherwise, the <a title="context object">context object</a>'s
            <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
            is an <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>, and the attribute must
            return the result of running the        
            <a data-spec="HTML5" title="xml-fragment-serialization-algorithm" class="externalDFN">XML fragment serialization algorithm</a> on the
            <a title="context object">context object</a> instead (this might throw an
            exception instead of returning a string).

            <p>On setting, these steps must be run:

            <ol>
                <li>Let <var>fragment</var> be the result of invoking the
                <a title="concept-parse-fragment">fragment parsing algorithm</a> with
                the new value as <var>markup</var>, and the
                <a title="context object">context object</a> as the <var>context element</var>.

                <li><a data-spec="DOM4" title="concept-node-replace-all" class="externalDFN">Replace all</a>
                with <var>fragment</var> within the <a title="context object">context object</a>.
            </ol>
        </dd>

        <!-- outerHTML -->
        <dt>[TreatNullAs=EmptyString] attribute DOMString outerHTML</dt>
        <dd>
            <p>The <dfn title="dom-element-outerhtml"><code>outerHTML</code></dfn> IDL
            attribute represents the markup of the
            <code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code> and its contents.

            <dl class=domintro>
              <dt><var>element</var> . <code title="dom-element-outerhtml">outerHTML</code> [ = <var>value</var> ]
              <dd>
                <p>Returns a fragment of HTML or XML that represents the element and its
                contents.

                <p>Can be set, to replace the element with nodes parsed from the given
                string.

                <p>In the case of an <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>,
                will throw a
                <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>InvalidStateError</code>
                if the element cannot be serialized to XML, and a
                <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>SyntaxError</code>
                if the given string is not well-formed.

                <p>Throws a
                <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>NoModificationAllowedError</code>
                if the parent of the element is the
                <code><a title="document" data-spec="DOM4" class="externalDFN">Document</a></code> node.
            </dl>

            <p>On getting, if the <a title="context object">context object</a>'s
            <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
            is an <a title="html-document" data-spec="DOM4" class="externalDFN">HTML document</a>, then the attribute
            must return the result of running the
            <a data-spec="HTML5" title="html-fragment-serialization-algorithm" class="externalDFN">HTML fragment serialization algorithm</a> on a
            fictional node whose only child is <a title="context object">context object</a>; otherwise, the
            <a title="context object">context object</a>'s
            <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
            is an <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>, and the attribute must
            return the result of running the
            <a data-spec="HTML5" title="xml-fragment-serialization-algorithm" class="externalDFN">XML fragment serialization algorithm</a> on that
            fictional node instead (this might throw an exception instead of returning a
            string).

            <p>On setting, the following steps must be run:

            <ol>
                <li>Let <var>parent</var> be the <a title="context object">context object</a>'s
                <a data-spec="DOM4" title="concept-tree-parent" class="externalDFN">parent</a>.

                <li>If <var>parent</var> is null, terminate these steps. There would be no
                way to obtain a reference to the nodes created even if the remaining steps
                were run.

                <li>If <var>parent</var> is a
                <code><a title="document" data-spec="DOM4" class="externalDFN">Document</a></code>, throw a
                <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>NoModificationAllowedError</code>
                exception and terminate these steps.

                <li>If <var>parent</var> is a
                <code><a title="documentfragment" data-spec="DOM4" class="externalDFN">DocumentFragment</a></code>, let
                <var>parent</var> be a new
                <code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code> with

                <ul>
                    <li><code>body</code> as its
                    <a data-spec="DOM4" title="concept-element-local-name" class="externalDFN">local name</a>,

                    <li>the <a data-spec="DOM4" title="html-namespace" class="externalDFN">HTML namespace</a> as its
                    <a data-spec="DOM4" title="concept-element-namespace" class="externalDFN">namespace</a>, and
                    <li>the <a title="context object">context object</a>'s
                    <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
                    as its
                    <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>.
                </ul>

                <li>Let <var>fragment</var> be the result of invoking the
                <a title="concept-parse-fragment">fragment parsing algorithm</a> with
                the new value as <var>markup</var>, and <var>parent</var> as
                the <var>context element</var>.

                <li><a data-spec="DOM4" title="concept-node-replace" class="externalDFN">Replace</a>
                the <a title="context object">context object</a> with <var>fragment</var> within
                the <a title="context object">context object</a>'s
                <a data-spec="DOM4" title="concept-tree-parent" class="externalDFN">parent</a>.
            </ol>
        </dd>

        <!-- insertAdjacentHTML -->
        <dt>void insertAdjacentHTML(DOMString position, DOMString text)</dt>
        <dd>
             <dl class=domintro>
                  <dt><var>element</var> . <code title="dom-element-insertadjacenthtml">insertAdjacentHTML</code>(<var>position</var>, <var>text</var>)

                  <dd>
                    <p>Parses the given string <var>text</var> as HTML or XML and inserts
                    the resulting nodes into the tree in the position given by the
                    <var>position</var> argument, as follows:

                    <dl>
                      <dt>"beforebegin"
                      <dd>Before the element itself.

                      <dt>"afterbegin"
                      <dd>Just inside the element, before its first child.

                      <dt>"beforeend"
                      <dd>Just inside the element, after its last child.

                      <dt>"afterend"
                      <dd>After the element itself.
                    </dl>

                    <p>Throws a <code>SyntaxError</code> exception if the arguments have invalid values (e.g., in the case of an
                    <a data-spec="DOM4" title="xml-document" class="externalDFN">XML document</a>, if the given string is
                    not well-formed).

                    <p>Throws a
                    <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>NoModificationAllowedError</code>
                    if the given position isn't possible (e.g. inserting elements
                    after the root element of a <code><a title="document" data-spec="DOM4" class="externalDFN">Document</a></code>).
                </dl>

                <p>The
                <dfn title="dom-element-insertadjacenthtml"><code>insertAdjacentHTML(<var>position</var>, <var>text</var>)</code></dfn>
                method must run these steps:

                <ol>
                    <li>Use the first matching item from this list:

                    <dl class=switch>
                        <dt>If <var>position</var> is an
                        <a data-spec="DOM4" class="externalDFN" title="ascii-case-insensitive">ASCII case-insensitive</a> match for
                        the string "beforebegin"

                        <dt>If <var>position</var> is an
                        <a data-spec="DOM4" class="externalDFN" title="ascii-case-insensitive">ASCII case-insensitive</a> match for
                        the string "afterend"

                        <dd>
                            <p>Let <var>context</var> be the <a title="context object">context object</a>'s
                            <a data-spec="DOM4" title="concept-tree-parent" class="externalDFN">parent</a>.

                            <p>If <var>context</var> is null or a
                            <a data-spec="DOM4" class="externalDFN" title="concept-document">document</a>, throw
                            a
                            <code><a data-spec="DOM4" title="domexception" class="externalDFN">DOMException</a></code> with name <code>NoModificationAllowedError</code>
                            and terminate these steps.

                        <dt>If <var>position</var> is an
                        <a data-spec="DOM4" class="externalDFN" title="ascii-case-insensitive">ASCII case-insensitive</a> match for
                        the string "afterbegin"

                        <dt>If <var>position</var> is an
                        <a data-spec="DOM4" class="externalDFN" title="ascii-case-insensitive">ASCII case-insensitive</a> match for
                        the string "beforeend"

                        <dd>Let <var>context</var> be the <a title="context object">context object</a>.

                        <dt>Otherwise
                        <dd>
                            <p>Throw a <code>SyntaxError</code> exception.
                    </dl>

                  <li>If <var>context</var> is not an
                    <code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code> or the following are all true:

                    <ul>
                      <li><var>context</var>'s
                      <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
                      is an <a title="html-document" data-spec="DOM4" class="externalDFN">HTML document</a>,

                      <li><var>context</var>'s
                      <a data-spec="DOM4" title="concept-element-local-name" class="externalDFN">local name</a>
                      is "<code>html</code>", and

                      <li><var>context</var>'s
                      <a data-spec="DOM4" title="concept-element-namespace" class="externalDFN">namespace</a>
                      is the <a data-spec="DOM4" title="html-namespace" class="externalDFN">HTML namespace</a>;
                    </ul>

                    <p>let <var>context</var> be a new
                    <code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code> with

                    <ul>
                      <li><code>body</code> as its
                      <a data-spec="DOM4" title="concept-element-local-name" class="externalDFN">local name</a>,

                      <li>the <a data-spec="DOM4" title="html-namespace" class="externalDFN">HTML namespace</a> as its
                      <a data-spec="DOM4" title="concept-element-namespace" class="externalDFN">namespace</a>, and

                      <li>the <a title="context object">context object</a>'s
                      <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
                      as its
                      <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>.
                    </ul>

                <li>Let <var>fragment</var> be the result of invoking the
                <a title="concept-parse-fragment">fragment parsing algorithm</a> with <var>text</var> as 
                <var>markup</var>, and <var>parent</var> as the <var>context element</var>.

                <li>Use the first matching item from this list:

                <dl class=switch>
                    <dt>If <var>position</var> is an
                    <a data-spec="DOM4" class="externalDFN" title="ascii-case-insensitive">ASCII case-insensitive</a> match for
                    the string "beforebegin"
            
                    <dd><a data-spec="DOM4" title="concept-node-insert" class="externalDFN">Insert</a>
                    <var>fragment</var> into the <a title="context object">context object</a>'s
                    <a data-spec="DOM4" title="concept-tree-parent" class="externalDFN">parent</a>
                    before the <a title="context object">context object</a>.

                    <dt>If <var>position</var> is an
                    <a data-spec="DOM4" class="externalDFN" title="ascii-case-insensitive">ASCII case-insensitive</a> match for
                    the string "afterbegin"

                    <dd><a data-spec="DOM4" title="concept-node-insert" class="externalDFN">Insert</a>
                    <var>fragment</var> into the <a title="context object">context object</a>
                    before its
                    <a data-spec="DOM4" title="concept-tree-first-child" class="externalDFN">first child</a>.

                    <dt>If <var>position</var> is an
                    <a data-spec="DOM4" class="externalDFN" title="ascii-case-insensitive">ASCII case-insensitive</a> match for
                    the string "beforeend"

                    <dd><a data-spec="DOM4" title="concept-node-append" class="externalDFN">Append</a>
                    <var>fragment</var> to the <a title="context object">context object</a>.

                    <dt>If <var>position</var> is an
                    <a data-spec="DOM4" class="externalDFN" title="ascii-case-insensitive">ASCII case-insensitive</a> match for
                    the string "afterend"

                    <dd><a data-spec="DOM4" title="concept-node-insert" class="externalDFN">Insert</a>
                    <var>fragment</var> into the <a title="context object">context object</a>'s
                    <a data-spec="DOM4" title="concept-tree-parent" class="externalDFN">parent</a>
                    before the <a title="context object">context object</a>'s
                    <a data-spec="DOM4" title="concept-tree-next-sibling" class="externalDFN">next sibling</a>.
                </dl>
            </ol>
        </dd>

    </dl>
</section>

<!-- Dropping this extention as it is not implemented, nor does it appear that any browser
     is currently interested in supporting it. Perhaps it can come back in a V2 of this spec
     if browsers become interested.
<section>
    <h1>Extensions to the <code><a title="text" data-spec="DOM4" class="externalDFN">Text</a></code> interface</h1>

    <dl class="idl" title="partial interface Text">
        <dt>attribute boolean serializeAsCDATA</dt>
        <dd>
            <dl class=domintro>
              <dt><var>text</var> .
                  <code title="dom-text-serializeascdata">serializeAsCDATA</code> [ = <var>value</var> ]
              <dd>Controls whether, in XML, this node is serialized as a CDATA section.
            </dl>

            <p><code><a title="text" data-spec="DOM4" class="externalDFN">Text</a></code> nodes have an additional
            associated flag, the <dfn>serialize as CDATA flag</dfn>.

            <p>The
            <dfn title="dom-text-serializeascdata"><code>serializeAsCDATA</code></dfn>
            attribute must return true if the <a title="context object">context object</a> has its
            <a>serialize as CDATA flag</a> set, or false otherwise.

            <p>Setting the <code title="dom-text-serializeascdata">serializeAsCDATA</code>
            attribute must, if the new value is true, set the
            <a title="context object">context object</a>'s <a>serialize as CDATA flag</a>, or unset
            it otherwise.
        </dd>
    </dl>
</section>
-->

<section>
    <h1>Extensions to the <code><a data-spec="DOM4" title="range" class="externalDFN">Range</a></code> interface</h1>

    <dl class="idl" title="partial interface Range">
        <dt>DocumentFragment createContextualFragment(DOMString fragment)</dt>
        <dd>
            <dl class=domintro>
              <dt><var>fragment</var> = <var>range</var> . <code title="dom-range-createcontextualfragment">createContextualFragment</code>(<var>fragment</var>)
              <dd>Returns a <code><a title="documentfragment" data-spec="DOM4" class="externalDFN">DocumentFragment</a></code>, created
                from the markup string given.
            </dl>

            <p>The
            <dfn title="dom-range-createcontextualfragment"><code>createContextualFragment(<var>fragment</var>)</code></dfn>
            method must run these steps:

            <ol>
                <li>Let <var>node</var> the <a title="context object">context object</a>'s
                <a data-spec="DOM4" title="concept-range-start-node" class="externalDFN">start node</a>.

                <p>Let <var>element</var> be as follows, depending on <var>node</var>'s interface:

                <dl class=switch>
                    <dt><code><a title="document" data-spec="DOM4" class="externalDFN">Document</a></code>
                    <dt><code><a title="documentfragment" data-spec="DOM4" class="externalDFN">DocumentFragment</a></code>
                    <dd>null

                    <dt><code><a title="element" data-spec="DOM4" class="externalDFN">Element</a></code>
                    <dd><var>node</var>

                    <dt><code><a title="text" data-spec="DOM4" class="externalDFN">Text</a></code>
                    <dt><code><a title="comment" data-spec="DOM4" class="externalDFN">Comment</a></code>
                    <dd><var>node</var>'s
                    <a data-spec="DOM4" class="externalDFN" title="parent-element">parent element</a>

                    <dt><code><a title="documenttype" data-spec="DOM4" class="externalDFN">DocumentType</a></code>
                    <dt><code><a title="processinginstruction" data-spec="DOM4" class="externalDFN">ProcessingInstruction</a></code>
                    <dd>[[DOM4]] prevents this case.
                </dl>

                <li>If either <var>element</var> is null or the following are all true:

                    <ul>
                      <li><var>element</var>'s
                      <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
                      is an <a title="html-document" data-spec="DOM4" class="externalDFN">HTML document</a>,

                      <li><var>element</var>'s
                      <a data-spec="DOM4" title="concept-element-local-name" class="externalDFN">local name</a>
                      is "<code>html</code>", and

                      <li><var>element</var>'s
                      <a data-spec="DOM4" title="concept-element-namespace" class="externalDFN">namespace</a>
                      is the <a data-spec="DOM4" title="html-namespace" class="externalDFN">HTML namespace</a>;
                    </ul>

                    <p>let <var>element</var> be a new
                    <a data-spec="DOM4" title="concept-element" class="externalDFN">element</a> with

                    <ul>
                      <li>"<code>body</code>" as its
                      <a data-spec="DOM4" title="concept-element-local-name" class="externalDFN">local name</a>,

                      <li>the <a data-spec="DOM4" title="html-namespace" class="externalDFN">HTML namespace</a> as its
                      <a data-spec="DOM4" title="concept-element-namespace" class="externalDFN">namespace</a>, and

                      <li>the <a title="context object">context object</a>'s
                      <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>
                      as its
                      <a title="concept-node-document" data-spec="DOM4" class="externalDFN">node document</a>.
                    </ul>

                <li>Let <var>fragment node</var> be the result of invoking the
                <a title="concept-parse-fragment">fragment parsing algorithm</a> with <var>fragment</var> as 
                <var>markup</var>, and <var>element</var> as the <var>context element</var>.

                <li>Unmark all scripts in <var>fragment node</var> as "already started".

                <li>Return <var>fragment node</var>.
            </ol>
        </dd>
    </dl>
</section>

<section class="appendix">
    <h1>Acknowledgements</h1>
    <p>Thanks to Ms2ger [<a href="http://www.mozilla.org">Mozilla</a>] for maintaining the initial 
       drafts of this specification and for its continued improvement in the 
       <a href="http://domparsing.spec.whatwg.org/">Living Specification</a>.

    <p>Thanks to Anne van Kesteren, Aryeh Gregor, Boris Zbarsky, Henri Sivonen, Simon Pieters and timeless
    for their useful comments.

    <p>Special thanks to Ian Hickson for defining the
    <code title="dom-element-innerhtml">innerHTML</code> and
    <code title="dom-element-outerhtml">outerHTML</code> attributes, and the
    <code title="dom-element-insertadjacenthtml">insertAdjacentHTML()</code> method in
    [[HTML5]] and his useful comments.
</section>

</body>
</html>