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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Please also have a look at our
### Changed

- `Selector::setSelector()` and `Selector` constructor will now throw exception
upon provision of an invalid selectior (#1498)
upon provision of an invalid selectior (#1498, #1502)
- Clean up extra whitespace in CSS selector (#1398)
- The array keys passed to `DeclarationBlock::setSelectors()` are no longer
preserved (#1407)
Expand Down
13 changes: 11 additions & 2 deletions src/Property/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Selector implements Renderable
*/
public const SELECTOR_VALIDATION_RX = '/
^(
# not whitespace only
(?!\\s*+$)
(?:
# any sequence of valid unescaped characters, except quotes
[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_^$|*=~\\[\\]()\\-\\s\\.:#+>,]++
Expand All @@ -57,7 +59,7 @@ class Selector implements Renderable
/ux';

/**
* @var string
* @var non-empty-string
*/
private $selector;

Expand All @@ -73,6 +75,8 @@ public static function isValid(string $selector): bool
}

/**
* @param non-empty-string $selector
*
* @throws \UnexpectedValueException if the selector is not valid
*/
final public function __construct(string $selector)
Expand All @@ -83,7 +87,7 @@ final public function __construct(string $selector)
/**
* @param list<Comment> $comments
*
* @return list<Component>
* @return non-empty-list<Component>
*
* @throws UnexpectedTokenException
*/
Expand Down Expand Up @@ -153,12 +157,17 @@ public static function parse(ParserState $parserState, array &$comments = []): s
return new static($selectorString);
}

/**
* @return non-empty-string
*/
public function getSelector(): string
{
return $this->selector;
}

/**
* @param non-empty-string $selector
*
* @throws \UnexpectedValueException if the selector is not valid
*/
public function setSelector(string $selector): void
Expand Down
24 changes: 6 additions & 18 deletions tests/Unit/Property/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ public function parseHandlesEvenNumberOfBackslashesBeforeQuote(): void
public static function provideInvalidSelectors(): array
{
return [
// This is currently broken.
// 'empty string' => [''],
'empty string' => [''],
'space' => [' '],
'tab' => ["\t"],
'line feed' => ["\n"],
'carriage return' => ["\r"],
'percent sign' => ['%'],
// This is currently broken.
// 'hash only' => ['#'],
Expand All @@ -176,16 +179,11 @@ public static function provideInvalidSelectors(): array
}

/**
* @return array<non-empty-string, array{0: string}>
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideInvalidSelectorsForParse(): array
{
return [
'empty string' => [''],
'space' => [' '],
'tab' => ["\t"],
'line feed' => ["\n"],
'carriage return' => ["\r"],
'a `:not` missing the closing brace' => [':not(a'],
'a `:not` missing the opening brace' => [':not a)'],
'attribute value missing closing single quote' => ['a[href=\'#top]'],
Expand Down Expand Up @@ -315,16 +313,6 @@ public function parseExtractsTwoCommentsFromSelector(): void
self::assertSame('comment2', $result[1]->getComment());
}

/**
* @test
*/
public function canConstructObjectWithEmptyState(): void
{
$subject = new Selector('');

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

/**
* @test
*
Expand Down