Skip to content

Commit bf1bff3

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`. Also, knowing if any whitespace was consumed will be needed for #1471.
1 parent 21bb0eb commit bf1bff3

5 files changed

Lines changed: 114 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
}

tests/Unit/Parsing/ParserStateTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,105 @@ public function consumeIfComesUpdatesLineNumber(): void
157157

158158
self::assertSame(2, $subject->currentLine());
159159
}
160+
161+
/**
162+
* @return array<non-empty-string, array{0: string, 1: string}>
163+
*/
164+
public static function provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption(): array
165+
{
166+
return [
167+
'nothing' => ['', ''],
168+
'space' => [' ', ' '],
169+
'tab' => ["\t", "\t"],
170+
'line feed' => ["\n", "\n"],
171+
'carriage return' => ["\r", "\r"],
172+
'two spaces' => [' ', ' '],
173+
'comment' => ['/*hello*/', ''],
174+
'comment with space to the left' => [' /*hello*/', ' '],
175+
'comment with space to the right' => ['/*hello*/ ', ' '],
176+
'two comments' => ['/*hello*//*bye*/', ''],
177+
'two comments with space between' => ['/*hello*/ /*bye*/', ' '],
178+
'two comments with line feed between' => ["/*hello*/\n/*bye*/", "\n"],
179+
];
180+
}
181+
182+
/**
183+
* @test
184+
*
185+
* @dataProvider provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption
186+
*/
187+
public function consumeWhiteSpaceReturnsTheConsumed(
188+
string $whitespaceMaybeWithComments,
189+
string $expectedConsumption
190+
): void {
191+
$subject = new ParserState($whitespaceMaybeWithComments, Settings::create());
192+
193+
$result = $subject->consumeWhiteSpace();
194+
195+
self::assertSame($expectedConsumption, $result);
196+
}
197+
198+
/**
199+
* @test
200+
*/
201+
public function consumeWhiteSpaceExtractsComment(): void
202+
{
203+
$commentText = 'Did they get you to trade your heroes for ghosts?';
204+
$subject = new ParserState('/*' . $commentText . '*/', Settings::create());
205+
206+
$result = [];
207+
$subject->consumeWhiteSpace($result);
208+
209+
self::assertInstanceOf(Comment::class, $result[0]);
210+
self::assertSame($commentText, $result[0]->getComment());
211+
}
212+
213+
/**
214+
* @test
215+
*/
216+
public function consumeWhiteSpaceExtractsTwoComments(): void
217+
{
218+
$commentText1 = 'Hot ashes for trees? Hot air for a cool breeze?';
219+
$commentText2 = 'Cold comfort for change? Did you exchange';
220+
$subject = new ParserState('/*' . $commentText1 . '*//*' . $commentText2 . '*/', Settings::create());
221+
222+
$result = [];
223+
$subject->consumeWhiteSpace($result);
224+
225+
self::assertInstanceOf(Comment::class, $result[0]);
226+
self::assertSame($commentText1, $result[0]->getComment());
227+
self::assertInstanceOf(Comment::class, $result[1]);
228+
self::assertSame($commentText2, $result[1]->getComment());
229+
}
230+
231+
/**
232+
* @return array<non-empty-string, array{0: non-empty-string}>
233+
*/
234+
public static function provideWhitespace(): array
235+
{
236+
return [
237+
'space' => [' '],
238+
'tab' => ["\t"],
239+
'line feed' => ["\n"],
240+
'carriage return' => ["\r"],
241+
'two spaces' => [' '],
242+
];
243+
}
244+
245+
/**
246+
* @test
247+
*
248+
* @dataProvider provideWhitespace
249+
*/
250+
public function consumeWhiteSpaceExtractsCommentWithSurroundingWhitespace(string $whitespace): void
251+
{
252+
$commentText = 'A walk-on part in the war for a lead role in a cage?';
253+
$subject = new ParserState($whitespace . '/*' . $commentText . '*/' . $whitespace, Settings::create());
254+
255+
$result = [];
256+
$subject->consumeWhiteSpace($result);
257+
258+
self::assertInstanceOf(Comment::class, $result[0]);
259+
self::assertSame($commentText, $result[0]->getComment());
260+
}
160261
}

0 commit comments

Comments
 (0)