Skip to content

Commit c1e2a65

Browse files
fix(css): print a custom property's value holding an edit as written
A comma or whitespace a visitor wrote was lost to the boundary minification; the value now prints as a rule holding an edit does.
1 parent c00db3a commit c1e2a65

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13645,9 +13645,10 @@ const _customPropertySource = (node, writer, from, to) => {
1364513645
* @returns {string} the printed value
1364613646
*/
1364713647
const _customPropertyValue = (path, node, writer, from, to, asWritten) => {
13648-
if (asWritten) return _customPropertySource(node, writer, from, to);
13649-
// Read once per value: a token holding a visitor's edit prints as it left it.
13650-
const edited = _edits.length !== 0 && _holdsEdit(node);
13648+
// A value holding a visitor's edit prints as written, as what holds one does.
13649+
if (asWritten || (_edits.length !== 0 && _holdsEdit(node))) {
13650+
return _customPropertySource(node, writer, from, to);
13651+
}
1365113652
let out = "";
1365213653
let at = from;
1365313654
// The boundary held back: whether a whitespace token stood in it, whether a
@@ -13695,10 +13696,7 @@ const _customPropertyValue = (path, node, writer, from, to, asWritten) => {
1369513696
operator = false;
1369613697
continue;
1369713698
}
13698-
const text =
13699-
edited && _holdsEdit(child)
13700-
? writer.get(child)
13701-
: _customPropertyToken(path, child, writer);
13699+
const text = _customPropertyToken(path, child, writer);
1370213700
// WHY: CSS Values 4 §10.1 requires whitespace on both sides of a `+` or `-`
1370313701
// in a math function, and this value is a token stream whose consuming
1370413702
// context is unknown here — `--tail:(1px) - (2px)` reaches a `calc()` by

‎test/unitCases/css/parser.unittest.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,11 +736,22 @@ describe("CssSyntax — SourceProcessor", () => {
736736
/** @type {import("../../../lib/css/syntax-parser").CssVisitorMap} */
737737
const visitors = { [NodeType.Hash]: (path) => path.replace("blue") };
738738
expect(print("a{--x: foo #f00 bar(#f00) ;color:#f00}", visitors)).toBe(
739-
"a{--x:foo blue bar(blue);color:blue}"
739+
"a{--x:foo blue bar(blue);color:blue}"
740740
);
741741
expect(
742742
print("a{--x: foo #f00 bar(#f00) }", visitors, "beautify")
743743
).toBe("a {\n--x: foo blue bar(blue);\n}");
744+
// A comma and whitespace print as written too.
745+
expect(
746+
print("a{--x: a, b c;b:x}", {
747+
[NodeType.Comma]: (path) => path.replace(" / "),
748+
[NodeType.Whitespace]: (path) => path.remove()
749+
})
750+
).toBe("a{--x:a / bc;b:x}");
751+
// Only a value holding an edit is printed as written.
752+
expect(print("a{--x: a b;--y: c #f00}", visitors)).toBe(
753+
"a{--x:a b;--y:c blue}"
754+
);
744755
});
745756

746757
it("does nothing on a walk that prints nothing", () => {

0 commit comments

Comments
 (0)