Skip to content

Commit 6e34f11

Browse files
committed
Upstream innerHTML from DOM Parsing spec
This includes the innerHTML property on Element and ShadowRoot. The fragment serializing algorithm steps and fragment parsing algorithm steps are also upstreamed.
1 parent 11ea4a2 commit 6e34f11

File tree

1 file changed

+119
-5
lines changed

1 file changed

+119
-5
lines changed

source

Lines changed: 119 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,8 +3369,8 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
33693369
<ref>DOMPARSING</ref></p>
33703370

33713371
<ul class="brief">
3372-
<li><dfn data-x="dom-innerHTML" data-x-href="https://w3c.github.io/DOM-Parsing/#dom-element-innerhtml"><code>innerHTML</code></dfn></li>
33733372
<li><dfn data-x="dom-outerHTML" data-x-href="https://w3c.github.io/DOM-Parsing/#dom-element-outerhtml"><code>outerHTML</code></dfn></li>
3373+
<li><dfn data-x="xml-serialization" data-x-href="https://w3c.github.io/DOM-Parsing/#dfn-xml-serialization"><code>XML serialization</code></dfn></li>
33743374
</ul>
33753375

33763376
<p>The following features are defined in <cite>Selection API</cite>: <ref>SELECTION</ref></p>
@@ -55937,7 +55937,7 @@ interface <dfn interface>HTMLLegendElement</dfn> : <span>HTMLElement</span> {
5593755937
owner</span> of "e" would be the outer form "a".</p>
5593855938

5593955939
<p>This happens as follows: First, the "e" node gets associated with "c" in the <span>HTML
55940-
parser</span>. Then, the <code data-x="dom-innerHTML">innerHTML</code> algorithm moves the nodes
55940+
parser</span>. Then, the <code data-x="dom-element-innerHTML">innerHTML</code> algorithm moves the nodes
5594155941
from the temporary document to the "b" element. At this point, the nodes see their ancestor chain
5594255942
change, and thus all the "magic" associations done by the parser are reset to normal ancestor
5594355943
associations.</p>
@@ -61875,7 +61875,7 @@ interface <dfn interface>HTMLScriptElement</dfn> : <span>HTMLElement</span> {
6187561875
<p class="note">When inserted using the <code data-x="dom-document-write">document.write()</code>
6187661876
method, <code>script</code> elements <a href="#document-written-scripts-intervention">usually</a>
6187761877
execute (typically blocking further script execution or HTML parsing). When inserted using the
61878-
<code data-x="dom-innerHTML">innerHTML</code> and <code data-x="dom-outerHTML">outerHTML</code>
61878+
<code data-x="dom-element-innerHTML">innerHTML</code> and <code data-x="dom-outerHTML">outerHTML</code>
6187961879
attributes, they do not execute at all.</p>
6188061880

6188161881
<p>The <code data-x="attr-script-defer">defer</code> attribute may be specified even if the <code
@@ -112384,11 +112384,13 @@ document.body.appendChild(frame)</code></pre>
112384112384
<pre><code class="idl">partial interface <span id="Element-partial">Element</span> {
112385112385
[<span>CEReactions</span>] undefined <span data-x="dom-Element-setHTMLUnsafe">setHTMLUnsafe</span>(<span data-x="tt-htmlstring">HTMLString</span> html);
112386112386
DOMString <span data-x="dom-Element-getHTML">getHTML</span>(optional <span>GetHTMLOptions</span> options = {});
112387+
[<span>CEReactions</span>] attribute [<span>LegacyNullToEmptyString</span>] <span data-x="tt-htmlstring">HTMLString</span> <span data-x="dom-Element-innerHTML">innerHTML</span>;
112387112388
};
112388112389

112389112390
partial interface <span id="ShadowRoot-partial">ShadowRoot</span> {
112390112391
[<span>CEReactions</span>] undefined <span data-x="dom-ShadowRoot-setHTMLUnsafe">setHTMLUnsafe</span>(<span data-x="tt-htmlstring">HTMLString</span> html);
112391112392
DOMString <span data-x="dom-ShadowRoot-getHTML">getHTML</span>(optional <span>GetHTMLOptions</span> options = {});
112393+
[<span>CEReactions</span>] attribute [<span>LegacyNullToEmptyString</span>] <span data-x="tt-htmlstring">HTMLString</span> <span data-x="dom-ShadowRoot-innerHTML">innerHTML</span>;
112392112394
};
112393112395

112394112396
dictionary <dfn dictionary>GetHTMLOptions</dfn> {
@@ -112691,6 +112693,118 @@ enum <dfn enum>DOMParserSupportedType</dfn> {
112691112693

112692112694
</div>
112693112695

112696+
<h4>innerHTML property</h4>
112697+
112698+
<dl class="domintro">
112699+
<dt><code data-x=""><var>element</var>.<span subdfn data-x="dom-Element-innerHTML">innerHTML</span> [ = <var>value</var> ]</code></dt>
112700+
<dd>
112701+
<p>Returns a fragment of HTML or XML that represents the element's contents.
112702+
112703+
<p>Can be set, to replace the contents of the element with nodes parsed from the given string.
112704+
112705+
<p>In the case of an XML document, throws a <span>"<code>InvalidStateError</code>"</span>
112706+
<code>DOMException</code> if the element cannot be serialized to XML, or a
112707+
<span>"<code>SyntaxError</code>"</span> <code>DOMException</code> if the given string is not
112708+
well-formed.
112709+
</dd>
112710+
112711+
<dt><code data-x=""><var>shadowRoot</var>.<span subdfn data-x="dom-ShadowRoot-innerHTML">innerHTML</span> [ = <var>value</var> ]</code></dt>
112712+
<dd>
112713+
<p>Returns a fragment of HTML that represents the shadow roots's contents.
112714+
112715+
<p>Can be set, to replace the contents of the shadow root with nodes parsed from the given
112716+
string.</p>
112717+
</dd>
112718+
</dl>
112719+
112720+
<p class="warning">These properties perform no sanitization to remove potentially-dangerous elements
112721+
and attributes like <code>script</code> or <span>event handler content attributes</span>.</p>
112722+
112723+
<div w-nodev>
112724+
112725+
<p>The <dfn export>fragment serializing algorithm steps</dfn>, given a DOM <span>Element</span>
112726+
or <span>DocumentFragment</span> referred to as <var>node</var> and a flag <dfn>require
112727+
well-formed</dfn>, are:</p>
112728+
112729+
<ol>
112730+
<li><p>Let <var>context document</var> be the value of <var>node</var>'s <span>node
112731+
document</span>.</p></li>
112732+
112733+
<li><p>If <var>context document</var> is an <span data-x="HTML documents">HTML document</span>,
112734+
return the result of <span>HTML fragment serialization algorithm</span> with <var>node</var>,
112735+
false, and « ».</p></li>
112736+
112737+
<li><p>Otherwise, <var>context document</var> is an <span data-x="XML documents">XML
112738+
document</span>; return an <span data-x="xml-serialization">XML serialization</span> of
112739+
<var>node</var> passing the flag <var>require well-formed</var>.</p></li>
112740+
</ol>
112741+
112742+
<p>The <dfn export>fragment parsing algorithm steps</dfn>, given a DOM <span>Element</span> or
112743+
<span>DocumentFragment</span> referred to as <var>context element</var> and string
112744+
<var>markup</var>, are:</p>
112745+
112746+
<ol>
112747+
<li><p>If the <var>context element</var>'s <span>node document</span> is an <span data-x="HTML
112748+
documents">HTML document</span>: let <var>algorithm</var> be the <span>HTML fragment parsing
112749+
algorithm</span>.</p></li>
112750+
112751+
<li><p>If the <var>context element</var>'s <span>node document</span> is an <span data-x="XML
112752+
documents">XML document</span>: let <var>algorithm</var> be the <span>XML fragment parsing
112753+
algorithm</span>.</p></li>
112754+
112755+
<li><p>Let <var>new children</var> be the result of invoking <var>algorithm</var> with
112756+
<var>markup</var> as the <var>input</var>, and <var>context element</var> as the <var
112757+
data-x="concept-frag-parse-context">context</var> element.</p></li>
112758+
112759+
<li><p>Let <var>fragment</var> be a new <span>DocumentFragment</span> whose <span>node
112760+
document</span> is <var>context element</var>'s <span>node document</span>.</p></li>
112761+
112762+
<li>
112763+
<p><span data-x="concept-node-append">Append</span> each <span>Node</span> in <var>new
112764+
children</var> to <var>fragment</var> (in <span>tree order</span>).</p>
112765+
112766+
<p class=note>This ensures the <span>node document</span> for the new <span
112767+
data-x="node">nodes</span> is correct.</p>
112768+
</li>
112769+
112770+
<li><p>Return the value of <var>fragment</var>.</p></li>
112771+
</ol>
112772+
112773+
<p>The <dfn attribute for="Element"><code
112774+
data-x="dom-Element-innerHTML">element.innerHTML</code></dfn> and <dfn attribute
112775+
for="ShadowRoot"><code data-x="dom-ShadowRoot-innerHTML">shadowRoot.innerHTML</code></dfn> getter
112776+
steps are to return the result of running <span>fragment serializing algorithm steps</span> with
112777+
<span>this</span>, and true.</p>
112778+
112779+
<p>The <code data-x="dom-Element-innerHTML">element.innerHTML</code> and <code
112780+
data-x="dom-ShadowRoot-innerHTML">shadowRoot.innerHTML</code> setters steps are:</p>
112781+
112782+
<ol>
112783+
<li><p>Let <var>context object</var> be <span>this</span>.</p></li>
112784+
<li><p>Let <var>context element</var> be the <var>context object</var>'s <span
112785+
data-x="concept-DocumentFragment-host">host</span> if <var>this</var> is a
112786+
<span>ShadowRoot</span> object, or <var>this</var> otherwise.</p></li>
112787+
112788+
<li><p>Let <var>fragment</var> be the result of invoking the <span>fragment parsing algorithm
112789+
steps</span> with <var>context element</var>, and the given value.</p></li>
112790+
112791+
<li>
112792+
<p>If <var>context object</var> is a <span>template</span> element, then let <var>context
112793+
object</var> be the <span>template</span> element's <span>template contents</span> (a
112794+
<span>DocumentFragment</span>).</p>
112795+
112796+
<p class=note>Setting <code data-x="dom-Element-innerHTML">innerHTML</code> on a
112797+
<span>template</span> element will replace all the nodes in its <span>template contents</span>
112798+
(<span>template</span>.<span data-x="template contents">contents</span>) rather than its <span
112799+
data-x="concept-tree-child">children</span>.</p>
112800+
</li>
112801+
112802+
<li><p><span data-x="concept-node-replace-all">Replace all</span> with <var>fragment</var> within
112803+
<var>context object</var>.</p></li>
112804+
</ol>
112805+
112806+
</div>
112807+
112694112808
<h3 split-filename="timers-and-user-prompts" id="timers">Timers</h3>
112695112809

112696112810
<p>The <code data-x="dom-setTimeout">setTimeout()</code> and <code
@@ -131459,10 +131573,10 @@ document.body.appendChild(text);
131459131573

131460131574
<p>This can enable cross-site scripting attacks. An example of this would be a page that lets the
131461131575
user enter some font family names that are then inserted into a CSS <code>style</code> block via
131462-
the DOM and which then uses the <code data-x="dom-innerHTML">innerHTML</code> IDL attribute to get
131576+
the DOM and which then uses the <code data-x="dom-element-innerHTML">innerHTML</code> IDL attribute to get
131463131577
the HTML serialization of that <code>style</code> element: if the user enters
131464131578
"<code data-x="">&lt;/style>&lt;script>attack&lt;/script></code>" as a font family name, <code
131465-
data-x="dom-innerHTML">innerHTML</code> will return markup that, if parsed in a different context,
131579+
data-x="dom-element-innerHTML">innerHTML</code> will return markup that, if parsed in a different context,
131466131580
would contain a <code>script</code> node, even though no <code>script</code> node existed in the
131467131581
original DOM.</p>
131468131582

0 commit comments

Comments
 (0)