|
| 1 | +import { createRequire } from 'node:module' |
| 2 | +const require = createRequire(import.meta.url) |
| 3 | +const assert = require('node:assert/strict') |
| 4 | +import { test } from 'vitest' |
| 5 | +const { JSDOM } = require('jsdom') |
| 6 | +const factory = require('../src/nwsapi.js') |
| 7 | + |
| 8 | +function fixture(t) { |
| 9 | + const { window } = new JSDOM( |
| 10 | + '<!doctype html><meta id="encoding" charset="utf-8">' + |
| 11 | + '<body><div id="a" class="x"></div><div id="b"></div>', |
| 12 | + ) |
| 13 | + t.onTestFinished(() => window.close()) |
| 14 | + return { |
| 15 | + document: window.document, |
| 16 | + nw: factory({ |
| 17 | + document: window.document, |
| 18 | + DOMException: window.DOMException, |
| 19 | + }), |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +for (const quote of ['"', "'"]) { |
| 24 | + for (const newline of ['\n', '\r', '\r\n', '\f']) { |
| 25 | + const attribute = '[class=' + quote + 'x' + newline + quote + ']' |
| 26 | + const selector = 'div' + attribute |
| 27 | + test('invalid attribute string: ' + JSON.stringify(selector), t => { |
| 28 | + const { document, nw } = fixture(t) |
| 29 | + const target = document.getElementById('a') |
| 30 | + for (let i = 0; i < 2; i++) { |
| 31 | + assert.throws(() => nw.select(selector, document), { |
| 32 | + name: 'SyntaxError', |
| 33 | + }) |
| 34 | + assert.throws(() => nw.first(selector, document), { |
| 35 | + name: 'SyntaxError', |
| 36 | + }) |
| 37 | + assert.throws(() => nw.match(attribute, target), { |
| 38 | + name: 'SyntaxError', |
| 39 | + }) |
| 40 | + assert.throws(() => nw.match(selector, target), { name: 'SyntaxError' }) |
| 41 | + } |
| 42 | + }) |
| 43 | + test('quiet invalid attribute string: ' + JSON.stringify(selector), t => { |
| 44 | + const { document, nw } = fixture(t) |
| 45 | + nw.configure({ VERBOSITY: false, LOGERRORS: false }) |
| 46 | + assert.deepEqual(nw.select(selector, document), []) |
| 47 | + assert.equal(nw.first(selector, document), null) |
| 48 | + assert.equal(nw.match(attribute, document.getElementById('a')), false) |
| 49 | + assert.equal(nw.match(selector, document.getElementById('a')), false) |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +for (const [selector, expected] of [ |
| 55 | + ['meta[charset="utf-8"', ['encoding']], |
| 56 | + ['meta[charset="utf-8', ['encoding']], |
| 57 | + ['div:not([class]', ['b']], |
| 58 | + ['div:not([class', ['b']], |
| 59 | + ['div:is([class="x"', ['a']], |
| 60 | + ['div\n[class="x"]', []], |
| 61 | + ['div[class="\\78 "]', ['a']], |
| 62 | +]) { |
| 63 | + test('valid selector: ' + JSON.stringify(selector), t => { |
| 64 | + const { document, nw } = fixture(t) |
| 65 | + for (let i = 0; i < 2; i++) { |
| 66 | + assert.deepEqual( |
| 67 | + nw.select(selector, document).map(e => e.id), |
| 68 | + expected, |
| 69 | + ) |
| 70 | + assert.equal( |
| 71 | + nw.match(selector, document.getElementById('a')), |
| 72 | + expected.includes('a'), |
| 73 | + ) |
| 74 | + } |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +// Every CSS newline form is a valid continuation after a backslash in a string. |
| 79 | +for (const newline of ['\n', '\r', '\r\n', '\f']) { |
| 80 | + test('valid line continuation: ' + JSON.stringify(newline), t => { |
| 81 | + const { document, nw } = fixture(t) |
| 82 | + assert.deepEqual( |
| 83 | + nw.select('div[class="x\\' + newline + '"]', document).map(e => e.id), |
| 84 | + ['a'], |
| 85 | + ) |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +import stringCases from './attribute-string-cases.mts' |
| 90 | +for (const { name, selector, value, valid = true } of stringCases) { |
| 91 | + test(name, t => { |
| 92 | + const { document, nw } = fixture(t) |
| 93 | + const target = document.getElementById('a') |
| 94 | + target.setAttribute('data-x', value) |
| 95 | + for (let i = 0; i < 2; ++i) { |
| 96 | + if (valid) { |
| 97 | + assert.deepEqual( |
| 98 | + nw.select(selector, document).map(e => e.id), |
| 99 | + ['a'], |
| 100 | + ) |
| 101 | + assert.equal(nw.first(selector, document), target) |
| 102 | + assert.equal(nw.match(selector, target), true) |
| 103 | + assert.equal(nw.match(selector, document.getElementById('b')), false) |
| 104 | + } else { |
| 105 | + assert.throws(() => nw.select(selector, document), { |
| 106 | + name: 'SyntaxError', |
| 107 | + }) |
| 108 | + assert.throws(() => nw.first(selector, document), { |
| 109 | + name: 'SyntaxError', |
| 110 | + }) |
| 111 | + assert.throws(() => nw.match(selector, target), { name: 'SyntaxError' }) |
| 112 | + } |
| 113 | + } |
| 114 | + }) |
| 115 | +} |
0 commit comments