|
| 1 | +const __dirname = import.meta.dirname |
| 2 | +import { createRequire } from 'node:module' |
| 3 | +const require = createRequire(import.meta.url) |
| 4 | +const assert = require('node:assert/strict') |
| 5 | +const { readFileSync } = require('node:fs') |
| 6 | +const { resolve } = require('node:path') |
| 7 | +import { test } from 'vitest' |
| 8 | +const vm = require('node:vm') |
| 9 | +const source = readFileSync(resolve(__dirname, '../src/nwsapi.js'), 'utf8') |
| 10 | +const createNwsapi = require('../src/nwsapi.js') |
| 11 | +// Route jsdom through the adapter from this checkout. Count actual adapter |
| 12 | +// instances so the reentry test cannot silently exercise jsdom's default engine. |
| 13 | +const { DOMSelector } = createNwsapi |
| 14 | +const resolved = require.resolve('@asamuzakjp/dom-selector', { |
| 15 | + paths: [require.resolve('jsdom')], |
| 16 | +}) |
| 17 | +const previous = require(resolved) |
| 18 | +let creations = 0, |
| 19 | + matchCalls = 0, |
| 20 | + depth = 0, |
| 21 | + peakDepth = 0 |
| 22 | +require.cache[resolved].exports = { |
| 23 | + DOMSelector: class extends DOMSelector { |
| 24 | + constructor(...args) { |
| 25 | + super(...args) |
| 26 | + creations++ |
| 27 | + const match = this.engine.match |
| 28 | + this.engine.match = function (...args) { |
| 29 | + matchCalls++ |
| 30 | + peakDepth = Math.max(peakDepth, ++depth) |
| 31 | + try { |
| 32 | + if (depth > 20) throw new Error('Reentry safety limit') |
| 33 | + return match.apply(this, args) |
| 34 | + } finally { |
| 35 | + depth-- |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + }, |
| 40 | +} |
| 41 | +const { JSDOM } = (() => { |
| 42 | + try { |
| 43 | + return require('jsdom') |
| 44 | + } finally { |
| 45 | + require.cache[resolved].exports = previous |
| 46 | + } |
| 47 | +})() |
| 48 | +const pseudos = [ |
| 49 | + ':modal', |
| 50 | + ':fullscreen', |
| 51 | + ':picture-in-picture', |
| 52 | + ':open', |
| 53 | + ':closed', |
| 54 | + ':popover-open', |
| 55 | +] |
| 56 | + |
| 57 | +function host(t) { |
| 58 | + const dom = new JSDOM('<!doctype html><div popover></div>') |
| 59 | + t.onTestFinished(() => dom.window.close()) |
| 60 | + return dom.window |
| 61 | +} |
| 62 | + |
| 63 | +for (const legacy of [false, true]) |
| 64 | + test(`delegating matchers are called once per document (LEGACY=${legacy})`, t => { |
| 65 | + const windows = [host(t), host(t)] |
| 66 | + const nw = createNwsapi({ document: windows[0].document }) |
| 67 | + nw.configure({ LEGACY: legacy }) |
| 68 | + const calls = [0, 0] |
| 69 | + const nodes = windows.map(w => w.document.querySelector('div')) |
| 70 | + windows.forEach((w, i) => { |
| 71 | + w.Element.prototype.matches = function (selector) { |
| 72 | + assert.ok(++calls[i] < 10, 'unbounded recursion') |
| 73 | + return nw.match(selector, this) |
| 74 | + } |
| 75 | + }) |
| 76 | + for (let i = 0; i < 50; i++) { |
| 77 | + for (const node of nodes) |
| 78 | + for (const pseudo of pseudos) |
| 79 | + assert.equal(nw.match(pseudo, node), false) |
| 80 | + } |
| 81 | + assert.deepEqual(calls, [1, 1]) |
| 82 | + }) |
| 83 | + |
| 84 | +test('browser bootstrap safely captures an already-delegating prototype', t => { |
| 85 | + const window = host(t) |
| 86 | + let nw, |
| 87 | + calls = 0 |
| 88 | + window.Element.prototype.matches = function (selector) { |
| 89 | + assert.ok(++calls < 10, 'unbounded recursion') |
| 90 | + return nw.match(selector, this) |
| 91 | + } |
| 92 | + const context = { document: window.document, Element: window.Element } |
| 93 | + vm.runInNewContext(source, context) |
| 94 | + nw = context.NW.Dom |
| 95 | + const node = window.document.querySelector('div') |
| 96 | + for (let i = 0; i < 50; i++) |
| 97 | + assert.equal(nw.match(':popover-open', node), false) |
| 98 | + assert.equal(calls, 1) |
| 99 | +}) |
| 100 | + |
| 101 | +test('the original jsdom Element.matches route uses this engine without recursion', t => { |
| 102 | + const window = host(t) |
| 103 | + const before = creations |
| 104 | + const beforeCalls = matchCalls |
| 105 | + peakDepth = 0 |
| 106 | + const node = window.document.body.firstElementChild |
| 107 | + for (let i = 0; i < 50; i++) |
| 108 | + for (const pseudo of pseudos) assert.equal(node.matches(pseudo), false) |
| 109 | + assert.equal( |
| 110 | + creations - before, |
| 111 | + 1, |
| 112 | + 'jsdom must exercise the source under test', |
| 113 | + ) |
| 114 | + assert.ok( |
| 115 | + matchCalls - beforeCalls <= 301, |
| 116 | + '300 calls allow only one delegation probe', |
| 117 | + ) |
| 118 | + assert.ok(peakDepth <= 2, 'the adapter must not hide repeated reentry') |
| 119 | +}) |
| 120 | + |
| 121 | +test('delegation remains cached when a re-entering matcher subsequently throws', t => { |
| 122 | + const window = host(t) |
| 123 | + const nw = createNwsapi({ document: window.document }) |
| 124 | + const node = window.document.querySelector('div') |
| 125 | + let calls = 0 |
| 126 | + window.Element.prototype.matches = function (selector) { |
| 127 | + calls++ |
| 128 | + nw.match(selector, this) |
| 129 | + throw new Error('host failed after re-entry') |
| 130 | + } |
| 131 | + for (let i = 0; i < 10; i++) |
| 132 | + assert.equal(nw.match(':popover-open', node), false) |
| 133 | + assert.equal(calls, 1) |
| 134 | +}) |
| 135 | + |
| 136 | +test('an unsupported selector does not disable other host state queries', t => { |
| 137 | + const window = host(t) |
| 138 | + window.Element.prototype.matches = function (selector) { |
| 139 | + if (selector === ':fullscreen') throw new Error('unsupported') |
| 140 | + return selector === ':popover-open' |
| 141 | + } |
| 142 | + const nw = createNwsapi({ document: window.document }) |
| 143 | + const node = window.document.querySelector('div') |
| 144 | + assert.equal(nw.match(':fullscreen', node), false) |
| 145 | + assert.equal(nw.match(':popover-open', node), true) |
| 146 | +}) |
| 147 | + |
| 148 | +test('re-entry involving another document marks the outer record', t => { |
| 149 | + const a = host(t), |
| 150 | + b = host(t) |
| 151 | + const nw = createNwsapi({ document: a.document }) |
| 152 | + const first = a.document.querySelector('div'), |
| 153 | + second = b.document.querySelector('div') |
| 154 | + let calls = 0 |
| 155 | + a.Element.prototype.matches = function (selector) { |
| 156 | + calls++ |
| 157 | + return nw.match(selector, second) |
| 158 | + } |
| 159 | + b.Element.prototype.matches = () => true |
| 160 | + assert.equal(nw.match(':popover-open', first), false) |
| 161 | + assert.equal(nw.match(':popover-open', second), true) |
| 162 | + assert.equal(nw.match(':popover-open', first), false) |
| 163 | + assert.equal(calls, 1) |
| 164 | +}) |
| 165 | + |
| 166 | +test('hosts without WeakMap retain bounded recursion and the single-document fast path', t => { |
| 167 | + const window = host(t) |
| 168 | + const context = { module: { exports: {} }, exports: {}, WeakMap: undefined } |
| 169 | + vm.runInNewContext(source, context) |
| 170 | + const nw = context.module.exports({ document: window.document }) |
| 171 | + nw.configure({ LEGACY: true }) |
| 172 | + let calls = 0 |
| 173 | + window.Element.prototype.matches = function (selector) { |
| 174 | + calls++ |
| 175 | + return nw.match(selector, this) |
| 176 | + } |
| 177 | + const node = window.document.querySelector('div') |
| 178 | + for (let i = 0; i < 50; i++) |
| 179 | + assert.equal(nw.match(':popover-open', node), false) |
| 180 | + assert.equal(calls, 1) |
| 181 | +}) |
0 commit comments