Skip to content

Commit 15c07b3

Browse files
committed
Fix and improve string token parsing
1 parent e60384d commit 15c07b3

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/wp-includes/html-api/class-wp-css-compound-selector-list.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ final protected static function parse_ident( string $input, int &$offset ): ?str
548548
* @return string|null
549549
*/
550550
final protected static function parse_string( string $input, int &$offset ): ?string {
551-
if ( $offset + 1 >= strlen( $input ) ) {
551+
if ( $offset >= strlen( $input ) ) {
552552
return null;
553553
}
554554

@@ -559,8 +559,19 @@ final protected static function parse_string( string $input, int &$offset ): ?st
559559

560560
$string_token = '';
561561

562-
$updated_offset = $offset + 1;
562+
$updated_offset = $offset + 1;
563+
$anything_else_mask = "\\\n{$ending_code_point}";
563564
while ( $updated_offset < strlen( $input ) ) {
565+
$anything_else_length = strcspn( $input, $anything_else_mask, $updated_offset );
566+
if ( $anything_else_length > 0 ) {
567+
$string_token .= substr( $input, $updated_offset, $anything_else_length );
568+
$updated_offset += $anything_else_length;
569+
570+
if ( $updated_offset >= strlen( $input ) ) {
571+
break;
572+
}
573+
}
574+
564575
switch ( $input[ $updated_offset ] ) {
565576
case '\\':
566577
++$updated_offset;
@@ -587,10 +598,6 @@ final protected static function parse_string( string $input, int &$offset ): ?st
587598
case $ending_code_point:
588599
++$updated_offset;
589600
break 2;
590-
591-
default:
592-
$string_token .= $input[ $updated_offset ];
593-
++$updated_offset;
594601
}
595602
}
596603

tests/phpunit/tests/html-api/wpCssCompoundSelectorList.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,16 @@ public static function data_strings(): array {
181181

182182
"'foo\\" => array( "'foo\\", 'foo', '' ),
183183

184+
'"' => array( '"', '', '' ),
185+
'"\\"' => array( '"\\"', '"', '' ),
186+
'"missing close' => array( '"missing close', 'missing close', '' ),
187+
184188
// Invalid
185189
'Invalid: (empty string)' => array( '' ),
186-
"Invalid: 'newline\\n'" => array( "'newline\n'" ),
187-
'Invalid: foo' => array( 'foo' ),
188-
'Invalid: \\"' => array( '\\"' ),
189190
'Invalid: .foo' => array( '.foo' ),
190191
'Invalid: #foo' => array( '#foo' ),
192+
"Invalid: 'newline\\n'" => array( "'newline\n'" ),
193+
'Invalid: foo' => array( 'foo' ),
191194
);
192195
}
193196

0 commit comments

Comments
 (0)