From af5b0550f6238d5491098034affd02a6ecb76363 Mon Sep 17 00:00:00 2001 From: Ultroncn <82553854+Ultroncn@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:20:17 +0000 Subject: [PATCH] fix: reject content dropped between an attribute operator and a quoted value Fixes #366 Parsing `[class=XXX'@{foo}-']` silently dropped the `XXX` between the operator and the quoted value: the bare word was first read as the value, then the string overwrote it. The same silent drop happened for a bare `~` in `[class=~'@{foo}-']`, the LESS escape syntax the issue was filed against (not valid in LESS 5). Reject both cases with a parse error instead of silently discarding input: a string token after a value is already defined now errors, and a bare `~` after an operator (not followed by `=`) now errors. Valid selectors are unaffected: quoted values, unquoted values, `~=` and other operators, and Sass interpolations all still parse. Added regression tests to src/__tests__/exceptions.mjs. Full suite: 1078 passing (node --test src/__tests__/*.mjs); typecheck clean. --- src/__tests__/exceptions.mjs | 14 ++++++++++++++ src/parser.js | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/__tests__/exceptions.mjs b/src/__tests__/exceptions.mjs index 358382a..3c29799 100644 --- a/src/__tests__/exceptions.mjs +++ b/src/__tests__/exceptions.mjs @@ -86,3 +86,17 @@ throws( ".foo !optional", "Unexpected '!'. Escaping special characters with \\ may help.", ); + +// Content between an attribute operator and a quoted value used to be +// silently dropped: `[class=XXX'@{foo}-']` parsed as `[class='@{foo}-']` +// and `[class=~'@{foo}-']` parsed as `[class='@{foo}-']`. +throws( + "unquoted value before a quoted value", + "[class=XXX'@{foo}-']", + "Unexpected string found; an attribute value was already defined.", +); +throws( + "bare tilde before a quoted value", + "[class=~'@{foo}-']", + 'Unexpected "~" found; an operator was already defined.', +); diff --git a/src/parser.js b/src/parser.js index 9206c8b..702ed2d 100644 --- a/src/parser.js +++ b/src/parser.js @@ -359,6 +359,17 @@ export default class Parser { ) { return this.expected("attribute operator", token[TOKEN.START_POS]); } + if ( + node.operator && + content === "~" && + !(next && next[TOKEN.TYPE] === tokens.equals) + ) { + // A bare `~` after the operator, such as in `[class=~'x']`, + // cannot form an operator and must not be silently dropped. + return this.error('Unexpected "~" found; an operator was already defined.', { + index: token[TOKEN.START_POS], + }); + } if (content === "~" && next && next[TOKEN.TYPE] === tokens.equals) { node.operator = content; lastAdded = "operator"; @@ -470,6 +481,14 @@ export default class Parser { }, ); } + if (node.value !== undefined) { + // A quoted value must not silently overwrite an unquoted value + // that was already read, such as the `XXX` in + // `[class=XXX'@{foo}-']`. + return this.error('Unexpected string found; an attribute value was already defined.', { + index: token[TOKEN.START_POS], + }); + } let { unescaped, quoteMark } = unescapeValue(content); node.value = unescaped; node.quoteMark = quoteMark;