Skip to content

Commit

Permalink
Add type selector class
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Nov 25, 2024
1 parent ea1c646 commit 184ad41
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/wp-includes/html-api/class-wp-css-selectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,46 @@ public static function parse( string $input, int &$offset ): ?self {
return new self( $result );
}
}

final class WP_CSS_Type_Selector extends WP_CSS_Selector_Parser {
/**
* @var string
*
* The type identifier string or '*'.
*/
public $ident;

private function __construct( string $ident ) {
$this->ident = $ident;
}

/**
* Parse a type selector
*
* > <type-selector> = <wq-name> | <ns-prefix>? '*'
* > <ns-prefix> = [ <ident-token> | '*' ]? '|'
* > <wq-name> = <ns-prefix>? <ident-token>
*
* Namespaces (e.g. |div, *|div, or namespace|div) are not supported,
* so this selector effectively matches * or ident.
*
* https://www.w3.org/TR/selectors/#grammar
*/
public static function parse( string $input, int &$offset ): ?self {
if ( $offset >= strlen( $input ) ) {
return false;
}

if ( '*' === $input[ $offset ] ) {
++$offset;
return new self( '*' );
}

$result = self::parse_ident( $input, $offset );
if ( null === $result ) {
return null;
}

return new self( $result );
}
}

0 comments on commit 184ad41

Please sign in to comment.