Skip to content

Commit 366a9d0

Browse files
fix(css): compose a style attribute after a write, ignore comment writes
A replaced declaration in a style attribute no longer feeds the list-wide rewrites, and a write to a comment no longer lands on a later node.
1 parent 93915e2 commit 366a9d0

4 files changed

Lines changed: 76 additions & 21 deletions

File tree

‎docs/syntax.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Conventions every language keeps, and a new member follows:
9292
- **Positions come as numbers and as ranges**, like acorn's `start`/`end` and `range`: `start`, `end`, `startOf` and `endOf` allocate nothing, so `CssParser` and `HtmlParser` read every position through them; `range`, `rangeOf` and `loc` (`[line, column, endLine, endColumn]`) are a fresh array per call.
9393
- **Walk children one way everywhere**: `for (let i = 0, c = path.child(0); c !== 0; c = path.child(++i))`. HTML keeps where the last `child` read stopped per parent, so this takes one step per child even with nested loops.
9494
- **Nothing is stored for a read.** A member reads the columns the parser already fills or derives its answer when called: HTML counts `index` along the sibling links and makes `loc`'s converter on its first call in a parse. The walk writes no more than its position, so a read nobody makes costs nothing.
95-
- **Writes act on the current node and only while printing.** `replace(text)` and `remove()` take effect when the node prints after its visitors, which still run its printer for what it records; on a walk that prints nothing they do nothing. In CSS, what holds a replaced node prints the text its children printed as, without the minifier's rewrites, which read fields the replacement no longer matches; the rest of the sheet minifies as before. HTML streams most elements to the output before their `exit` and JavaScript decides parentheses and spacing from the original node, so neither has writes yet.
95+
- **Writes act on the current node and only while printing.** `replace(text)` and `remove()` take effect when the node prints after its visitors, which still run its printer for what it records; on a walk that prints nothing, or on a comment, which prints from the token stream, they do nothing. In CSS, what holds a replaced node prints the text its children printed as, without the minifier's rewrites, which read fields the replacement no longer matches; the rest of the sheet minifies as before. HTML streams most elements to the output before their `exit` and JavaScript decides parentheses and spacing from the original node, so neither has writes yet.
9696
- **Reads go through the path, and webpack's own plugins read through it.** `CssParser` and `HtmlParser` use the members above and nothing else from a node, so what they do a plugin can; only the printers keep raw readers of their own (`_rawName`, `_attributeList`, …).
9797

9898
## Generated tables

‎lib/css/syntax-parser.js‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,7 +4340,7 @@ const _exitNode = (node, parent, index, b, writer) => {
43404340
if (_edits.length === 0) {
43414341
if (writer.printNode(node, A)) {
43424342
_noteEdit(node, writer);
4343-
if (parent === null) _edits.length = 0;
4343+
if (parent === null && _blockContentsNodes === null) _edits.length = 0;
43444344
}
43454345
} else {
43464346
_printNearEdits(node, parent, writer);
@@ -4375,7 +4375,29 @@ const _printNearEdits = (node, parent, writer) => {
43754375
_S._prefixingOn = prefixing;
43764376
}
43774377
}
4378-
if (parent === null) _edits.length = 0;
4378+
if (parent === null && _blockContentsNodes === null) _edits.length = 0;
4379+
};
4380+
4381+
/**
4382+
* Compose a style attribute holding a replaced declaration as its declarations
4383+
* printed, without rewrites, as a rule holding one is.
4384+
* @param {(Rule | Declaration)[]} nodes the top-level nodes, in source order
4385+
* @param {PrintContext} writer the print context
4386+
* @returns {void}
4387+
*/
4388+
const _emitPlainBlockContents = (nodes, writer) => {
4389+
const transforms = _S._transforms;
4390+
const prefixing = _S._prefixingOn;
4391+
_S._transforms = /** @type {Required<CssTransformOptions>} */ (
4392+
_plainTransforms
4393+
);
4394+
_S._prefixingOn = false;
4395+
try {
4396+
_emitBlockContents(nodes, writer);
4397+
} finally {
4398+
_S._transforms = transforms;
4399+
_S._prefixingOn = prefixing;
4400+
}
43794401
};
43804402

43814403
// Where each node a visitor replaced since the top-level node began stands, as
@@ -8422,11 +8444,16 @@ const grammar = (input, visitors, writer, options) => {
84228444
) {
84238445
/** @type {PrintContext} */ (writer).retract(_S._charsetPiece);
84248446
}
8425-
} else {
8447+
} else if (_edits.length === 0) {
84268448
_emitBlockContents(
84278449
_blockContentsNodes,
84288450
/** @type {PrintContext} */ (writer)
84298451
);
8452+
} else {
8453+
_emitPlainBlockContents(
8454+
_blockContentsNodes,
8455+
/** @type {PrintContext} */ (writer)
8456+
);
84308457
}
84318458
} catch (err) {
84328459
if (err !== WALK_STOPPED) throw err;
@@ -20173,19 +20200,22 @@ const A = {
2017320200
},
2017420201
/**
2017520202
* Print the current node as `text` in place of what the printer writes; on a
20176-
* walk that prints nothing it does nothing. What holds the node then prints
20177-
* its children's text as they printed, without the minifier's rewrites.
20203+
* walk that prints nothing, or on a comment, it does nothing. What holds the
20204+
* node then prints its children's text as they printed, without the rewrites.
2017820205
* @param {string} text what the node prints as, a declaration's `;` optional
2017920206
* @returns {void}
2018020207
*/
2018120208
replace(text) {
2018220209
const writer = _stream.writer;
2018320210
if (writer === undefined) return;
2018420211
const node = _S._currentNode;
20212+
const type = _types[_nodeIndex(node)];
20213+
// A comment is read from the token stream, never printed as a node.
20214+
if (type === NodeType.Comment) return;
2018520215
// A declaration's text carries the `;` the block parts them with.
2018620216
writer.replace(
2018720217
node,
20188-
_types[_nodeIndex(node)] === NodeType.Declaration &&
20218+
type === NodeType.Declaration &&
2018920219
text.length !== 0 &&
2019020220
text.charCodeAt(text.length - 1) !== CC_SEMICOLON
2019120221
? `${text};`

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,31 @@ describe("CssSyntax — SourceProcessor", () => {
684684
).toBe("a{margin:0;margin-top:1px}b{margin:1px 0 0}");
685685
});
686686

687+
it("composes a style attribute's declarations as they printed", () => {
688+
const result = new SourceProcessor()
689+
.use({
690+
[NodeType.Declaration]: (path) => {
691+
if (path.name() === "margin-top") path.replace("padding:0");
692+
}
693+
})
694+
.process(
695+
"margin-top:1px;margin-bottom:1px;margin-left:2px;margin-right:2px",
696+
{ mode: "minify", as: "block-contents" }
697+
);
698+
expect(result && result.code).toBe(
699+
"padding:0;margin-bottom:1px;margin-left:2px;margin-right:2px"
700+
);
701+
});
702+
703+
it("leaves a comment and what follows it as written", () => {
704+
// A comment is read from the token stream, not printed as a node.
705+
expect(
706+
print("/*! x */a{color:red}b{top:0}", {
707+
[NodeType.Comment]: (path) => path.replace("/*y*/")
708+
})
709+
).toBe("/*! x */a{color:red}b{top:0}");
710+
});
711+
687712
it("does nothing on a walk that prints nothing", () => {
688713
let seen = 0;
689714
new SourceProcessor()

‎types.d.ts‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6632,8 +6632,8 @@ type CssNodePath<T = number, S = unknown> = Omit<
66326632
stop(): void;
66336633
/**
66346634
* Print the current node as `text` in place of what the printer writes; on a
6635-
* walk that prints nothing it does nothing. What holds the node then prints
6636-
* its children's text as they printed, without the minifier's rewrites.
6635+
* walk that prints nothing, or on a comment, it does nothing. What holds the
6636+
* node then prints its children's text as they printed, without the rewrites.
66376637
*/
66386638
replace(text: string): void;
66396639
/**
@@ -7556,8 +7556,8 @@ declare class CssSourceProcessor<TState = unknown> extends SourceProcessor<
75567556
stop(): void;
75577557
/**
75587558
* Print the current node as `text` in place of what the printer writes; on a
7559-
* walk that prints nothing it does nothing. What holds the node then prints
7560-
* its children's text as they printed, without the minifier's rewrites.
7559+
* walk that prints nothing, or on a comment, it does nothing. What holds the
7560+
* node then prints its children's text as they printed, without the rewrites.
75617561
*/
75627562
replace(text: string): void;
75637563
/**
@@ -38020,8 +38020,8 @@ declare namespace exports {
3802038020
stop(): void;
3802138021
/**
3802238022
* Print the current node as `text` in place of what the printer writes; on a
38023-
* walk that prints nothing it does nothing. What holds the node then prints
38024-
* its children's text as they printed, without the minifier's rewrites.
38023+
* walk that prints nothing, or on a comment, it does nothing. What holds the
38024+
* node then prints its children's text as they printed, without the rewrites.
3802538025
*/
3802638026
replace(text: string): void;
3802738027
/**
@@ -38195,8 +38195,8 @@ declare namespace exports {
3819538195
stop(): void;
3819638196
/**
3819738197
* Print the current node as `text` in place of what the printer writes; on a
38198-
* walk that prints nothing it does nothing. What holds the node then prints
38199-
* its children's text as they printed, without the minifier's rewrites.
38198+
* walk that prints nothing, or on a comment, it does nothing. What holds the
38199+
* node then prints its children's text as they printed, without the rewrites.
3820038200
*/
3820138201
replace(text: string): void;
3820238202
/**
@@ -38272,8 +38272,8 @@ declare namespace exports {
3827238272
stop(): void;
3827338273
/**
3827438274
* Print the current node as `text` in place of what the printer writes; on a
38275-
* walk that prints nothing it does nothing. What holds the node then prints
38276-
* its children's text as they printed, without the minifier's rewrites.
38275+
* walk that prints nothing, or on a comment, it does nothing. What holds the
38276+
* node then prints its children's text as they printed, without the rewrites.
3827738277
*/
3827838278
replace(text: string): void;
3827938279
/**
@@ -38424,8 +38424,8 @@ declare namespace exports {
3842438424
stop(): void;
3842538425
/**
3842638426
* Print the current node as `text` in place of what the printer writes; on a
38427-
* walk that prints nothing it does nothing. What holds the node then prints
38428-
* its children's text as they printed, without the minifier's rewrites.
38427+
* walk that prints nothing, or on a comment, it does nothing. What holds the
38428+
* node then prints its children's text as they printed, without the rewrites.
3842938429
*/
3843038430
replace(text: string): void;
3843138431
/**
@@ -38499,8 +38499,8 @@ declare namespace exports {
3849938499
stop(): void;
3850038500
/**
3850138501
* Print the current node as `text` in place of what the printer writes; on a
38502-
* walk that prints nothing it does nothing. What holds the node then prints
38503-
* its children's text as they printed, without the minifier's rewrites.
38502+
* walk that prints nothing, or on a comment, it does nothing. What holds the
38503+
* node then prints its children's text as they printed, without the rewrites.
3850438504
*/
3850538505
replace(text: string): void;
3850638506
/**

0 commit comments

Comments
 (0)