Skip to content

Commit f027b43

Browse files
committed
[BUGFIX] Allow comma in quoted string in selector
Split by commas during parsing, not after.
1 parent 595a47b commit f027b43

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Please also have a look at our
110110

111111
### Fixed
112112

113+
- Allow comma in attribute selector value (when quoted) (#1323)
113114
- Allow comma in selectors (e.g. `:not(html, body)`) (#1293)
114115
- Insert `Rule` before sibling even with different property name
115116
(in `RuleSet::addRule()`) (#1270)

src/RuleSet/DeclarationBlock.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
4040
$comments = [];
4141
$result = new DeclarationBlock($parserState->currentLine());
4242
try {
43+
$selectors = [];
4344
$selectorParts = [];
4445
$stringWrapperCharacter = null;
45-
static $stopCharacters = ['{', '}', '\'', '"'];
46+
$consumedNextCharacter = false;
47+
static $stopCharacters = ['{', '}', '\'', '"', ','];
4648
do {
47-
$selectorParts[] = $parserState->consume(1)
48-
. $parserState->consumeUntil($stopCharacters, false, false, $comments);
49+
if (!$consumedNextCharacter) {
50+
$selectorParts[] = $parserState->consume(1);
51+
}
52+
$selectorParts[] = $parserState->consumeUntil($stopCharacters, false, false, $comments);
4953
$nextCharacter = $parserState->peek();
54+
$consumedNextCharacter = false;
5055
switch ($nextCharacter) {
5156
case '\'':
5257
// The fallthrough is intentional.
@@ -59,9 +64,18 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
5964
}
6065
}
6166
break;
67+
case ',':
68+
if (!\is_string($stringWrapperCharacter)) {
69+
$selectors[] = \implode('', $selectorParts);
70+
$selectorParts = [];
71+
$parserState->consume(1);
72+
$consumedNextCharacter = true;
73+
}
74+
break;
6275
}
6376
} while (!\in_array($nextCharacter, ['{', '}'], true) || \is_string($stringWrapperCharacter));
64-
$result->setSelectors(\implode('', $selectorParts), $list);
77+
$selectors[] = \implode('', $selectorParts); // add final or only selector
78+
$result->setSelectors($selectors, $list);
6579
if ($parserState->comes('{')) {
6680
$parserState->consume(1);
6781
}

tests/Unit/RuleSet/DeclarationBlockTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public static function provideSelector(): array
6868
'type & pseudo-class' => ['a:hover'],
6969
'`not`' => [':not(#your-mug)'],
7070
'pseudo-element' => ['::before'],
71-
'attribute with `"`' => ['[alt="{}()[]\\"\'"]'],
72-
'attribute with `\'`' => ['[alt=\'{}()[]"\\\'\']'],
71+
'attribute with `"`' => ['[alt="{}()[]\\"\'",]'],
72+
'attribute with `\'`' => ['[alt=\'{}()[]"\\\'\',]'],
7373
];
7474
}
7575

0 commit comments

Comments
 (0)