PHP
6 days ago
class-convertedurl.php
6 days ago
class-cssurlprocessor.php
6 days ago
class-nativeurlintextprocessorwrapper.php
6 days ago
class-urlintextprocessor.php
6 days ago
class-urlrewritecache.php
6 days ago
class-wpurl.php
6 days ago
class-wpwhatwgurl.php
6 days ago
class-wpwhatwgurlsearchparams.php
6 days ago
functions.php
6 days ago
public-suffix-list.php
6 days ago
class-cssurlprocessor.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\DataLiberation\URL; |
| 4 | |
| 5 | use WordPress\DataLiberation\CSS\CSSProcessor; |
| 6 | |
| 7 | /** |
| 8 | * Provides URL specific helpers on top of the CSSProcessor tokenizer. |
| 9 | */ |
| 10 | class CSSURLProcessor { |
| 11 | /** |
| 12 | * @var CSSProcessor |
| 13 | */ |
| 14 | private $processor; |
| 15 | |
| 16 | /** |
| 17 | * @param string $css CSS source without wrapping braces. |
| 18 | */ |
| 19 | public function __construct( string $css ) { |
| 20 | $this->processor = CSSProcessor::create( $css ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Moves the cursor to the next URL token, if available. |
| 25 | * |
| 26 | * @return bool |
| 27 | */ |
| 28 | public function next_url(): bool { |
| 29 | while ( $this->processor->next_token() ) { |
| 30 | $type = $this->processor->get_token_type(); |
| 31 | |
| 32 | // Direct URL token. |
| 33 | if ( CSSProcessor::TOKEN_URL === $type ) { |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | // url() function with STRING token. |
| 38 | if ( CSSProcessor::TOKEN_FUNCTION === $type && |
| 39 | 0 === strcasecmp( $this->processor->get_token_value(), 'url' ) ) { |
| 40 | // Look ahead for STRING token, skipping whitespace. |
| 41 | while ( $this->processor->next_token() ) { |
| 42 | $inner_type = $this->processor->get_token_type(); |
| 43 | if ( CSSProcessor::TOKEN_WHITESPACE === $inner_type ) { |
| 44 | continue; // Skip whitespace. |
| 45 | } |
| 46 | if ( CSSProcessor::TOKEN_STRING === $inner_type ) { |
| 47 | return true; // Found the URL string. |
| 48 | } |
| 49 | // Hit something else (like RIGHT_PAREN or another token). |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the raw (decoded) URL for the current match. |
| 59 | * |
| 60 | * @return string|false |
| 61 | */ |
| 62 | public function get_raw_url() { |
| 63 | $value = $this->processor->get_token_value(); |
| 64 | return false !== $value ? $value : false; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Replaces the currently matched URL with a new value. |
| 69 | * |
| 70 | * @param string $new_url Replacement URL without quoting. |
| 71 | * @return bool |
| 72 | */ |
| 73 | public function set_raw_url( string $new_url ): bool { |
| 74 | return $this->processor->set_token_value( $new_url ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the updated CSS with all replacements applied. |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | public function get_updated_css(): string { |
| 83 | return $this->processor->get_updated_css(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Determines whether the current URL is a data URI. |
| 88 | * |
| 89 | * @return bool |
| 90 | */ |
| 91 | public function is_data_uri(): bool { |
| 92 | return $this->processor->is_data_uri(); |
| 93 | } |
| 94 | } |
| 95 |