8000 NodeList.item() returns removed nodes · Issue #499 · xmldom/xmldom · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
NodeList.item() returns removed nodes #499
Closed
@qtow

Description

@qtow

Example:

const doc = new DOMParser().parseFromString("<root><child/></root>", "text/xml")
const nodes = doc.getElementsByTagName("child");

// correct: { length: 1, item_0: "child" }
const first = {
    length: nodes.length,
    item_0: nodes.item(0)?.tagName ?? null,
};

doc.firstChild.removeChild(doc.firstChild.firstChild);

// should be: { length: 0, item_0: null}
// incorrect: { length: 0, item_0: "child" }
const second = {
    length: nodes.length,
    item_0: nodes.item(0)?.tagName ?? null,
};

console.log(first);
console.log(second);

Cause:
LiveNodeList is updated at dom.js:312 by copying all owned properties from an array. If the new array is shorter, indexes >= length are not removed.

Possible Solution:
Before or after copy(ls, list), explicitly delete the extra properties:

for (var i = ls.length; i in list; i += 1) {
    delete list[i];
}

Runtime & Version:
xmldom: 0.8.8
nodejs: 20.3.0

Additional context:
This causes problems with patterns like this:

for (let node; (node = nodes.item(0)) !== null; node.parentNode.removeChild(node)) {
    // ...
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghelp-wantedExternal contributions welcome

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0