Skip to content

Commit 4f13f9b

Browse files
annevkdizhang168
authored andcommitted
Remove quadratic behavior from clone a node
Strictly speaking this should be an editorial change, but as it's fairly significant not marking it as such. This improves integration with HTML as well. All the arguments are exported as HTML needs them as well. And they are named parameters now for clarity. Corresponding HTML PR: whatwg/html#10859. Fixes #1219. Closes #1220.
1 parent 8a726bb commit 4f13f9b

File tree

1 file changed

+95
-68
lines changed

1 file changed

+95
-68
lines changed

dom.bs

Lines changed: 95 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -974,8 +974,8 @@ object, an <a>event listener</a> is a broader concept as can be seen above.
974974
which takes an <a>event</a> <var>event</var>, and returns an {{EventTarget}} object. Unless
975975
specified otherwise it returns null.
976976

977-
<p class=note><a for=/>Nodes</a>, <a for=/>shadow roots</a>, and <a>documents</a>
978-
override the <a>get the parent</a> algorithm.
977+
<p class=note><a for=/>Nodes</a>, <a for=/>shadow roots</a>, and <a for=/>documents</a> override the
978+
<a>get the parent</a> algorithm.
979979

980980
<p>Each {{EventTarget}} object can have an associated
981981
<dfn export for=EventTarget>activation behavior</dfn> algorithm. The
@@ -2220,7 +2220,7 @@ It is represented as follows:
22202220

22212221
<ul class=domTree>
22222222
<li>
2223-
<a>Document</a>
2223+
<a for=/>Document</a>
22242224
<ul>
22252225
<li class=t10><a>Doctype</a>: <code>html</code>
22262226
<li class=t1>{{Element}}: <code>html</code> <span class=t2><code class="attribute name">class</code>="<code class="attribute value">e</code>"</span>
@@ -4107,7 +4107,7 @@ interface Node : EventTarget {
41074107
[CEReactions] attribute DOMString? textContent;
41084108
[CEReactions] undefined normalize();
41094109

4110-
[CEReactions, NewObject] Node cloneNode(optional boolean deep = false);
4110+
[CEReactions, NewObject] Node cloneNode(optional boolean subtree = false);
41114111
boolean isEqualNode(Node? otherNode);
41124112
boolean isSameNode(Node? otherNode); // legacy alias of ===
41134113

@@ -4304,7 +4304,7 @@ statement, switching on the interface <a>this</a> <a>implements</a>:
43044304
<dt><code><var>node</var> . {{Node/ownerDocument}}</code>
43054305
<dd>
43064306
Returns the <a for=Node>node document</a>.
4307-
Returns null for <a>documents</a>.
4307+
Returns null for <a for=/>documents</a>.
43084308

43094309
<dt><code><var>node</var> . {{Node/getRootNode()}}</code>
43104310
<dd>Returns <var>node</var>'s <a for=tree>root</a>.
@@ -4531,9 +4531,9 @@ each <a for=tree>descendant</a> <a>exclusive <code>Text</code> node</a> <var>nod
45314531
<hr>
45324532

45334533
<dl class=domintro>
4534-
<dt><code><var>node</var> . <a method for=Node lt=cloneNode()>cloneNode([<var>deep</var> = false])</a></code>
4534+
<dt><code><var>node</var> . <a method for=Node lt=cloneNode()>cloneNode([<var>subtree</var> = false])</a></code>
45354535
<dd>Returns a copy of <var>node</var>. If
4536-
<var>deep</var> is true, the copy also includes the
4536+
<var>subtree</var> is true, the copy also includes the
45374537
<var>node</var>'s <a for=tree>descendants</a>.
45384538

45394539
<dt><code><var>node</var> . {{Node/isEqualNode(otherNode)}}</code>
@@ -4543,33 +4543,83 @@ each <a for=tree>descendant</a> <a>exclusive <code>Text</code> node</a> <var>nod
45434543

45444544
<div class=impl>
45454545

4546+
<div algorithm>
45464547
<a lt="Other applicable specifications">Specifications</a> may define
45474548
<dfn export id=concept-node-clone-ext>cloning steps</dfn> for all or some <a for=/>nodes</a>. The
4548-
algorithm is passed <var>copy</var>, <var>node</var>, <var>document</var>, and an optional
4549-
<i>clone children flag</i>, as indicated in the <a lt="clone a node">clone</a> algorithm.
4549+
algorithm is passed <var ignore>node</var>, <var ignore>copy</var>, and <var ignore>subtree</var> as
4550+
indicated in the <a>clone a node</a> algorithm.
45504551

4551-
<p class=note>HTML defines <a>cloning steps</a> for <{script}> and <{input}>
4552-
elements. SVG ought to do the same for its <{script}> elements, but does not call this out
4553-
at the moment.
4552+
<p class=note>HTML defines <a>cloning steps</a> for several elements, such as <{input}>, <{script}>,
4553+
and <{template}>. SVG ought to do the same for its <{script}> elements, but does not.
4554+
</div>
45544555

4555-
<p>To <dfn export id=concept-node-clone lt="clone a node" local-lt="clone">clone</dfn> a
4556-
<var>node</var>, with an optional <var>document</var> and <i>clone children flag</i>, run these
4557-
steps:
4558-
<!-- This algorithm is used by dom-Node-cloneNode, dom-Document-importNode,
4559-
dom-Range-extractContents, dom-Range-cloneContents -->
4556+
<div algorithm="clone a node">
4557+
<p>To <dfn export id=concept-node-clone>clone a node</dfn> given a <a for=/>node</a> <var>node</var>
4558+
and an optional <a for=/>document</a> <dfn export for="clone a node"><var>document</var></dfn>
4559+
(default <var>node</var>'s <a for=Node>node document</a>), boolean
4560+
<dfn export for="clone a node"><var>subtree</var></dfn> (default false), and
4561+
<a for=/>node</a>-or-null <dfn export for="clone a node"><var>parent</var></dfn> (default null):
45604562

45614563
<ol>
4562-
<li><p>If <var>document</var> is not given, then set <var>document</var> to <var>node</var>'s
4563-
<a for=Node>node document</a>.
4564+
<li><p><a for=/>Assert</a>: <var>node</var> is not a <a for=/>document</a> or <var>node</var> is
4565+
<var>document</var>.
45644566

4565-
<li><a for=/>Assert</a>: <var>node</var> is not a <a for=/>document</a> or <var>node</var> is
4567+
<li><p>Let <var>copy</var> be the result of <a>cloning a single node</a> given <var>node</var> and
45664568
<var>document</var>.
45674569

4570+
<li><p>Run any <a>cloning steps</a> defined for <var>node</var> in
4571+
<a>other applicable specifications</a> and pass <var>node</var>, <var>copy</var>, and
4572+
<var>subtree</var> as parameters.
4573+
4574+
<li><p>If <var>parent</var> is non-null, then <a for=/>append</a> <var>copy</var> to
4575+
<var>parent</var>.
4576+
4577+
<li><p>If <var>subtree</var> is true, then for each <var>child</var> of <var>node</var>'s
4578+
<a for=tree>children</a>, in <a>tree order</a>: <a>clone a node</a> given <var>child</var> with
4579+
<a for="clone a node"><i>document</i></a> set to <var>document</var>,
4580+
<a for="clone a node"><i>subtree</i></a> set to <var>subtree</var>, and
4581+
<a for="clone a node"><i>parent</i></a> set to <var>copy</var>.
4582+
4583+
<li>
4584+
<p>If <var>node</var> is an <a for=/>element</a>, <var>node</var> is a
4585+
<a for=Element>shadow host</a>, and <var>node</var>'s <a for=Element>shadow root</a>'s
4586+
<a for=ShadowRoot>clonable</a> is true:
4587+
4588+
<ol>
4589+
<li><p><a for=/>Assert</a>: <var>copy</var> is not a <a for=Element>shadow host</a>.
4590+
4591+
<li><p><a>Attach a shadow root</a> with <var>copy</var>, <var>node</var>'s
4592+
<a for=Element>shadow root</a>'s <a for=ShadowRoot>mode</a>, true, <var>node</var>'s
4593+
<a for=Element>shadow root</a>'s <a for=ShadowRoot>serializable</a>, <var>node</var>'s
4594+
<a for=Element>shadow root</a>'s <a for=ShadowRoot>delegates focus</a>, and <var>node</var>'s
4595+
<a for=Element>shadow root</a>'s <a for=ShadowRoot>slot assignment</a>.
4596+
4597+
<li><p>Set <var>copy</var>'s <a for=Element>shadow root</a>'s <a for=ShadowRoot>declarative</a>
4598+
to <var>node</var>'s <a for=Element>shadow root</a>'s <a for=ShadowRoot>declarative</a>.
4599+
4600+
<li><p>For each <var>child</var> of <var>node</var>'s <a for=Element>shadow root</a>'s
4601+
<a for=tree>children</a>, in <a>tree order</a>: <a>clone a node</a> given <var>child</var> with
4602+
<a for="clone a node"><i>document</i></a> set to <var>document</var>,
4603+
<a for="clone a node"><i>subtree</i></a> set to <var>subtree</var>, and
4604+
<a for="clone a node"><i>parent</i></a> set to <var>copy</var>'s <a for=Element>shadow root</a>.
4605+
</ol>
4606+
4607+
<li><p>Return <var>copy</var>.
4608+
</ol>
4609+
</div>
4610+
4611+
<div algorithm>
4612+
<p>To <dfn>clone a single node</dfn> given a <a for=/>node</a> <var>node</var> and
4613+
<a for=/>document</a> <var>document</var>:
4614+
4615+
<ol>
4616+
<li><p>Let <var>copy</var> be null.
4617+
45684618
<li>
45694619
<p>If <var>node</var> is an <a for=/>element</a>:
45704620

45714621
<ol>
4572-
<li><p>Let <var>copy</var> be the result of <a>creating an element</a>, given
4622+
<li><p>Set <var>copy</var> to the result of <a>creating an element</a>, given
45734623
<var>document</var>, <var>node</var>'s <a for=Element>local name</a>, <var>node</var>'s
45744624
<a for=Element>namespace</a>, <var>node</var>'s <a for=Element>namespace prefix</a>, and
45754625
<var>node</var>'s <a><code>is</code> value</a>.
@@ -4579,16 +4629,15 @@ dom-Range-extractContents, dom-Range-cloneContents -->
45794629
<a for=Element>attribute list</a>:
45804630

45814631
<ol>
4582-
<li><p>Let <var>copyAttribute</var> be a <a>clone</a> of <var>attribute</var>.
4632+
<li><p>Let <var>copyAttribute</var> be the result of <a>cloning a single node</a> given
4633+
<var>attribute</var> and <var>document</var>.
45834634

45844635
<li><p><a lt="append an attribute">Append</a> <var>copyAttribute</var> to <var>copy</var>.
45854636
</ol>
4586-
</li>
45874637
</ol>
4588-
</li>
45894638

45904639
<li>
4591-
<p>Otherwise, let <var>copy</var> be a <a for=/>node</a> that <a>implements</a> the same
4640+
<p>Otherwise, set <var>copy</var> to a <a for=/>node</a> that <a>implements</a> the same
45924641
interfaces as <var>node</var>, and fulfills these additional requirements, switching on the
45934642
interface <var>node</var> <a>implements</a>:
45944643

@@ -4618,54 +4667,28 @@ dom-Range-extractContents, dom-Range-cloneContents -->
46184667
<dd><p>Do nothing.
46194668
</dl>
46204669

4670+
<li><p><a for=/>Assert</a>: <var>copy</var> is a <a for=/>node</a>.
4671+
46214672
<li><p>If <var>node</var> is a <a for=/>document</a>, then set <var>document</var> to
46224673
<var>copy</var>.
46234674

46244675
<li><p>Set <var>copy</var>'s <a for=Node>node document</a> to <var>document</var>.
46254676

4626-
<li><p>Run any <a>cloning steps</a> defined for <var>node</var> in
4627-
<a>other applicable specifications</a> and pass <var>copy</var>, <var>node</var>,
4628-
<var>document</var>, and the <i>clone children flag</i> if set, as parameters.
4629-
4630-
<li><p>If the <i>clone children flag</i> is set, then for each <a for=tree>child</a>
4631-
<var>child</var> of <var>node</var>, in <a>tree order</a>: <a>append</a> the result of
4632-
<a lt="clone a node">cloning</a> <var>child</var> with <var>document</var> and the
4633-
<i>clone children flag</i> set, to <var>copy</var>.
4634-
4635-
<li>
4636-
<p>If <var>node</var> is a <a for=Element>shadow host</a> whose <a for=/>shadow root</a>'s
4637-
<a for=ShadowRoot>clonable</a> is true:
4638-
4639-
<ol>
4640-
<li><p>Assert: <var>copy</var> is not a <a for=Element>shadow host</a>.
4641-
4642-
<li><p>Run <a>attach a shadow root</a> with <var>copy</var>, <var>node</var>'s
4643-
<a for=Element>shadow root</a>'s <a for=ShadowRoot>mode</a>, true, <var>node</var>'s
4644-
<a for=Element>shadow root</a>'s <a for=ShadowRoot>serializable</a>, <var>node</var>'s
4645-
<a for=Element>shadow root</a>'s <a for=ShadowRoot>delegates focus</a>, and <var>node</var>'s
4646-
<a for=Element>shadow root</a>'s <a for=ShadowRoot>slot assignment</a>.
4647-
4648-
<li><p>Set <var>copy</var>'s <a for=Element>shadow root</a>'s <a for=ShadowRoot>declarative</a>
4649-
to <var>node</var>'s <a for=Element>shadow root</a>'s <a for=ShadowRoot>declarative</a>.
4650-
4651-
<li><p>For each <a for=tree>child</a> <var>child</var> of <var>node</var>'s
4652-
<a for=Element>shadow root</a>, in <a>tree order</a>: <a>append</a> the result of
4653-
<a lt="clone a node">cloning</a> <var>child</var> with <var>document</var> and the
4654-
<i>clone children flag</i> set, to <var>copy</var>'s <a for=Element>shadow root</a>.
4655-
</ol>
4656-
46574677
<li><p>Return <var>copy</var>.
46584678
</ol>
4679+
</div>
46594680

4660-
<p>The <dfn method for=Node><code>cloneNode(<var>deep</var>)</code></dfn> method steps are:
4681+
<div algorithm>
4682+
<p>The <dfn method for=Node><code>cloneNode(<var>subtree</var>)</code></dfn> method steps are:
46614683

46624684
<ol>
46634685
<li><p>If <a>this</a> is a <a for=/>shadow root</a>, then <a>throw</a> a
46644686
"{{NotSupportedError!!exception}}" {{DOMException}}.
46654687

4666-
<li><p>Return a <a lt="clone a node">clone</a> of <a>this</a>, with the
4667-
<i>clone children flag</i> set if <var>deep</var> is true.
4688+
<li><p>Return the result of <a>cloning a node</a> given <a>this</a> with
4689+
<a for="clone a node"><i>subtree</i></a> set to <var>subtree</var>.
46684690
</ol>
4691+
</div>
46694692

46704693
<p>A <a for=/>node</a> <var>A</var> <dfn export for=Node id=concept-node-equals>equals</dfn> a
46714694
<a for=/>node</a> <var>B</var> if all of the following conditions are true:
@@ -5137,7 +5160,7 @@ interface Document : Node {
51375160
[NewObject] Comment createComment(DOMString data);
51385161
[NewObject] ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);
51395162

5140-
[CEReactions, NewObject] Node importNode(Node node, optional boolean deep = false);
5163+
[CEReactions, NewObject] Node importNode(Node node, optional boolean subtree = false);
51415164
[CEReactions] Node adoptNode(Node node);
51425165

51435166
[NewObject] Attr createAttribute(DOMString localName);
@@ -5198,9 +5221,9 @@ is "<code>quirks</code>", and
51985221
<a for=Document>mode</a> is "<code>limited-quirks</code>".
51995222

52005223
<div class=note>
5201-
<p>The <a for=Document>mode</a> is only ever changed from the default for <a>documents</a> created
5202-
by the <a>HTML parser</a> based on the presence, absence, or value of the DOCTYPE string, and by a
5203-
new <a for=/>browsing context</a> (initial "<code>about:blank</code>"). [[!HTML]]
5224+
<p>The <a for=Document>mode</a> is only ever changed from the default for <a for=/>documents</a>
5225+
created by the <a>HTML parser</a> based on the presence, absence, or value of the DOCTYPE string,
5226+
and by a new <a for=/>browsing context</a> (initial "<code>about:blank</code>"). [[!HTML]]
52045227

52055228
<p><a>No-quirks mode</a> was originally known as "standards mode" and <a>limited-quirks mode</a>
52065229
was once known as "almost standards mode". They have been renamed because their details are now
@@ -5564,9 +5587,9 @@ method steps are:
55645587
<hr>
55655588

55665589
<dl class=domintro>
5567-
<dt><var>clone</var> = <var>document</var> . <a method for=Document lt=importNode()>importNode(<var>node</var> [, <var>deep</var> = false])</a>
5590+
<dt><var>clone</var> = <var>document</var> . <a method for=Document lt=importNode()>importNode(<var>node</var> [, <var>subtree</var> = false])</a>
55685591
<dd>
5569-
<p>Returns a copy of <var>node</var>. If <var>deep</var> is true, the copy also includes the
5592+
<p>Returns a copy of <var>node</var>. If <var>subtree</var> is true, the copy also includes the
55705593
<var>node</var>'s <a for=tree>descendants</a>.
55715594

55725595
<p>If <var>node</var> is a <a for=/>document</a> or a <a for=/>shadow root</a>, throws a
@@ -5583,16 +5606,19 @@ method steps are:
55835606
"{{HierarchyRequestError!!exception}}" {{DOMException}}.
55845607
</dl>
55855608

5586-
<p>The <dfn method for=Document><code>importNode(<var>node</var>, <var>deep</var>)</code></dfn>
5609+
<div algorithm>
5610+
<p>The <dfn method for=Document><code>importNode(<var>node</var>, <var>subtree</var>)</code></dfn>
55875611
method steps are:
55885612

55895613
<ol>
55905614
<li><p>If <var>node</var> is a <a for=/>document</a> or <a for=/>shadow root</a>, then <a>throw</a>
55915615
a "{{NotSupportedError!!exception}}" {{DOMException}}.
55925616

5593-
<li><p>Return a <a lt="clone a node">clone</a> of <var>node</var>, with <a>this</a> and the
5594-
<i>clone children flag</i> set if <var>deep</var> is true.
5617+
<li><p>Return the result of <a>cloning a node</a> given <var>node</var> with
5618+
<a for="clone a node"><i>document</i></a> set to <a>this</a> and
5619+
<a for="clone a node"><i>subtree</i></a> set to <var>subtree</var>.
55955620
</ol>
5621+
</div>
55965622

55975623
<p><a lt="Other applicable specifications">Specifications</a> may define
55985624
<dfn export id=concept-node-adopt-ext>adopting steps</dfn> for all or some <a for=/>nodes</a>. The
@@ -10432,6 +10458,7 @@ Daniel Clark,
1043210458
Daniel Glazman,
1043310459
Darien Maillet Valentine<!-- bhathos; GitHub -->,
1043410460
Darin Fisher,
10461+
David Baron,
1043510462
David Bruant,
1043610463
David Flanagan,
1043710464
David Håsäther,

0 commit comments

Comments
 (0)