GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: preserve whitespace before a `*` namespace in attribute selectors by spokodev · Pull Request #325 · postcss/postcss-selector-parser

Conversation

@spokodev

@spokodev spokodev commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Problem

Leading whitespace before a * (universal) namespace in an attribute selector is dropped on a round-trip, while it is preserved for a named namespace or no namespace:

const parser = require("postcss-selector-parser");
const rt = (s) => { let o; parser((r) => (o = r.toString())).processSync(s); return o; };

rt("[ *|a ]");  // "[*|a ]"   ← leading space lost
rt("[ ns|a ]"); // "[ ns|a ]" (ok)
rt("[ |a ]");   // "[ |a ]"   (ok)
rt("[ a ]");    // "[ a ]"    (ok)

Root cause

In attribute(), the tokens.asterisk branch records the raw spacing with the wrong variable:

if (spaceBefore) {
  node.spaces.attribute.before = spaceBefore;
  spaceBefore = "";          // cleared here
}
if (commentBefore) {
  node.raws.spaces.attribute.before = spaceBefore;  // ← always "" now
  commentBefore = "";
}

spaceBefore has already been set to "", so raws.spaces.attribute.before is emptied. Since toString prefers the raw spacing when present, the leading whitespace disappears. The tokens.word branch right below does the same thing correctly with commentBefore.

Fix

Use commentBefore for the raw spacing, matching the word-namespace branch (one word).

Test plan

  • Added an attribute test for a universal namespace with surrounding spaces ([ *|bar ]); the tree was already correct, but the toString round-trip failed on main and passes with the fix.
  • A round-trip property check over generated selectors flagged this case; the fix clears it with no regressions. Full suite green (779), eslint/tsc clean.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@spokodev
fix: preserve whitespace before a * namespace in attribute selectors
98324a2
The `*` branch of the attribute parser stored the raw leading whitespace
from `spaceBefore`, but that variable had just been cleared a few lines
above, so `raws.spaces.attribute.before` became empty and `[ *|a ]`
round-tripped to `[*|a ]`. The word-namespace branch already uses
`commentBefore` here; do the same for `*`.
@MoOx

MoOx commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Nice, thanks again !

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@MoOx
MoOx merged commit 4d8437f into postcss:main Aug 7, 2026
@claude claude Bot mentioned this pull request Sep 1, 2026
build(deps-dev): bump css-loader from 7.1.4 to 7.1.5 dimagi/open-chat-studio#4341
Merged
@mtrezza mtrezza mentioned this pull request Sep 24, 2026
refactor: Bump postcss-selector-parser from 7.1.1 to 7.1.6 parse-community/parse-dashboard#3464
Merged
@fossabot fossabot Bot mentioned this pull request Oct 19, 2026
build(deps-dev): bump postcss-selector-parser from 7.1.4 to 7.1.6 in /bitwarden_license/src/Sso UchihaCortez/server#6
Open
@claude claude Bot mentioned this pull request Oct 21, 2026
Bump postcss-selector-parser from 7.0.0 to 7.1.6 dimagi/commcare-hq#38277
Merged
@fossabot fossabot Bot mentioned this pull request Nov 1, 2026
build(deps-dev): bump postcss-selector-parser from 7.1.4 to 7.1.7 in /src/Admin UchihaCortez/server#9
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@spokodev @MoOx
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.