@xmldom/xmldom 0.9.4 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/lib/dom.js +17 -15
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.9.5](https://github.com/xmldom/xmldom/compare/0.9.4...0.9.5)
8
+
9
+ ### Fixed
10
+
11
+ - fix: re-index childNodes on insertBefore [`#763`](https://github.com/xmldom/xmldom/issues/763) / [`#766`](https://github.com/xmldom/xmldom/pull/766)
12
+
13
+ Thank you,
14
+ [@mureinik](https://github.com/mureinik),
15
+ for your contributions.
16
+
17
+
7
18
  ## [0.9.4](https://github.com/xmldom/xmldom/compare/0.9.3...0.9.4)
8
19
 
9
20
  ### Fixed
package/lib/dom.js CHANGED
@@ -1623,37 +1623,39 @@ function _onRemoveAttribute(doc, el, newAttr, remove) {
1623
1623
  }
1624
1624
 
1625
1625
  /**
1626
- * Updates `el.childNodes`, adjusting the indexed items and its `length`.
1627
- * If `newChild` is provided, it will be appended to the childNodes list.
1628
- * Otherwise, it's assumed that an item has been removed,
1629
- * and `el.firstNode` and its `.nextSibling` are used to iterate over the current list of child
1630
- * nodes, effectively reindexing them.
1626
+ * Updates `parent.childNodes`, adjusting the indexed items and its `length`.
1627
+ * If `newChild` is provided and has no nextSibling, it will be appended.
1628
+ * Otherwise, it's assumed that an item has been removed or inserted,
1629
+ * and `parent.firstNode` and its `.nextSibling` to re-indexing all child nodes of `parent`.
1631
1630
  *
1632
1631
  * @param {Document} doc
1633
1632
  * The parent document of `el`.
1634
- * @param {Node} el
1633
+ * @param {Node} parent
1635
1634
  * The parent node whose childNodes list needs to be updated.
1636
1635
  * @param {Node} [newChild]
1637
1636
  * The new child node to be appended. If not provided, the function assumes a node has been
1638
1637
  * removed.
1639
1638
  * @private
1640
1639
  */
1641
- function _onUpdateChild(doc, el, newChild) {
1640
+ function _onUpdateChild(doc, parent, newChild) {
1642
1641
  if (doc && doc._inc) {
1643
1642
  doc._inc++;
1644
- //update childNodes
1645
- var cs = el.childNodes;
1646
- if (newChild) {
1647
- cs[cs.length++] = newChild;
1643
+ var childNodes = parent.childNodes;
1644
+ // assumes nextSibling and previousSibling were already configured upfront
1645
+ if (newChild && !newChild.nextSibling) {
1646
+ // if an item has been appended, we only need to update the last index and the length
1647
+ childNodes[childNodes.length++] = newChild;
1648
1648
  } else {
1649
- var child = el.firstChild;
1649
+ // otherwise we need to reindex all items,
1650
+ // which can take a while when processing nodes with a lot of children
1651
+ var child = parent.firstChild;
1650
1652
  var i = 0;
1651
1653
  while (child) {
1652
- cs[i++] = child;
1654
+ childNodes[i++] = child;
1653
1655
  child = child.nextSibling;
1654
1656
  }
1655
- cs.length = i;
1656
- delete cs[cs.length];
1657
+ childNodes.length = i;
1658
+ delete childNodes[childNodes.length];
1657
1659
  }
1658
1660
  }
1659
1661
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xmldom/xmldom",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "description": "A pure JavaScript W3C standard-based (XML DOM Level 2 Core) DOMParser and XMLSerializer module.",
5
5
  "keywords": [
6
6
  "w3c",