Skip to content
Open
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
22 changes: 22 additions & 0 deletions src/__tests__/exceptions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ throws("unclosed pseudo element", "button::");
throws("unclosed pseudo class", "a:");
throws("unclosed attribute selector", '[name="james"][href');

// Constructs left open at end of input. These threw a raw TypeError before
// `attribute`, `namespace` and `parentheses` guarded against running out of
// tokens. Asserted by message rather than by type: the default
// `{instanceOf: Error}` check is satisfied by a TypeError, which is why the
// existing "unclosed attribute selector" case above passed throughout.
throws(
"unclosed attribute at end of input",
"a[href",
"Expected a closing square bracket.",
);
throws(
"unclosed attribute with value at end of input",
"a[href=x",
"Expected a closing square bracket.",
);
throws("trailing namespace pipe", ".foo|", "Unexpected '|'.");
throws(
"unclosed parenthesis at end of input",
"a(",
"Expected a closing parenthesis.",
);

throws("no opening parenthesis", ")");
throws("no opening parenthesis (2)", ":global.foo)");
throws("no opening parenthesis (3)", "h1:not(h2:not(h3)))");
Expand Down
18 changes: 17 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ export default class Parser {
attr.push(this.currToken);
this.position++;
}
if (!this.currToken) {
// Ran off the end of the token stream: the attribute was never closed.
// Point at the opening bracket, which is where the author needs to look.
return this.expected("closing square bracket", startingToken[TOKEN.START_POS]);
}
if (this.currToken[TOKEN.TYPE] !== tokens.closeSquare) {
return this.expected("closing square bracket", this.currToken[TOKEN.START_POS]);
}
Expand Down Expand Up @@ -697,6 +702,11 @@ export default class Parser {

namespace() {
const before = (this.prevToken && this.content(this.prevToken)) || true;
if (!this.nextToken) {
// A trailing `|` with nothing after it. `unexpectedPipe` reports against
// `currToken`, which is the pipe itself and always present here.
return this.unexpectedPipe();
}
if (this.nextToken[TOKEN.TYPE] === tokens.word) {
this.position++;
return this.word(before);
Expand Down Expand Up @@ -730,6 +740,7 @@ export default class Parser {
parentheses() {
let last = this.current.last;
let unbalanced = 1;
const openingToken = this.currToken;
this.position++;
if (last && last.type === types.PSEUDO) {
const selector = new Selector({
Expand Down Expand Up @@ -805,7 +816,12 @@ export default class Parser {
}
}
if (unbalanced) {
return this.expected("closing parenthesis", this.currToken[TOKEN.START_POS]);
// `currToken` is undefined when the token stream ran out before the
// parenthesis was closed; fall back to the opening one.
return this.expected(
"closing parenthesis",
(this.currToken || openingToken)[TOKEN.START_POS],
);
}
}

Expand Down