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
20 changes: 20 additions & 0 deletions src/__tests__/namespaces.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,23 @@ throws("lone pipe symbol with trailing spaces", "| ");
throws("lone pipe symbol with surrounding spaces", " | ");
throws("trailing pipe symbol with a namespace", "foo| ");
throws("trailing pipe symbol with any namespace", "*| ");

test("`|` after a pseudo-class does not reuse its name as a namespace", "a:hover|b", (t, tree) => {
t.deepEqual(tree.nodes[0].nodes.length, 3);
t.deepEqual(tree.nodes[0].nodes[1].type, "pseudo");
t.deepEqual(tree.nodes[0].nodes[1].value, ":hover");
t.deepEqual(tree.nodes[0].nodes[2].type, "tag");
t.deepEqual(tree.nodes[0].nodes[2].namespace, true);
t.deepEqual(tree.nodes[0].nodes[2].value, "b");
});

test(
"`|*` after a pseudo-element does not reuse its name as a namespace",
"::before|*",
(t, tree) => {
t.deepEqual(tree.nodes[0].nodes.length, 2);
t.deepEqual(tree.nodes[0].nodes[0].value, "::before");
t.deepEqual(tree.nodes[0].nodes[1].type, "universal");
t.deepEqual(tree.nodes[0].nodes[1].namespace, true);
},
);
14 changes: 10 additions & 4 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,10 @@ export default class Parser {

combinator() {
if (this.content() === "|") {
return this.namespace();
// A word or `*` right before this `|` was already consumed by whatever
// came before, such as the name of a pseudo in `:hover|a`, so it cannot be
// the prefix. Only a skipped `&` can still be one.
return this.namespace(false);
}
// We need to decide between a space that's a descendant combinator and meaningless whitespace at the end of a selector.
let nextSigTokenPos = this.locateNextMeaningfulToken(this.position);
Expand Down Expand Up @@ -831,15 +834,18 @@ export default class Parser {
return this.error(`Unexpected '|'.`, this.currToken[TOKEN.START_POS]);
}

namespace() {
/**
* @param {boolean} [prefixed] whether the token before the `|` is still
* unconsumed, as it is when `word()` or `universal()` sees the `|` next.
*/
namespace(prefixed = true) {
const prev = this.prevToken;
// Only treat the previous token as a namespace prefix when it can actually
// be one (a type/word or the universal `*`). A comment, comma, combinator
// or whitespace before `|` means an empty namespace, not a prefix.
const before =
prev &&
(prev[TOKEN.TYPE] === tokens.word ||
prev[TOKEN.TYPE] === tokens.asterisk ||
((prefixed && (prev[TOKEN.TYPE] === tokens.word || prev[TOKEN.TYPE] === tokens.asterisk)) ||
prev[TOKEN.TYPE] === tokens.ampersand)
? this.content(prev)
: true;
Expand Down
Loading