@miaskiewicz/turbo-dom 0.1.36 → 0.1.37

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.36",
3
+ "version": "0.1.37",
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",
@@ -150,7 +150,7 @@ export class Node extends EventTarget {
150
150
  if (ref) kids.splice(i, 0, node); else kids.push(node);
151
151
  node.parentNode = this;
152
152
  node.ownerDocument = this.ownerDocument;
153
- notifyMutation(this, { type: 'childList', target: this, addedNodes: [node], removedNodes: [], nextSibling: ref || null });
153
+ notifyMutation(this, 'childList', node, null, ref || null);
154
154
  return node;
155
155
  }
156
156
 
@@ -161,7 +161,7 @@ export class Node extends EventTarget {
161
161
  const next = kids[i + 1] || null;
162
162
  kids.splice(i, 1);
163
163
  node.parentNode = null;
164
- notifyMutation(this, { type: 'childList', target: this, addedNodes: [], removedNodes: [node], nextSibling: next });
164
+ notifyMutation(this, 'childList', null, node, next);
165
165
  return node;
166
166
  }
167
167
 
@@ -216,7 +216,7 @@ export class Node extends EventTarget {
216
216
  }
217
217
  // normalize mutates __kids in place — bump __version (invalidate cached queries)
218
218
  // and feed MutationObservers, like every other childList mutation path.
219
- if (changed) notifyMutation(this, { type: 'childList', target: this, addedNodes: [], removedNodes: [], nextSibling: null });
219
+ if (changed) notifyMutation(this, 'childList', null, null, null);
220
220
  }
221
221
  replaceChildren(...nodes) { this.__kids = []; this.__touch(); for (const n of nodes) this.appendChild(typeof n === 'string' ? this.ownerDocument.createTextNode(n) : n); }
222
222
  // bitmask: 1 DISCONNECTED, 2 PRECEDING, 4 FOLLOWING, 8 CONTAINS, 16 CONTAINED_BY
@@ -282,7 +282,7 @@ Object.assign(Node.prototype, {
282
282
  class CharacterData extends Node {
283
283
  constructor(ownerDocument, data) { super(ownerDocument); this._data = data ?? ''; }
284
284
  get data() { return this._data; }
285
- set data(v) { const old = this._data; this._data = String(v); notifyMutation(this, { type: 'characterData', target: this, oldValue: old, addedNodes: [], removedNodes: [] }); }
285
+ set data(v) { const old = this._data; this._data = String(v); notifyMutation(this, 'characterData', null, null, null, null, old); }
286
286
  get nodeValue() { return this._data; }
287
287
  set nodeValue(v) { this.data = v; }
288
288
  get length() { return this._data.length; }
@@ -407,14 +407,14 @@ export class Element extends Node {
407
407
  const old = a ? a.value : null;
408
408
  if (a) a.value = String(value);
409
409
  else this.__attrs.push({ name, value: String(value), prefix: '' });
410
- notifyMutation(this, { type: 'attributes', target: this, attributeName: name, oldValue: old, addedNodes: [], removedNodes: [] });
410
+ notifyMutation(this, 'attributes', null, null, null, name, old);
411
411
  }
412
412
  removeAttribute(name) {
413
413
  if (!this.__ns) name = ('' + name).toLowerCase();
414
414
  if (this.__attrs === undefined) this.__attrs = this.__buildAttrs();
415
415
  const a = this.__attrs.find((x) => x.name === name);
416
416
  this.__attrs = this.__attrs.filter((x) => x.name !== name);
417
- if (a) notifyMutation(this, { type: 'attributes', target: this, attributeName: name, oldValue: a.value, addedNodes: [], removedNodes: [] });
417
+ if (a) notifyMutation(this, 'attributes', null, null, null, name, a.value);
418
418
  }
419
419
  toggleAttribute(name, force) {
420
420
  const has = this.hasAttribute(name);
@@ -1303,29 +1303,38 @@ function isDescendant(node, ancestor) {
1303
1303
  while (n) { if (n === ancestor) return true; n = n.parentNode; }
1304
1304
  return false;
1305
1305
  }
1306
- function notifyMutation(target, record) {
1306
+ const EMPTY_NODES = []; // shared read-only addedNodes/removedNodes for empty records
1307
+
1308
+ // Mutation notification — takes primitives (a single added/removed node, etc) so
1309
+ // the common case (no MutationObservers — most test files) allocates NOTHING: it
1310
+ // only bumps __version and returns. The MutationRecord object + its node arrays
1311
+ // are built solely when an observer is actually registered.
1312
+ function notifyMutation(target, type, added, removed, nextSibling, attributeName, oldValue) {
1307
1313
  const doc = target.ownerDocument;
1308
1314
  if (!doc) return;
1309
1315
  // bump the DOM version on every structural/attribute mutation — invalidates
1310
1316
  // the document's getElementsBy* caches. Unconditional (independent of observers).
1311
1317
  doc.__version = (doc.__version || 0) + 1;
1312
- if (!doc.__mo || doc.__mo.length === 0) return;
1313
- for (const reg of doc.__mo) {
1318
+ const mo = doc.__mo;
1319
+ if (!mo || mo.length === 0) return;
1320
+ const addedNodes = added ? [added] : EMPTY_NODES;
1321
+ const removedNodes = removed ? [removed] : EMPTY_NODES;
1322
+ for (const reg of mo) {
1314
1323
  const { obs, target: obsTarget, options } = reg;
1315
- const onTarget = record.target === obsTarget;
1316
- const inSubtree = options.subtree && isDescendant(record.target, obsTarget);
1324
+ const onTarget = target === obsTarget;
1325
+ const inSubtree = options.subtree && isDescendant(target, obsTarget);
1317
1326
  if (!onTarget && !inSubtree) continue;
1318
- if (record.type === 'childList' && !options.childList) continue;
1319
- if (record.type === 'attributes' && !options.attributes) continue;
1320
- if (record.type === 'attributes' && options.attributeFilter && !options.attributeFilter.includes(record.attributeName)) continue;
1321
- if (record.type === 'characterData' && !options.characterData) continue;
1327
+ if (type === 'childList' && !options.childList) continue;
1328
+ if (type === 'attributes' && !options.attributes) continue;
1329
+ if (type === 'attributes' && options.attributeFilter && !options.attributeFilter.includes(attributeName)) continue;
1330
+ if (type === 'characterData' && !options.characterData) continue;
1322
1331
  const rec = {
1323
- type: record.type, target: record.target,
1324
- addedNodes: record.addedNodes || [], removedNodes: record.removedNodes || [],
1325
- previousSibling: record.previousSibling || null, nextSibling: record.nextSibling || null,
1326
- attributeName: record.attributeName || null, attributeNamespace: null,
1327
- oldValue: (record.type === 'attributes' && options.attributeOldValue) ||
1328
- (record.type === 'characterData' && options.characterDataOldValue) ? (record.oldValue ?? null) : null,
1332
+ type, target,
1333
+ addedNodes, removedNodes,
1334
+ previousSibling: null, nextSibling: nextSibling || null,
1335
+ attributeName: attributeName || null, attributeNamespace: null,
1336
+ oldValue: (type === 'attributes' && options.attributeOldValue) ||
1337
+ (type === 'characterData' && options.characterDataOldValue) ? (oldValue ?? null) : null,
1329
1338
  };
1330
1339
  obs.__enqueue(rec);
1331
1340
  doc.__scheduleMO(obs);