@miaskiewicz/turbo-dom 0.1.10 → 0.1.11

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.11",
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;