@miaskiewicz/turbo-dom 0.1.35 → 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.
|
|
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",
|
package/src/runtime/dom.mjs
CHANGED
|
@@ -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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
1313
|
-
|
|
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 =
|
|
1316
|
-
const inSubtree = options.subtree && isDescendant(
|
|
1324
|
+
const onTarget = target === obsTarget;
|
|
1325
|
+
const inSubtree = options.subtree && isDescendant(target, obsTarget);
|
|
1317
1326
|
if (!onTarget && !inSubtree) continue;
|
|
1318
|
-
if (
|
|
1319
|
-
if (
|
|
1320
|
-
if (
|
|
1321
|
-
if (
|
|
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
|
|
1324
|
-
addedNodes
|
|
1325
|
-
previousSibling:
|
|
1326
|
-
attributeName:
|
|
1327
|
-
oldValue: (
|
|
1328
|
-
(
|
|
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);
|
|
@@ -1362,7 +1371,7 @@ export class Document extends Node {
|
|
|
1362
1371
|
this.__cache = []; // idx -> handle (identity memoization / nodeAt)
|
|
1363
1372
|
this.__active = null; // activeElement
|
|
1364
1373
|
this.defaultView = null; // set by environment (window)
|
|
1365
|
-
this.__mo =
|
|
1374
|
+
this.__mo = null; // registered MutationObservers (lazy — most files never observe())
|
|
1366
1375
|
this.__moPending = null; // observers with queued records awaiting microtask
|
|
1367
1376
|
}
|
|
1368
1377
|
get nodeType() { return DOCUMENT_NODE; }
|
|
@@ -1371,10 +1380,11 @@ export class Document extends Node {
|
|
|
1371
1380
|
// ---- MutationObserver registry ----
|
|
1372
1381
|
__moRegister(obs, target, options) {
|
|
1373
1382
|
// replace existing registration for (obs,target) per spec
|
|
1374
|
-
this.__mo
|
|
1383
|
+
if (!this.__mo) this.__mo = [];
|
|
1384
|
+
else this.__mo = this.__mo.filter((r) => !(r.obs === obs && r.target === target));
|
|
1375
1385
|
this.__mo.push({ obs, target, options });
|
|
1376
1386
|
}
|
|
1377
|
-
__moUnregister(obs) { this.__mo = this.__mo.filter((r) => r.obs !== obs); }
|
|
1387
|
+
__moUnregister(obs) { if (this.__mo) this.__mo = this.__mo.filter((r) => r.obs !== obs); }
|
|
1378
1388
|
__scheduleMO(obs) {
|
|
1379
1389
|
if (!this.__moPending) this.__moPending = new Set();
|
|
1380
1390
|
if (this.__moPending.has(obs)) return;
|
|
Binary file
|