| 1 |
<?php |
| 2 |
/** |
| 3 |
* @file |
| 4 |
* @license https://opensource.org/licenses/Apache-2.0 Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Wikimedia\CSS\Grammar; |
| 8 |
|
| 9 |
use Closure; |
| 10 |
use Wikimedia\CSS\Objects\ComponentValueList; |
| 11 |
use Wikimedia\CSS\Objects\Token; |
| 12 |
use Wikimedia\CSS\Parser\Parser; |
| 13 |
use Wikimedia\CSS\Sanitizer\PropertySanitizer; |
| 14 |
|
| 15 |
/** |
| 16 |
* Factory for predefined Grammar matchers |
| 17 |
* @note For security, the attr() and var() functions are not supported, |
| 18 |
* although as a limited exception var() is allowed for color attributes |
| 19 |
* in `::colorFuncs()`. |
| 20 |
*/ |
| 21 |
class MatcherFactory { |
| 22 |
/** @var MatcherFactory|null */ |
| 23 |
private static $instance = null; |
| 24 |
|
| 25 |
/** @var (Matcher|Matcher[])[] Cache of constructed matchers */ |
| 26 |
protected $cache = []; |
| 27 |
|
| 28 |
/** @var string[] length units */ |
| 29 |
protected static $lengthUnits = [ |
| 30 |
// Font-relative units |
| 31 |
'em', 'rem', 'ex', 'rex', |
| 32 |
'cap', 'rcap', 'ch', 'rch', |
| 33 |
'ic', 'ric', 'lh', 'rlh', |
| 34 |
// Viewport-relative units |
| 35 |
'vw', 'svw', 'lvw', 'dvw', |
| 36 |
'vh', 'svh', 'lvh', 'dvh', |
| 37 |
'vi', 'svi', 'lvi', 'dvi', |
| 38 |
'vb', 'svb', 'lvb', 'dvb', |
| 39 |
'vmin', 'svmin', 'lvmin', 'dvmin', |
| 40 |
'vmax', 'svmax', 'lvmax', 'dvmax', |
| 41 |
// Absolute units |
| 42 |
'cm', 'mm', 'Q', 'in', 'pc', 'pt', 'px', |
| 43 |
]; |
| 44 |
|
| 45 |
/** @var string[] angle units */ |
| 46 |
protected static $angleUnits = [ 'deg', 'grad', 'rad', 'turn' ]; |
| 47 |
|
| 48 |
/** @var string[] time units */ |
| 49 |
protected static $timeUnits = [ 's', 'ms' ]; |
| 50 |
|
| 51 |
/** @var string[] frequency units */ |
| 52 |
protected static $frequencyUnits = [ 'Hz', 'kHz' ]; |
| 53 |
|
| 54 |
/** @var string[] resolution units */ |
| 55 |
protected static $resolutionUnits = [ 'dpi', 'dpcm', 'dppx', 'x' ]; |
| 56 |
|
| 57 |
/** |
| 58 |
* Return a static instance of the factory |
| 59 |
* @return MatcherFactory |
| 60 |
*/ |
| 61 |
public static function singleton() { |
| 62 |
if ( !self::$instance ) { |
| 63 |
self::$instance = new self(); |
| 64 |
} |
| 65 |
return self::$instance; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Matcher for optional whitespace |
| 70 |
* @return Matcher |
| 71 |
*/ |
| 72 |
public function optionalWhitespace() { |
| 73 |
return $this->cache[__METHOD__] |
| 74 |
??= new WhitespaceMatcher( [ 'significant' => false ] ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Matcher for required whitespace |
| 79 |
* @return Matcher |
| 80 |
*/ |
| 81 |
public function significantWhitespace() { |
| 82 |
return $this->cache[__METHOD__] |
| 83 |
??= new WhitespaceMatcher( [ 'significant' => true ] ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Matcher for a comma |
| 88 |
* @return Matcher |
| 89 |
*/ |
| 90 |
public function comma() { |
| 91 |
return $this->cache[__METHOD__] |
| 92 |
??= new TokenMatcher( Token::T_COMMA ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Matcher for an arbitrary identifier |
| 97 |
* @return Matcher |
| 98 |
*/ |
| 99 |
public function ident() { |
| 100 |
return $this->cache[__METHOD__] |
| 101 |
??= new TokenMatcher( Token::T_IDENT ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Matcher for a <custom-ident> |
| 106 |
* |
| 107 |
* Note this doesn't implement the semantic restriction about assigning |
| 108 |
* meaning to various idents in a complex value, as CSS Sanitizer doesn't |
| 109 |
* deal with semantics on that level. |
| 110 |
* |
| 111 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#custom-idents |
| 112 |
* @param string[] $exclude Additional values to exclude, all-lowercase. |
| 113 |
* @return Matcher |
| 114 |
*/ |
| 115 |
public function customIdent( array $exclude = [] ) { |
| 116 |
$exclude = array_merge( [ |
| 117 |
// https://www.w3.org/TR/2024/WD-css-values-4-20240312/#common-keywords |
| 118 |
'initial', 'inherit', 'unset', 'default', |
| 119 |
// https://www.w3.org/TR/2022/CR-css-cascade-4-20220113/#all-shorthand |
| 120 |
'revert' |
| 121 |
], $exclude ); |
| 122 |
return new TokenMatcher( Token::T_IDENT, static function ( Token $t ) use ( $exclude ) { |
| 123 |
return !in_array( strtolower( $t->value() ), $exclude, true ); |
| 124 |
} ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Matcher for a string |
| 129 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#strings |
| 130 |
* @warning If the string will be used as a URL, use self::urlstring() instead. |
| 131 |
* @return Matcher |
| 132 |
*/ |
| 133 |
public function string() { |
| 134 |
return $this->cache[__METHOD__] |
| 135 |
??= new TokenMatcher( Token::T_STRING ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Matcher for a string containing a URL |
| 140 |
* @param string $type Type of resource referenced, e.g. "image" or "audio". |
| 141 |
* Not used here, but might be used by a subclass to validate the URL more strictly. |
| 142 |
* @return Matcher |
| 143 |
*/ |
| 144 |
public function urlstring( $type ) { |
| 145 |
return $this->string(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Matcher for a URL |
| 150 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#urls |
| 151 |
* @param string $type Type of resource referenced, e.g. "image" or "audio". |
| 152 |
* Not used here, but might be used by a subclass to validate the URL more strictly. |
| 153 |
* @return Matcher |
| 154 |
*/ |
| 155 |
public function url( $type ) { |
| 156 |
return $this->cache[__METHOD__] |
| 157 |
??= new UrlMatcher(); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* CSS-wide value keywords |
| 162 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#common-keywords |
| 163 |
* @return Matcher |
| 164 |
*/ |
| 165 |
public function cssWideKeywords() { |
| 166 |
return $this->cache[__METHOD__] |
| 167 |
??= new KeywordMatcher( [ |
| 168 |
// https://www.w3.org/TR/2024/WD-css-values-4-20240312/#common-keywords |
| 169 |
'initial', 'inherit', 'unset', |
| 170 |
// added by https://www.w3.org/TR/2022/CR-css-cascade-4-20220113/#all-shorthand |
| 171 |
'revert' |
| 172 |
] ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Matcher for a calculation <calc-sum> |
| 177 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#calc-syntax |
| 178 |
* @return Matcher |
| 179 |
*/ |
| 180 |
protected function calcSum() { |
| 181 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 182 |
$ows = $this->optionalWhitespace(); |
| 183 |
$ws = $this->significantWhitespace(); |
| 184 |
|
| 185 |
// Definitions are recursive. This will be used by reference and later |
| 186 |
// will be replaced. |
| 187 |
$calcValue = new NothingMatcher(); |
| 188 |
|
| 189 |
$calcProduct = new Juxtaposition( [ |
| 190 |
&$calcValue, |
| 191 |
Quantifier::star( |
| 192 |
new Juxtaposition( [ $ows, new DelimMatcher( [ '*', '/' ] ), $ows, &$calcValue ] ) |
| 193 |
), |
| 194 |
] ); |
| 195 |
|
| 196 |
$calcSum = new Juxtaposition( [ |
| 197 |
$ows, |
| 198 |
$calcProduct, |
| 199 |
Quantifier::star( new Juxtaposition( [ |
| 200 |
$ws, new DelimMatcher( [ '+', '-' ] ), $ws, $calcProduct |
| 201 |
] ) ), |
| 202 |
$ows, |
| 203 |
] ); |
| 204 |
// Save it to the cache before it is fully resolved, so that we can call |
| 205 |
// number() etc. This allows things like calc( sin( 1 ) ) since sin() is |
| 206 |
// a math function on the same level as calc() itself. |
| 207 |
$this->cache[__METHOD__] = $calcSum; |
| 208 |
|
| 209 |
$calcKeyword = new KeywordMatcher( [ 'e', 'pi', 'infinity', '-infinity', 'NaN' ] ); |
| 210 |
// calc() forces values to be numeric so it is safe to allow custom props here |
| 211 |
$customProp = new FunctionMatcher( 'var', new Juxtaposition( [ |
| 212 |
new CustomPropertyMatcher(), |
| 213 |
Quantifier::optional( $calcSum ), |
| 214 |
], true ) ); |
| 215 |
|
| 216 |
// Complete the recursive rule <calc-value> |
| 217 |
$calcValue = new Alternative( [ |
| 218 |
$this->number(), |
| 219 |
$this->dimension(), |
| 220 |
$this->percentage(), |
| 221 |
$calcKeyword, |
| 222 |
$customProp, |
| 223 |
new BlockMatcher( Token::T_LEFT_PAREN, $calcSum ) |
| 224 |
] ); |
| 225 |
} |
| 226 |
return $this->cache[__METHOD__]; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Create a function which returns true if the name passed to it is a |
| 231 |
* function with the specified number or type of arguments. |
| 232 |
* |
| 233 |
* @param int|string $argType |
| 234 |
* @return Closure |
| 235 |
*/ |
| 236 |
private function makeFuncNameChecker( $argType ) { |
| 237 |
$funcArgs = [ |
| 238 |
'calc' => 1, |
| 239 |
'min' => '#', |
| 240 |
'max' => '#', |
| 241 |
'clamp' => 'clamp', |
| 242 |
'round' => 'round', |
| 243 |
'mod' => 2, |
| 244 |
'rem' => 2, |
| 245 |
'sin' => 1, |
| 246 |
'cos' => 1, |
| 247 |
'tan' => 1, |
| 248 |
'asin' => 1, |
| 249 |
'acos' => 1, |
| 250 |
'atan' => 1, |
| 251 |
'atan2' => 2, |
| 252 |
'pow' => 2, |
| 253 |
'sqrt' => 1, |
| 254 |
'hypot' => '#', |
| 255 |
'log' => 'log', |
| 256 |
'exp' => 1, |
| 257 |
'abs' => 1, |
| 258 |
'sign' => 1, |
| 259 |
]; |
| 260 |
return static function ( $name ) use ( $funcArgs, $argType ) { |
| 261 |
// phpcs:ignore Generic.ControlStructures.DisallowYodaConditions |
| 262 |
return $funcArgs[ strtolower( $name ) ] ?? null === $argType; |
| 263 |
}; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Match either a math function such as calc() or a specified value type. |
| 268 |
* |
| 269 |
* Note: CSS Values Level 4 is much more permissive than Level 3. Checking |
| 270 |
* of types such as length is deferred until runtime, and non-integer return |
| 271 |
* values in an integer context are rounded instead of being rejected at |
| 272 |
* parse time. |
| 273 |
* |
| 274 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#calc-syntax |
| 275 |
* @return Matcher |
| 276 |
*/ |
| 277 |
protected function mathFunction( Matcher $typeMatcher ) { |
| 278 |
$calcSum = $this->calcSum(); |
| 279 |
|
| 280 |
$matchers = [ $typeMatcher ]; |
| 281 |
|
| 282 |
// Functions with one argument |
| 283 |
$matchers[] = new FunctionMatcher( |
| 284 |
$this->makeFuncNameChecker( 1 ), |
| 285 |
$calcSum |
| 286 |
); |
| 287 |
|
| 288 |
// Functions with two arguments |
| 289 |
$matchers[] = new FunctionMatcher( |
| 290 |
$this->makeFuncNameChecker( 2 ), |
| 291 |
new Juxtaposition( [ $calcSum, $calcSum ], true ) |
| 292 |
); |
| 293 |
|
| 294 |
// Functions with N arguments separated by commas |
| 295 |
$matchers[] = new FunctionMatcher( |
| 296 |
$this->makeFuncNameChecker( '#' ), |
| 297 |
Quantifier::hash( $calcSum ) |
| 298 |
); |
| 299 |
|
| 300 |
// clamp |
| 301 |
$clampArg = new Alternative( [ |
| 302 |
$calcSum, |
| 303 |
new KeywordMatcher( 'none' ) |
| 304 |
] ); |
| 305 |
$matchers[] = new FunctionMatcher( |
| 306 |
$this->makeFuncNameChecker( 'clamp' ), |
| 307 |
new Juxtaposition( [ $clampArg, $calcSum, $clampArg ], true ) |
| 308 |
); |
| 309 |
|
| 310 |
// round |
| 311 |
$roundingStrategy = new KeywordMatcher( [ 'nearest', 'up', 'down', 'to-zero' ] ); |
| 312 |
$matchers[] = new FunctionMatcher( |
| 313 |
$this->makeFuncNameChecker( 'round' ), |
| 314 |
new Juxtaposition( |
| 315 |
[ |
| 316 |
Quantifier::optional( $roundingStrategy ), |
| 317 |
$calcSum, |
| 318 |
Quantifier::optional( $calcSum ) |
| 319 |
], |
| 320 |
true |
| 321 |
) |
| 322 |
); |
| 323 |
|
| 324 |
// log |
| 325 |
$matchers[] = new FunctionMatcher( |
| 326 |
$this->makeFuncNameChecker( 'log' ), |
| 327 |
new Juxtaposition( |
| 328 |
[ |
| 329 |
$calcSum, |
| 330 |
Quantifier::optional( $calcSum ) |
| 331 |
], |
| 332 |
true |
| 333 |
) |
| 334 |
); |
| 335 |
return new Alternative( $matchers ); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Matcher for an integer value, without math functions |
| 340 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#integers |
| 341 |
* @return Matcher |
| 342 |
*/ |
| 343 |
protected function rawInteger() { |
| 344 |
return $this->cache[__METHOD__] |
| 345 |
??= new TokenMatcher( Token::T_NUMBER, static function ( Token $t ) { |
| 346 |
// The spec says it must match /^[+-]\d+$/, but the tokenizer |
| 347 |
// should have marked any other number token as a 'number' |
| 348 |
// anyway so let's not bother checking. |
| 349 |
return $t->typeFlag() === 'integer'; |
| 350 |
} ); |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* @return TokenMatcher |
| 355 |
*/ |
| 356 |
public function colorHex(): TokenMatcher { |
| 357 |
return $this->cache[__METHOD__] |
| 358 |
??= new TokenMatcher( Token::T_HASH, static function ( Token $t ) { |
| 359 |
return preg_match( '/^([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $t->value() ); |
| 360 |
} ); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Matcher for an integer value |
| 365 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#integers |
| 366 |
* @return Matcher |
| 367 |
*/ |
| 368 |
public function integer() { |
| 369 |
return $this->cache[__METHOD__] |
| 370 |
??= $this->mathFunction( $this->rawInteger() ); |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Matcher for a real number, without math functions |
| 375 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#numbers |
| 376 |
* @return Matcher |
| 377 |
*/ |
| 378 |
public function rawNumber() { |
| 379 |
return $this->cache[__METHOD__] |
| 380 |
??= new TokenMatcher( Token::T_NUMBER ); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Matcher for a real number |
| 385 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#numbers |
| 386 |
* @return Matcher |
| 387 |
*/ |
| 388 |
public function number() { |
| 389 |
return $this->cache[__METHOD__] |
| 390 |
??= $this->mathFunction( $this->rawNumber() ); |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Ratio values |
| 395 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#ratios |
| 396 |
* @return Matcher |
| 397 |
*/ |
| 398 |
public function ratio() { |
| 399 |
return $this->cache[__METHOD__] |
| 400 |
// <ratio> = <number [0,∞]> [ / <number [0,∞]> ]? |
| 401 |
??= new Alternative( [ |
| 402 |
$this->rawNumber(), |
| 403 |
new Juxtaposition( [ |
| 404 |
$this->rawNumber(), |
| 405 |
$this->optionalWhitespace(), |
| 406 |
new DelimMatcher( [ '/' ] ), |
| 407 |
$this->optionalWhitespace(), |
| 408 |
$this->rawNumber(), |
| 409 |
] ), |
| 410 |
] ); |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Matcher for a percentage value, without math functions |
| 415 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#percentages |
| 416 |
* @return Matcher |
| 417 |
*/ |
| 418 |
public function rawPercentage() { |
| 419 |
return $this->cache[__METHOD__] |
| 420 |
??= new TokenMatcher( Token::T_PERCENTAGE ); |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Matcher for a percentage value |
| 425 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#percentages |
| 426 |
* @return Matcher |
| 427 |
*/ |
| 428 |
public function percentage() { |
| 429 |
return $this->cache[__METHOD__] |
| 430 |
??= $this->mathFunction( $this->rawPercentage() ); |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Matcher for a length-percentage value |
| 435 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#typedef-length-percentage |
| 436 |
* @return Matcher |
| 437 |
*/ |
| 438 |
public function lengthPercentage() { |
| 439 |
return $this->cache[__METHOD__] |
| 440 |
??= $this->mathFunction( |
| 441 |
new Alternative( [ $this->rawLength(), $this->rawPercentage() ] ) |
| 442 |
); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Matcher for a frequency-percentage value |
| 447 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#typedef-frequency-percentage |
| 448 |
* @return Matcher |
| 449 |
*/ |
| 450 |
public function frequencyPercentage() { |
| 451 |
return $this->cache[__METHOD__] |
| 452 |
??= $this->mathFunction( |
| 453 |
new Alternative( [ $this->rawFrequency(), $this->rawPercentage() ] ) |
| 454 |
); |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Matcher for an angle-percentage value |
| 459 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#typedef-angle-percentage |
| 460 |
* @return Matcher |
| 461 |
*/ |
| 462 |
public function anglePercentage() { |
| 463 |
return $this->cache[__METHOD__] |
| 464 |
??= $this->mathFunction( |
| 465 |
new Alternative( [ $this->rawAngle(), $this->rawPercentage() ] ) |
| 466 |
); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Matcher for a time-percentage value |
| 471 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#typedef-time-percentage |
| 472 |
* @return Matcher |
| 473 |
*/ |
| 474 |
public function timePercentage() { |
| 475 |
return $this->cache[__METHOD__] |
| 476 |
??= $this->mathFunction( |
| 477 |
new Alternative( [ $this->rawTime(), $this->rawPercentage() ] ) |
| 478 |
); |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* A convenience method for matching <number>|<percentage>. |
| 483 |
* |
| 484 |
* In CSS Values 3 there was a <number-percentage> production, but this was |
| 485 |
* removed in CSS Values 4 with the note "<number> and <percentage> can't |
| 486 |
* be combined in calc()". Things that previously used <number-percentage> |
| 487 |
* were updated to use <number>|<percentage>. So, following Values 4, we |
| 488 |
* will return a matcher for <number>|<percentage> here. |
| 489 |
* |
| 490 |
* Note that calc(1 + 50%) is still allowed at parse time since <calc-value> |
| 491 |
* can now be either <number> or <percentage>. |
| 492 |
* |
| 493 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#percentages |
| 494 |
* @return Matcher |
| 495 |
*/ |
| 496 |
public function numberPercentage() { |
| 497 |
return $this->cache[__METHOD__] |
| 498 |
??= new Alternative( [ $this->number(), $this->percentage() ] ); |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Matcher for a dimension value |
| 503 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#dimensions |
| 504 |
* @return Matcher |
| 505 |
*/ |
| 506 |
public function dimension() { |
| 507 |
return $this->cache[__METHOD__] |
| 508 |
??= new TokenMatcher( Token::T_DIMENSION ); |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* Matches the number 0 |
| 513 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#zero-value |
| 514 |
* @return Matcher |
| 515 |
*/ |
| 516 |
public function zero() { |
| 517 |
return $this->cache[__METHOD__] |
| 518 |
??= new TokenMatcher( Token::T_NUMBER, static function ( Token $t ) { |
| 519 |
return $t->value() === 0 || $t->value() === 0.0; |
| 520 |
} ); |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Matcher for a length value, without math functions |
| 525 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#lengths |
| 526 |
* @return Matcher |
| 527 |
*/ |
| 528 |
protected function rawLength() { |
| 529 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 530 |
$unitsRe = '/^(' . implode( '|', self::$lengthUnits ) . ')$/i'; |
| 531 |
|
| 532 |
$this->cache[__METHOD__] = new Alternative( [ |
| 533 |
$this->zero(), |
| 534 |
new TokenMatcher( Token::T_DIMENSION, static function ( Token $t ) use ( $unitsRe ) { |
| 535 |
return preg_match( $unitsRe, $t->unit() ); |
| 536 |
} ), |
| 537 |
] ); |
| 538 |
} |
| 539 |
return $this->cache[__METHOD__]; |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Matcher for a length value |
| 544 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#lengths |
| 545 |
* @return Matcher |
| 546 |
*/ |
| 547 |
public function length() { |
| 548 |
return $this->cache[__METHOD__] |
| 549 |
??= $this->mathFunction( $this->rawLength() ); |
| 550 |
} |
| 551 |
|
| 552 |
/** |
| 553 |
* Matcher for an angle value, without math functions |
| 554 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#angles |
| 555 |
* @return Matcher |
| 556 |
*/ |
| 557 |
protected function rawAngle() { |
| 558 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 559 |
$unitsRe = '/^(' . implode( '|', self::$angleUnits ) . ')$/i'; |
| 560 |
|
| 561 |
$this->cache[__METHOD__] = new TokenMatcher( Token::T_DIMENSION, |
| 562 |
static function ( Token $t ) use ( $unitsRe ) { |
| 563 |
return preg_match( $unitsRe, $t->unit() ); |
| 564 |
} |
| 565 |
); |
| 566 |
} |
| 567 |
return $this->cache[__METHOD__]; |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* Matcher for an angle value |
| 572 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#angles |
| 573 |
* @return Matcher |
| 574 |
*/ |
| 575 |
public function angle() { |
| 576 |
return $this->cache[__METHOD__] |
| 577 |
??= $this->mathFunction( $this->rawAngle() ); |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Matcher for a duration (time) value, without math functions |
| 582 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#time |
| 583 |
* @return Matcher |
| 584 |
*/ |
| 585 |
protected function rawTime() { |
| 586 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 587 |
$unitsRe = '/^(' . implode( '|', self::$timeUnits ) . ')$/i'; |
| 588 |
|
| 589 |
$this->cache[__METHOD__] = new TokenMatcher( Token::T_DIMENSION, |
| 590 |
static function ( Token $t ) use ( $unitsRe ) { |
| 591 |
return preg_match( $unitsRe, $t->unit() ); |
| 592 |
} |
| 593 |
); |
| 594 |
} |
| 595 |
return $this->cache[__METHOD__]; |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Matcher for a duration (time) value |
| 600 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#time |
| 601 |
* @return Matcher |
| 602 |
*/ |
| 603 |
public function time() { |
| 604 |
return $this->cache[__METHOD__] |
| 605 |
??= $this->mathFunction( $this->rawTime() ); |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Matcher for a frequency value, without math functions |
| 610 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#frequency |
| 611 |
* @return Matcher |
| 612 |
*/ |
| 613 |
protected function rawFrequency() { |
| 614 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 615 |
$unitsRe = '/^(' . implode( '|', self::$frequencyUnits ) . ')$/i'; |
| 616 |
|
| 617 |
$this->cache[__METHOD__] = new TokenMatcher( Token::T_DIMENSION, |
| 618 |
static function ( Token $t ) use ( $unitsRe ) { |
| 619 |
return preg_match( $unitsRe, $t->unit() ); |
| 620 |
} |
| 621 |
); |
| 622 |
} |
| 623 |
return $this->cache[__METHOD__]; |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* Matcher for a frequency value |
| 628 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#frequency |
| 629 |
* @return Matcher |
| 630 |
*/ |
| 631 |
public function frequency() { |
| 632 |
return $this->cache[__METHOD__] |
| 633 |
??= $this->mathFunction( $this->rawFrequency() ); |
| 634 |
} |
| 635 |
|
| 636 |
/** |
| 637 |
* Matcher for a raw resolution value, without math functions |
| 638 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#resolution |
| 639 |
* @return Matcher |
| 640 |
*/ |
| 641 |
protected function rawResolution() { |
| 642 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 643 |
$unitsRe = '/^(' . implode( '|', self::$resolutionUnits ) . ')$/i'; |
| 644 |
$this->cache[__METHOD__] = new TokenMatcher( Token::T_DIMENSION, |
| 645 |
static function ( Token $t ) use ( $unitsRe ) { |
| 646 |
return preg_match( $unitsRe, $t->unit() ); |
| 647 |
} |
| 648 |
); |
| 649 |
} |
| 650 |
return $this->cache[__METHOD__]; |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* Matcher for a resolution value |
| 655 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#resolution |
| 656 |
* @return Matcher |
| 657 |
*/ |
| 658 |
public function resolution() { |
| 659 |
return $this->cache[__METHOD__] |
| 660 |
??= $this->mathFunction( $this->rawResolution() ); |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Matcher for either a given value type or a var() with the value type |
| 665 |
* as fallback. |
| 666 |
* |
| 667 |
* @param Matcher $fallback |
| 668 |
* @return Matcher |
| 669 |
*/ |
| 670 |
private function rawOrCustomProp( Matcher $fallback ): Matcher { |
| 671 |
$withCustomProp = new FunctionMatcher( 'var', new Juxtaposition( [ |
| 672 |
new CustomPropertyMatcher(), |
| 673 |
Quantifier::optional( $fallback ), |
| 674 |
], true ) ); |
| 675 |
return new Alternative( [ $fallback, $withCustomProp ] ); |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Matchers for color functions |
| 680 |
* @return Matcher[] |
| 681 |
*/ |
| 682 |
protected function colorFuncs() { |
| 683 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 684 |
// Color functions require arguments to be numeric so allowing var() is safe |
| 685 |
$n = $this->rawOrCustomProp( $this->number() ); |
| 686 |
$p = $this->rawOrCustomProp( $this->percentage() ); |
| 687 |
$hue = new Alternative( [ $this->angle(), $n ] ); |
| 688 |
|
| 689 |
$none = new KeywordMatcher( 'none' ); |
| 690 |
$nPNone = new Alternative( [ $this->rawOrCustomProp( $this->numberPercentage() ), $none ] ); |
| 691 |
$hueNone = new Alternative( [ $hue, $none ] ); |
| 692 |
|
| 693 |
$colorSpaceParams = new Alternative( [ |
| 694 |
new Juxtaposition( [ |
| 695 |
new KeywordMatcher( [ |
| 696 |
'srgb', 'srgb-linear', 'display-p3', 'a98-rgb', |
| 697 |
'prophoto-rgb', 'rec2020' |
| 698 |
] ), |
| 699 |
Quantifier::count( $nPNone, 3, 3 ), |
| 700 |
] ), |
| 701 |
new Juxtaposition( [ |
| 702 |
new KeywordMatcher( [ 'xyz', 'xyz-d50', 'xyz-d65' ] ), |
| 703 |
Quantifier::count( $nPNone, 3, 3 ), |
| 704 |
] ), |
| 705 |
] ); |
| 706 |
|
| 707 |
$optionalAlpha = Quantifier::optional( new Juxtaposition( |
| 708 |
[ new DelimMatcher( '/' ), new Alternative( [ $nPNone, $none ] ) ] |
| 709 |
) ); |
| 710 |
$optionalLegacyAlpha = Quantifier::optional( $nPNone ); |
| 711 |
|
| 712 |
$rgb = new Alternative( [ |
| 713 |
new Juxtaposition( [ Quantifier::hash( $p, 3, 3 ), $optionalLegacyAlpha ], true ), |
| 714 |
new Juxtaposition( [ Quantifier::hash( $n, 3, 3 ), $optionalLegacyAlpha ], true ), |
| 715 |
new Juxtaposition( [ $nPNone, $nPNone, $nPNone, $optionalAlpha ] ), |
| 716 |
] ); |
| 717 |
$hsl = new Alternative( [ |
| 718 |
new Juxtaposition( [ $hue, $p, $p, $optionalLegacyAlpha ], true ), |
| 719 |
new Juxtaposition( [ $hueNone, $nPNone, $nPNone, $optionalAlpha ] ), |
| 720 |
] ); |
| 721 |
$hwb = new Juxtaposition( [ $hueNone, $nPNone, $nPNone, $optionalAlpha ] ); |
| 722 |
$lab = new Juxtaposition( [ $nPNone, $nPNone, $nPNone, $optionalAlpha ] ); |
| 723 |
$lch = new Juxtaposition( [ $nPNone, $nPNone, $hueNone, $optionalAlpha ] ); |
| 724 |
$color = new Juxtaposition( [ $colorSpaceParams, $optionalAlpha ] ); |
| 725 |
|
| 726 |
$this->cache[__METHOD__] = [ |
| 727 |
new FunctionMatcher( 'rgb', $rgb ), |
| 728 |
new FunctionMatcher( 'rgba', $rgb ), |
| 729 |
new FunctionMatcher( 'hsl', $hsl ), |
| 730 |
new FunctionMatcher( 'hsla', $hsl ), |
| 731 |
new FunctionMatcher( 'hwb', $hwb ), |
| 732 |
new FunctionMatcher( 'lab', $lab ), |
| 733 |
new FunctionMatcher( 'lch', $lch ), |
| 734 |
new FunctionMatcher( 'oklab', $lab ), |
| 735 |
new FunctionMatcher( 'oklch', $lch ), |
| 736 |
new FunctionMatcher( 'color', $color ) |
| 737 |
]; |
| 738 |
} |
| 739 |
return $this->cache[__METHOD__]; |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* Matcher for a color value, *not* including a custom property reference. |
| 744 |
* |
| 745 |
* Because custom properties can lead to unexpected behavior (generally |
| 746 |
* a bad thing for security) when concatenated together, this matcher |
| 747 |
* should be used for CSS rules which allow value concatenation. |
| 748 |
* For example, `border-color` allows up to 4 `var(...)` expressions to |
| 749 |
* potentially be concatenated. |
| 750 |
* |
| 751 |
* @see https://www.w3.org/TR/2022/CR-css-variables-1-20220616/#custom-property |
| 752 |
* @return Matcher |
| 753 |
*/ |
| 754 |
public function safeColor() { |
| 755 |
return $this->cache[__METHOD__] |
| 756 |
??= new Alternative( array_merge( [ |
| 757 |
$this->colorWords(), |
| 758 |
$this->colorHex(), |
| 759 |
], $this->colorFuncs() ) ); |
| 760 |
} |
| 761 |
|
| 762 |
/** |
| 763 |
* Matcher for a color value, including a possible custom property |
| 764 |
* reference and light-dark color function. |
| 765 |
* |
| 766 |
* Follows: |
| 767 |
* * https://www.w3.org/TR/2025/CRD-css-color-4-20250424/ |
| 768 |
* * https://www.w3.org/TR/css-variables-1/ |
| 769 |
* * https://www.w3.org/TR/2024/WD-css-color-5-20240229/#funcdef-light-dark |
| 770 |
* |
| 771 |
* @return Matcher |
| 772 |
*/ |
| 773 |
public function color() { |
| 774 |
return $this->cache[__METHOD__] |
| 775 |
??= new Alternative( [ |
| 776 |
$this->safeColor(), |
| 777 |
new FunctionMatcher( 'var', new Juxtaposition( [ |
| 778 |
new CustomPropertyMatcher(), |
| 779 |
Quantifier::optional( $this->safeColor() ), |
| 780 |
], true ) ), |
| 781 |
new FunctionMatcher( 'light-dark', new Juxtaposition( [ |
| 782 |
$this->safeColor(), |
| 783 |
$this->safeColor(), |
| 784 |
], true ) ), |
| 785 |
] ); |
| 786 |
} |
| 787 |
|
| 788 |
/** |
| 789 |
* Matcher for an image value |
| 790 |
* @see https://www.w3.org/TR/2023/CRD-css-images-3-20231218/#image-values |
| 791 |
* @return Matcher |
| 792 |
*/ |
| 793 |
public function image() { |
| 794 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 795 |
// https://www.w3.org/TR/2023/CRD-css-images-3-20231218/#gradients |
| 796 |
$c = $this->comma(); |
| 797 |
$colorStop = new Juxtaposition( [ |
| 798 |
$this->color(), |
| 799 |
Quantifier::optional( $this->lengthPercentage() ), |
| 800 |
] ); |
| 801 |
$colorStopList = new Juxtaposition( [ |
| 802 |
$colorStop, |
| 803 |
Quantifier::hash( new Juxtaposition( [ |
| 804 |
Quantifier::optional( $this->lengthPercentage() ), |
| 805 |
$colorStop |
| 806 |
], true ) ), |
| 807 |
], true ); |
| 808 |
$atPosition = new Juxtaposition( [ new KeywordMatcher( 'at' ), $this->position() ] ); |
| 809 |
|
| 810 |
$linearGradient = new Juxtaposition( [ |
| 811 |
Quantifier::optional( new Juxtaposition( [ |
| 812 |
new Alternative( [ |
| 813 |
new Alternative( [ |
| 814 |
$this->zero(), |
| 815 |
$this->angle(), |
| 816 |
] ), |
| 817 |
new Juxtaposition( [ new KeywordMatcher( 'to' ), UnorderedGroup::someOf( [ |
| 818 |
new KeywordMatcher( [ 'left', 'right' ] ), |
| 819 |
new KeywordMatcher( [ 'top', 'bottom' ] ), |
| 820 |
] ) ] ) |
| 821 |
] ), |
| 822 |
$c |
| 823 |
] ) ), |
| 824 |
$colorStopList, |
| 825 |
] ); |
| 826 |
$radialGradient = new Juxtaposition( [ |
| 827 |
Quantifier::optional( new Juxtaposition( [ |
| 828 |
new Alternative( [ |
| 829 |
new Juxtaposition( [ |
| 830 |
new Alternative( [ |
| 831 |
UnorderedGroup::someOf( [ new KeywordMatcher( 'circle' ), $this->length() ] ), |
| 832 |
UnorderedGroup::someOf( [ |
| 833 |
new KeywordMatcher( 'ellipse' ), |
| 834 |
Quantifier::count( $this->lengthPercentage(), 2, 2 ) |
| 835 |
] ), |
| 836 |
UnorderedGroup::someOf( [ |
| 837 |
new KeywordMatcher( [ 'circle', 'ellipse' ] ), |
| 838 |
new KeywordMatcher( [ |
| 839 |
'closest-corner', 'closest-side', 'farthest-corner', 'farthest-side', |
| 840 |
] ), |
| 841 |
] ), |
| 842 |
] ), |
| 843 |
Quantifier::optional( $atPosition ), |
| 844 |
] ), |
| 845 |
$atPosition |
| 846 |
] ), |
| 847 |
$c |
| 848 |
] ) ), |
| 849 |
$colorStopList, |
| 850 |
] ); |
| 851 |
|
| 852 |
// Putting it all together |
| 853 |
$this->cache[__METHOD__] = new Alternative( [ |
| 854 |
$this->url( 'image' ), |
| 855 |
new FunctionMatcher( 'linear-gradient', $linearGradient ), |
| 856 |
new FunctionMatcher( 'radial-gradient', $radialGradient ), |
| 857 |
new FunctionMatcher( 'repeating-linear-gradient', $linearGradient ), |
| 858 |
new FunctionMatcher( 'repeating-radial-gradient', $radialGradient ), |
| 859 |
] ); |
| 860 |
} |
| 861 |
return $this->cache[__METHOD__]; |
| 862 |
} |
| 863 |
|
| 864 |
/** |
| 865 |
* Matcher for a position value |
| 866 |
* @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#typedef-position |
| 867 |
* @return Matcher |
| 868 |
*/ |
| 869 |
public function position() { |
| 870 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 871 |
$lp = $this->lengthPercentage(); |
| 872 |
|
| 873 |
$this->cache[__METHOD__] = new Alternative( [ |
| 874 |
new KeywordMatcher( [ 'left', 'center', 'right', 'top', 'bottom' ] ), |
| 875 |
$lp, |
| 876 |
UnorderedGroup::allOf( [ |
| 877 |
new KeywordMatcher( [ 'left', 'center', 'right' ] ), |
| 878 |
new KeywordMatcher( [ 'top', 'center', 'bottom' ] ) |
| 879 |
] ), |
| 880 |
new Juxtaposition( [ |
| 881 |
new Alternative( [ |
| 882 |
new KeywordMatcher( [ 'left', 'center', 'right' ] ), |
| 883 |
$lp |
| 884 |
] ), |
| 885 |
new Alternative( [ |
| 886 |
new KeywordMatcher( [ 'top', 'center', 'bottom' ] ), |
| 887 |
$lp |
| 888 |
] ), |
| 889 |
] ), |
| 890 |
UnorderedGroup::allOf( [ |
| 891 |
new Juxtaposition( [ |
| 892 |
new KeywordMatcher( [ 'left', 'right' ] ), |
| 893 |
$lp |
| 894 |
] ), |
| 895 |
new Juxtaposition( [ |
| 896 |
new KeywordMatcher( [ 'top', 'bottom' ] ), |
| 897 |
$lp |
| 898 |
] ), |
| 899 |
] ) |
| 900 |
] ); |
| 901 |
} |
| 902 |
return $this->cache[__METHOD__]; |
| 903 |
} |
| 904 |
|
| 905 |
/** |
| 906 |
* Matcher for a bg-position value |
| 907 |
* @see https://www.w3.org/TR/2024/CRD-css-backgrounds-3-20240311/#typedef-bg-position |
| 908 |
* @return Matcher |
| 909 |
*/ |
| 910 |
public function bgPosition() { |
| 911 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 912 |
$lp = $this->lengthPercentage(); |
| 913 |
$olp = Quantifier::optional( $lp ); |
| 914 |
$center = new KeywordMatcher( 'center' ); |
| 915 |
$leftRight = new KeywordMatcher( [ 'left', 'right' ] ); |
| 916 |
$topBottom = new KeywordMatcher( [ 'top', 'bottom' ] ); |
| 917 |
|
| 918 |
$this->cache[__METHOD__] = new Alternative( [ |
| 919 |
new Alternative( [ $center, $leftRight, $topBottom, $lp ] ), |
| 920 |
new Juxtaposition( [ |
| 921 |
new Alternative( [ $center, $leftRight, $lp ] ), |
| 922 |
new Alternative( [ $center, $topBottom, $lp ] ), |
| 923 |
] ), |
| 924 |
UnorderedGroup::allOf( [ |
| 925 |
new Alternative( [ $center, new Juxtaposition( [ $leftRight, $olp ] ) ] ), |
| 926 |
new Alternative( [ $center, new Juxtaposition( [ $topBottom, $olp ] ) ] ), |
| 927 |
] ), |
| 928 |
] ); |
| 929 |
} |
| 930 |
return $this->cache[__METHOD__]; |
| 931 |
} |
| 932 |
|
| 933 |
/** |
| 934 |
* Matcher for a CSS media query |
| 935 |
* @see https://www.w3.org/TR/2017/CR-mediaqueries-4-20170905/#mq-syntax |
| 936 |
* Level 5 accessibility queries are also supported |
| 937 |
* @see https://drafts.csswg.org/mediaqueries-5/#mf-user-preferences |
| 938 |
* @param bool $strict Only allow defined query types |
| 939 |
* @return Matcher |
| 940 |
*/ |
| 941 |
public function cssMediaQuery( $strict = true ) { |
| 942 |
$key = __METHOD__ . ':' . ( $strict ? 'strict' : 'unstrict' ); |
| 943 |
if ( !isset( $this->cache[$key] ) ) { |
| 944 |
if ( $strict ) { |
| 945 |
$generalEnclosed = new NothingMatcher(); |
| 946 |
|
| 947 |
$mediaType = new KeywordMatcher( [ |
| 948 |
'all', 'print', 'screen', 'speech', |
| 949 |
// deprecated |
| 950 |
'tty', 'tv', 'projection', 'handheld', 'braille', 'embossed', 'aural' |
| 951 |
] ); |
| 952 |
|
| 953 |
$rangeFeatures = [ |
| 954 |
'width', 'height', 'aspect-ratio', 'resolution', 'color', 'color-index', 'monochrome', |
| 955 |
// deprecated |
| 956 |
'device-width', 'device-height', 'device-aspect-ratio' |
| 957 |
]; |
| 958 |
$discreteFeatures = [ |
| 959 |
'orientation', 'scan', 'grid', 'update', 'overflow-block', 'overflow-inline', 'color-gamut', |
| 960 |
'pointer', 'hover', 'any-pointer', 'any-hover', 'scripting', 'prefers-color-scheme', |
| 961 |
'prefers-reduced-motion', 'prefers-reduced-transparency', |
| 962 |
'prefers-contrast', 'forced-colors' |
| 963 |
]; |
| 964 |
$mfName = new KeywordMatcher( array_merge( |
| 965 |
$rangeFeatures, |
| 966 |
array_map( static function ( $f ) { |
| 967 |
return "min-$f"; |
| 968 |
}, $rangeFeatures ), |
| 969 |
array_map( static function ( $f ) { |
| 970 |
return "max-$f"; |
| 971 |
}, $rangeFeatures ), |
| 972 |
$discreteFeatures |
| 973 |
) ); |
| 974 |
} else { |
| 975 |
$anythingPlus = new AnythingMatcher( [ 'quantifier' => '+' ] ); |
| 976 |
$generalEnclosed = new Alternative( [ |
| 977 |
new FunctionMatcher( null, $anythingPlus ), |
| 978 |
new BlockMatcher( Token::T_LEFT_PAREN, |
| 979 |
new Juxtaposition( [ $this->ident(), $anythingPlus ] ) |
| 980 |
), |
| 981 |
] ); |
| 982 |
$mediaType = $this->ident(); |
| 983 |
$mfName = $this->ident(); |
| 984 |
} |
| 985 |
|
| 986 |
$posInt = $this->mathFunction( |
| 987 |
new TokenMatcher( Token::T_NUMBER, static function ( Token $t ) { |
| 988 |
return $t->typeFlag() === 'integer' && preg_match( '/^\+?\d+$/', $t->representation() ); |
| 989 |
} ) |
| 990 |
); |
| 991 |
$eq = new DelimMatcher( '=' ); |
| 992 |
$oeq = Quantifier::optional( new Juxtaposition( [ new NoWhitespace, $eq ] ) ); |
| 993 |
$ltgteq = Quantifier::optional( new Alternative( [ |
| 994 |
$eq, |
| 995 |
new Juxtaposition( [ new DelimMatcher( [ '<', '>' ] ), $oeq ] ), |
| 996 |
] ) ); |
| 997 |
$lteq = new Juxtaposition( [ new DelimMatcher( '<' ), $oeq ] ); |
| 998 |
$gteq = new Juxtaposition( [ new DelimMatcher( '>' ), $oeq ] ); |
| 999 |
$mfValue = new Alternative( [ |
| 1000 |
$this->number(), |
| 1001 |
$this->dimension(), |
| 1002 |
$this->ident(), |
| 1003 |
new KeywordMatcher( [ 'light', 'dark' ] ), |
| 1004 |
new Juxtaposition( [ $posInt, new DelimMatcher( '/' ), $posInt ] ), |
| 1005 |
] ); |
| 1006 |
|
| 1007 |
// temporary |
| 1008 |
$mediaInParens = new NothingMatcher(); |
| 1009 |
$mediaNot = new Juxtaposition( [ new KeywordMatcher( 'not' ), &$mediaInParens ] ); |
| 1010 |
$mediaAnd = new Juxtaposition( [ new KeywordMatcher( 'and' ), &$mediaInParens ] ); |
| 1011 |
$mediaOr = new Juxtaposition( [ new KeywordMatcher( 'or' ), &$mediaInParens ] ); |
| 1012 |
$mediaCondition = new Alternative( [ |
| 1013 |
$mediaNot, |
| 1014 |
new Juxtaposition( [ |
| 1015 |
&$mediaInParens, |
| 1016 |
new Alternative( [ |
| 1017 |
Quantifier::star( $mediaAnd ), |
| 1018 |
Quantifier::star( $mediaOr ), |
| 1019 |
] ) |
| 1020 |
] ), |
| 1021 |
] ); |
| 1022 |
$mediaConditionWithoutOr = new Alternative( [ |
| 1023 |
$mediaNot, |
| 1024 |
new Juxtaposition( [ &$mediaInParens, Quantifier::star( $mediaAnd ) ] ), |
| 1025 |
] ); |
| 1026 |
$mediaFeature = new BlockMatcher( Token::T_LEFT_PAREN, new Alternative( [ |
| 1027 |
// <mf-plain> |
| 1028 |
new Juxtaposition( [ $mfName, new TokenMatcher( Token::T_COLON ), $mfValue ] ), |
| 1029 |
// <mf-boolean> |
| 1030 |
$mfName, |
| 1031 |
// <mf-range>, 1st alternative |
| 1032 |
new Juxtaposition( [ $mfName, $ltgteq, $mfValue ] ), |
| 1033 |
// <mf-range>, 2nd alternative |
| 1034 |
new Juxtaposition( [ $mfValue, $ltgteq, $mfName ] ), |
| 1035 |
// <mf-range>, 3rd alt |
| 1036 |
new Juxtaposition( [ $mfValue, $lteq, $mfName, $lteq, $mfValue ] ), |
| 1037 |
// <mf-range>, 4th alt |
| 1038 |
new Juxtaposition( [ $mfValue, $gteq, $mfName, $gteq, $mfValue ] ), |
| 1039 |
] ) ); |
| 1040 |
$mediaInParens = new Alternative( [ |
| 1041 |
new BlockMatcher( Token::T_LEFT_PAREN, $mediaCondition ), |
| 1042 |
$mediaFeature, |
| 1043 |
$generalEnclosed, |
| 1044 |
] ); |
| 1045 |
|
| 1046 |
$this->cache[$key] = new Alternative( [ |
| 1047 |
$mediaCondition, |
| 1048 |
new Juxtaposition( [ |
| 1049 |
Quantifier::optional( new KeywordMatcher( [ 'not', 'only' ] ) ), |
| 1050 |
$mediaType, |
| 1051 |
Quantifier::optional( new Juxtaposition( [ |
| 1052 |
new KeywordMatcher( 'and' ), |
| 1053 |
$mediaConditionWithoutOr, |
| 1054 |
] ) ) |
| 1055 |
] ) |
| 1056 |
] ); |
| 1057 |
} |
| 1058 |
|
| 1059 |
return $this->cache[$key]; |
| 1060 |
} |
| 1061 |
|
| 1062 |
/** |
| 1063 |
* Matcher for a CSS media query list |
| 1064 |
* @see https://www.w3.org/TR/2017/CR-mediaqueries-4-20170905/#mq-syntax |
| 1065 |
* @param bool $strict Only allow defined query types |
| 1066 |
* @return Matcher |
| 1067 |
*/ |
| 1068 |
public function cssMediaQueryList( $strict = true ) { |
| 1069 |
$key = __METHOD__ . ':' . ( $strict ? 'strict' : 'unstrict' ); |
| 1070 |
return $this->cache[$key] |
| 1071 |
??= Quantifier::hash( $this->cssMediaQuery( $strict ), 0, INF ); |
| 1072 |
} |
| 1073 |
|
| 1074 |
/** |
| 1075 |
* Matcher for a "supports-condition" |
| 1076 |
* @see https://www.w3.org/TR/2013/CR-css3-conditional-20130404/#supports_condition |
| 1077 |
* @param PropertySanitizer|null $declarationSanitizer Check declarations against this Sanitizer |
| 1078 |
* @param bool $strict Only accept defined syntax. Default true. |
| 1079 |
* @return Matcher |
| 1080 |
*/ |
| 1081 |
public function cssSupportsCondition( |
| 1082 |
?PropertySanitizer $declarationSanitizer = null, $strict = true |
| 1083 |
) { |
| 1084 |
$ws = $this->significantWhitespace(); |
| 1085 |
$anythingPlus = new AnythingMatcher( [ 'quantifier' => '+' ] ); |
| 1086 |
|
| 1087 |
if ( $strict ) { |
| 1088 |
$generalEnclosed = new NothingMatcher(); |
| 1089 |
} else { |
| 1090 |
$generalEnclosed = new Alternative( [ |
| 1091 |
new FunctionMatcher( null, $anythingPlus ), |
| 1092 |
new BlockMatcher( Token::T_LEFT_PAREN, new Juxtaposition( [ $this->ident(), $anythingPlus ] ) ), |
| 1093 |
] ); |
| 1094 |
} |
| 1095 |
|
| 1096 |
// temp |
| 1097 |
$supportsConditionBlock = new NothingMatcher(); |
| 1098 |
$supportsConditionInParens = new Alternative( [ |
| 1099 |
&$supportsConditionBlock, |
| 1100 |
new BlockMatcher( Token::T_LEFT_PAREN, $this->cssDeclaration( $declarationSanitizer ) ), |
| 1101 |
$generalEnclosed, |
| 1102 |
] ); |
| 1103 |
$supportsCondition = new Alternative( [ |
| 1104 |
new Juxtaposition( [ new KeywordMatcher( 'not' ), $ws, $supportsConditionInParens ] ), |
| 1105 |
new Juxtaposition( [ $supportsConditionInParens, Quantifier::plus( new Juxtaposition( [ |
| 1106 |
$ws, new KeywordMatcher( 'and' ), $ws, $supportsConditionInParens |
| 1107 |
] ) ) ] ), |
| 1108 |
new Juxtaposition( [ $supportsConditionInParens, Quantifier::plus( new Juxtaposition( [ |
| 1109 |
$ws, new KeywordMatcher( 'or' ), $ws, $supportsConditionInParens |
| 1110 |
] ) ) ] ), |
| 1111 |
$supportsConditionInParens, |
| 1112 |
] ); |
| 1113 |
$supportsConditionBlock = new BlockMatcher( Token::T_LEFT_PAREN, $supportsCondition ); |
| 1114 |
|
| 1115 |
return $supportsCondition; |
| 1116 |
} |
| 1117 |
|
| 1118 |
/** |
| 1119 |
* Matcher for a declaration |
| 1120 |
* @param PropertySanitizer|null $declarationSanitizer Check declarations against this Sanitizer |
| 1121 |
* @return Matcher |
| 1122 |
*/ |
| 1123 |
public function cssDeclaration( ?PropertySanitizer $declarationSanitizer = null ) { |
| 1124 |
$anythingPlus = new AnythingMatcher( [ 'quantifier' => '+' ] ); |
| 1125 |
|
| 1126 |
return new CheckedMatcher( |
| 1127 |
$anythingPlus, |
| 1128 |
static function ( ComponentValueList $list, GrammarMatch $match, array $options ) |
| 1129 |
use ( $declarationSanitizer ) |
| 1130 |
{ |
| 1131 |
$cvlist = new ComponentValueList( $match->getValues() ); |
| 1132 |
$parser = Parser::newFromTokens( $cvlist->toTokenArray() ); |
| 1133 |
$declaration = $parser->parseDeclaration(); |
| 1134 |
if ( !$declaration || $parser->getParseErrors() ) { |
| 1135 |
return false; |
| 1136 |
} |
| 1137 |
if ( !$declarationSanitizer ) { |
| 1138 |
return true; |
| 1139 |
} |
| 1140 |
$reset = $declarationSanitizer->stashSanitizationErrors(); |
| 1141 |
$ret = $declarationSanitizer->sanitize( $declaration ); |
| 1142 |
$errors = $declarationSanitizer->getSanitizationErrors(); |
| 1143 |
unset( $reset ); |
| 1144 |
return $ret === $declaration && !$errors; |
| 1145 |
} |
| 1146 |
); |
| 1147 |
} |
| 1148 |
|
| 1149 |
/** |
| 1150 |
* Matcher for single easing functions from CSS Easing Functions Level 1 |
| 1151 |
* @see https://www.w3.org/TR/2023/CRD-css-easing-1-20230213/#typedef-easing-function |
| 1152 |
* @return Matcher |
| 1153 |
*/ |
| 1154 |
public function cssSingleEasingFunction() { |
| 1155 |
return $this->cache[__METHOD__] |
| 1156 |
??= new Alternative( [ |
| 1157 |
new KeywordMatcher( [ |
| 1158 |
'ease', 'linear', 'ease-in', 'ease-out', 'ease-in-out', 'step-start', 'step-end' |
| 1159 |
] ), |
| 1160 |
new FunctionMatcher( 'steps', new Juxtaposition( [ |
| 1161 |
$this->integer(), |
| 1162 |
Quantifier::optional( new KeywordMatcher( [ |
| 1163 |
'jump-start', 'jump-end', 'jump-none', 'jump-both', 'start', 'end' |
| 1164 |
] ) ), |
| 1165 |
], true ) ), |
| 1166 |
new FunctionMatcher( 'cubic-bezier', Quantifier::hash( $this->number(), 4, 4 ) ), |
| 1167 |
] ); |
| 1168 |
} |
| 1169 |
|
| 1170 |
/** |
| 1171 |
* Matcher for <counter-style> |
| 1172 |
* @see https://www.w3.org/TR/2021/CR-css-counter-styles-3-20210727/#typedef-counter-style |
| 1173 |
* @return Matcher |
| 1174 |
*/ |
| 1175 |
public function counterStyle() { |
| 1176 |
return $this->cache[__METHOD__] ??= new Alternative( [ |
| 1177 |
$this->customIdent( [ 'none' ] ), |
| 1178 |
new FunctionMatcher( |
| 1179 |
'symbols', |
| 1180 |
// "If the system is alphabetic or numeric, there must be at least two |
| 1181 |
// <string>s or <image>s, or else the function is invalid." |
| 1182 |
// Implement that by modifying the grammar |
| 1183 |
new Alternative( [ |
| 1184 |
new Juxtaposition( [ |
| 1185 |
new KeywordMatcher( [ 'numeric', 'alphabetic' ] ), |
| 1186 |
Quantifier::count( |
| 1187 |
new Alternative( [ |
| 1188 |
$this->string(), |
| 1189 |
$this->image() |
| 1190 |
] ), |
| 1191 |
2, INF |
| 1192 |
) |
| 1193 |
] ), |
| 1194 |
new Juxtaposition( [ |
| 1195 |
Quantifier::optional( new KeywordMatcher( [ |
| 1196 |
'cyclic', 'symbolic', 'fixed' |
| 1197 |
] ) ), |
| 1198 |
Quantifier::plus( |
| 1199 |
new Alternative( [ |
| 1200 |
$this->string(), |
| 1201 |
$this->image() |
| 1202 |
] ) |
| 1203 |
) |
| 1204 |
] ) |
| 1205 |
] ) |
| 1206 |
) |
| 1207 |
] ); |
| 1208 |
} |
| 1209 |
|
| 1210 |
/***************************************************************************/ |
| 1211 |
// region CSS Selectors Level 3 |
| 1212 |
/** |
| 1213 |
* @name CSS Selectors Level 3 |
| 1214 |
* https://www.w3.org/TR/2018/REC-selectors-3-20181106/#w3cselgrammar |
| 1215 |
*/ |
| 1216 |
|
| 1217 |
/** |
| 1218 |
* List of selectors (selectors_group) |
| 1219 |
* |
| 1220 |
* selector [ COMMA S* selector ]* |
| 1221 |
* |
| 1222 |
* Capturing is set up for the `selector`s. |
| 1223 |
* |
| 1224 |
* @return Matcher |
| 1225 |
*/ |
| 1226 |
public function cssSelectorList() { |
| 1227 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1228 |
// Technically the spec doesn't allow whitespace before the comma, |
| 1229 |
// but I'd guess every browser does. So just use Quantifier::hash. |
| 1230 |
$selector = $this->cssSelector()->capture( 'selector' ); |
| 1231 |
$this->cache[__METHOD__] = Quantifier::hash( $selector ); |
| 1232 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1233 |
} |
| 1234 |
return $this->cache[__METHOD__]; |
| 1235 |
} |
| 1236 |
|
| 1237 |
/** |
| 1238 |
* A single selector (selector) |
| 1239 |
* |
| 1240 |
* simple_selector_sequence [ combinator simple_selector_sequence ]* |
| 1241 |
* |
| 1242 |
* Capturing is set up for the `simple_selector_sequence`s (as 'simple') and `combinator`. |
| 1243 |
* |
| 1244 |
* @return Matcher |
| 1245 |
*/ |
| 1246 |
public function cssSelector() { |
| 1247 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1248 |
$simple = $this->cssSimpleSelectorSeq()->capture( 'simple' ); |
| 1249 |
$this->cache[__METHOD__] = new Juxtaposition( [ |
| 1250 |
$simple, |
| 1251 |
Quantifier::star( new Juxtaposition( [ |
| 1252 |
$this->cssCombinator()->capture( 'combinator' ), |
| 1253 |
$simple, |
| 1254 |
] ) ) |
| 1255 |
] ); |
| 1256 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1257 |
} |
| 1258 |
return $this->cache[__METHOD__]; |
| 1259 |
} |
| 1260 |
|
| 1261 |
/** |
| 1262 |
* A CSS combinator (combinator) |
| 1263 |
* |
| 1264 |
* PLUS S* | GREATER S* | TILDE S* | S+ |
| 1265 |
* |
| 1266 |
* (combinators can be surrounded by whitespace) |
| 1267 |
* |
| 1268 |
* @return Matcher |
| 1269 |
*/ |
| 1270 |
public function cssCombinator() { |
| 1271 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1272 |
$this->cache[__METHOD__] = new Alternative( [ |
| 1273 |
new Juxtaposition( [ |
| 1274 |
$this->optionalWhitespace(), |
| 1275 |
new DelimMatcher( [ '+', '>', '~' ] ), |
| 1276 |
$this->optionalWhitespace(), |
| 1277 |
] ), |
| 1278 |
$this->significantWhitespace(), |
| 1279 |
] ); |
| 1280 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1281 |
} |
| 1282 |
return $this->cache[__METHOD__]; |
| 1283 |
} |
| 1284 |
|
| 1285 |
/** |
| 1286 |
* A simple selector sequence (simple_selector_sequence) |
| 1287 |
* |
| 1288 |
* [ type_selector | universal ] |
| 1289 |
* [ HASH | class | attrib | pseudo | negation ]* |
| 1290 |
* | [ HASH | class | attrib | pseudo | negation ]+ |
| 1291 |
* |
| 1292 |
* The following captures are set: |
| 1293 |
* - element: [ type_selector | universal ] |
| 1294 |
* - id: HASH |
| 1295 |
* - class: class |
| 1296 |
* - attrib: attrib |
| 1297 |
* - pseudo: pseudo |
| 1298 |
* - negation: negation |
| 1299 |
* |
| 1300 |
* @return Matcher |
| 1301 |
*/ |
| 1302 |
public function cssSimpleSelectorSeq() { |
| 1303 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1304 |
$hashEtc = new Alternative( [ |
| 1305 |
$this->cssID()->capture( 'id' ), |
| 1306 |
$this->cssClass()->capture( 'class' ), |
| 1307 |
$this->cssAttrib()->capture( 'attrib' ), |
| 1308 |
$this->cssPseudo()->capture( 'pseudo' ), |
| 1309 |
$this->cssNegation()->capture( 'negation' ), |
| 1310 |
] ); |
| 1311 |
|
| 1312 |
$this->cache[__METHOD__] = new Alternative( [ |
| 1313 |
new Juxtaposition( [ |
| 1314 |
Alternative::create( [ |
| 1315 |
$this->cssTypeSelector(), |
| 1316 |
$this->cssUniversal(), |
| 1317 |
] )->capture( 'element' ), |
| 1318 |
Quantifier::star( $hashEtc ) |
| 1319 |
] ), |
| 1320 |
Quantifier::plus( $hashEtc ) |
| 1321 |
] ); |
| 1322 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1323 |
} |
| 1324 |
return $this->cache[__METHOD__]; |
| 1325 |
} |
| 1326 |
|
| 1327 |
/** |
| 1328 |
* A type selector, i.e. a tag name (type_selector) |
| 1329 |
* |
| 1330 |
* [ namespace_prefix ] ? element_name |
| 1331 |
* |
| 1332 |
* where element_name is |
| 1333 |
* |
| 1334 |
* IDENT |
| 1335 |
* |
| 1336 |
* @return Matcher |
| 1337 |
*/ |
| 1338 |
public function cssTypeSelector() { |
| 1339 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1340 |
$this->cache[__METHOD__] = new Juxtaposition( [ |
| 1341 |
$this->cssOptionalNamespacePrefix(), |
| 1342 |
new TokenMatcher( Token::T_IDENT ) |
| 1343 |
] ); |
| 1344 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1345 |
} |
| 1346 |
return $this->cache[__METHOD__]; |
| 1347 |
} |
| 1348 |
|
| 1349 |
/** |
| 1350 |
* A namespace prefix (namespace_prefix) |
| 1351 |
* |
| 1352 |
* [ IDENT | '*' ]? '|' |
| 1353 |
* |
| 1354 |
* @return Matcher |
| 1355 |
*/ |
| 1356 |
public function cssNamespacePrefix() { |
| 1357 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1358 |
$this->cache[__METHOD__] = new Juxtaposition( [ |
| 1359 |
Quantifier::optional( new Alternative( [ |
| 1360 |
$this->ident(), |
| 1361 |
new DelimMatcher( [ '*' ] ), |
| 1362 |
] ) ), |
| 1363 |
new DelimMatcher( [ '|' ] ), |
| 1364 |
] ); |
| 1365 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1366 |
} |
| 1367 |
return $this->cache[__METHOD__]; |
| 1368 |
} |
| 1369 |
|
| 1370 |
/** |
| 1371 |
* An optional namespace prefix |
| 1372 |
* |
| 1373 |
* [ namespace_prefix ]? |
| 1374 |
* |
| 1375 |
* @return Matcher |
| 1376 |
*/ |
| 1377 |
private function cssOptionalNamespacePrefix() { |
| 1378 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1379 |
$this->cache[__METHOD__] = Quantifier::optional( $this->cssNamespacePrefix() ); |
| 1380 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1381 |
} |
| 1382 |
return $this->cache[__METHOD__]; |
| 1383 |
} |
| 1384 |
|
| 1385 |
/** |
| 1386 |
* The universal selector (universal) |
| 1387 |
* |
| 1388 |
* [ namespace_prefix ]? '*' |
| 1389 |
* |
| 1390 |
* @return Matcher |
| 1391 |
*/ |
| 1392 |
public function cssUniversal() { |
| 1393 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1394 |
$this->cache[__METHOD__] = new Juxtaposition( [ |
| 1395 |
$this->cssOptionalNamespacePrefix(), |
| 1396 |
new DelimMatcher( [ '*' ] ) |
| 1397 |
] ); |
| 1398 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1399 |
} |
| 1400 |
return $this->cache[__METHOD__]; |
| 1401 |
} |
| 1402 |
|
| 1403 |
/** |
| 1404 |
* An ID selector |
| 1405 |
* |
| 1406 |
* HASH |
| 1407 |
* |
| 1408 |
* @return Matcher |
| 1409 |
*/ |
| 1410 |
public function cssID() { |
| 1411 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1412 |
$this->cache[__METHOD__] = new TokenMatcher( Token::T_HASH, static function ( Token $t ) { |
| 1413 |
return $t->typeFlag() === 'id'; |
| 1414 |
} ); |
| 1415 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1416 |
} |
| 1417 |
return $this->cache[__METHOD__]; |
| 1418 |
} |
| 1419 |
|
| 1420 |
/** |
| 1421 |
* A class selector (class) |
| 1422 |
* |
| 1423 |
* '.' IDENT |
| 1424 |
* |
| 1425 |
* @return Matcher |
| 1426 |
*/ |
| 1427 |
public function cssClass() { |
| 1428 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1429 |
$this->cache[__METHOD__] = new Juxtaposition( [ |
| 1430 |
new DelimMatcher( [ '.' ] ), |
| 1431 |
$this->ident() |
| 1432 |
] ); |
| 1433 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1434 |
} |
| 1435 |
return $this->cache[__METHOD__]; |
| 1436 |
} |
| 1437 |
|
| 1438 |
/** |
| 1439 |
* An attribute selector (attrib) |
| 1440 |
* |
| 1441 |
* '[' S* [ namespace_prefix ]? IDENT S* |
| 1442 |
* [ [ PREFIXMATCH | |
| 1443 |
* SUFFIXMATCH | |
| 1444 |
* SUBSTRINGMATCH | |
| 1445 |
* '=' | |
| 1446 |
* INCLUDES | |
| 1447 |
* DASHMATCH ] S* [ IDENT | STRING ] S* |
| 1448 |
* ]? ']' |
| 1449 |
* |
| 1450 |
* Captures are set for the attribute, test, and value. Note that these |
| 1451 |
* captures will probably be relative to the contents of the SimpleBlock |
| 1452 |
* that this matcher matches! |
| 1453 |
* |
| 1454 |
* @return Matcher |
| 1455 |
*/ |
| 1456 |
public function cssAttrib() { |
| 1457 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1458 |
// An attribute is going to be parsed by the parser as a |
| 1459 |
// SimpleBlock, so that's what we need to look for here. |
| 1460 |
|
| 1461 |
$this->cache[__METHOD__] = new BlockMatcher( Token::T_LEFT_BRACKET, |
| 1462 |
new Juxtaposition( [ |
| 1463 |
$this->optionalWhitespace(), |
| 1464 |
Juxtaposition::create( [ |
| 1465 |
$this->cssOptionalNamespacePrefix(), |
| 1466 |
$this->ident(), |
| 1467 |
] )->capture( 'attribute' ), |
| 1468 |
$this->optionalWhitespace(), |
| 1469 |
Quantifier::optional( new Juxtaposition( [ |
| 1470 |
// Sigh. They removed various tokens from CSS Syntax 3, but didn't update the grammar |
| 1471 |
// in CSS Selectors 3. Wing it with a hint from CSS Selectors 4's <attr-matcher> |
| 1472 |
( new Juxtaposition( [ |
| 1473 |
Quantifier::optional( new DelimMatcher( [ '^', '$', '*', '~', '|' ] ) ), |
| 1474 |
new DelimMatcher( [ '=' ] ), |
| 1475 |
] ) )->capture( 'test' ), |
| 1476 |
$this->optionalWhitespace(), |
| 1477 |
Alternative::create( [ |
| 1478 |
$this->ident(), |
| 1479 |
$this->string(), |
| 1480 |
] )->capture( 'value' ), |
| 1481 |
$this->optionalWhitespace(), |
| 1482 |
] ) ), |
| 1483 |
] ) |
| 1484 |
); |
| 1485 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1486 |
} |
| 1487 |
return $this->cache[__METHOD__]; |
| 1488 |
} |
| 1489 |
|
| 1490 |
/** |
| 1491 |
* A pseudo-class or pseudo-element (pseudo) |
| 1492 |
* |
| 1493 |
* ':' ':'? [ IDENT | functional_pseudo ] |
| 1494 |
* |
| 1495 |
* Where functional_pseudo is |
| 1496 |
* |
| 1497 |
* FUNCTION S* expression ')' |
| 1498 |
* |
| 1499 |
* Although this actually only matches the pseudo-selectors defined in the |
| 1500 |
* following sources: |
| 1501 |
* - https://www.w3.org/TR/2018/REC-selectors-3-20181106/#pseudo-classes |
| 1502 |
* - https://www.w3.org/TR/2022/WD-css-pseudo-4-20221230/ |
| 1503 |
* - https://www.w3.org/TR/2022/WD-selectors-4-20221111/#the-dir-pseudo |
| 1504 |
* |
| 1505 |
* @return Matcher |
| 1506 |
*/ |
| 1507 |
public function cssPseudo() { |
| 1508 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1509 |
$colon = new TokenMatcher( Token::T_COLON ); |
| 1510 |
$ows = $this->optionalWhitespace(); |
| 1511 |
$anplusb = new Juxtaposition( [ $ows, $this->cssANplusB(), $ows ] ); |
| 1512 |
$dirValues = new KeywordMatcher( [ 'ltr', 'rtl' ] ); |
| 1513 |
$this->cache[__METHOD__] = new Alternative( [ |
| 1514 |
new Juxtaposition( [ |
| 1515 |
$colon, |
| 1516 |
new Alternative( [ |
| 1517 |
new KeywordMatcher( [ |
| 1518 |
'link', 'visited', 'hover', 'active', 'focus', 'target', 'enabled', 'disabled', 'checked', |
| 1519 |
'indeterminate', 'root', 'first-child', 'last-child', 'first-of-type', |
| 1520 |
'last-of-type', 'only-child', 'only-of-type', 'empty', |
| 1521 |
// CSS2-compat elements with class syntax |
| 1522 |
'first-line', 'first-letter', 'before', 'after', |
| 1523 |
] ), |
| 1524 |
new FunctionMatcher( 'lang', new Juxtaposition( [ $ows, $this->ident(), $ows ] ) ), |
| 1525 |
new FunctionMatcher( 'dir', new Juxtaposition( [ $ows, $dirValues, $ows ] ) ), |
| 1526 |
new FunctionMatcher( 'nth-child', $anplusb ), |
| 1527 |
new FunctionMatcher( 'nth-last-child', $anplusb ), |
| 1528 |
new FunctionMatcher( 'nth-of-type', $anplusb ), |
| 1529 |
new FunctionMatcher( 'nth-last-of-type', $anplusb ), |
| 1530 |
] ), |
| 1531 |
] ), |
| 1532 |
new Juxtaposition( [ |
| 1533 |
$colon, |
| 1534 |
$colon, |
| 1535 |
new Alternative( [ |
| 1536 |
new Juxtaposition( [ |
| 1537 |
new KeywordMatcher( 'first-letter' ), |
| 1538 |
$colon, |
| 1539 |
$colon, |
| 1540 |
new KeywordMatcher( [ 'prefix', 'postfix' ] ), |
| 1541 |
] ), |
| 1542 |
new KeywordMatcher( [ |
| 1543 |
'first-line', 'first-letter', 'before', 'after', 'selection', 'target-text', |
| 1544 |
'spelling-error', 'grammar-error', 'marker', 'placeholder', |
| 1545 |
'file-selector-button', |
| 1546 |
] ), |
| 1547 |
] ), |
| 1548 |
] ), |
| 1549 |
] ); |
| 1550 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1551 |
} |
| 1552 |
return $this->cache[__METHOD__]; |
| 1553 |
} |
| 1554 |
|
| 1555 |
/** |
| 1556 |
* An "AN+B" form |
| 1557 |
* |
| 1558 |
* https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#anb-microsyntax |
| 1559 |
* |
| 1560 |
* @return Matcher |
| 1561 |
*/ |
| 1562 |
public function cssANplusB() { |
| 1563 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1564 |
// Quoth the spec: |
| 1565 |
// > The An+B notation was originally defined using a slightly |
| 1566 |
// > different tokenizer than the rest of CSS, resulting in a |
| 1567 |
// > somewhat odd definition when expressed in terms of CSS tokens. |
| 1568 |
// That's a bit of an understatement |
| 1569 |
|
| 1570 |
$plusQ = Quantifier::optional( new DelimMatcher( [ '+' ] ) ); |
| 1571 |
$n = new KeywordMatcher( [ 'n' ] ); |
| 1572 |
$dashN = new KeywordMatcher( [ '-n' ] ); |
| 1573 |
$nDash = new KeywordMatcher( [ 'n-' ] ); |
| 1574 |
$plusQN = new Juxtaposition( [ $plusQ, $n ] ); |
| 1575 |
$plusQNDash = new Juxtaposition( [ $plusQ, $nDash ] ); |
| 1576 |
$nDimension = new TokenMatcher( Token::T_DIMENSION, static function ( Token $t ) { |
| 1577 |
return $t->typeFlag() === 'integer' && !strcasecmp( $t->unit(), 'n' ); |
| 1578 |
} ); |
| 1579 |
$nDashDimension = new TokenMatcher( Token::T_DIMENSION, static function ( Token $t ) { |
| 1580 |
return $t->typeFlag() === 'integer' && !strcasecmp( $t->unit(), 'n-' ); |
| 1581 |
} ); |
| 1582 |
$nDashDigitDimension = new TokenMatcher( Token::T_DIMENSION, static function ( Token $t ) { |
| 1583 |
return $t->typeFlag() === 'integer' && preg_match( '/^n-\d+$/i', $t->unit() ); |
| 1584 |
} ); |
| 1585 |
$nDashDigitIdent = new TokenMatcher( Token::T_IDENT, static function ( Token $t ) { |
| 1586 |
return preg_match( '/^n-\d+$/i', $t->value() ); |
| 1587 |
} ); |
| 1588 |
$dashNDashDigitIdent = new TokenMatcher( Token::T_IDENT, static function ( Token $t ) { |
| 1589 |
return preg_match( '/^-n-\d+$/i', $t->value() ); |
| 1590 |
} ); |
| 1591 |
$signedInt = new TokenMatcher( Token::T_NUMBER, static function ( Token $t ) { |
| 1592 |
return $t->typeFlag() === 'integer' && preg_match( '/^[+-]/', $t->representation() ); |
| 1593 |
} ); |
| 1594 |
$signlessInt = new TokenMatcher( Token::T_NUMBER, static function ( Token $t ) { |
| 1595 |
return $t->typeFlag() === 'integer' && preg_match( '/^\d/', $t->representation() ); |
| 1596 |
} ); |
| 1597 |
$plusOrMinus = new DelimMatcher( [ '+', '-' ] ); |
| 1598 |
$S = $this->optionalWhitespace(); |
| 1599 |
|
| 1600 |
$this->cache[__METHOD__] = new Alternative( [ |
| 1601 |
new KeywordMatcher( [ 'odd', 'even' ] ), |
| 1602 |
new TokenMatcher( Token::T_NUMBER, static function ( Token $t ) { |
| 1603 |
return $t->typeFlag() === 'integer'; |
| 1604 |
} ), |
| 1605 |
$nDimension, |
| 1606 |
$plusQN, |
| 1607 |
$dashN, |
| 1608 |
$nDashDigitDimension, |
| 1609 |
new Juxtaposition( [ $plusQ, $nDashDigitIdent ] ), |
| 1610 |
$dashNDashDigitIdent, |
| 1611 |
new Juxtaposition( [ $nDimension, $S, $signedInt ] ), |
| 1612 |
new Juxtaposition( [ $plusQN, $S, $signedInt ] ), |
| 1613 |
new Juxtaposition( [ $dashN, $S, $signedInt ] ), |
| 1614 |
new Juxtaposition( [ $nDashDimension, $S, $signlessInt ] ), |
| 1615 |
new Juxtaposition( [ $plusQNDash, $S, $signlessInt ] ), |
| 1616 |
new Juxtaposition( [ new KeywordMatcher( [ '-n-' ] ), $S, $signlessInt ] ), |
| 1617 |
new Juxtaposition( [ $nDimension, $S, $plusOrMinus, $S, $signlessInt ] ), |
| 1618 |
new Juxtaposition( [ $plusQN, $S, $plusOrMinus, $S, $signlessInt ] ), |
| 1619 |
new Juxtaposition( [ $dashN, $S, $plusOrMinus, $S, $signlessInt ] ) |
| 1620 |
] ); |
| 1621 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1622 |
} |
| 1623 |
return $this->cache[__METHOD__]; |
| 1624 |
} |
| 1625 |
|
| 1626 |
/** |
| 1627 |
* A negation (negation) |
| 1628 |
* |
| 1629 |
* ':' not( S* [ type_selector | universal | HASH | class | attrib | pseudo ] S* ')' |
| 1630 |
* |
| 1631 |
* @return Matcher |
| 1632 |
*/ |
| 1633 |
public function cssNegation() { |
| 1634 |
if ( !isset( $this->cache[__METHOD__] ) ) { |
| 1635 |
// A negation is going to be parsed by the parser as a colon |
| 1636 |
// followed by a CSSFunction, so that's what we need to look for |
| 1637 |
// here. |
| 1638 |
|
| 1639 |
$this->cache[__METHOD__] = new Juxtaposition( [ |
| 1640 |
new TokenMatcher( Token::T_COLON ), |
| 1641 |
new FunctionMatcher( 'not', |
| 1642 |
new Juxtaposition( [ |
| 1643 |
$this->optionalWhitespace(), |
| 1644 |
new Alternative( [ |
| 1645 |
$this->cssTypeSelector(), |
| 1646 |
$this->cssUniversal(), |
| 1647 |
$this->cssID(), |
| 1648 |
$this->cssClass(), |
| 1649 |
$this->cssAttrib(), |
| 1650 |
$this->cssPseudo(), |
| 1651 |
] ), |
| 1652 |
$this->optionalWhitespace(), |
| 1653 |
] ) |
| 1654 |
) |
| 1655 |
] ); |
| 1656 |
$this->cache[__METHOD__]->setDefaultOptions( [ 'skip-whitespace' => false ] ); |
| 1657 |
} |
| 1658 |
return $this->cache[__METHOD__]; |
| 1659 |
} |
| 1660 |
|
| 1661 |
/** |
| 1662 |
* @return KeywordMatcher |
| 1663 |
*/ |
| 1664 |
public function colorWords(): KeywordMatcher { |
| 1665 |
return $this->cache[__METHOD__] |
| 1666 |
??= new KeywordMatcher( [ |
| 1667 |
// Basic colors |
| 1668 |
'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', |
| 1669 |
'lime', 'maroon', 'navy', 'olive', 'purple', 'red', |
| 1670 |
'silver', 'teal', 'white', 'yellow', |
| 1671 |
// Extended colors |
| 1672 |
'aliceblue', 'antiquewhite', 'aquamarine', 'azure', |
| 1673 |
'beige', 'bisque', 'blanchedalmond', 'blueviolet', 'brown', |
| 1674 |
'burlywood', 'cadetblue', 'chartreuse', 'chocolate', |
| 1675 |
'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', |
| 1676 |
'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', |
| 1677 |
'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta', |
| 1678 |
'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', |
| 1679 |
'darksalmon', 'darkseagreen', 'darkslateblue', |
| 1680 |
'darkslategray', 'darkslategrey', 'darkturquoise', |
| 1681 |
'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', |
| 1682 |
'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite', |
| 1683 |
'forestgreen', 'gainsboro', 'ghostwhite', 'gold', |
| 1684 |
'goldenrod', 'greenyellow', 'grey', 'honeydew', 'hotpink', |
| 1685 |
'indianred', 'indigo', 'ivory', 'khaki', 'lavender', |
| 1686 |
'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', |
| 1687 |
'lightcoral', 'lightcyan', 'lightgoldenrodyellow', |
| 1688 |
'lightgray', 'lightgreen', 'lightgrey', 'lightpink', |
| 1689 |
'lightsalmon', 'lightseagreen', 'lightskyblue', |
| 1690 |
'lightslategray', 'lightslategrey', 'lightsteelblue', |
| 1691 |
'lightyellow', 'limegreen', 'linen', 'magenta', |
| 1692 |
'mediumaquamarine', 'mediumblue', 'mediumorchid', |
| 1693 |
'mediumpurple', 'mediumseagreen', 'mediumslateblue', |
| 1694 |
'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', |
| 1695 |
'midnightblue', 'mintcream', 'mistyrose', 'moccasin', |
| 1696 |
'navajowhite', 'oldlace', 'olivedrab', 'orange', |
| 1697 |
'orangered', 'orchid', 'palegoldenrod', 'palegreen', |
| 1698 |
'paleturquoise', 'palevioletred', 'papayawhip', |
| 1699 |
'peachpuff', 'peru', 'pink', 'plum', 'powderblue', |
| 1700 |
'rebeccapurple', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', |
| 1701 |
'sandybrown', 'seagreen', 'seashell', 'sienna', 'skyblue', |
| 1702 |
'slateblue', 'slategray', 'slategrey', 'snow', |
| 1703 |
'springgreen', 'steelblue', 'tan', 'thistle', 'tomato', |
| 1704 |
'turquoise', 'violet', 'wheat', 'whitesmoke', |
| 1705 |
'yellowgreen', |
| 1706 |
// Other keywords |
| 1707 |
'transparent', 'currentColor', |
| 1708 |
// System colors |
| 1709 |
'AccentColor', 'AccentColorText', 'ActiveText', |
| 1710 |
'ButtonBorder', 'ButtonFace', 'ButtonText', |
| 1711 |
'Canvas', 'CanvasText', 'Field', 'FieldText', |
| 1712 |
'GrayText', 'Highlight', 'HighlightText', |
| 1713 |
'LinkText', 'Mark', 'MarkText', |
| 1714 |
'SelectedItem', 'SelectedItemText', 'VisitedText', |
| 1715 |
] ); |
| 1716 |
} |
| 1717 |
|
| 1718 |
// endregion -- end of CSS Selectors Level 3 |
| 1719 |
|
| 1720 |
} |
| 1721 |
|
| 1722 |
/* |
| 1723 |
* This file uses VisualStudio style region/endregion fold markers which are |
| 1724 |
* recognised by PHPStorm. If modelines are enabled, the following editor |
| 1725 |
* configuration will also enable folding in vim, if it is in the last 5 lines |
| 1726 |
* of the file. We also use "@name" which creates sections in Doxygen. |
| 1727 |
* |
| 1728 |
* vim: foldmarker=//\ region,//\ endregion foldmethod=marker |
| 1729 |
*/ |