Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/__tests__/exceptions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@
throws("bad syntax (2)", "! .body");

throws("missing backslash for semicolon", ".;");
throws("missing backslash for semicolon (2)", ".\;");

Check warning on line 85 in src/__tests__/exceptions.mjs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, lts/*)

eslint(no-useless-escape)

Unnecessary escape character ';'

Check warning on line 85 in src/__tests__/exceptions.mjs

View workflow job for this annotation

GitHub Actions / test (windows-latest, latest)

eslint(no-useless-escape)

Unnecessary escape character ';'

Check warning on line 85 in src/__tests__/exceptions.mjs

View workflow job for this annotation

GitHub Actions / test (windows-latest, lts/*)

eslint(no-useless-escape)

Unnecessary escape character ';'

Check warning on line 85 in src/__tests__/exceptions.mjs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, latest)

eslint(no-useless-escape)

Unnecessary escape character ';'
throws(
"unexpected / foo",
"-Option\/root",

Check warning on line 88 in src/__tests__/exceptions.mjs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, lts/*)

eslint(no-useless-escape)

Unnecessary escape character '/'

Check warning on line 88 in src/__tests__/exceptions.mjs

View workflow job for this annotation

GitHub Actions / test (windows-latest, latest)

eslint(no-useless-escape)

Unnecessary escape character '/'

Check warning on line 88 in src/__tests__/exceptions.mjs

View workflow job for this annotation

GitHub Actions / test (windows-latest, lts/*)

eslint(no-useless-escape)

Unnecessary escape character '/'

Check warning on line 88 in src/__tests__/exceptions.mjs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, latest)

eslint(no-useless-escape)

Unnecessary escape character '/'
"Unexpected '/'. Escaping special characters with \\ may help.",
);
throws(
Expand All @@ -93,3 +93,17 @@
".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.',
);
19 changes: 19 additions & 0 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
Loading