@miaskiewicz/turbo-dom 0.1.48 → 0.1.50

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.48",
3
+ "version": "0.1.50",
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,22 +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
- // version-cache the element-filtered array (like cachedQSA): re-filter only when
634
- // the DOM version changes, not on every children[i]/length access. Live —
635
- // every mutation bumps Document.__version, invalidating the cache.
636
- return (this.__childrenList = liveHTMLCollection(() => {
637
- const d = self.ownerDocument, v = d ? (d.__version || 0) : 0;
638
- if (self.__childrenArr !== undefined && self.__childrenArrV === v) return self.__childrenArr;
639
- self.__childrenArrV = v;
640
- return (self.__childrenArr = self.__children().filter((n) => n.nodeType === ELEMENT_NODE));
641
- }));
642
+ return (this.__childrenList = liveHTMLCollection(() => self.__elementChildren()));
642
643
  }
643
- get childElementCount() { return this.__children().filter((n) => n.nodeType === ELEMENT_NODE).length; }
644
- get firstElementChild() { return this.__children().find((n) => n.nodeType === ELEMENT_NODE) ?? null; }
645
- 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; }
646
647
  get nextElementSibling() { let n = this.nextSibling; while (n && n.nodeType !== ELEMENT_NODE) n = n.nextSibling; return n || null; }
647
648
  get previousElementSibling() { let n = this.previousSibling; while (n && n.nodeType !== ELEMENT_NODE) n = n.previousSibling; return n || null; }
648
649
 
@@ -659,8 +660,16 @@ export class Element extends Node {
659
660
  closest(sel) { let n = this; while (n && n.nodeType === ELEMENT_NODE) { if (n.matches(sel)) return n; n = n.parentNode; } return null; }
660
661
  querySelector(sel) { return cachedQS(this, sel); }
661
662
  querySelectorAll(sel) { return cachedQSA(this, sel); }
662
- getElementsByTagName(tag) { const self = this; return liveHTMLCollection(() => collectByTag(self, tag.toLowerCase())); }
663
- getElementsByClassName(cls) { const self = this; const classes = splitClasses(cls); return liveHTMLCollection(() => collectByClass(self, classes)); }
663
+ getElementsByTagName(tag) {
664
+ const self = this, t = tag.toLowerCase();
665
+ let cv = -1, ca; // version-cache the subtree walk within this collection (live; re-walk on mutation)
666
+ return liveHTMLCollection(() => { const d = self.ownerDocument, v = d ? (d.__version || 0) : 0; if (v === cv) return ca; cv = v; return (ca = collectByTag(self, t)); });
667
+ }
668
+ getElementsByClassName(cls) {
669
+ const self = this, classes = splitClasses(cls);
670
+ let cv = -1, ca;
671
+ return liveHTMLCollection(() => { const d = self.ownerDocument, v = d ? (d.__version || 0) : 0; if (v === cv) return ca; cv = v; return (ca = collectByClass(self, classes)); });
672
+ }
664
673
 
665
674
  // ---- innerHTML / outerHTML ----
666
675
  get innerHTML() { return serializeInner(this); }
@@ -952,8 +961,16 @@ export class ShadowRoot extends DocumentFragment {
952
961
  visit(this);
953
962
  return found;
954
963
  }
955
- getElementsByTagName(tag) { const self = this; return liveHTMLCollection(() => collectByTag(self, tag.toLowerCase())); }
956
- getElementsByClassName(cls) { const self = this; const classes = splitClasses(cls); return liveHTMLCollection(() => collectByClass(self, classes)); }
964
+ getElementsByTagName(tag) {
965
+ const self = this, t = tag.toLowerCase();
966
+ let cv = -1, ca; // version-cache the subtree walk within this collection (live; re-walk on mutation)
967
+ return liveHTMLCollection(() => { const d = self.ownerDocument, v = d ? (d.__version || 0) : 0; if (v === cv) return ca; cv = v; return (ca = collectByTag(self, t)); });
968
+ }
969
+ getElementsByClassName(cls) {
970
+ const self = this, classes = splitClasses(cls);
971
+ let cv = -1, ca;
972
+ return liveHTMLCollection(() => { const d = self.ownerDocument, v = d ? (d.__version || 0) : 0; if (v === cv) return ca; cv = v; return (ca = collectByClass(self, classes)); });
973
+ }
957
974
  // the topmost root of any node within is this shadow root, never the document
958
975
  get activeElement() { return null; }
959
976
  }