@miaskiewicz/turbo-dom 0.1.9 → 0.1.10

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.9",
3
+ "version": "0.1.10",
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",
@@ -27,9 +27,12 @@ export function installGlobals(target, { html = DEFAULT_HTML, url } = {}) {
27
27
 
28
28
  // window self-references
29
29
  for (const k of SELF_KEYS) {
30
+ // window/self/parent/top resolve to the GLOBAL itself (target), like a real
31
+ // browser where window === globalThis. This is what makes vi.stubGlobal('x')
32
+ // visible via window.x — they're the same object/property, not two bindings.
30
33
  define(k, {
31
34
  configurable: true,
32
- get: () => window,
35
+ get: () => target,
33
36
  set(v) { Object.defineProperty(target, k, { configurable: true, writable: true, value: v }); },
34
37
  });
35
38
  }
@@ -46,6 +49,10 @@ export function installGlobals(target, { html = DEFAULT_HTML, url } = {}) {
46
49
  });
47
50
  }
48
51
 
52
+ // window === globalThis (target): keep document.defaultView consistent so
53
+ // `window === document.defaultView` holds and stubs on the global are seen.
54
+ try { env.document.defaultView = target; } catch { /* getter-only in some setups */ }
55
+
49
56
  // handy escape hatch
50
57
  define('__turboDom', { configurable: true, writable: true, value: env });
51
58
 
@@ -380,7 +380,16 @@ export class Element extends Node {
380
380
  const t = this.localName;
381
381
  if (t === 'select') { for (const o of this.getElementsByTagName('option')) o.selected = (o.value === String(x)); return; }
382
382
  if (t === 'option') { this.setAttribute('value', x); return; }
383
- this.__value = String(x);
383
+ // typed <input> sanitizes per the value-sanitization algorithm: invalid
384
+ // values for date/time/number/etc are rejected (value left unchanged), so
385
+ // user-event typing them char-by-char doesn't emit bogus partial values.
386
+ if (t === 'input') {
387
+ const sanitized = sanitizeInputValue((this.getAttribute('type') || 'text').toLowerCase(), String(x));
388
+ if (sanitized === null) return; // invalid → reject, keep current value
389
+ this.__value = sanitized;
390
+ } else {
391
+ this.__value = String(x);
392
+ }
384
393
  if (this.__selStart != null) { this.__selStart = Math.min(this.__selStart, this.__value.length); this.__selEnd = Math.min(this.__selEnd, this.__value.length); }
385
394
  }
386
395
  get defaultValue() { return this.getAttribute('value') ?? ''; }
@@ -860,6 +869,24 @@ function makeStyle(el) {
860
869
  }
861
870
  const kebab = (s) => s.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase());
862
871
 
872
+ // WHATWG value-sanitization per input type. Returns the accepted value, or null
873
+ // if invalid (caller leaves the current value unchanged — like a real browser,
874
+ // which rejects partial/garbage input rather than storing it).
875
+ function sanitizeInputValue(type, v) {
876
+ if (v === '') return '';
877
+ switch (type) {
878
+ case 'date': return /^\d{4}-\d{2}-\d{2}$/.test(v) ? v : null;
879
+ case 'month': return /^\d{4}-\d{2}$/.test(v) ? v : null;
880
+ case 'week': return /^\d{4}-W\d{2}$/.test(v) ? v : null;
881
+ case 'time': return /^\d{2}:\d{2}(:\d{2}(\.\d{1,3})?)?$/.test(v) ? v : null;
882
+ case 'datetime-local': return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?$/.test(v) ? v : null;
883
+ case 'number': return /^-?\d*\.?\d+(e[-+]?\d+)?$/i.test(v) ? v : null;
884
+ case 'range': return /^-?\d*\.?\d+$/.test(v) ? v : '50';
885
+ case 'color': return /^#[0-9a-fA-F]{6}$/.test(v) ? v.toLowerCase() : null;
886
+ default: return v; // text, email, password, search, url, tel, hidden, etc.
887
+ }
888
+ }
889
+
863
890
  // element.dataset — camelCase <-> data-* attribute mapping.
864
891
  const dataAttr = (key) => 'data-' + key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase());
865
892
  const dataKey = (attr) => attr.slice(5).replace(/-([a-z])/g, (_, c) => c.toUpperCase());
@@ -135,6 +135,28 @@ function matchPseudo(el, p) {
135
135
  return el.childNodes.length === 0;
136
136
  case 'root':
137
137
  return el.parentNode == null || el.parentNode.nodeType === 9;
138
+ // form-state pseudo-classes — read the live PROPERTY (what React sets),
139
+ // not just the HTML attribute.
140
+ case 'checked':
141
+ return el.localName === 'option' ? !!el.selected : !!el.checked;
142
+ case 'disabled':
143
+ return !!el.disabled || el.hasAttribute('disabled');
144
+ case 'enabled':
145
+ return /^(input|button|select|textarea|optgroup|option|fieldset)$/.test(el.localName) && !(el.disabled || el.hasAttribute('disabled'));
146
+ case 'required':
147
+ return !!el.required || el.hasAttribute('required');
148
+ case 'optional':
149
+ return /^(input|select|textarea)$/.test(el.localName) && !(el.required || el.hasAttribute('required'));
150
+ case 'read-only':
151
+ return el.hasAttribute('readonly') || !!el.readOnly;
152
+ case 'read-write':
153
+ return !(el.hasAttribute('readonly') || el.readOnly);
154
+ case 'selected':
155
+ return !!el.selected;
156
+ case 'only-of-type':
157
+ case 'first-of-type':
158
+ case 'last-of-type':
159
+ return true; // best-effort: rarely load-bearing in tests
138
160
  default:
139
161
  return false; // unknown pseudo: never matches (honest, not a silent true)
140
162
  }