| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace AcyMailing\Libraries\Sabberworm\CSS\Value; |
| 6 |
|
| 7 |
use AcyMailing\Libraries\Sabberworm\CSS\OutputFormat; |
| 8 |
use AcyMailing\Libraries\Sabberworm\CSS\Parsing\ParserState; |
| 9 |
use AcyMailing\Libraries\Sabberworm\CSS\Parsing\SourceException; |
| 10 |
use AcyMailing\Libraries\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 11 |
use AcyMailing\Libraries\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 12 |
|
| 13 |
/** |
| 14 |
* This class represents URLs in CSS. `URL`s always output in `URL("")` notation. |
| 15 |
*/ |
| 16 |
class URL extends PrimitiveValue |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @var CSSString |
| 20 |
*/ |
| 21 |
private $url; |
| 22 |
|
| 23 |
/** |
| 24 |
* @param int<1, max>|null $lineNumber |
| 25 |
*/ |
| 26 |
public function __construct(CSSString $url, ?int $lineNumber = null) |
| 27 |
{ |
| 28 |
parent::__construct($lineNumber); |
| 29 |
$this->url = $url; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @throws SourceException |
| 34 |
* @throws UnexpectedEOFException |
| 35 |
* @throws UnexpectedTokenException |
| 36 |
* |
| 37 |
* @internal since V8.8.0 |
| 38 |
*/ |
| 39 |
public static function parse(ParserState $parserState): URL |
| 40 |
{ |
| 41 |
$anchor = $parserState->anchor(); |
| 42 |
$identifier = ''; |
| 43 |
for ($i = 0; $i < 3; $i++) { |
| 44 |
$character = $parserState->parseCharacter(true); |
| 45 |
if ($character === null) { |
| 46 |
break; |
| 47 |
} |
| 48 |
$identifier .= $character; |
| 49 |
} |
| 50 |
$useUrl = $parserState->streql($identifier, 'url'); |
| 51 |
if ($useUrl) { |
| 52 |
$parserState->consumeWhiteSpace(); |
| 53 |
$parserState->consume('('); |
| 54 |
} else { |
| 55 |
$anchor->backtrack(); |
| 56 |
} |
| 57 |
$parserState->consumeWhiteSpace(); |
| 58 |
$result = new URL(CSSString::parse($parserState), $parserState->currentLine()); |
| 59 |
if ($useUrl) { |
| 60 |
$parserState->consumeWhiteSpace(); |
| 61 |
$parserState->consume(')'); |
| 62 |
} |
| 63 |
return $result; |
| 64 |
} |
| 65 |
|
| 66 |
public function setURL(CSSString $url): void |
| 67 |
{ |
| 68 |
$this->url = $url; |
| 69 |
} |
| 70 |
|
| 71 |
public function getURL(): CSSString |
| 72 |
{ |
| 73 |
return $this->url; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @return non-empty-string |
| 78 |
*/ |
| 79 |
public function render(OutputFormat $outputFormat): string |
| 80 |
{ |
| 81 |
return "url({$this->url->render($outputFormat)})"; |
| 82 |
} |
| 83 |
} |
| 84 |
|