Skip to content

Commit ac89bae

Browse files
committed
[TASK] Ensure Selector content is not whitespace-only
1 parent e4c7f2c commit ac89bae

3 files changed

Lines changed: 18 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Please also have a look at our
1818
### Changed
1919

2020
- `Selector::setSelector()` and `Selector` constructor will now throw exception
21-
upon provision of an invalid selectior (#1498)
21+
upon provision of an invalid selectior (#1498, #1502)
2222
- Clean up extra whitespace in CSS selector (#1398)
2323
- The array keys passed to `DeclarationBlock::setSelectors()` are no longer
2424
preserved (#1407)

src/Property/Selector.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Selector implements Renderable
3131
*/
3232
public const SELECTOR_VALIDATION_RX = '/
3333
^(
34+
# not whitespace only
35+
(?!\\s*+$)
3436
(?:
3537
# any sequence of valid unescaped characters, except quotes
3638
[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_^$|*=~\\[\\]()\\-\\s\\.:#+>,]++
@@ -57,7 +59,7 @@ class Selector implements Renderable
5759
/ux';
5860

5961
/**
60-
* @var string
62+
* @var non-empty-string
6163
*/
6264
private $selector;
6365

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

7577
/**
78+
* @param non-empty-string $selector
79+
*
7680
* @throws \UnexpectedValueException if the selector is not valid
7781
*/
7882
final public function __construct(string $selector)
@@ -83,7 +87,7 @@ final public function __construct(string $selector)
8387
/**
8488
* @param list<Comment> $comments
8589
*
86-
* @return list<Component>
90+
* @return non-empty-list<Component>
8791
*
8892
* @throws UnexpectedTokenException
8993
*/
@@ -153,12 +157,17 @@ public static function parse(ParserState $parserState, array &$comments = []): s
153157
return new static($selectorString);
154158
}
155159

160+
/**
161+
* @return non-empty-string
162+
*/
156163
public function getSelector(): string
157164
{
158165
return $this->selector;
159166
}
160167

161168
/**
169+
* @param non-empty-string $selector
170+
*
162171
* @throws \UnexpectedValueException if the selector is not valid
163172
*/
164173
public function setSelector(string $selector): void

tests/Unit/Property/SelectorTest.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ public function parseHandlesEvenNumberOfBackslashesBeforeQuote(): void
161161
public static function provideInvalidSelectors(): array
162162
{
163163
return [
164-
// This is currently broken.
165-
// 'empty string' => [''],
164+
'empty string' => [''],
165+
'space' => [' '],
166+
'tab' => ["\t"],
167+
'line feed' => ["\n"],
168+
'carriage return' => ["\r"],
166169
'percent sign' => ['%'],
167170
// This is currently broken.
168171
// 'hash only' => ['#'],
@@ -176,16 +179,11 @@ public static function provideInvalidSelectors(): array
176179
}
177180

178181
/**
179-
* @return array<non-empty-string, array{0: string}>
182+
* @return array<non-empty-string, array{0: non-empty-string}>
180183
*/
181184
public static function provideInvalidSelectorsForParse(): array
182185
{
183186
return [
184-
'empty string' => [''],
185-
'space' => [' '],
186-
'tab' => ["\t"],
187-
'line feed' => ["\n"],
188-
'carriage return' => ["\r"],
189187
'a `:not` missing the closing brace' => [':not(a'],
190188
'a `:not` missing the opening brace' => [':not a)'],
191189
'attribute value missing closing single quote' => ['a[href=\'#top]'],
@@ -315,16 +313,6 @@ public function parseExtractsTwoCommentsFromSelector(): void
315313
self::assertSame('comment2', $result[1]->getComment());
316314
}
317315

318-
/**
319-
* @test
320-
*/
321-
public function canConstructObjectWithEmptyState(): void
322-
{
323-
$subject = new Selector('');
324-
325-
self::assertSame('', $subject->getSelector());
326-
}
327-
328316
/**
329317
* @test
330318
*

0 commit comments

Comments
 (0)