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

Inconsitent appendChild behavior from Web implementation #3802

Open
sukima opened this issue Dec 11, 2024 · 1 comment
Open

Inconsitent appendChild behavior from Web implementation #3802

sukima opened this issue Dec 11, 2024 · 1 comment

Comments

@sukima
Copy link

sukima commented Dec 11, 2024

Basic info:

  • Node.js version: 22.12.0
  • jsdom version: 25.0.1

Minimal reproduction case

Given a document with the following XML:

<bar>
  <bar id="theBar"><child-bars/></bar>
  <child-bars/>
</bar>

using appendChild to move #theBar into the parent child-bars element will generate the error [DOMException [HierarchyRequestError]: The operation would yield an incorrect node tree.].

const { JSDOM } = require('jsdom');
const xml = `
  <bar>
    <bar id="theBar"><child-bars/></bar>
    <child-bars/>
  </bar>
`.trim();
const { document } = new JSDOM(xml, { contentType: 'text/xml' }).window;
for (const bar of [...document.querySelectorAll('bar')]) {
  if (!bar.parentElement) continue;
  bar.parentElement.querySelector(':scope > child-bars').appendChild(bar);
}

How does similar code behave in browsers?

Comparatively running the same in FireFox works as expected.

const xml = `
  <bar>
    <bar id="theBar"><child-bars/></bar>
    <child-bars/>
  </bar>
`.trim();
const doc = new DOMParser().parseFromString(xml, 'text/xml');
for (const bar of [...doc.querySelectorAll('bar')]) {
  if (!bar.parentElement) continue;
  bar.parentElement.querySelector(':scope > child-bars').appendChild(bar);
}

JSFiddle Link

@domenic
Copy link
Member

domenic commented Dec 11, 2024

Hmm, can you test without using :scope? Our support for that selector has historically not been great, so I wonder if the problem might be there, instead of in appendChild().

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

No branches or pull requests

2 participants