Skip to content

Conversation

@8ctopus
Copy link

@8ctopus 8ctopus commented Nov 28, 2025

This PR cleans the css selector from extra whitespace:

$input = <<<CSS
p    >
    small {
font-size:.7em
}

CSS;

$document = (new \Sabberworm\CSS\Parser($input))
    ->parse();

echo $document->render(\Sabberworm\CSS\OutputFormat::createPretty());

without the PR

p    >
    small {
        font-size: .7em;
}

with the PR:

p > small {
        font-size: .7em;
}

@coveralls
Copy link

Coverage Status

coverage: 59.658%. remained the same
when pulling 1806b17 on 8ctopus:cleanup-selector
into c29c15a on MyIntervals:main.

Copy link
Collaborator

@JakeQZ JakeQZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. It's a nice idea to tidy up the whitespace in the selectors.

However, it's not as simple as it seems. We would also need to avoid touching the whitespace within attribute selectors. Although an unlikely real-world example, this change would break a selector like a[title="extra space"].

So I think this would require a more complex regex that handles quoted strings (single and double quotes) to avoid touching them. I'm not sure how a[title=extra space] is supposed to be interpreted; that would need checking up on.

If you're willing to give this a go, we would welcome the tidying functionality, with some tests added to SelectorTest to make sure it reformats only the whitespace it should. I also understand if this now seems like too big a job - instead we could convert this PR to a 'would be nice to have' feature request.

@JakeQZ
Copy link
Collaborator

JakeQZ commented Nov 29, 2025

If you're willing to give this a go

DeclarationBlock::parse() may be the place to look for removing whitespace, though I'm not sure how.

I also note that setSelectors would fail for a[title="this, and that"], though the parser would process it fine.

@8ctopus, whether or not you proceed with this PR, I thank you for helping us identify some gremlins :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants