Skip to content

Commit c8662b8

Browse files
committed
[TASK] Have comsumeWhiteSpace() return the consumed
Instead have the comments array passed by reference to be appended to. This avoids various methods having to do an `array_merge`, and knowing if any whitespace was consumed will be needed for #1471.
1 parent 21bb0eb commit c8662b8

4 files changed

Lines changed: 13 additions & 10 deletions

File tree

src/CSSList/CSSList.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function parseList(ParserState $parserState, CSSList $list): void
6868
$usesLenientParsing = $parserState->getSettings()->usesLenientParsing();
6969
$comments = [];
7070
while (!$parserState->isEnd()) {
71-
$comments = \array_merge($comments, $parserState->consumeWhiteSpace());
71+
$parserState->consumeWhiteSpace($comments);
7272
$listItem = null;
7373
if ($usesLenientParsing) {
7474
try {
@@ -93,7 +93,8 @@ public static function parseList(ParserState $parserState, CSSList $list): void
9393
$listItem->addComments($comments);
9494
$list->append($listItem);
9595
}
96-
$comments = $parserState->consumeWhiteSpace();
96+
$comments = [];
97+
$parserState->consumeWhiteSpace($comments);
9798
}
9899
$list->addComments($comments);
99100
if (!$isRoot && !$usesLenientParsing) {

src/Parsing/ParserState.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function parseCharacter(bool $isForIdentifier): ?string
188188
}
189189

190190
/**
191-
* @return list<Comment>
191+
* @param list<Comment> $comments
192192
*
193193
* @throws UnexpectedEOFException
194194
* @throws UnexpectedTokenException
@@ -197,12 +197,12 @@ public function parseCharacter(bool $isForIdentifier): ?string
197197
* This method may change the state of the object by advancing the internal position;
198198
* it does not simply 'get' a value.
199199
*/
200-
public function consumeWhiteSpace(): array
200+
public function consumeWhiteSpace(array &$comments = []): string
201201
{
202-
$comments = [];
202+
$consumed = '';
203203
do {
204204
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
205-
$this->consume(1);
205+
$consumed .= $this->consume(1);
206206
}
207207
if ($this->parserSettings->usesLenientParsing()) {
208208
try {
@@ -219,7 +219,7 @@ public function consumeWhiteSpace(): array
219219
}
220220
} while ($comment instanceof Comment);
221221

222-
return $comments;
222+
return $consumed;
223223
}
224224

225225
/**

src/Rule/Rule.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ public function __construct(string $rule, ?int $lineNumber = null, ?int $columnN
6565
*/
6666
public static function parse(ParserState $parserState, array $commentsBeforeRule = []): Rule
6767
{
68-
$comments = \array_merge($commentsBeforeRule, $parserState->consumeWhiteSpace());
68+
$comments = $commentsBeforeRule;
69+
$parserState->consumeWhiteSpace($comments);
6970
$rule = new Rule(
7071
$parserState->parseIdentifier(!$parserState->comes('--')),
7172
$parserState->currentLine(),
7273
$parserState->currentColumn()
7374
);
75+
$parserState->consumeWhiteSpace($comments);
7476
$rule->setComments($comments);
75-
$rule->addComments($parserState->consumeWhiteSpace());
7677
$parserState->consume(':');
7778
$value = Value::parseValue($parserState, self::listDelimiterForRule($rule->getRule()));
7879
$rule->setValue($value);

src/RuleSet/RuleSet.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet):
5959
$parserState->consume(';');
6060
}
6161
while (true) {
62-
$commentsBeforeRule = $parserState->consumeWhiteSpace();
62+
$commentsBeforeRule = [];
63+
$parserState->consumeWhiteSpace($commentsBeforeRule);
6364
if ($parserState->comes('}')) {
6465
break;
6566
}

0 commit comments

Comments
 (0)