parsec-web-core 0.3.1

The core of parsec-web: an HTML parser that follows the WHATWG spec, with CSS selectors and a mutable DOM. Depend on parsec-web.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env node
// Turns web-platform-tests' selector tests into data for tests/wpt-selectors.rs.
// Run by scripts/fetch-wpt-selectors.sh:
//   node scripts/wpt-selectors-cases.js WPT_CHECKOUT tests/wpt-selectors
//
// The tests are JavaScript, so each test file's inline scripts run here in
// a sandbox whose `document` and elements aren't a DOM: querySelector,
// querySelectorAll, matches and closest return placeholders, and the
// assertions the test makes about those placeholders are recorded as cases
// (selector, root, what the test expects). A test that needs anything a
// static document can't give (computed style, focus, shadow DOM, a live
// NodeList) throws inside the sandbox and is listed as skipped with the
// reason, as is a whole file nothing in which could run.
'use strict';

const fs = require('fs');
const path = require('path');
const vm = require('vm');

const [src, out] = process.argv.slice(2);
if (!src || !out) {
  console.error('usage: wpt-selectors-cases.js WPT_CHECKOUT OUT_DIR');
  process.exit(2);
}

// ---- dom/nodes: selectors.js is a data list -------------------------------

function domNodesData() {
  const code = fs.readFileSync(path.join(src, 'dom/nodes/selectors.js'), 'utf8');
  const lists = vm.runInNewContext(
    code + ';({invalidSelectors, validSelectors, scopedSelectors})',
    {}
  );
  return {
    invalid: lists.invalidSelectors,
    valid: lists.validSelectors,
    scoped: lists.scopedSelectors,
  };
}

// ---- the sandbox ----------------------------------------------------------

class Unsupported extends Error {}
const unsupported = (what) => {
  throw new Unsupported(what);
};

const TOKEN = /^(\d+)$/;

// What a selector query returned: a placeholder the assertions give meaning to.
class Sym {
  constructor(syms, kind, root, selector) {
    if (typeof selector !== 'string') unsupported('a selector that is not a string');
    this.kind = kind; // all | first | matches | closest
    this.root = root;
    this.selector = selector;
    this.n = syms.push(this) - 1;
  }
  get token() {
    return `${this.n}`;
  }
}

function symProxy(sym) {
  return new Proxy(sym, {
    get(target, p) {
      if (p === '__sym') return target;
      if (p === 'id') {
        if (target.kind === 'first' || target.kind === 'closest') return target.token;
        unsupported('.id of a ' + target.kind + ' result');
      }
      if (target.kind === 'all') {
        // Array.from(list).map(e => e.id).sort().join() then yields the token.
        if (p === Symbol.iterator) {
          return function* () {
            yield new Proxy(
              {},
              {
                get(_, q) {
                  if (q === 'id') return target.token;
                  unsupported('an element of a querySelectorAll result (.' + String(q) + ')');
                },
              }
            );
          };
        }
        if (p === 'length') return target.token;
      }
      unsupported(`.${String(p)} of a ${target.kind} result`);
    },
  });
}

function makeSandbox(elements) {
  const syms = [];
  const refs = new WeakMap(); // element proxy -> "#id" | "html" | "body"
  const state = { test: null, cases: [], skipped: [], throwing: null };

  function element(ref) {
    const proxy = new Proxy(
      {},
      {
        get(_, p) {
          const known = elements.get(ref);
          switch (p) {
            case 'querySelector':
              return (s) => symProxy(new Sym(syms, 'first', ref, s));
            case 'querySelectorAll':
              return (s) => symProxy(new Sym(syms, 'all', ref, s));
            case 'matches':
            case 'webkitMatchesSelector':
              return (s) => symProxy(new Sym(syms, 'matches', ref, s));
            case 'closest':
              return (s) => symProxy(new Sym(syms, 'closest', ref, s));
            case 'id':
              return ref.startsWith('#') ? ref.slice(1) : '';
            // Read off the element's start tag in the HTML.
            case 'tagName':
              if (known) return known.tag.toUpperCase();
              break;
            case 'localName':
              if (known) return known.tag;
              break;
            case 'getAttribute':
              if (known) return (name) => known.attrs.get(name.toLowerCase()) ?? null;
              break;
            case 'hasAttribute':
              if (known) return (name) => known.attrs.has(name.toLowerCase());
              break;
            case 'dataset':
              if (known) {
                return new Proxy(
                  {},
                  {
                    get: (_, q) =>
                      known.attrs.get('data-' + String(q).replace(/[A-Z]/g, (c) => '-' + c.toLowerCase())),
                  }
                );
              }
              break;
            // Only ever a test's label in these files.
            case 'textContent':
              return ref;
          }
          unsupported(`element.${String(p)}`);
        },
      }
    );
    refs.set(proxy, ref);
    return proxy;
  }

  const document = new Proxy(
    {},
    {
      get(_, p) {
        switch (p) {
          case 'querySelector':
            // Looking an element up by id is setup, not the thing under test.
            return (s) =>
              /^#[\w-]+$/.test(s) ? element(s) : symProxy(new Sym(syms, 'first', 'document', s));
          case 'querySelectorAll':
            return (s) => symProxy(new Sym(syms, 'all', 'document', s));
          case 'getElementById':
            return (id) => element('#' + id);
          case 'documentElement':
            return element('html');
          case 'body':
            return element('body');
          default:
            unsupported(`document.${String(p)}`);
        }
      },
    }
  );

  // The Sym an assertion's argument stands for, if any.
  function symOf(x) {
    if (x && typeof x === 'object' && x.__sym instanceof Sym) return x.__sym;
    if (typeof x === 'string') {
      const m = TOKEN.exec(x);
      if (m) return syms[+m[1]];
    }
    if (Array.isArray(x) && x.length === 1) return symOf(x[0]);
    return null;
  }

  // An expected element: "#id", "html", "body" or null.
  function expectedRef(x) {
    if (x === null || x === undefined || x === '') return null;
    if (typeof x === 'string') return '#' + x;
    if (refs.has(x)) return refs.get(x);
    unsupported('an expected value that is not an element or an id');
  }

  function record(c) {
    if (!state.test) unsupported('an assertion outside test()');
    state.cases.push(Object.assign({ test: state.test }, c));
  }

  function assertEquals(a, b) {
    let sym = symOf(a);
    let other = b;
    if (!sym) {
      sym = symOf(b);
      other = a;
    }
    if (!sym) return; // nothing of ours in it
    const base = { kind: sym.kind, root: sym.root, selector: sym.selector };
    if (sym.kind === 'all') {
      if (typeof other === 'number') {
        return record(Object.assign(base, { kind: 'count', expect: other }));
      }
      if (typeof other === 'string') {
        if (//.test(other)) unsupported('comparing two query results');
        // formatElements(): ids sorted and joined with commas.
        const ids = other === '' ? [] : other.split(',');
        return record(Object.assign(base, { expect: ids.map((i) => '#' + i), ordered: false }));
      }
      if (Array.isArray(other)) {
        return record(Object.assign(base, { expect: other.map(expectedRef), ordered: true }));
      }
      unsupported('comparing a querySelectorAll result with ' + typeof other);
    }
    if (sym.kind === 'matches') {
      if (typeof other !== 'boolean') unsupported('comparing matches() with ' + typeof other);
      return record(Object.assign(base, { expect: other }));
    }
    record(Object.assign(base, { expect: expectedRef(other) }));
  }

  function assertBool(x, expect) {
    const sym = symOf(x);
    if (!sym) return;
    if (sym.kind !== 'matches') unsupported(`assert_${expect} on a ${sym.kind} result`);
    record({ kind: 'matches', root: sym.root, selector: sym.selector, expect });
  }

  function assertThrows(...args) {
    const fn = args.find((a) => typeof a === 'function');
    if (!fn) unsupported('assert_throws without a function');
    const before = syms.length;
    state.throwing = true;
    try {
      fn();
    } catch (e) {
      if (e instanceof Unsupported) throw e;
      // Setup inside the function failing before the query is fine: the
      // queries made so far are the cases.
    } finally {
      state.throwing = false;
    }
    if (syms.length === before) unsupported('assert_throws without a selector query');
    for (const s of syms.slice(before)) {
      record({ kind: 'parse', selector: s.selector, expect: false });
    }
  }

  function test(fn, name) {
    if (typeof fn !== 'function') [fn, name] = [name, fn];
    const label = typeof name === 'string' ? name : '(unnamed)';
    state.test = label;
    const before = state.cases.length;
    try {
      fn({ add_cleanup() {}, step_func: (f) => f, done() {} });
      if (state.cases.length === before) {
        state.skipped.push({ test: label, reason: 'asserts nothing about a selector query' });
      }
    } catch (e) {
      state.cases.length = before;
      const reason =
        e instanceof Unsupported
          ? `needs ${e.message}`
          : `${e && e.constructor ? e.constructor.name : 'error'}: ${e && e.message}`;
      state.skipped.push({ test: label, reason });
    }
    state.test = null;
  }

  function skipTest(reason) {
    return (fn, name) => {
      if (typeof fn !== 'function') [fn, name] = [name, fn];
      state.skipped.push({ test: typeof name === 'string' ? name : '(unnamed)', reason });
    };
  }

  // /css/support/parsing-testcommon.js: serialisation isn't checked, only
  // whether the selector parses.
  function parseCase(valid) {
    return (selector) => {
      state.test = JSON.stringify(selector) + (valid ? ' should be a valid selector' : ' should be an invalid selector');
      state.cases.push({ test: state.test, kind: 'parse', selector, expect: valid });
      state.test = null;
    };
  }

  // /css/support/query-testcommon.js
  function testQuerySelector(root, selector, expected) {
    if (!Array.isArray(expected)) expected = [expected];
    const ref = root === sandbox.document ? 'document' : refs.get(root);
    if (!ref) unsupported('a query root that is not an element');
    state.test = `Selector '${selector}" should find the expected elements`;
    state.cases.push({
      test: state.test,
      kind: 'all',
      root: ref,
      selector,
      expect: expected.map(expectedRef),
      ordered: true,
    });
    state.test = null;
  }

  const sandbox = {
    document,
    test,
    setup() {},
    done() {},
    promise_test: skipTest('runs asynchronously'),
    async_test: skipTest('runs asynchronously'),
    assert_equals: assertEquals,
    assert_not_equals: (a, b) => {
      if (symOf(a) || symOf(b)) unsupported('assert_not_equals on a query result');
    },
    assert_array_equals: assertEquals,
    assert_true: (x) => assertBool(x, true),
    assert_false: (x) => assertBool(x, false),
    assert_throws_dom: assertThrows,
    assert_throws_js: assertThrows,
    assert_unreached: (m) => {
      throw new Error('assert_unreached: ' + m);
    },
    test_valid_selector: parseCase(true),
    test_valid_forgiving_selector: parseCase(true),
    test_invalid_selector: parseCase(false),
    test_query_selector: testQuerySelector,
    console: { log() {} },
    _loadHandlers: [],
    addEventListener(type, fn) {
      if (type === 'load') sandbox._loadHandlers.push(fn);
    },
  };
  sandbox.window = sandbox;
  sandbox.self = sandbox;
  for (const ref of elements.keys()) {
    const id = ref.slice(1);
    if (/^[A-Za-z_$][\w$]*$/.test(id) && !(id in sandbox)) sandbox[id] = element(ref);
  }
  return { sandbox, state };
}

// ---- one test file --------------------------------------------------------

const SKIP_PATTERNS = [
  [/getComputedStyle|\.style\b|cssRules|CSS\.supports/, 'computed style or CSSOM'],
  [/test_driver|testdriver|\.focus\(|\.click\(|hover|active|accesskey/i, 'user interaction'],
  [/attachShadow|shadowRoot|<slot|::part|::slotted/, 'shadow DOM'],
  [/innerHTML|appendChild|insertAdjacent|createElement|\.remove\(\)|setAttribute|removeAttribute|classList|className/,
    'a live DOM changed from script'],
  [/promise_test|async_test|requestAnimationFrame|setTimeout|<iframe/, 'running asynchronously in a browser'],
];

function reasonFor(html, state) {
  const m = SKIP_PATTERNS.find(([re]) => re.test(html));
  if (m) return `needs ${m[1]}`;
  const first = state && state.skipped[0];
  return first ? first.reason : 'reads the DOM from script';
}

function extract(rel) {
  const html = fs.readFileSync(path.join(src, rel), 'utf8');
  const entry = { file: rel };
  if (/\.xht(ml)?$/.test(rel)) return Object.assign(entry, { skipped_because: 'an XHTML document' });
  if (/\/crashtests\//.test(rel)) return Object.assign(entry, { skipped_because: 'a crash test: needs a browser' });
  if (/\/invalidation\//.test(rel)) return Object.assign(entry, { skipped_because: 'invalidation: needs a live DOM' });
  if (/-manual\.html$/.test(rel)) return Object.assign(entry, { skipped_because: 'a manual test' });
  if (!/testharness\.js/.test(html)) return Object.assign(entry, { skipped_because: 'a reftest: needs rendering' });

  // Every element with an id, by "#id": its tag and attributes.
  const elements = new Map();
  for (const [, tag, rest] of html.matchAll(/<([a-zA-Z][\w-]*)(\s[^>]*)?>/g)) {
    const attrs = new Map();
    for (const m of (rest ?? '').matchAll(/([^\s"'=<>\/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+)))?/g)) {
      attrs.set(m[1].toLowerCase(), m[2] ?? m[3] ?? m[4] ?? '');
    }
    if (attrs.has('id') && !elements.has('#' + attrs.get('id'))) {
      elements.set('#' + attrs.get('id'), { tag: tag.toLowerCase(), attrs });
    }
  }
  const { sandbox, state } = makeSandbox(elements);
  const context = vm.createContext(sandbox);
  // An end tag may carry spaces or junk before its `>`, as browsers allow.
  const scripts = [...html.matchAll(/<script(\s[^>]*)?>([\s\S]*?)<\/script[^>]*>/gi)];
  try {
    for (const [, attrs, code] of scripts) {
      if (attrs && /\bsrc\s*=/.test(attrs)) {
        const srcAttr = /src\s*=\s*["']?([^"'\s>]+)/.exec(attrs)[1];
        if (!/testharness|testharnessreport|parsing-testcommon|query-testcommon/.test(srcAttr)) {
          unsupported(`the script ${srcAttr}`);
        }
        continue;
      }
      if (attrs && /type\s*=\s*["']?(module|text\/plain)/.test(attrs)) unsupported('a module script');
      vm.runInContext(code, context, { timeout: 5000, filename: rel });
    }
    for (const fn of [sandbox.onload, ...sandbox._loadHandlers]) {
      if (typeof fn === 'function') fn();
    }
  } catch (e) {
    if (state.cases.length === 0) {
      const reason = e instanceof Unsupported ? `needs ${e.message}` : reasonFor(html, state);
      return Object.assign(entry, { skipped_because: reason });
    }
    state.skipped.push({ test: '(rest of the file)', reason: e instanceof Unsupported ? `needs ${e.message}` : String(e) });
  }
  if (state.cases.length === 0) return Object.assign(entry, { skipped_because: reasonFor(html, state) });
  return Object.assign(entry, { cases: state.cases, skipped: state.skipped });
}

function walk(dir, rel = '') {
  const out = [];
  for (const name of fs.readdirSync(path.join(dir, rel)).sort()) {
    const r = path.join(rel, name);
    if (fs.statSync(path.join(dir, r)).isDirectory()) out.push(...walk(dir, r));
    else out.push(r);
  }
  return out;
}

const files = walk(src, 'css/selectors')
  .filter((f) => /\.(html|xht|xhtml)$/.test(f))
  .filter((f) => !/-ref\.|\/reference\/|\/resources\//.test(f))
  .filter((f) => !/-notref\.|-noref\./.test(f));
files.unshift('dom/nodes/Element-closest.html');

const result = {
  commit: fs.readFileSync(path.join(src, 'COMMIT'), 'utf8').trim(),
  dom: domNodesData(),
  files: files.map(extract),
};

fs.mkdirSync(out, { recursive: true });
fs.writeFileSync(path.join(out, 'cases.json'), JSON.stringify(result, null, 1) + '\n');
const keep = ['dom/nodes/ParentNode-querySelector-All-content.html'];
for (const f of result.files) if (f.cases) keep.push(f.file);
for (const f of keep) {
  fs.mkdirSync(path.join(out, path.dirname(f)), { recursive: true });
  fs.copyFileSync(path.join(src, f), path.join(out, f));
}
const ran = result.files.filter((f) => f.cases);
console.error(
  `${ran.length} files with ${ran.reduce((n, f) => n + f.cases.length, 0)} cases, ` +
    `${result.files.length - ran.length} files skipped`
);