Skip to content

Document doesn't track lost child #456

Description

@edemaine

Describe the bug
Since the 0.8.4 update, documents do not seem to realize that they've lost their (only) child when they get reparented.

To Reproduce
https://stackblitz.com/edit/js-xmldom-template-myg9ht?file=index.js

const doc = new DOMParser().parseFromString('<svg/>', 'image/svg+xml');
const oldRoot = doc.firstChild;
const newRoot = doc.createElement('svg');
newRoot.appendChild(oldRoot);
console.log('After first move, document has', doc.childNodes.length, 'children (should be 0)');
doc.appendChild(newRoot);
console.log('After second move, document has', doc.childNodes.length, 'children (should be 1)');

Expected behavior
newRoot.appendChild(oldRoot) should change the oldRoot's parent to be the newRoot, which should mean that the document loses its child. Running the above code in Chrome's console confirms this behavior. Instead I get:

xmldom 0.8.4 instead says that the document still has 1 child after the first move, so the second move fails according to the new one-child-per-document rule. (Error: Hierarchy request error: Only one element can be added and only after doctype)

xmldom 0.8.3 somehow says 1 child after both moves, which is why I didn't see this issue before, but it's still wrong (should be 0 after the first move).

Runtime & Version:
xmldom version: 0.8.4
runtime version: Node v18.7.0
other related software and version: WIndows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds investigationInformation is missing and needs to be researched

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions