| 1 |
<?php |
| 2 |
|
| 3 |
namespace 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 |
* @internal |
| 17 |
*/ |
| 18 |
const NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX = '/ |
| 19 |
(\.[\w]+) # classes |
| 20 |
| |
| 21 |
\[(\w+) # attributes |
| 22 |
| |
| 23 |
(\:( # pseudo classes |
| 24 |
link|visited|active |
| 25 |
|hover|focus |
| 26 |
|lang |
| 27 |
|target |
| 28 |
|enabled|disabled|checked|indeterminate |
| 29 |
|root |
| 30 |
|nth-child|nth-last-child|nth-of-type|nth-last-of-type |
| 31 |
|first-child|last-child|first-of-type|last-of-type |
| 32 |
|only-child|only-of-type |
| 33 |
|empty|contains |
| 34 |
)) |
| 35 |
/ix'; |
| 36 |
|
| 37 |
/** |
| 38 |
* regexp for specificity calculations |
| 39 |
* |
| 40 |
* @var string |
| 41 |
* |
| 42 |
* @internal |
| 43 |
*/ |
| 44 |
const ELEMENTS_AND_PSEUDO_ELEMENTS_RX = '/ |
| 45 |
((^|[\s\+\>\~]+)[\w]+ # elements |
| 46 |
| |
| 47 |
\:{1,2}( # pseudo-elements |
| 48 |
after|before|first-letter|first-line|selection |
| 49 |
)) |
| 50 |
/ix'; |
| 51 |
|
| 52 |
/** |
| 53 |
* regexp for specificity calculations |
| 54 |
* |
| 55 |
* @var string |
| 56 |
* |
| 57 |
* @internal since 8.5.2 |
| 58 |
*/ |
| 59 |
const SELECTOR_VALIDATION_RX = '/ |
| 60 |
^( |
| 61 |
(?: |
| 62 |
[a-zA-Z0-9\x{00A0}-\x{FFFF}_^$|*="\'~\[\]()\-\s\.:#+>]* # any sequence of valid unescaped characters |
| 63 |
(?:\\\\.)? # a single escaped character |
| 64 |
(?:([\'"]).*?(?<!\\\\)\2)? # a quoted text like [id="example"] |
| 65 |
)* |
| 66 |
)$ |
| 67 |
/ux'; |
| 68 |
|
| 69 |
/** |
| 70 |
* @var string |
| 71 |
*/ |
| 72 |
private $sSelector; |
| 73 |
|
| 74 |
/** |
| 75 |
* @var int|null |
| 76 |
*/ |
| 77 |
private $iSpecificity; |
| 78 |
|
| 79 |
/** |
| 80 |
* @param string $sSelector |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
* |
| 84 |
* @internal since V8.8.0 |
| 85 |
*/ |
| 86 |
public static function isValid($sSelector) |
| 87 |
{ |
| 88 |
return preg_match(static::SELECTOR_VALIDATION_RX, $sSelector); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param string $sSelector |
| 93 |
* @param bool $bCalculateSpecificity @deprecated since V8.8.0, will be removed in V9.0.0 |
| 94 |
*/ |
| 95 |
public function __construct($sSelector, $bCalculateSpecificity = false) |
| 96 |
{ |
| 97 |
$this->setSelector($sSelector); |
| 98 |
if ($bCalculateSpecificity) { |
| 99 |
$this->getSpecificity(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function getSelector() |
| 107 |
{ |
| 108 |
return $this->sSelector; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @param string $sSelector |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function setSelector($sSelector) |
| 117 |
{ |
| 118 |
$this->sSelector = trim($sSelector); |
| 119 |
$this->iSpecificity = null; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @return string |
| 124 |
* |
| 125 |
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead. |
| 126 |
*/ |
| 127 |
public function __toString() |
| 128 |
{ |
| 129 |
return $this->getSelector(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @return int |
| 134 |
*/ |
| 135 |
public function getSpecificity() |
| 136 |
{ |
| 137 |
if ($this->iSpecificity === null) { |
| 138 |
$a = 0; |
| 139 |
/// @todo should exclude \# as well as "#" |
| 140 |
$aMatches = null; |
| 141 |
$b = substr_count($this->sSelector, '#'); |
| 142 |
$c = preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->sSelector, $aMatches); |
| 143 |
$d = preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->sSelector, $aMatches); |
| 144 |
$this->iSpecificity = ($a * 1000) + ($b * 100) + ($c * 10) + $d; |
| 145 |
} |
| 146 |
return $this->iSpecificity; |
| 147 |
} |
| 148 |
} |
| 149 |
|