@miaskiewicz/turbo-dom 0.1.10 → 0.1.12

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.10",
3
+ "version": "0.1.12",
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
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -434,6 +434,16 @@ export class Element extends Node {
434
434
  set placeholder(x) { this.setAttribute('placeholder', x); }
435
435
  get href() { return this.getAttribute('href') ?? ''; }
436
436
  set href(x) { this.setAttribute('href', x); }
437
+ get download() { return this.getAttribute('download') ?? ''; }
438
+ set download(x) { this.setAttribute('download', x); }
439
+ get rel() { return this.getAttribute('rel') ?? ''; }
440
+ set rel(x) { this.setAttribute('rel', x); }
441
+ get referrerPolicy() { return this.getAttribute('referrerpolicy') ?? ''; }
442
+ set referrerPolicy(x) { this.setAttribute('referrerpolicy', x); }
443
+ get src() { return this.getAttribute('src') ?? ''; }
444
+ set src(x) { this.setAttribute('src', x); }
445
+ get alt() { return this.getAttribute('alt') ?? ''; }
446
+ set alt(x) { this.setAttribute('alt', x); }
437
447
 
438
448
  // option
439
449
  get selected() { return this.__selected !== undefined ? this.__selected : this.hasAttribute('selected'); }
@@ -153,15 +153,53 @@ function matchPseudo(el, p) {
153
153
  return !(el.hasAttribute('readonly') || el.readOnly);
154
154
  case 'selected':
155
155
  return !!el.selected;
156
- case 'only-of-type':
156
+ // positional pseudo-classes (1-based, support An+B / odd / even)
157
+ case 'nth-child':
158
+ return nthMatch(p.arg, siblingIndex(el) + 1);
159
+ case 'nth-last-child':
160
+ return nthMatch(p.arg, siblingCount(el) - siblingIndex(el));
161
+ case 'nth-of-type':
162
+ return nthMatch(p.arg, typeIndex(el) + 1);
163
+ case 'nth-last-of-type':
164
+ return nthMatch(p.arg, typeCount(el) - typeIndex(el));
157
165
  case 'first-of-type':
166
+ return typeIndex(el) === 0;
158
167
  case 'last-of-type':
159
- return true; // best-effort: rarely load-bearing in tests
168
+ return typeIndex(el) === typeCount(el) - 1;
169
+ case 'only-of-type':
170
+ return typeCount(el) === 1;
160
171
  default:
161
172
  return false; // unknown pseudo: never matches (honest, not a silent true)
162
173
  }
163
174
  }
164
175
 
176
+ function elementSiblings(el) {
177
+ const p = el.parentNode;
178
+ if (!p) return [el];
179
+ const kids = typeof p.__children === 'function' ? p.__children() : Array.from(p.childNodes || []);
180
+ return kids.filter((n) => n.nodeType === 1);
181
+ }
182
+ function siblingIndex(el) { return elementSiblings(el).indexOf(el); }
183
+ function siblingCount(el) { return elementSiblings(el).length; }
184
+ function typeIndex(el) { return elementSiblings(el).filter((n) => n.localName === el.localName).indexOf(el); }
185
+ function typeCount(el) { return elementSiblings(el).filter((n) => n.localName === el.localName).length; }
186
+
187
+ // match An+B / odd / even / integer against a 1-based index
188
+ function nthMatch(arg, index) {
189
+ const a0 = String(arg || '').trim().toLowerCase();
190
+ if (a0 === 'odd') return index % 2 === 1;
191
+ if (a0 === 'even') return index % 2 === 0;
192
+ const m = /^([+-]?\d*)n\s*([+-]\s*\d+)?$/.exec(a0.replace(/\s+/g, ''));
193
+ if (m) {
194
+ let a = m[1] === '' || m[1] === '+' ? 1 : m[1] === '-' ? -1 : parseInt(m[1], 10);
195
+ const b = m[2] ? parseInt(m[2].replace(/\s/g, ''), 10) : 0;
196
+ if (a === 0) return index === b;
197
+ return (index - b) % a === 0 && (index - b) / a >= 0;
198
+ }
199
+ const n = parseInt(a0, 10);
200
+ return !Number.isNaN(n) && index === n;
201
+ }
202
+
165
203
  function previousElement(el) {
166
204
  let n = el.previousSibling;
167
205
  while (n && n.nodeType !== 1) n = n.previousSibling;
@@ -57,6 +57,19 @@ class TurboFormData {
57
57
  [Symbol.iterator]() { return this.entries(); }
58
58
  }
59
59
 
60
+ // A tag-specific HTML*Element interface. Not constructible — exists so
61
+ // `el instanceof HTMLXElement` is true only for elements with the matching
62
+ // localName (matcher = string or RegExp). Avoids aliasing every interface to
63
+ // Element (which made `instanceof HTMLAnchorElement` true for ALL elements).
64
+ function tagClass(matcher) {
65
+ const test = typeof matcher === 'string' ? (n) => n === matcher : (n) => matcher.test(n);
66
+ const C = function () { throw new TypeError('Illegal constructor'); };
67
+ Object.defineProperty(C, Symbol.hasInstance, {
68
+ value: (o) => o != null && o.nodeType === 1 && test(o.localName),
69
+ });
70
+ return C;
71
+ }
72
+
60
73
  // Capture host functions at module load — BEFORE any installGlobals() can shadow
61
74
  // the bare names on globalThis (which would make these delegates call themselves).
62
75
  const hostSetTimeout = globalThis.setTimeout;
@@ -83,17 +96,21 @@ export function createWindow(document, { url = 'http://localhost/' } = {}) {
83
96
  UIEvent, MouseEvent, PointerEvent, KeyboardEvent, InputEvent, FocusEvent,
84
97
  CompositionEvent, WheelEvent, TouchEvent, DragEvent, ProgressEvent, ClipboardEvent,
85
98
  DataTransfer,
86
- // generic elements are plain Element → `el instanceof HTMLElement` is true.
87
- // HTMLIFrameElement MUST be a distinct class so React's iframe-descent loop
88
- // (`while (el instanceof HTMLIFrameElement)`) terminates on normal elements.
99
+ // every element is a plain Element → `el instanceof HTMLElement` is true.
100
+ // Tag-specific interfaces match by localName via Symbol.hasInstance, so
101
+ // `el instanceof HTMLAnchorElement` is true ONLY for <a> (not every element),
102
+ // and React's `while (el instanceof HTMLIFrameElement)` loop terminates.
89
103
  HTMLElement: Element, SVGElement: Element,
90
- HTMLIFrameElement: class HTMLIFrameElement extends Element {},
91
- HTMLInputElement: Element, HTMLTextAreaElement: Element, HTMLSelectElement: Element,
92
- HTMLOptionElement: Element, HTMLButtonElement: Element, HTMLAnchorElement: Element,
93
- HTMLFormElement: Element, HTMLImageElement: Element, HTMLCanvasElement: Element,
94
- HTMLTemplateElement: Element, HTMLLabelElement: Element, HTMLDivElement: Element,
95
- HTMLSpanElement: Element, HTMLParagraphElement: Element, HTMLUListElement: Element,
96
- HTMLLIElement: Element, HTMLHeadingElement: Element, HTMLBodyElement: Element,
104
+ HTMLAnchorElement: tagClass('a'), HTMLInputElement: tagClass('input'),
105
+ HTMLTextAreaElement: tagClass('textarea'), HTMLSelectElement: tagClass('select'),
106
+ HTMLOptionElement: tagClass('option'), HTMLButtonElement: tagClass('button'),
107
+ HTMLFormElement: tagClass('form'), HTMLImageElement: tagClass('img'),
108
+ HTMLCanvasElement: tagClass('canvas'), HTMLTemplateElement: tagClass('template'),
109
+ HTMLLabelElement: tagClass('label'), HTMLDivElement: tagClass('div'),
110
+ HTMLSpanElement: tagClass('span'), HTMLParagraphElement: tagClass('p'),
111
+ HTMLUListElement: tagClass('ul'), HTMLLIElement: tagClass('li'),
112
+ HTMLBodyElement: tagClass('body'), HTMLIFrameElement: tagClass('iframe'),
113
+ HTMLHeadingElement: tagClass(/^h[1-6]$/),
97
114
  HTMLDocument: Document, DocumentFragment, ShadowRoot: DocumentFragment,
98
115
  MutationObserver, DOMParser, XMLSerializer,
99
116
  URL: makeURL(), URLSearchParams,