| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sabberworm\CSS\Property; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Sabberworm\CSS\Comment\Comment; |
| 7 |
use WCPOS\Vendor\Sabberworm\CSS\Comment\Commentable; |
| 8 |
use WCPOS\Vendor\Sabberworm\CSS\Comment\CommentContainer; |
| 9 |
use WCPOS\Vendor\Sabberworm\CSS\CSSElement; |
| 10 |
use WCPOS\Vendor\Sabberworm\CSS\OutputFormat; |
| 11 |
use WCPOS\Vendor\Sabberworm\CSS\Parsing\ParserState; |
| 12 |
use WCPOS\Vendor\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 13 |
use WCPOS\Vendor\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 14 |
use WCPOS\Vendor\Sabberworm\CSS\Position\Position; |
| 15 |
use WCPOS\Vendor\Sabberworm\CSS\Position\Positionable; |
| 16 |
use WCPOS\Vendor\Sabberworm\CSS\ShortClassNameProvider; |
| 17 |
use WCPOS\Vendor\Sabberworm\CSS\Value\RuleValueList; |
| 18 |
use WCPOS\Vendor\Sabberworm\CSS\Value\Value; |
| 19 |
use function WCPOS\Vendor\Safe\preg_match; |
| 20 |
/** |
| 21 |
* `Declaration`s just have a string key (the property name) and a 'Value'. |
| 22 |
* |
| 23 |
* In CSS, `Declaration`s are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];” |
| 24 |
*/ |
| 25 |
class Declaration implements Commentable, CSSElement, Positionable |
| 26 |
{ |
| 27 |
use CommentContainer; |
| 28 |
use Position; |
| 29 |
use ShortClassNameProvider; |
| 30 |
/** |
| 31 |
* @var non-empty-string |
| 32 |
*/ |
| 33 |
private $propertyName; |
| 34 |
/** |
| 35 |
* @var RuleValueList|string|null |
| 36 |
*/ |
| 37 |
private $value; |
| 38 |
/** |
| 39 |
* @var bool |
| 40 |
*/ |
| 41 |
private $isImportant = \false; |
| 42 |
/** |
| 43 |
* @param non-empty-string $propertyName |
| 44 |
* @param int<1, max>|null $lineNumber |
| 45 |
* @param int<0, max>|null $columnNumber |
| 46 |
*/ |
| 47 |
public function __construct(string $propertyName, ?int $lineNumber = null, ?int $columnNumber = null) |
| 48 |
{ |
| 49 |
$this->propertyName = $propertyName; |
| 50 |
$this->setPosition($lineNumber, $columnNumber); |
| 51 |
} |
| 52 |
/** |
| 53 |
* @param list<Comment> $commentsBefore |
| 54 |
* |
| 55 |
* @throws UnexpectedEOFException |
| 56 |
* @throws UnexpectedTokenException |
| 57 |
* |
| 58 |
* @internal since V8.8.0 |
| 59 |
*/ |
| 60 |
public static function parse(ParserState $parserState, array $commentsBefore = []) : self |
| 61 |
{ |
| 62 |
$comments = $commentsBefore; |
| 63 |
$parserState->consumeWhiteSpace($comments); |
| 64 |
$declaration = new self($parserState->parseIdentifier(!$parserState->comes('--')), $parserState->currentLine(), $parserState->currentColumn()); |
| 65 |
$parserState->consumeWhiteSpace($comments); |
| 66 |
$declaration->setComments($comments); |
| 67 |
$parserState->consume(':'); |
| 68 |
$value = Value::parseValue($parserState, self::getDelimitersForPropertyValue($declaration->getPropertyName())); |
| 69 |
$declaration->setValue($value); |
| 70 |
$parserState->consumeWhiteSpace(); |
| 71 |
if ($parserState->comes('!')) { |
| 72 |
$parserState->consume('!'); |
| 73 |
$parserState->consumeWhiteSpace(); |
| 74 |
$parserState->consume('important'); |
| 75 |
$declaration->setIsImportant(\true); |
| 76 |
} |
| 77 |
$parserState->consumeWhiteSpace(); |
| 78 |
while ($parserState->comes(';')) { |
| 79 |
$parserState->consume(';'); |
| 80 |
} |
| 81 |
return $declaration; |
| 82 |
} |
| 83 |
/** |
| 84 |
* Returns a list of delimiters (or separators). |
| 85 |
* The first item is the innermost separator (or, put another way, the highest-precedence operator). |
| 86 |
* The sequence continues to the outermost separator (or lowest-precedence operator). |
| 87 |
* |
| 88 |
* @param non-empty-string $propertyName |
| 89 |
* |
| 90 |
* @return list<non-empty-string> |
| 91 |
*/ |
| 92 |
private static function getDelimitersForPropertyValue(string $propertyName) : array |
| 93 |
{ |
| 94 |
if (preg_match('/^font($|-)/', $propertyName) === 1) { |
| 95 |
return [',', '/', ' ']; |
| 96 |
} |
| 97 |
switch ($propertyName) { |
| 98 |
case 'src': |
| 99 |
return [' ', ',']; |
| 100 |
default: |
| 101 |
return [',', ' ', '/']; |
| 102 |
} |
| 103 |
} |
| 104 |
/** |
| 105 |
* @param non-empty-string $propertyName |
| 106 |
*/ |
| 107 |
public function setPropertyName(string $propertyName) : void |
| 108 |
{ |
| 109 |
$this->propertyName = $propertyName; |
| 110 |
} |
| 111 |
/** |
| 112 |
* @return non-empty-string |
| 113 |
*/ |
| 114 |
public function getPropertyName() : string |
| 115 |
{ |
| 116 |
return $this->propertyName; |
| 117 |
} |
| 118 |
/** |
| 119 |
* @param non-empty-string $propertyName |
| 120 |
* |
| 121 |
* @deprecated in v9.2, will be removed in v10.0; use `setPropertyName()` instead. |
| 122 |
*/ |
| 123 |
public function setRule(string $propertyName) : void |
| 124 |
{ |
| 125 |
$this->propertyName = $propertyName; |
| 126 |
} |
| 127 |
/** |
| 128 |
* @return non-empty-string |
| 129 |
* |
| 130 |
* @deprecated in v9.2, will be removed in v10.0; use `getPropertyName()` instead. |
| 131 |
*/ |
| 132 |
public function getRule() : string |
| 133 |
{ |
| 134 |
return $this->propertyName; |
| 135 |
} |
| 136 |
/** |
| 137 |
* @return RuleValueList|string|null |
| 138 |
*/ |
| 139 |
public function getValue() |
| 140 |
{ |
| 141 |
return $this->value; |
| 142 |
} |
| 143 |
/** |
| 144 |
* @param RuleValueList|string|null $value |
| 145 |
*/ |
| 146 |
public function setValue($value) : void |
| 147 |
{ |
| 148 |
$this->value = $value; |
| 149 |
} |
| 150 |
/** |
| 151 |
* Adds a value to the existing value. Value will be appended if a `RuleValueList` exists of the given type. |
| 152 |
* Otherwise, the existing value will be wrapped by one. |
| 153 |
* |
| 154 |
* @param RuleValueList|array<int, RuleValueList> $value |
| 155 |
*/ |
| 156 |
public function addValue($value, string $type = ' ') : void |
| 157 |
{ |
| 158 |
if (!\is_array($value)) { |
| 159 |
$value = [$value]; |
| 160 |
} |
| 161 |
if (!$this->value instanceof RuleValueList || $this->value->getListSeparator() !== $type) { |
| 162 |
$currentValue = $this->value; |
| 163 |
$this->value = new RuleValueList($type, $this->getLineNumber()); |
| 164 |
if ($currentValue !== null && $currentValue !== '') { |
| 165 |
$this->value->addListComponent($currentValue); |
| 166 |
} |
| 167 |
} |
| 168 |
foreach ($value as $valueItem) { |
| 169 |
$this->value->addListComponent($valueItem); |
| 170 |
} |
| 171 |
} |
| 172 |
public function setIsImportant(bool $isImportant) : void |
| 173 |
{ |
| 174 |
$this->isImportant = $isImportant; |
| 175 |
} |
| 176 |
public function getIsImportant() : bool |
| 177 |
{ |
| 178 |
return $this->isImportant; |
| 179 |
} |
| 180 |
/** |
| 181 |
* @return non-empty-string |
| 182 |
*/ |
| 183 |
public function render(OutputFormat $outputFormat) : string |
| 184 |
{ |
| 185 |
$formatter = $outputFormat->getFormatter(); |
| 186 |
$result = "{$formatter->comments($this)}{$this->propertyName}:{$formatter->spaceAfterRuleName()}"; |
| 187 |
if ($this->value instanceof Value) { |
| 188 |
// Can also be a ValueList |
| 189 |
$result .= $this->value->render($outputFormat); |
| 190 |
} else { |
| 191 |
$result .= $this->value; |
| 192 |
} |
| 193 |
if ($this->isImportant) { |
| 194 |
$result .= ' !important'; |
| 195 |
} |
| 196 |
$result .= ';'; |
| 197 |
return $result; |
| 198 |
} |
| 199 |
/** |
| 200 |
* @return array<string, bool|int|float|string|array<mixed>|null> |
| 201 |
* |
| 202 |
* @internal |
| 203 |
*/ |
| 204 |
public function getArrayRepresentation() : array |
| 205 |
{ |
| 206 |
return [ |
| 207 |
'class' => $this->getShortClassName(), |
| 208 |
'propertyName' => $this->propertyName, |
| 209 |
// We're using the term "property value" here to match the wording used in the specs: |
| 210 |
// https://www.w3.org/TR/CSS22/syndata.html#declaration |
| 211 |
'propertyValue' => $this->value, |
| 212 |
'important' => $this->isImportant, |
| 213 |
]; |
| 214 |
} |
| 215 |
} |
| 216 |
|