Skip to content

Commit c00db3a

Browse files
fix(css): print a visitor's write inside a custom property's value
The value is written back from source, so a replaced token in it was lost in both modes.
1 parent e969356 commit c00db3a

3 files changed

Lines changed: 54 additions & 12 deletions

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13598,6 +13598,39 @@ const _valueComponents = (path, node, writer) => {
1359813598
return components;
1359913599
};
1360013600

13601+
/**
13602+
* A custom property's value as written, kept comments included, and with the
13603+
* text a visitor's edit left in place of each token holding one.
13604+
* @param {Node} node the declaration whose tokens are printed
13605+
* @param {PrintContext} writer the print context (holds the kept comments)
13606+
* @param {number} from source offset the tokens start at
13607+
* @param {number} to source offset they end at
13608+
* @returns {string} the printed value
13609+
*/
13610+
const _customPropertySource = (node, writer, from, to) => {
13611+
// Straight from source, so the kept comments in it are already there —
13612+
// claim them, or the writer emits them a second time ahead of the next
13613+
// top-level node.
13614+
writer.takeInserts(from, to);
13615+
const input = _S._input;
13616+
if (_edits.length === 0 || !_holdsEdit(node)) {
13617+
const value = input.slice(from, to);
13618+
return to === input.length ? _closeAtEof(value) : value;
13619+
}
13620+
let out = "";
13621+
let at = from;
13622+
const i0 = _nodeIndex(node);
13623+
const first = _listStarts[i0];
13624+
const last = first + _listLens[i0];
13625+
for (let k = first; k < last; k++) {
13626+
const child = _nodeRef(_flat[k]);
13627+
if (!_holdsEdit(child)) continue;
13628+
out += input.slice(at, _startOf(child)) + writer.get(child);
13629+
at = _endOf(child);
13630+
}
13631+
return out + input.slice(at, to);
13632+
};
13633+
1360113634
/**
1360213635
* A custom property's value, token for token: every token is written back as
1360313636
* written, and a run of whitespace and dropped comments between two of them is
@@ -13608,9 +13641,13 @@ const _valueComponents = (path, node, writer) => {
1360813641
* @param {PrintContext} writer the print context (holds the kept comments)
1360913642
* @param {number} from source offset the tokens start at
1361013643
* @param {number} to source offset they end at
13644+
* @param {boolean=} asWritten keep the boundaries as written too, as beautify does
1361113645
* @returns {string} the printed value
1361213646
*/
13613-
const _customPropertyValue = (path, node, writer, from, to) => {
13647+
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);
1361413651
let out = "";
1361513652
let at = from;
1361613653
// The boundary held back: whether a whitespace token stood in it, whether a
@@ -13658,7 +13695,10 @@ const _customPropertyValue = (path, node, writer, from, to) => {
1365813695
operator = false;
1365913696
continue;
1366013697
}
13661-
const text = _customPropertyToken(path, child, writer);
13698+
const text =
13699+
edited && _holdsEdit(child)
13700+
? writer.get(child)
13701+
: _customPropertyToken(path, child, writer);
1366213702
// WHY: CSS Values 4 §10.1 requires whitespace on both sides of a `+` or `-`
1366313703
// in a math function, and this value is a token stream whose consuming
1366413704
// context is unknown here — `--tail:(1px) - (2px)` reaches a `calc()` by

‎lib/css/syntax-printer.js‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5511,16 +5511,7 @@ const _printDeclarationNode = (path, writer, minify, soft) => {
55115511
// would be a different CSSOM; only the boundaries between them go.
55125512
const from = path.start(path.child(0));
55135513
const to = path.end(path.child(count - 1));
5514-
if (minify) {
5515-
value = _customPropertyValue(path, path.node, writer, from, to);
5516-
} else {
5517-
// Straight from source, so the kept comments in it are already
5518-
// there — claim them, or the writer emits them a second time
5519-
// ahead of the next top-level node.
5520-
writer.takeInserts(from, to);
5521-
value = _S._input.slice(from, to);
5522-
if (to === _S._input.length) value = _closeAtEof(value);
5523-
}
5514+
value = _customPropertyValue(path, path.node, writer, from, to, !minify);
55245515
// Closed as printed, since a `}` may still follow; the grammar leaves it
55255516
// open where nothing does, as the engine keeps it.
55265517
const last = _S._eofPrinted;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,17 @@ describe("CssSyntax — SourceProcessor", () => {
732732
}
733733
});
734734

735+
it("prints a replacement in a custom property's value, the rest as written", () => {
736+
/** @type {import("../../../lib/css/syntax-parser").CssVisitorMap} */
737+
const visitors = { [NodeType.Hash]: (path) => path.replace("blue") };
738+
expect(print("a{--x: foo #f00 bar(#f00) ;color:#f00}", visitors)).toBe(
739+
"a{--x:foo blue bar(blue);color:blue}"
740+
);
741+
expect(
742+
print("a{--x: foo #f00 bar(#f00) }", visitors, "beautify")
743+
).toBe("a {\n--x: foo blue bar(blue);\n}");
744+
});
745+
735746
it("does nothing on a walk that prints nothing", () => {
736747
let seen = 0;
737748
new SourceProcessor()

0 commit comments

Comments
 (0)