Skip to content

Commit 8d14932

Browse files
authored
HTML parser should not insert nodes the DOM would reject
"Insert an element at the adjusted insertion location" aborted when it was "not possible to insert" the element, without defining what that meant. Also, the "initial" and "before html" insertion modes append a doctype and the html element directly to the document, which the DOM does not allow when it already has a doctype or element child. Also stop describing the node an insertion location finds itself in as an element, as it can be a Document or DocumentFragment node. Tests: web-platform-tests/wpt#61396 web-platform-tests/wpt#62020 See whatwg#1706.
1 parent 5633568 commit 8d14932

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

source

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,7 +3344,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
33443344
<li>The <dfn data-x="finding flattened slottables" data-x-href="https://dom.spec.whatwg.org/#find-flattened-slotables">find flattened slottables</dfn> algorithm</li>
33453345
<li>The <dfn data-x-href="https://dom.spec.whatwg.org/#slottable-manual-slot-assignment">manual slot assignment</dfn> concept</li>
33463346
<li>The <dfn data-x-href="https://dom.spec.whatwg.org/#assign-a-slot">assign a slot</dfn> algorithm</li>
3347-
<li>The <dfn data-x-href="https://dom.spec.whatwg.org/#concept-node-pre-insert">pre-insert</dfn>, <dfn data-x="concept-node-insert" data-x-href="https://dom.spec.whatwg.org/#concept-node-insert">insert</dfn>, <dfn data-x="concept-node-append" data-x-href="https://dom.spec.whatwg.org/#concept-node-append">append</dfn>, <dfn data-x="concept-node-replace" data-x-href="https://dom.spec.whatwg.org/#concept-node-replace">replace</dfn>, <dfn data-x="concept-node-replace-all" data-x-href="https://dom.spec.whatwg.org/#concept-node-replace-all">replace all</dfn>, <dfn data-x-href="https://dom.spec.whatwg.org/#string-replace-all">string replace all</dfn>, <dfn data-x="concept-node-remove" data-x-href="https://dom.spec.whatwg.org/#concept-node-remove">remove</dfn>, and <dfn data-x="concept-node-adopt" data-x-href="https://dom.spec.whatwg.org/#concept-node-adopt">adopt</dfn> algorithms for nodes</li>
3347+
<li>The <dfn data-x-href="https://dom.spec.whatwg.org/#concept-node-pre-insert">pre-insert</dfn>, <dfn data-x-href="https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity">ensure pre-insert validity</dfn>, <dfn data-x="concept-node-insert" data-x-href="https://dom.spec.whatwg.org/#concept-node-insert">insert</dfn>, <dfn data-x="concept-node-append" data-x-href="https://dom.spec.whatwg.org/#concept-node-append">append</dfn>, <dfn data-x="concept-node-replace" data-x-href="https://dom.spec.whatwg.org/#concept-node-replace">replace</dfn>, <dfn data-x="concept-node-replace-all" data-x-href="https://dom.spec.whatwg.org/#concept-node-replace-all">replace all</dfn>, <dfn data-x-href="https://dom.spec.whatwg.org/#string-replace-all">string replace all</dfn>, <dfn data-x="concept-node-remove" data-x-href="https://dom.spec.whatwg.org/#concept-node-remove">remove</dfn>, and <dfn data-x="concept-node-adopt" data-x-href="https://dom.spec.whatwg.org/#concept-node-adopt">adopt</dfn> algorithms for nodes</li>
33483348
<li>The <dfn data-x="concept-node-insert-ext" data-x-href="https://dom.spec.whatwg.org/#concept-node-insert-ext">insertion steps</dfn>,
33493349
<li>The <dfn data-x="concept-node-post-connection-ext" data-x-href="https://dom.spec.whatwg.org/#concept-node-post-connection-ext">post-connection steps</dfn>,
33503350
<dfn data-x="concept-node-remove-ext" data-x-href="https://dom.spec.whatwg.org/#concept-node-remove-ext">removing steps</dfn>,
@@ -144611,7 +144611,7 @@ dictionary <dfn dictionary>StorageEventInit</dfn> : <span>EventInit</span> {
144611144611

144612144612
<div algorithm>
144613144613
<p>The <dfn>appropriate place for inserting a node</dfn>, optionally using a particular
144614-
<i>override target</i>, is the position in an element returned by running the following steps:</p>
144614+
<i>override target</i>, is the position in a node returned by running the following steps:</p>
144615144615

144616144616
<ol>
144617144617
<li>
@@ -144870,13 +144870,18 @@ dictionary <dfn dictionary>StorageEventInit</dfn> : <span>EventInit</span> {
144870144870

144871144871
<div algorithm>
144872144872
<p>To <dfn>insert an element at the adjusted insertion location</dfn> with an element
144873-
<var>element</var>:</p>
144873+
<var>element</var> and an optional insertion location <var>insertionLocation</var> (default
144874+
null):</p>
144874144875

144875144876
<ol>
144876-
<li><p>Let <var>insertionLocation</var> be the <span>adjusted insertion location</span>.</p></li>
144877+
<li><p>Set <var>insertionLocation</var> to the <span>adjusted insertion location</span> given
144878+
<var>insertionLocation</var>.</p></li>
144879+
144880+
<li><p>If <var>insertionLocation</var> is in a <code>Document</code> node and that
144881+
<code>Document</code> node has an element child, then return.</p></li>
144877144882

144878-
<li><p>If it is not possible to insert <var>element</var> at <var>insertionLocation</var>, abort
144879-
these steps.</p></li>
144883+
<li><p><span>Assert</span>: <span>ensure pre-insert validity</span> given <var>element</var>, the
144884+
node in which <var>insertionLocation</var> finds itself, null, and « » does not throw.</p></li>
144880144885

144881144886
<li><p>If the parser was not created as part of the <span>HTML fragment parsing
144882144887
algorithm</span>, then push a new <span>element queue</span> onto <var>element</var>'s
@@ -144889,10 +144894,6 @@ dictionary <dfn dictionary>StorageEventInit</dfn> : <span>EventInit</span> {
144889144894
<span>relevant agent</span>'s <span>custom element reactions stack</span>, and <span>invoke
144890144895
custom element reactions</span> in that queue.</p></li>
144891144896
</ol>
144892-
144893-
<p class="note">If the <span>adjusted insertion location</span> cannot accept more elements, e.g.,
144894-
because it's a <code>Document</code> that already has an element child, then <var>element</var> is
144895-
dropped on the floor.</p>
144896144897
</div>
144897144898

144898144899
<!-- The names of these algorithms are kinda confusing; e.g. see the confusion in
@@ -144911,7 +144912,7 @@ dictionary <dfn dictionary>StorageEventInit</dfn> : <span>EventInit</span> {
144911144912

144912144913
<li><p>Let <var>element</var> be the result of <span data-x="create an element for the
144913144914
token">creating an element for the token</span> given <var>token</var>, <var>namespace</var>,
144914-
and the element in which the <var>adjustedInsertionLocation</var> finds itself.</p></li>
144915+
and the node in which the <var>adjustedInsertionLocation</var> finds itself.</p></li>
144915144916

144916144917
<li><p>If <var>onlyAddToElementStack</var> is false, then run <span>insert an element at the
144917144918
adjusted insertion location</span> with <var>element</var>.</p></li>
@@ -145060,7 +145061,7 @@ dictionary <dfn dictionary>StorageEventInit</dfn> : <span>EventInit</span> {
145060145061
data-x="concept-cd-data">data</span>.</p>
145061145062

145062145063
<p>Otherwise, let <var>text</var> be the result of <span data-x="create a text node">creating a
145063-
text node</span> given the <span>node document</span> of the element in which
145064+
text node</span> given the <span>node document</span> of the node in which
145064145065
<var>insertionLocation</var> finds itself and <var>data</var>, and insert <var>text</var> at
145065145066
<var>insertionLocation</var>.</p>
145066145067
</li>
@@ -145251,8 +145252,9 @@ document.body.appendChild(text);
145251145252
doctype</span> given the <code>Document</code> node, the name given in the DOCTYPE token (or the
145252145253
empty string if the name was missing), the public identifier given in the DOCTYPE token (or the
145253145254
empty string if the public identifier was missing), and the system identifier given in the
145254-
DOCTYPE token (or the empty string if the system identifier was missing). Then append
145255-
<var>doctype</var> to the <code>Document</code> node.</p>
145255+
DOCTYPE token (or the empty string if the system identifier was missing). Then, if the
145256+
<code>Document</code> node has neither a <code>DocumentType</code> child nor an element child,
145257+
append <var>doctype</var> to the <code>Document</code> node.</p>
145256145258

145257145259
<p class="note">This also ensures that the <code>DocumentType</code> node is returned as the
145258145260
value of the <code data-x="dom-Document-doctype">doctype</code> attribute of the
@@ -145398,8 +145400,9 @@ document.body.appendChild(text);
145398145400
<dt>A start tag whose tag name is "html"</dt>
145399145401
<dd>
145400145402
<p><span>Create an element for the token</span> in the <span>HTML namespace</span>, with the
145401-
<code>Document</code> as the intended parent. Append it to the <code>Document</code> object. Put
145402-
this element in the <span>stack of open elements</span>.</p>
145403+
<code>Document</code> as the intended parent. <span>Insert an element at the adjusted insertion
145404+
location</span> with that element and the <code>Document</code>, after its last child (if any).
145405+
Put this element in the <span>stack of open elements</span>.</p>
145403145406

145404145407
<p>Switch the <span>insertion mode</span> to "<span data-x="insertion mode: before head">before
145405145408
head</span>".</p>
@@ -145618,7 +145621,7 @@ document.body.appendChild(text);
145618145621
inserting a node</span>.</p></li>
145619145622

145620145623
<li><p><span>Create an element for the token</span> in the <span>HTML namespace</span>, with
145621-
the intended parent being the element in which the <var>adjustedInsertionLocation</var> finds
145624+
the intended parent being the node in which the <var>adjustedInsertionLocation</var> finds
145622145625
itself.</p></li>
145623145626

145624145627
<li>
@@ -145705,7 +145708,7 @@ document.body.appendChild(text);
145705145708
<li><p>Let the <var>adjustedInsertionLocation</var> be the <span>appropriate place for
145706145709
inserting a node</span>.</p></li>
145707145710

145708-
<li><p>Let <var>intendedParent</var> be the element in which the
145711+
<li><p>Let <var>intendedParent</var> be the node in which the
145709145712
<var>adjustedInsertionLocation</var> finds itself.</p></li>
145710145713

145711145714
<li><p>Let <var>document</var> be <var>intendedParent</var>'s <span>node

0 commit comments

Comments
 (0)