CSSFunction.php
5 days ago
CSSString.php
5 days ago
CalcFunction.php
5 days ago
CalcRuleValueList.php
5 days ago
Color.php
5 days ago
LineName.php
5 days ago
PrimitiveValue.php
5 days ago
RuleValueList.php
5 days ago
Size.php
5 days ago
URL.php
5 days ago
Value.php
5 days ago
ValueList.php
5 days ago
URL.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace ProfilePressVendor\Sabberworm\CSS\Value; |
| 5 | |
| 6 | use ProfilePressVendor\Sabberworm\CSS\OutputFormat; |
| 7 | use ProfilePressVendor\Sabberworm\CSS\Parsing\ParserState; |
| 8 | use ProfilePressVendor\Sabberworm\CSS\Parsing\SourceException; |
| 9 | use ProfilePressVendor\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 10 | use ProfilePressVendor\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 11 | use ProfilePressVendor\Sabberworm\CSS\ShortClassNameProvider; |
| 12 | /** |
| 13 | * This class represents URLs in CSS. `URL`s always output in `URL("")` notation. |
| 14 | */ |
| 15 | class URL extends PrimitiveValue |
| 16 | { |
| 17 | use ShortClassNameProvider; |
| 18 | /** |
| 19 | * @var CSSString |
| 20 | */ |
| 21 | private $url; |
| 22 | /** |
| 23 | * @param int<1, max>|null $lineNumber |
| 24 | */ |
| 25 | public function __construct(CSSString $url, ?int $lineNumber = null) |
| 26 | { |
| 27 | parent::__construct($lineNumber); |
| 28 | $this->url = $url; |
| 29 | } |
| 30 | /** |
| 31 | * @throws SourceException |
| 32 | * @throws UnexpectedEOFException |
| 33 | * @throws UnexpectedTokenException |
| 34 | * |
| 35 | * @internal since V8.8.0 |
| 36 | */ |
| 37 | public static function parse(ParserState $parserState): URL |
| 38 | { |
| 39 | $anchor = $parserState->anchor(); |
| 40 | $identifier = ''; |
| 41 | for ($i = 0; $i < 3; $i++) { |
| 42 | $character = $parserState->parseCharacter(\true); |
| 43 | if ($character === null) { |
| 44 | break; |
| 45 | } |
| 46 | $identifier .= $character; |
| 47 | } |
| 48 | $useUrl = $parserState->streql($identifier, 'url'); |
| 49 | if ($useUrl) { |
| 50 | $parserState->consumeWhiteSpace(); |
| 51 | $parserState->consume('('); |
| 52 | } else { |
| 53 | $anchor->backtrack(); |
| 54 | } |
| 55 | $parserState->consumeWhiteSpace(); |
| 56 | $result = new URL(CSSString::parse($parserState), $parserState->currentLine()); |
| 57 | if ($useUrl) { |
| 58 | $parserState->consumeWhiteSpace(); |
| 59 | $parserState->consume(')'); |
| 60 | } |
| 61 | return $result; |
| 62 | } |
| 63 | public function setURL(CSSString $url): void |
| 64 | { |
| 65 | $this->url = $url; |
| 66 | } |
| 67 | public function getURL(): CSSString |
| 68 | { |
| 69 | return $this->url; |
| 70 | } |
| 71 | /** |
| 72 | * @return non-empty-string |
| 73 | */ |
| 74 | public function render(OutputFormat $outputFormat): string |
| 75 | { |
| 76 | return "url({$this->url->render($outputFormat)})"; |
| 77 | } |
| 78 | /** |
| 79 | * @return array<string, bool|int|float|string|array<mixed>|null> |
| 80 | * |
| 81 | * @internal |
| 82 | */ |
| 83 | public function getArrayRepresentation(): array |
| 84 | { |
| 85 | return [ |
| 86 | 'class' => $this->getShortClassName(), |
| 87 | // We're using the term "uri" here to match the wording used in the specs: |
| 88 | // https://www.w3.org/TR/CSS22/syndata.html#uri |
| 89 | 'uri' => $this->url->getArrayRepresentation(), |
| 90 | ]; |
| 91 | } |
| 92 | } |
| 93 |