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
15 changes: 15 additions & 0 deletions src/__tests__/namespaces.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ test("ns alias for namespace", "f\\oo|h1.foo", (t, tree) => {
t.deepEqual(tag.ns, "bar");
});

test("empty namespace after a comment is not a prefix", "/* c */|b", (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[1].namespace, true);
t.deepEqual(tree.nodes[0].nodes[1].value, "b");
});

test("empty namespace after a comma is not a prefix", ".a,|b", (t, tree) => {
t.deepEqual(tree.nodes[1].nodes[0].namespace, true);
t.deepEqual(tree.nodes[1].nodes[0].value, "b");
});

test("empty namespace after a combinator is not a prefix", ".a > |b", (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[2].namespace, true);
t.deepEqual(tree.nodes[0].nodes[2].value, "b");
});

throws("lone pipe symbol", "|");
throws("lone pipe symbol with leading spaces", " |");
throws("lone pipe symbol with trailing spaces", "| ");
Expand Down
12 changes: 11 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,17 @@ export default class Parser {
}

namespace() {
const before = (this.prevToken && this.content(this.prevToken)) || 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 ||
prev[TOKEN.TYPE] === tokens.ampersand)
? this.content(prev)
: true;
if (this.nextToken[TOKEN.TYPE] === tokens.word) {
this.position++;
return this.word(before);
Expand Down