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
9 changes: 9 additions & 0 deletions src/__tests__/lossy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ ava("namespace - all elements without namespace, space before", testLossy, " |
ava("namespace - all elements without namespace, space after", testLossy, "|* ", "|*");
ava("namespace - tag with no namespace, space before", testLossy, " |button", "|button");
ava("namespace - tag with no namespace, space after", testLossy, "|button ", "|button");
ava("namespace - descendant tag with no namespace", testLossy, "section |input", "section |input");
ava("namespace - descendant universal with no namespace", testLossy, "main\t|*", "main |*");
ava("namespace - child tag with no namespace", testLossy, "section > |input", "section> |input");
ava(
"namespace - comment in the descendant gap",
testLossy,
"nav /* note */ |a[href]",
"nav /* note */|a[href]",
);
ava(
"namespace - inside attribute, space before",
testLossy,
Expand Down
6 changes: 0 additions & 6 deletions src/__tests__/property/known.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ export const KNOWN = [
note: "#189: a comment before a combinator is kept in the combinator's raws",
match: (f) => f.type === "combinator" && f.text.includes("/*"),
},
{
property: "lossy output is stable",
tracking: "not filed yet",
note: "lossy output drops the whitespace before a `|`, so `a |b` becomes `a|b`",
match: (f) => /\s\|(?!=)|\*\/\|/.test(f.input),
},
{
property: "values set through the API read back the same",
tracking: `${ISSUE}357`,
Expand Down
11 changes: 10 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,13 @@ export default class Parser {
let { space, rawSpace } = this.convertWhitespaceNodesToSpace(
spaceOrDescendantSelectorNodes,
);
if (this.options.lossy && node.value === "|") {
// Keep the descendant space before an empty namespace prefix.
space = " ";
if (rawSpace) {
rawSpace = " " + rawSpace;
}
}
node.spaces.before = space;
node.rawSpaceBefore = rawSpace;
}
Expand Down Expand Up @@ -773,7 +780,9 @@ export default class Parser {
}

if (this.currToken && this.currToken[TOKEN.TYPE] === tokens.space) {
node.spaces.after = this.optionalSpace(this.content());
// A bare pipe must stay separate from the preceding combinator.
const beforeEmptyNamespace = this.nextToken && this.content(this.nextToken) === "|";
node.spaces.after = this.lossySpace(this.content(), beforeEmptyNamespace);
this.position++;
}

Expand Down