Closed
Description
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
Type
Projects
Status
Done