Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Please also have a look at our

### Changed

- Cleanup extra whitespace in css selector (#1398)
- The array keys passed to `DeclarationBlock::setSelectors()` are no longer
preserved (#1407)

Expand Down
8 changes: 7 additions & 1 deletion src/Property/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sabberworm\CSS\Renderable;

use function Safe\preg_match;
use function Safe\preg_replace;

/**
* Class representing a single CSS selector. Selectors have to be split by the comma prior to being passed into this
Expand Down Expand Up @@ -74,7 +75,12 @@ public function getSelector(): string

public function setSelector(string $selector): void
{
$this->selector = \trim($selector);
$selector = \trim($selector);

$hasAttribute = \strpos($selector, '[') !== false;

// Whitespace can't be adjusted within an attribute selector, as it would change its meaning
$this->selector = !$hasAttribute ? preg_replace('/\\s++/', ' ', $selector) : $selector;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ public function selectorIgnoresInFile(): void
$document = self::parsedStructureForFile('selector-ignores', Settings::create()->withMultibyteSupport(true));
$expected = '.some[selectors-may=\'contain-a-{\'] {}'
. "\n"
. '.this-selector .valid {width: 100px;}'
. '.this-selector .valid {width: 100px;}'
. "\n"
. '@media only screen and (min-width: 200px) {.test {prop: val;}}';
self::assertSame($expected, $document->render());
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Property/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,51 @@ public function isValidForInvalidSelectorReturnsFalse(string $selector): void
{
self::assertFalse(Selector::isValid($selector));
}

/**
* @test
*/
public function cleansUpSpacesWithinSelector(): void
{
$selector = 'p > small';

$subject = new Selector($selector);

self::assertSame('p > small', $subject->getSelector());
}

/**
* @test
*/
public function cleansUpTabsWithinSelector(): void
{
$selector = "p\t>\tsmall";

$subject = new Selector($selector);

self::assertSame('p > small', $subject->getSelector());
}

/**
* @test
*/
public function cleansUpNewLineWithinSelector(): void
{
$selector = "p\n>\nsmall";

$subject = new Selector($selector);

self::assertSame('p > small', $subject->getSelector());
}


/**
* @test
*/
public function doesNotCleanupSpacesWithinAttributeSelector(): void
{
$subject = new Selector('a[title="extra space"]');

self::assertSame('a[title="extra space"]', $subject->getSelector());
}
}