Rewrite node insertion algorithm to match the spec (#35999)

Per [spec](https://dom.spec.whatwg.org/#concept-node-insert), adoption
of new node should be done while inserting the node. This patch moves
the call site of `adopt` to inside `insert` to match it.

It also rewrites some existing code to better match the spec without any
behavioral changes.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by
`[X]` when the step is complete, and replace `___` with appropriate
data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox
is checked, so that we can help you if you get stuck somewhere along the
way.-->

<!-- Pull requests that do not address these steps are welcome, but they
will require additional verification as part of the review process. -->

---------

Signed-off-by: Xiaocheng Hu <xiaochengh.work@gmail.com>
This commit is contained in:
Xiaocheng Hu 2025-04-28 18:56:53 +08:00 committed by GitHub
parent cf41012257
commit 02b38adf43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 96 additions and 90 deletions

View file

@ -624655,6 +624655,13 @@
{}
]
],
"Node-childNodes-cache-2.html": [
"9079fc6ea7a146cb854389bf4f1b8c302a102746",
[
null,
{}
]
],
"Node-childNodes-cache.html": [
"da9e32c6a9cf579f3d7c6ce2e3208e04f90d7105",
[

View file

@ -1,39 +0,0 @@
[adopted-callback.html]
[Moving the shadow host's shadow of a custom element from the owner document into the document of the template elements must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the shadow host's shadow of a custom element from the owner document into a new document must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the <template>'s content of a custom element from the owner document into a new document must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the shadow host's shadow of a custom element from the owner document into a cloned document must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the <template>'s content of a custom element from the owner document into a cloned document must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the shadow host's shadow of a custom element from the owner document into a document created by createHTMLDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the <template>'s content of a custom element from the owner document into a document created by createHTMLDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the shadow host's shadow of a custom element from the owner document into an HTML document created by createDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the <template>'s content of a custom element from the owner document into an HTML document created by createDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the shadow host's shadow of a custom element from the owner document into the document of an iframe must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the <template>'s content of a custom element from the owner document into the document of an iframe must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the shadow host's shadow of a custom element from the owner document into an HTML document fetched by XHR must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving the <template>'s content of a custom element from the owner document into an HTML document fetched by XHR must enqueue and invoke adoptedCallback]
expected: FAIL

View file

@ -1,12 +1,3 @@
[adoption.window.html]
[appendChild() and DocumentFragment with host]
expected: FAIL
[adoptNode() and DocumentFragment with host]
expected: FAIL
[appendChild() and DocumentFragment]
expected: FAIL
[appendChild() and ShadowRoot]
expected: FAIL

View file

@ -1,6 +1,3 @@
[mutations.window.html]
[Mutating the style element: mutating a Comment node]
expected: FAIL
[Mutating the style element: inserting an empty DocumentFragment node]
expected: FAIL

View file

@ -0,0 +1,16 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Node.childNodes caching bug with replaceChild</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-childnodes">
<link rel=author title="Xiaocheng Hu" href="mailto:xiaochengh.work@gmail.com">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="target"><div id="first"></div><div id="second"></div><div id="third"></div><div id="last"></div></div>
<script>
test(function() {
let target = document.getElementById("target");
assert_array_equals(Array.from(target.childNodes).map(node => node.id), ["first", "second", "third", "last"]);
target.replaceChild(target.childNodes[2], target.childNodes[1]);
assert_array_equals(Array.from(target.childNodes).map(node => node.id), ["first", "third", "last"]);
});
</script>