@@ -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 }
0 commit comments