Skip to content

Keep explicitly set raws.before when inserting nodes into root - #2111

Merged
ai merged 1 commit into
postcss:mainfrom
MahinAnowar:fix/root-normalize-explicit-before
Jul 13, 2026
Merged

ai merged 1 commit into
postcss:mainfrom
MahinAnowar:fix/root-normalize-explicit-before

Conversation

@MahinAnowar

Copy link
Copy Markdown
Contributor

Fixes #2017

Root.normalize unconditionally copies sample.raws.before onto every inserted node, so a node created with an explicit raws.before (like the loud comment in the issue, built with postcss.comment({ text, raws: { before: '' } })) loses it on insertion. Container.normalize already treats raws.before as user-owned and only fills it when missing — this brings the root-level path in line with that, which is also what you suggested in the issue thread.

The rule I went with is type-agnostic: if an incoming node is detached (no parent) and has raws.before set, that value is kept. Nodes that are attached somewhere else at insertion time are still renormalized to the target root's spacing, so moving nodes between roots behaves exactly as before — there's a guard test for that, and the existing "keeps spaces on moving root" test still passes.

One existing test needed its expected string updated: the </style> escape test sets rule.raws.before = '\n</style>' on a detached rule, but the old overwrite silently discarded that value before the stringifier ever saw it, so the escape of before was never actually exercised. With the fix the explicit before survives and renders escaped (\3c /style>), which as far as I can tell is what that test meant to check all along, since it sets both before and after.

Full suite passes (670/670), plus eslint and check-dts.

@ai
ai merged commit 00d0dd2 into postcss:main Jul 13, 2026
6 of 10 checks passed
@ai

ai commented Jul 13, 2026

Copy link
Copy Markdown
Member

Thanks. Released in 8.5.19.

hanzo-dev pushed a commit to hanzoai/postcss that referenced this pull request Sep 9, 2026
Anber added a commit to callstack/linaria that referenced this pull request Nov 5, 2026
…tical css (#1533)

* test(server): pin the whitespace before keyframes appended to critical css

collect gives the keyframes it appends to critical the whitespace of the
last critical rule. postcss 8.5 keeps the whitespace the keyframes had in
the source stylesheet instead, so a blank line before them ends up in the
critical css. The test passes with postcss 8.4.31 and fails with 8.5.

* fix(server): set the whitespace before keyframes appended to critical css

postcss 8.5.19 made `Root#normalize` keep the `raws.before` of a detached node on `append`
(postcss/postcss#2111), so the keyframes that collect appends after the critical rules kept
the whitespace they had in the stylesheet instead of taking that of the last critical node,
and a blank line before them in the source reached the critical css.

collect now sets that whitespace itself when critical holds two or more nodes, as postcss 8.4
does on `append`, so the output is the same with postcss 8.4 and 8.5. After a single node both
versions keep the whitespace of the stylesheet, as before.

* Revert "test(server): pin the whitespace before keyframes appended to critical css" and its fix

This reverts commits 54f5bcd and
2e264a0, so that the test is added again
together with the guard for a single critical rule.

* test(server): pin the whitespace before keyframes appended to critical css

With postcss 8.4, keyframes that collect appends to critical after two or
more critical rules take the whitespace of the last rule. postcss 8.5 keeps
the whitespace the keyframes had in the source stylesheet instead, so a
blank line before them ends up in the critical css. The first test passes
with postcss 8.4.31 and fails with 8.5.

When critical holds a single rule, the appended keyframes keep their
stylesheet whitespace with postcss 8.4 and 8.5 alike. The second test
guards that case.

* fix(server): set the whitespace before keyframes appended to critical css

postcss 8.5.19 made `Root#normalize` keep the `raws.before` of a detached node on `append`
(postcss/postcss#2111), so the keyframes that collect appends after the critical rules kept
the whitespace they had in the stylesheet instead of taking that of the last critical node,
and a blank line before them in the source reached the critical css.

collect now sets that whitespace itself when critical holds two or more nodes, as postcss 8.4
does on `append`, so the output is the same with postcss 8.4 and 8.5. After a single node both
versions keep the whitespace of the stylesheet, as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.before() modifies raws.before after insertion.

2 participants