Skip to content

Commit

Permalink
Upstream the insertAdjacentHTML method from DOM Parsing and Serializa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
lukewarlow committed Apr 17, 2024
1 parent d6a5f09 commit d9c4198
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions source
Expand Up @@ -112401,6 +112401,7 @@ document.body.appendChild(frame)</code></pre>
DOMString <span data-x="dom-Element-getHTML">getHTML</span>(optional <span>GetHTMLOptions</span> options = {});

[<span>CEReactions</span>] attribute [<span>LegacyNullToEmptyString</span>] <span data-x="tt-htmlstring">HTMLString</span> <span data-x="dom-Element-innerHTML">innerHTML</span>;
[<span>CEReactions</span>] undefined <span data-x="dom-Element-insertAdjacentHTML">insertAdjacentHTML</span>(DOMString position, <span data-x="tt-htmlstring">HTMLString</span> text);
};

partial interface <span id="ShadowRoot-partial">ShadowRoot</span> {
Expand Down Expand Up @@ -112842,6 +112843,137 @@ enum <dfn enum>DOMParserSupportedType</dfn> {

</div>

<h4>The <code data-x="dom-Element-insertAdjacentHTML">insertAdjacentHTML(<var>position</var>, <var>text</var>)</code> method</h4>

<p class="XXX">The <code data-x="dom-Element-insertAdjacentHTML">insertAdjacentHTML(<var>position</var>, <var>text</var>)</code> method has a number of outstanding issues
in the <cite>DOM Parsing and Serialization</cite> <a href="https://github.com/w3c/DOM-Parsing/issues">issue
tracker</a>, documenting various problems with its specification.</p>

<dl class="domintro">
<dt><code data-x=""><var>element</var>.<span subdfn data-x="dom-Element-insertAdjacentHTML">insertAdjacentHTML</span>(<var>position</var>, <var>text</var>)</code></dt>
<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:</p>

<dl>
<dt>"beforebegin"</dt> <dd>Before the element itself (i.e., after <var>element</var>'s previous sibling)</dd>

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

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

<dt>"afterend"</dt> <dd>After the element itself (i.e., before <var>element</var>'s next sibling)</dd>
</dl>

<p>Throws a <span>"<code>SyntaxError</code>"</span> <code>DOMException</code> if the arguments
have invalid values (e.g., in the case of an <span data-x="XML documents">XML document</span>,
if the given string is not well-formed).</p>

<p>Throws a <span>"<code>NoModificationAllowedError</code>"</span> <code>DOMException</code> if
the given position isn't possible (e.g. inserting elements after the root element of a
<code>Document</code>).</p>
</dd>
</dl>

<p class="warning">This method performs no sanitization to remove potentially-dangerous
elements and attributes like <code>script</code> or <span>event handler content
attributes</span>.</p>

<div w-nodev>

<p><code>Element</code>'s <dfn attribute for="Element"><code
data-x="dom-Element-insertAdjacentHTML">insertAdjacentHTML(<var>position</var>, <var>text</var>)</code></dfn> method steps are:
</p>

<ol>
<li><p>Let <var>context</var> be null.</p></li>

<li>Use the first matching item from this list:
<dl class=switch>
<dt>If <var>position</var> is an <span>ASCII case-insensitive</span> match for the string
"beforebegin"</dt>
<dt>If <var>position</var> is an <span>ASCII case-insensitive</span> match for the string
"afterend"</dt>
<dd>
<ol>
<li><p>Set <var>context</var> to <span>this</span>'s <span data-x="dom-parent">parent</span>.<p></li>

<li><p>If <var>context</var> is null or a <code>Document</code>, throw a
<span>"<code>NoModificationAllowedError</code>"</span> <code>DOMException</code>.<p></li>
</ol>
</dd>

<dt>If <var>position</var> is an <span>ASCII case-insensitive</span> match for the string
"afterbegin"</dt>
<dt>If <var>position</var> is an <span>ASCII case-insensitive</span> match for the string
"beforeend"</dt>
<dd>Set <var>context</var> to <span>this</span>.</dd>

<dt>Otherwise</dt>
<dd><p>Throw a <span>"<code>SyntaxError</code>"</span> <code>DOMException</code>.</p></dd>
</dl>
</li>

<li>
<p>If <var>context</var> is not an <code>Element</code> or the follow are all true:</p>

<ul>
<li><p><var>context</var>'s <span>node document</span> is an HTML document,</p></li>
<li><p><var>context</var>'s <span data-x="concept-element-local-name">local name</span> is
"html", and</p></li>
<li><p><var>context</var>'s <span data-x="concept-element-namespace">namespace</span> is the
<span>HTML namespace</span>;</p></li>
</ul>
<p>set <var>context</var> to the result of <span data-x="create an element">creating an
element</span> given <span>this</span>'s <span>node document</span>, <code>body</code>, and the
<span>HTML namespace</span>.</p>
</li>

<li>
<p>Let <var>fragment</var> be the result of invoking the <span>fragment parsing algorithm
steps</span> with <var>context</var> and <var>text</var>.</p>
</li>

<li>Use the first matching item from this list:
<dl class=switch>
<dt>If <var>position</var> is an <span>ASCII case-insensitive</span> match for the string "beforebegin"</dt>
<dd>
<p><span data-x="concept-node-insert">Insert</span> <var>fragment</var> into
<span>this</span>'s <span data-x="dom-parent">parent</span> before <span>this</span>.</p>
</dd>

<dt>If <var>position</var> is an <span>ASCII case-insensitive</span> match for the string "afterend"</dt>
<dd>
<p><span data-x="concept-node-insert">Insert</span> <var>fragment</var> into
<span>this</span> before its <span>first child</span>.</p>
</dd>

<dt>If <var>position</var> is an <span>ASCII case-insensitive</span> match for the string "afterbegin"</dt>
<dd>
<p><span data-x="concept-node-append">Append</span> <var>fragment</var> to
<span>this</span>.</p>
</dd>

<dt>If <var>position</var> is an <span>ASCII case-insensitive</span> match for the string "beforeend"</dt>
<dd>
<p><span data-x="concept-node-insert">Insert</span> <var>fragment</var> into
<span>this</span>'s <span data-x="dom-parent">parent</span> before <span>this</span>'s <span>next
sibling</span>.</p>
</dd>
</dl>
</li>
</ol>

<p class=note>As with other direct <code>Node</code>-manipulation APIs (and unlike <span
data-x="dom-Element-innerHTML">innerHTML</span>), <span
data-x="dom-Element-insertAdjacentHTML">insertAdjacentHTML</span> does not include any special
handling for <code>template</code> elements. In most cases you will want to use
<code>template</code>.<span data-x="template contents">content</span>.<span
data-x="dom-Element-insertAdjacentHTML">insertAdjacentHTML</span> instead of directly
manipulating the child nodes of a <code>template</code> element.</p>

</div>

<h3 split-filename="timers-and-user-prompts" id="timers">Timers</h3>

<p>The <code data-x="dom-setTimeout">setTimeout()</code> and <code
Expand Down

0 comments on commit d9c4198

Please sign in to comment.