AtRule.php
3 years ago
CSSNamespace.php
3 years ago
Charset.php
3 years ago
Import.php
3 years ago
KeyframeSelector.php
3 years ago
Selector.php
3 years ago
Selector.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Sabberworm\CSS\Property; |
| 4 | |
| 5 | /** |
| 6 | * Class representing a single CSS selector. Selectors have to be split by the comma prior to being passed into this |
| 7 | * class. |
| 8 | */ |
| 9 | class Selector |
| 10 | { |
| 11 | /** |
| 12 | * regexp for specificity calculations |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | const NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX = '/ |
| 17 | (\\.[\\w]+) # classes |
| 18 | | |
| 19 | \\[(\\w+) # attributes |
| 20 | | |
| 21 | (\\:( # pseudo classes |
| 22 | link|visited|active |
| 23 | |hover|focus |
| 24 | |lang |
| 25 | |target |
| 26 | |enabled|disabled|checked|indeterminate |
| 27 | |root |
| 28 | |nth-child|nth-last-child|nth-of-type|nth-last-of-type |
| 29 | |first-child|last-child|first-of-type|last-of-type |
| 30 | |only-child|only-of-type |
| 31 | |empty|contains |
| 32 | )) |
| 33 | /ix'; |
| 34 | /** |
| 35 | * regexp for specificity calculations |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | const ELEMENTS_AND_PSEUDO_ELEMENTS_RX = '/ |
| 40 | ((^|[\\s\\+\\>\\~]+)[\\w]+ # elements |
| 41 | | |
| 42 | \\:{1,2}( # pseudo-elements |
| 43 | after|before|first-letter|first-line|selection |
| 44 | )) |
| 45 | /ix'; |
| 46 | /** |
| 47 | * regexp for specificity calculations |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
| 51 | const SELECTOR_VALIDATION_RX = '/ |
| 52 | ^( |
| 53 | (?: |
| 54 | [a-zA-Z0-9\\x{00A0}-\\x{FFFF}_^$|*="\'~\\[\\]()\\-\\s\\.:#+>]* # any sequence of valid unescaped characters |
| 55 | (?:\\\\.)? # a single escaped character |
| 56 | (?:([\'"]).*?(?<!\\\\)\\2)? # a quoted text like [id="example"] |
| 57 | )* |
| 58 | )$ |
| 59 | /ux'; |
| 60 | /** |
| 61 | * @var string |
| 62 | */ |
| 63 | private $sSelector; |
| 64 | /** |
| 65 | * @var int|null |
| 66 | */ |
| 67 | private $iSpecificity; |
| 68 | /** |
| 69 | * @param string $sSelector |
| 70 | * |
| 71 | * @return bool |
| 72 | */ |
| 73 | public static function isValid($sSelector) |
| 74 | { |
| 75 | return \preg_match(static::SELECTOR_VALIDATION_RX, $sSelector); |
| 76 | } |
| 77 | /** |
| 78 | * @param string $sSelector |
| 79 | * @param bool $bCalculateSpecificity |
| 80 | */ |
| 81 | public function __construct($sSelector, $bCalculateSpecificity = \false) |
| 82 | { |
| 83 | $this->setSelector($sSelector); |
| 84 | if ($bCalculateSpecificity) { |
| 85 | $this->getSpecificity(); |
| 86 | } |
| 87 | } |
| 88 | /** |
| 89 | * @return string |
| 90 | */ |
| 91 | public function getSelector() |
| 92 | { |
| 93 | return $this->sSelector; |
| 94 | } |
| 95 | /** |
| 96 | * @param string $sSelector |
| 97 | * |
| 98 | * @return void |
| 99 | */ |
| 100 | public function setSelector($sSelector) |
| 101 | { |
| 102 | $this->sSelector = \trim($sSelector); |
| 103 | $this->iSpecificity = null; |
| 104 | } |
| 105 | /** |
| 106 | * @return string |
| 107 | */ |
| 108 | public function __toString() |
| 109 | { |
| 110 | return $this->getSelector(); |
| 111 | } |
| 112 | /** |
| 113 | * @return int |
| 114 | */ |
| 115 | public function getSpecificity() |
| 116 | { |
| 117 | if ($this->iSpecificity === null) { |
| 118 | $a = 0; |
| 119 | /// @todo should exclude \# as well as "#" |
| 120 | $aMatches = null; |
| 121 | $b = \substr_count($this->sSelector, '#'); |
| 122 | $c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->sSelector, $aMatches); |
| 123 | $d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->sSelector, $aMatches); |
| 124 | $this->iSpecificity = $a * 1000 + $b * 100 + $c * 10 + $d; |
| 125 | } |
| 126 | return $this->iSpecificity; |
| 127 | } |
| 128 | } |
| 129 |