@miaskiewicz/turbo-dom 0.1.47 → 0.1.49

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miaskiewicz/turbo-dom",
3
- "version": "0.1.47",
3
+ "version": "0.1.49",
4
4
  "description": "Faster, more spec-correct DOM for test runners — native html5ever (Rust/WASM) parser + lazy copy-on-write DOM. A drop-in-style alternative to jsdom/happy-dom for vitest & jest.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -627,14 +627,23 @@ export class Element extends Node {
627
627
  }
628
628
 
629
629
  // ---- element-only traversal (live) ----
630
+ // version-cache the element-filtered child array (like cachedQSA): re-filter only
631
+ // when the DOM version changes, not on every children/childElementCount/first/last
632
+ // access. Live — every mutation bumps Document.__version, invalidating the cache.
633
+ __elementChildren() {
634
+ const d = this.ownerDocument, v = d ? (d.__version || 0) : 0;
635
+ if (this.__childrenArr !== undefined && this.__childrenArrV === v) return this.__childrenArr;
636
+ this.__childrenArrV = v;
637
+ return (this.__childrenArr = this.__children().filter((n) => n.nodeType === ELEMENT_NODE));
638
+ }
630
639
  get children() {
631
640
  if (this.__childrenList) return this.__childrenList;
632
641
  const self = this;
633
- return (this.__childrenList = liveHTMLCollection(() => self.__children().filter((n) => n.nodeType === ELEMENT_NODE)));
642
+ return (this.__childrenList = liveHTMLCollection(() => self.__elementChildren()));
634
643
  }
635
- get childElementCount() { return this.__children().filter((n) => n.nodeType === ELEMENT_NODE).length; }
636
- get firstElementChild() { return this.__children().find((n) => n.nodeType === ELEMENT_NODE) ?? null; }
637
- get lastElementChild() { const e = this.__children().filter((n) => n.nodeType === ELEMENT_NODE); return e[e.length - 1] ?? null; }
644
+ get childElementCount() { return this.__elementChildren().length; }
645
+ get firstElementChild() { return this.__elementChildren()[0] ?? null; }
646
+ get lastElementChild() { const e = this.__elementChildren(); return e[e.length - 1] ?? null; }
638
647
  get nextElementSibling() { let n = this.nextSibling; while (n && n.nodeType !== ELEMENT_NODE) n = n.nextSibling; return n || null; }
639
648
  get previousElementSibling() { let n = this.previousSibling; while (n && n.nodeType !== ELEMENT_NODE) n = n.previousSibling; return n || null; }
640
649