@miaskiewicz/turbo-dom 0.1.50 → 0.1.52

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.50",
3
+ "version": "0.1.52",
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)); }
@@ -705,7 +717,12 @@ export class Element extends Node {
705
717
  cloneNode(deep = false) {
706
718
  const el = new Element(this.ownerDocument, this.localName, this.__ns);
707
719
  el.__attrs = (this.__attrs ?? (this.__attrs = this.__buildAttrs())).map((a) => ({ ...a }));
708
- if (deep) for (const c of this.__children()) el.appendChild(c.cloneNode(true));
720
+ if (deep) {
721
+ // build the cloned child array directly — the clone is detached, so per-child
722
+ // appendChild's notifyMutation (version bump) + reparent check are pure waste.
723
+ const src = this.__children(), od = el.ownerDocument, kids = el.__kids = new Array(src.length);
724
+ for (let i = 0; i < src.length; i++) { const cl = src[i].cloneNode(true); cl.parentNode = el; cl.ownerDocument = od; kids[i] = cl; }
725
+ }
709
726
  return el;
710
727
  }
711
728