Repository navigation
Keep explicitly set raws.before when inserting nodes into root - #2111
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2017
Root.normalizeunconditionally copiessample.raws.beforeonto every inserted node, so a node created with an explicitraws.before(like the loud comment in the issue, built withpostcss.comment({ text, raws: { before: '' } })) loses it on insertion.Container.normalizealready treatsraws.beforeas 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.beforeset, 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 setsrule.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 ofbeforewas never actually exercised. With the fix the explicitbeforesurvives and renders escaped (\3c /style>), which as far as I can tell is what that test meant to check all along, since it sets bothbeforeandafter.Full suite passes (670/670), plus eslint and check-dts.