Skip to content

Commit

Permalink
Fix and improve string token parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Dec 5, 2024
1 parent e60384d commit 15c07b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/wp-includes/html-api/class-wp-css-compound-selector-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ final protected static function parse_ident( string $input, int &$offset ): ?str
* @return string|null
*/
final protected static function parse_string( string $input, int &$offset ): ?string {
if ( $offset + 1 >= strlen( $input ) ) {
if ( $offset >= strlen( $input ) ) {
return null;
}

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

$string_token = '';

$updated_offset = $offset + 1;
$updated_offset = $offset + 1;
$anything_else_mask = "\\\n{$ending_code_point}";
while ( $updated_offset < strlen( $input ) ) {
$anything_else_length = strcspn( $input, $anything_else_mask, $updated_offset );
if ( $anything_else_length > 0 ) {
$string_token .= substr( $input, $updated_offset, $anything_else_length );
$updated_offset += $anything_else_length;

if ( $updated_offset >= strlen( $input ) ) {
break;
}
}

switch ( $input[ $updated_offset ] ) {
case '\\':
++$updated_offset;
Expand All @@ -587,10 +598,6 @@ final protected static function parse_string( string $input, int &$offset ): ?st
case $ending_code_point:
++$updated_offset;
break 2;

default:
$string_token .= $input[ $updated_offset ];
++$updated_offset;
}
}

Expand Down
9 changes: 6 additions & 3 deletions tests/phpunit/tests/html-api/wpCssCompoundSelectorList.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,16 @@ public static function data_strings(): array {

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

'"' => array( '"', '', '' ),
'"\\"' => array( '"\\"', '"', '' ),
'"missing close' => array( '"missing close', 'missing close', '' ),

// Invalid
'Invalid: (empty string)' => array( '' ),
"Invalid: 'newline\\n'" => array( "'newline\n'" ),
'Invalid: foo' => array( 'foo' ),
'Invalid: \\"' => array( '\\"' ),
'Invalid: .foo' => array( '.foo' ),
'Invalid: #foo' => array( '#foo' ),
"Invalid: 'newline\\n'" => array( "'newline\n'" ),
'Invalid: foo' => array( 'foo' ),
);
}

Expand Down

0 comments on commit 15c07b3

Please sign in to comment.