Skip to content

Commit a9d4b5d

Browse files
committed
Report whether a token ended at its delimiter
1 parent 52576f1 commit a9d4b5d

2 files changed

Lines changed: 131 additions & 2 deletions

File tree

‎src/wp-includes/css-api/class-wp-css-token-processor.php‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,17 @@ class WP_CSS_Token_Processor {
292292
*/
293293
private $token_unit = null;
294294

295+
/**
296+
* Whether the current token ended at its closing delimiter.
297+
*
298+
* Comment, string, url and bad-url tokens end at `*\/`, the matching quote
299+
* or `)`. When the input ends first, the token is complete but this flag
300+
* is false. Every other token type is always terminated.
301+
*
302+
* @var bool
303+
*/
304+
private $token_is_terminated = true;
305+
295306
/**
296307
* Constructor for the CSS processor.
297308
*
@@ -363,8 +374,14 @@ public function next_token(): bool {
363374
$this->token_starts_at = $this->at;
364375
$this->token_value_starts_at = $this->at;
365376

366-
$end = strpos( $this->css, '*/', $this->at + 2 );
367-
$this->at = false !== $end ? $end + 2 : $this->length;
377+
$end = strpos( $this->css, '*/', $this->at + 2 );
378+
if ( false === $end ) {
379+
// The comment runs to the end of the input.
380+
$this->token_is_terminated = false;
381+
$this->at = $this->length;
382+
} else {
383+
$this->at = $end + 2;
384+
}
368385
$this->token_length = $this->at - $this->token_starts_at;
369386
$this->token_value_length = $this->token_length - 4;
370387
return true;
@@ -825,6 +842,34 @@ public function get_token_unit(): ?string {
825842
return $this->token_unit;
826843
}
827844

845+
/**
846+
* Reports whether the current token ended at its closing delimiter.
847+
*
848+
* Comment, string, url and bad-url tokens have a closing delimiter: `*\/`,
849+
* the matching quote, or `)`. When the input ends before the delimiter,
850+
* the tokenizer still returns the token, and this method returns false.
851+
* It returns true for every other token type, which have no closing
852+
* delimiter, and false before the first call to next_token().
853+
*
854+
* Example:
855+
*
856+
* $processor = WP_CSS_Token_Processor::create( '"abc' );
857+
* $processor->next_token();
858+
* $processor->get_token_type(); // 'string-token'
859+
* $processor->is_token_terminated(); // false
860+
*
861+
* @see https://www.w3.org/TR/css-syntax-3/#consume-comment
862+
* @see https://www.w3.org/TR/css-syntax-3/#consume-string-token
863+
* @see https://www.w3.org/TR/css-syntax-3/#consume-url-token
864+
*
865+
* @since 7.2.0
866+
*
867+
* @return bool Whether the current token ended at its closing delimiter.
868+
*/
869+
public function is_token_terminated(): bool {
870+
return null !== $this->token_type && $this->token_is_terminated;
871+
}
872+
828873
/**
829874
* Serializes a plain PHP string as CSS identifier text.
830875
*
@@ -977,6 +1022,7 @@ private function after_token(): void {
9771022
$this->token_length = null;
9781023
$this->token_value = false;
9791024
$this->token_unit = null;
1025+
$this->token_is_terminated = true;
9801026
$this->token_value_starts_at = null;
9811027
$this->token_value_length = null;
9821028
}
@@ -1084,6 +1130,7 @@ private function consume_string(): bool {
10841130
// EOF
10851131
// This is a parse error. Return the <string-token>.
10861132
$this->token_type = self::TOKEN_STRING;
1133+
$this->token_is_terminated = false;
10871134
$this->token_length = $this->at - $this->token_starts_at;
10881135
$this->token_value_starts_at = $value_starts_at;
10891136
$this->token_value_length = $this->at - $value_starts_at;
@@ -1321,6 +1368,7 @@ private function consume_url(): bool {
13211368
if ( $this->at >= $this->length ) {
13221369
// EOF is a parse error, but we return the <url-token> anyway.
13231370
$this->token_type = self::TOKEN_URL;
1371+
$this->token_is_terminated = false;
13241372
$this->token_length = $this->at - $this->token_starts_at;
13251373
$this->token_value_starts_at = $value_starts_at;
13261374
$this->token_value_length = $value_ends_at - $value_starts_at;
@@ -1398,6 +1446,7 @@ private function consume_url(): bool {
13981446
// EOF
13991447
// This is a parse error. Return the <url-token>.
14001448
$this->token_type = self::TOKEN_URL;
1449+
$this->token_is_terminated = false;
14011450
$this->token_length = $this->at - $this->token_starts_at;
14021451
$this->token_value_starts_at = $value_starts_at;
14031452
$this->token_value_length = $this->at - $value_starts_at;
@@ -1415,6 +1464,9 @@ private function consume_url(): bool {
14151464
* @return bool
14161465
*/
14171466
private function consume_remnants_of_bad_url(): bool {
1467+
// Set to true when a `)` ends the token.
1468+
$this->token_is_terminated = false;
1469+
14181470
while ( $this->at < $this->length ) {
14191471
$this->at += strcspn( $this->css, ')\\', $this->at );
14201472

@@ -1431,6 +1483,7 @@ private function consume_remnants_of_bad_url(): bool {
14311483
}
14321484
} elseif ( ')' === $this->css[ $this->at ] ) {
14331485
++$this->at;
1486+
$this->token_is_terminated = true;
14341487
break;
14351488
}
14361489
}

‎tests/phpunit/tests/css-api/wpCssTokenProcessor.php‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,82 @@ public function test_bad_string_token_value_is_null(): void {
205205
$this->assertNull( $processor->get_token_value() );
206206
}
207207

208+
/**
209+
* Tests that is_token_terminated() reports whether the last token ended at its closing delimiter.
210+
*
211+
* @ticket 65738
212+
* @dataProvider data_is_token_terminated
213+
*/
214+
public function test_is_token_terminated( string $css, string $expected_type, bool $expected ): void {
215+
$processor = WP_CSS_Token_Processor::create( $css );
216+
217+
$this->assertFalse( $processor->is_token_terminated(), 'Before the first token there is nothing to report.' );
218+
219+
$type = null;
220+
while ( $processor->next_token() ) {
221+
$type = $processor->get_token_type();
222+
$terminated = $processor->is_token_terminated();
223+
}
224+
225+
$this->assertSame( $expected_type, $type, 'The last token type did not match.' );
226+
$this->assertSame( $expected, $terminated, 'The last token termination flag did not match.' );
227+
}
228+
229+
/**
230+
* Data provider for test_is_token_terminated().
231+
*
232+
* @return array[]
233+
*/
234+
public static function data_is_token_terminated(): array {
235+
return array(
236+
'comment terminated' => array( '/* a */', WP_CSS_Token_Processor::TOKEN_COMMENT, true ),
237+
'comment empty terminated' => array( '/**/', WP_CSS_Token_Processor::TOKEN_COMMENT, true ),
238+
'comment unterminated' => array( '/* a', WP_CSS_Token_Processor::TOKEN_COMMENT, false ),
239+
'comment "/*/" unterminated' => array( '/*/', WP_CSS_Token_Processor::TOKEN_COMMENT, false ),
240+
'comment terminated at end of input' => array( 'a /**/', WP_CSS_Token_Processor::TOKEN_COMMENT, true ),
241+
'string terminated' => array( '"a"', WP_CSS_Token_Processor::TOKEN_STRING, true ),
242+
'string escaped backslash' => array( '"a\\\\"', WP_CSS_Token_Processor::TOKEN_STRING, true ),
243+
'string unterminated' => array( '"a', WP_CSS_Token_Processor::TOKEN_STRING, false ),
244+
'string escape consumed the quote' => array( '"a\\"', WP_CSS_Token_Processor::TOKEN_STRING, false ),
245+
'string lone quote' => array( '"', WP_CSS_Token_Processor::TOKEN_STRING, false ),
246+
'string single quoted terminated' => array( "a 'b'", WP_CSS_Token_Processor::TOKEN_STRING, true ),
247+
'string single quoted unterminated' => array( "a 'b", WP_CSS_Token_Processor::TOKEN_STRING, false ),
248+
'url terminated' => array( 'url(a)', WP_CSS_Token_Processor::TOKEN_URL, true ),
249+
'url escaped backslash' => array( 'url(a\\\\)', WP_CSS_Token_Processor::TOKEN_URL, true ),
250+
'url unterminated' => array( 'url(a', WP_CSS_Token_Processor::TOKEN_URL, false ),
251+
'url escape consumed the paren' => array( 'url(a\\)', WP_CSS_Token_Processor::TOKEN_URL, false ),
252+
'url trailing whitespace' => array( 'url(a ', WP_CSS_Token_Processor::TOKEN_URL, false ),
253+
'url whitespace then paren' => array( 'url(a )', WP_CSS_Token_Processor::TOKEN_URL, true ),
254+
'url empty unterminated' => array( 'url(', WP_CSS_Token_Processor::TOKEN_URL, false ),
255+
'bad url terminated' => array( 'url(a b)', WP_CSS_Token_Processor::TOKEN_BAD_URL, true ),
256+
'bad url unterminated' => array( 'url(a b', WP_CSS_Token_Processor::TOKEN_BAD_URL, false ),
257+
'ident at end of input' => array( 'color: red', WP_CSS_Token_Processor::TOKEN_IDENT, true ),
258+
'ident backslash at end of input' => array( 'abc\\', WP_CSS_Token_Processor::TOKEN_IDENT, true ),
259+
'function at end of input' => array( 'calc(', WP_CSS_Token_Processor::TOKEN_FUNCTION, true ),
260+
);
261+
}
262+
263+
/**
264+
* Tests that is_token_terminated() is reset for the token after an unterminated one.
265+
*
266+
* @ticket 65738
267+
*/
268+
public function test_is_token_terminated_resets_between_tokens(): void {
269+
$processor = WP_CSS_Token_Processor::create( "'a\nb" );
270+
271+
$this->assertTrue( $processor->next_token() );
272+
$this->assertSame( WP_CSS_Token_Processor::TOKEN_BAD_STRING, $processor->get_token_type() );
273+
$this->assertTrue( $processor->is_token_terminated() );
274+
275+
$this->assertTrue( $processor->next_token() );
276+
$this->assertSame( WP_CSS_Token_Processor::TOKEN_WHITESPACE, $processor->get_token_type() );
277+
$this->assertTrue( $processor->is_token_terminated() );
278+
279+
$this->assertTrue( $processor->next_token() );
280+
$this->assertSame( WP_CSS_Token_Processor::TOKEN_IDENT, $processor->get_token_type() );
281+
$this->assertTrue( $processor->is_token_terminated() );
282+
}
283+
208284
/**
209285
* Tests that hash tokens expose the proper type flag.
210286
*

0 commit comments

Comments
 (0)