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

replaceChildren can not replace a Document's documentElement #1045

Open
bwrrp opened this issue Jan 14, 2022 · 1 comment
Open

replaceChildren can not replace a Document's documentElement #1045

bwrrp opened this issue Jan 14, 2022 · 1 comment

Comments

@bwrrp
Copy link
Contributor

bwrrp commented Jan 14, 2022

Given a document that contains an element, and the following code:

const fragment = document.createDocumentFragment();
const element = fragment.appendChild(document.createElement('element'));
document.replaceChildren(fragment);

Expected result: document now only contains element
Actual result: HierarchyRequestError

This is caused by replaceChildren calling "ensure pre-insertion validity without taking into consideration that the existing children of the document will be removed. This then fails on the following rule:

Otherwise, if node (the fragment) has one element child and either parent (the document) has an element child, child is a doctype, or child is non-null and a doctype is following child.

Perhaps an argument should be added to make this algorithm ignore pre-existing children when called from replaceChild, or perhaps replaceChildren should use a modified copy of these steps, as was done in replaceChild?

@bwrrp bwrrp changed the title replaceChildren can not replace a Documents documentElement replaceChildren can not replace a Document's documentElement Jan 14, 2022
@annevk
Copy link
Member

annevk commented Jan 14, 2022

Thanks for noticing this! I agree it makes sense to fix this. I think ideally we figure out a way to unify these two algorithms and make it suitable for all situations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants