@miaskiewicz/turbo-dom 0.1.49 → 0.1.51

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.49",
3
+ "version": "0.1.51",
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",
@@ -644,8 +644,20 @@ export class Element extends Node {
644
644
  get childElementCount() { return this.__elementChildren().length; }
645
645
  get firstElementChild() { return this.__elementChildren()[0] ?? null; }
646
646
  get lastElementChild() { const e = this.__elementChildren(); return e[e.length - 1] ?? null; }
647
- get nextElementSibling() { let n = this.nextSibling; while (n && n.nodeType !== ELEMENT_NODE) n = n.nextSibling; return n || null; }
648
- get previousElementSibling() { let n = this.previousSibling; while (n && n.nodeType !== ELEMENT_NODE) n = n.previousSibling; return n || null; }
647
+ // one indexOf + scan, not nextSibling-per-step (each of which re-runs indexOf → O(n²)
648
+ // when text nodes sit between elements). Reads __children() live — no cache.
649
+ get nextElementSibling() {
650
+ const p = this.parentNode; if (!p) return null;
651
+ const k = p.__children(); let i = k.indexOf(this); if (i < 0) return null;
652
+ for (i++; i < k.length; i++) if (k[i].nodeType === ELEMENT_NODE) return k[i];
653
+ return null;
654
+ }
655
+ get previousElementSibling() {
656
+ const p = this.parentNode; if (!p) return null;
657
+ const k = p.__children(); let i = k.indexOf(this);
658
+ for (i--; i >= 0; i--) if (k[i].nodeType === ELEMENT_NODE) return k[i];
659
+ return null;
660
+ }
649
661
 
650
662
  // ---- modern insertion ----
651
663
  append(...nodes) { for (const n of nodes) this.appendChild(toNode(this.ownerDocument, n)); }
@@ -660,8 +672,16 @@ export class Element extends Node {
660
672
  closest(sel) { let n = this; while (n && n.nodeType === ELEMENT_NODE) { if (n.matches(sel)) return n; n = n.parentNode; } return null; }
661
673
  querySelector(sel) { return cachedQS(this, sel); }
662
674
  querySelectorAll(sel) { return cachedQSA(this, sel); }
663
- getElementsByTagName(tag) { const self = this; return liveHTMLCollection(() => collectByTag(self, tag.toLowerCase())); }
664
- getElementsByClassName(cls) { const self = this; const classes = splitClasses(cls); return liveHTMLCollection(() => collectByClass(self, classes)); }
675
+ getElementsByTagName(tag) {
676
+ const self = this, t = tag.toLowerCase();
677
+ let cv = -1, ca; // version-cache the subtree walk within this collection (live; re-walk on mutation)
678
+ return liveHTMLCollection(() => { const d = self.ownerDocument, v = d ? (d.__version || 0) : 0; if (v === cv) return ca; cv = v; return (ca = collectByTag(self, t)); });
679
+ }
680
+ getElementsByClassName(cls) {
681
+ const self = this, classes = splitClasses(cls);
682
+ let cv = -1, ca;
683
+ return liveHTMLCollection(() => { const d = self.ownerDocument, v = d ? (d.__version || 0) : 0; if (v === cv) return ca; cv = v; return (ca = collectByClass(self, classes)); });
684
+ }
665
685
 
666
686
  // ---- innerHTML / outerHTML ----
667
687
  get innerHTML() { return serializeInner(this); }
@@ -953,8 +973,16 @@ export class ShadowRoot extends DocumentFragment {
953
973
  visit(this);
954
974
  return found;
955
975
  }
956
- getElementsByTagName(tag) { const self = this; return liveHTMLCollection(() => collectByTag(self, tag.toLowerCase())); }
957
- getElementsByClassName(cls) { const self = this; const classes = splitClasses(cls); return liveHTMLCollection(() => collectByClass(self, classes)); }
976
+ getElementsByTagName(tag) {
977
+ const self = this, t = tag.toLowerCase();
978
+ let cv = -1, ca; // version-cache the subtree walk within this collection (live; re-walk on mutation)
979
+ return liveHTMLCollection(() => { const d = self.ownerDocument, v = d ? (d.__version || 0) : 0; if (v === cv) return ca; cv = v; return (ca = collectByTag(self, t)); });
980
+ }
981
+ getElementsByClassName(cls) {
982
+ const self = this, classes = splitClasses(cls);
983
+ let cv = -1, ca;
984
+ return liveHTMLCollection(() => { const d = self.ownerDocument, v = d ? (d.__version || 0) : 0; if (v === cv) return ca; cv = v; return (ca = collectByClass(self, classes)); });
985
+ }
958
986
  // the topmost root of any node within is this shadow root, never the document
959
987
  get activeElement() { return null; }
960
988
  }