Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ParentNode.prototype.replaceContents(nodes) #755

Conversation

jonathantneal
Copy link

The ParentNode.replaceContents() method would remove all child nodes from the ParentNode while allowing the insertion of a new set of nodes.

Resolves #478

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for starting on this so quickly! I think this needs a slightly different approach, which I've tried to outline inline. Basically what I'd like to see is for this to build upon the same primitive innerHTML and textContent use, but it'll need some additional checks as the input isn't necessary restricted to Text and Element nodes as it is with those APIs.

@@ -2839,6 +2839,7 @@ interface mixin ParentNode {

[CEReactions, Unscopable] void prepend((Node or DOMString)... nodes);
[CEReactions, Unscopable] void append((Node or DOMString)... nodes);
[CEReactions, Unscopable] void replaceContents((Node or DOMString)... nodes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think per the discussion we ended up with replaceChildren() as a name, as "contents" doesn't always refer to nodes. Did I miss something?

@@ -2878,6 +2879,15 @@ Element includes ParentNode;
the <a>node tree</a> are violated.
<!-- "NotFoundError" is impossible -->

<dt><code><var>node</var> . <a method for=ParentNode lt="replaceContents()">replaceContents</a>(<var>nodes</var>)</code>
<dd>
<p>Inserts <var>nodes</var> after the <a>last child</a> of <var>node</var>, while replacing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs rewording.

<li><a for=/>Remove</a> all
<var>parent</var>'s <a>children</a>, in
<a>tree order</a>, with the
<i>suppress observers flag</i> set.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should strive to use the https://dom.spec.whatwg.org/#concept-node-replace-all primitive here, which would take care of most of the work, preceded by a call to https://dom.spec.whatwg.org/#converting-nodes-into-a-node.

And I guess we need to run https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity as well.

@jonathantneal
Copy link
Author

I intend to make corrections next week. Thanks for the great feedback, and also for getting it back to me so quickly.

@annevk annevk added do not merge yet Pull request must not be merged per rationale in comment needs tests Moving the issue forward requires someone to write tests labels Dec 6, 2019
@annevk annevk closed this in #851 Apr 20, 2020
annevk pushed a commit that referenced this pull request Apr 20, 2020
mfreed7 pushed a commit to mfreed7/dom that referenced this pull request Apr 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge yet Pull request must not be merged per rationale in comment needs tests Moving the issue forward requires someone to write tests
Development

Successfully merging this pull request may close these issues.

Proposal: ParentNode.replaceAll() / ParentNode.replaceChildren()
2 participants