Skip to content

Commit f6a0bf1

Browse files
committed
Handle attribute string errors and escaped line continuations (#198)
1 parent 0922fc7 commit f6a0bf1

8 files changed

Lines changed: 357 additions & 6 deletions

File tree

‎.github/workflows/node.js.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ jobs:
2525
- run: npm run upstream:clone
2626
- run: npm run upstream:verify
2727
- run: npx playwright install --with-deps chromium
28+
- run: npm run test:browser
2829
- run: npm run test:upstream

‎assets/repo/coverage.svg‎

Lines changed: 1 addition & 1 deletion
Loading

‎package.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
"test:package": "node test/jsdom-adapter-package.mts",
100100
"test:upstream": "node scripts/build.mts && playwright test --config .config/playwright.config.mts",
101101
"upstream:clone": "node scripts/git-partial-submodule.mts clone",
102-
"upstream:verify": "node scripts/git-partial-submodule.mts verify"
102+
"upstream:verify": "node scripts/git-partial-submodule.mts verify",
103+
"test:attributes": "vitest run --config .config/vitest.config.mts test/attribute-parse-error.test.mts",
104+
"test:attributes:browser": "NWSAPI_BROWSER=1 vitest run --config .config/vitest.config.mts test/attribute-strings-browser.test.mts",
105+
"test:browser": "NWSAPI_BROWSER=1 vitest run --config .config/vitest.config.mts"
103106
}
104107
}

‎src/nwsapi.mts‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
HexNumbers: RegExp('^[0-9a-fA-F]'),
6464
EscOrQuote: RegExp('^\\\\|[\\x22\\x27]'),
6565
RegExpChar: RegExp('(?!\\\\)[\\\\^$.,*+?()[\\]{}|\\/]', 'g'),
66-
TrimSpaces: RegExp('^' + WSP + '+|' + WSP + '+$|' + VSP, 'g'),
66+
TrimSpaces: RegExp('^' + WSP + '+|' + WSP + '+$', 'g'),
6767
SplitGroup: RegExp('(\\([^)]*\\)|\\[[^[]*\\]|\\\\.|[^,])+', 'g'),
6868
CommaGroup: RegExp('(\\s*,\\s*)' + NOT.square_enc + NOT.parens_enc, 'g'),
6969
FixEscapes: RegExp('\\\\([0-9a-fA-F]{1,6}' + WSP + '?|.)|([\\x22\\x27])', 'g'),
@@ -933,8 +933,8 @@
933933

934934
pseudonames = '[-\\w]+',
935935
pseudoparms = '(?:[-+]?\\d*)(?:n\\s?[-+]?\\s?\\d*)',
936-
doublequote = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*(?:"|$)',
937-
singlequote = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*(?:'|$)",
936+
doublequote = '"[^"\\\\' + VSP + ']*(?:\\\\.[^"\\\\' + VSP + ']*)*(?:"|$)',
937+
singlequote = "'[^'\\\\" + VSP + "]*(?:\\\\.[^'\\\\" + VSP + "]*)*(?:'|$)",
938938

939939
attrparser = identifier + '|' + doublequote + '|' + singlequote,
940940

@@ -1179,6 +1179,7 @@
11791179
// attributes resolver
11801180
case '[':
11811181
match = selector.match(Patterns.attribute);
1182+
if (!match) { break; }
11821183
NS = match[0].match(STD.namespaces);
11831184
name = match[1];
11841185
expr = name.split(':');
@@ -1826,6 +1827,44 @@
18261827
return { factory: f };
18271828
},
18281829

1830+
// Consume string continuations before whitespace normalization. Preserve
1831+
// escape boundaries: removing a continuation must not extend a hex escape.
1832+
stringContinuations =
1833+
function(selectors) {
1834+
if (!/[\r\n\f]/.test(selectors)) { return selectors; }
1835+
var i = 0, j, c, next, quote = '', result = '', length = selectors.length;
1836+
while (i < length) {
1837+
c = selectors[i++];
1838+
if (c == '\\' && i == length && quote) { break; }
1839+
if (c == '\\' && i < length) {
1840+
next = selectors[i];
1841+
if (quote && /[\r\n\f]/.test(next)) {
1842+
++i;
1843+
if (next == '\r' && selectors[i] == '\n') { ++i; }
1844+
continue;
1845+
}
1846+
if (quote && /[0-9a-f]/i.test(next)) {
1847+
j = i;
1848+
while (i < length && i - j < 6 && /[0-9a-f]/i.test(selectors[i])) { ++i; }
1849+
result += '\\' + ('000000' + selectors.slice(j, i)).slice(-6);
1850+
if (/[\x20\t\r\n\f]/.test(selectors[i] || '')) {
1851+
next = selectors[i++];
1852+
if (next == '\r' && selectors[i] == '\n') { ++i; }
1853+
}
1854+
continue;
1855+
}
1856+
result += c + selectors[i++];
1857+
continue;
1858+
}
1859+
if (c == quote) { quote = ''; }
1860+
else if (!quote && (c == '"' || c == "'")) { quote = c; }
1861+
result += c;
1862+
}
1863+
// EOF closes a string. Keep its trailing whitespace inside that string
1864+
// so selector trimming cannot erase a bad newline or a literal space.
1865+
return result + quote;
1866+
},
1867+
18291868
// unique parser entry point for all
18301869
// methods (type matching/selecting)
18311870
parse =
@@ -1851,7 +1890,7 @@
18511890
}
18521891

18531892
// normalize input string
1854-
parsed = selectors.
1893+
parsed = stringContinuations(selectors).
18551894
replace(/\x00|\\$/g, '\ufffd').
18561895
replace(REX.CombineWSP, '\x20').
18571896
replace(REX.PseudosWSP, '$1').

‎test/attribute-parse-error.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Attribute parse errors
2+
3+
Run with Node.js ≥ 22:
4+
5+
```sh
6+
pnpm install
7+
pnpm run test:attributes
8+
pnpm exec playwright install chromium
9+
pnpm run test:attributes:browser
10+
```
11+
12+
The suite checks LF, CR, CRLF, and form feed inside single- and double-quoted
13+
attribute values. Selection, first-match lookup, and direct attribute
14+
matching must report `SyntaxError`, not leak a `TypeError`. Quiet mode
15+
must return no match. Repeated calls check that caches do not change errors.
16+
17+
Valid cases cover missing closing tokens, selector whitespace, and a hex
18+
escape. End of input can close a construct; a newline inside a quoted
19+
string is not end of input.
20+
21+
Escaped line continuations are consumed before whitespace normalization.
22+
Hex escapes keep their boundaries when a continuation is removed. Coverage
23+
includes literal backslashes, escaped quotes, attribute operators, repeated
24+
continuations and end-of-input recovery. Raw newlines remain invalid even
25+
at the end of input, and `match()` checks the whole tag-prefixed selector.
26+
27+
All attribute tests are required to pass. The browser command compares
28+
selection, first-match lookup and matching against Chromium, twice per
29+
selector to exercise cached calls. It requires no WPT checkout or server.
30+
31+
Validation: 163 attribute tests, four runtime tests and 11 adapter tests pass
32+
with jsdom 30.0.1. All 155 browser cases agree with Chromium 151.0.7922.34. The
33+
41-page WPT runner from #167 also passes with its baseline unchanged.
34+
35+
See [CSS string tokenization](https://drafts.csswg.org/css-syntax/#consume-string-token)
36+
and [function consumption](https://drafts.csswg.org/css-syntax/#consume-function).
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

‎test/attribute-string-cases.mts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const cases = []
2+
for (const quote of ['"', "'"]) {
3+
for (const newline of ['\n', '\r', '\r\n', '\f']) {
4+
const suffix = JSON.stringify({ quote, newline })
5+
for (const [name, raw, value] of [
6+
['middle continuation', 'x\\' + newline + 'y', 'xy'],
7+
['hex boundary', '\\7\\' + newline + '8', '\x078'],
8+
['hex newline terminator', '\\78' + newline + 'y', 'xy'],
9+
['six-digit hex boundary', '\\000078\\' + newline + '9', 'x9'],
10+
[
11+
'escaped quote',
12+
'x\\' + quote + '\\' + newline + 'y',
13+
'x' + quote + 'y',
14+
],
15+
['literal backslash', 'x' + '\\'.repeat(3) + newline + 'y', 'x\\y'],
16+
['consecutive continuations', '\\' + newline + 'x\\' + newline, 'x'],
17+
]) {
18+
cases.push({
19+
name: name + ': ' + suffix,
20+
value,
21+
selector: 'div[data-x=' + quote + raw + quote + ']',
22+
})
23+
}
24+
cases.push({
25+
name: 'EOF after continuation: ' + suffix,
26+
value: 'x',
27+
selector: 'div[data-x=' + quote + 'x\\' + newline,
28+
})
29+
cases.push({
30+
name: 'EOF preserves string space: ' + suffix,
31+
value: 'x ',
32+
selector: 'div[data-x=' + quote + 'x\\' + newline + ' ',
33+
})
34+
cases.push({
35+
name: 'EOF after final backslash: ' + suffix,
36+
value: 'x',
37+
selector: 'div[data-x=' + quote + 'x\\' + newline + '\\',
38+
})
39+
cases.push({
40+
name: 'raw newline at EOF is invalid: ' + suffix,
41+
value: 'x',
42+
valid: false,
43+
selector: 'div[data-x=' + quote + 'x' + newline,
44+
})
45+
for (const operator of ['~=', '|=', '^=', '$=', '*=']) {
46+
cases.push({
47+
name: 'hex continuation with ' + operator + ': ' + suffix,
48+
value: 'x',
49+
selector:
50+
'div[data-x' + operator + quote + '\\78\\' + newline + quote + ']',
51+
})
52+
}
53+
cases.push({
54+
name: 'even backslashes do not escape newline: ' + suffix,
55+
value: 'x\\y',
56+
valid: false,
57+
selector:
58+
'div[data-x=' +
59+
quote +
60+
'x' +
61+
'\\'.repeat(2) +
62+
newline +
63+
'y' +
64+
quote +
65+
']',
66+
})
67+
}
68+
}
69+
export default cases

0 commit comments

Comments
 (0)