| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sabberworm\CSS\Value; |
| 4 |
|
| 5 |
use Sabberworm\CSS\OutputFormat; |
| 6 |
use Sabberworm\CSS\Parsing\ParserState; |
| 7 |
use Sabberworm\CSS\Parsing\SourceException; |
| 8 |
use Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 9 |
use Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 10 |
|
| 11 |
/** |
| 12 |
* This class represents URLs in CSS. `URL`s always output in `URL("")` notation. |
| 13 |
*/ |
| 14 |
class URL extends PrimitiveValue |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var CSSString |
| 18 |
*/ |
| 19 |
private $oURL; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param int $iLineNo |
| 23 |
*/ |
| 24 |
public function __construct(CSSString $oURL, $iLineNo = 0) |
| 25 |
{ |
| 26 |
parent::__construct($iLineNo); |
| 27 |
$this->oURL = $oURL; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @return URL |
| 32 |
* |
| 33 |
* @throws SourceException |
| 34 |
* @throws UnexpectedEOFException |
| 35 |
* @throws UnexpectedTokenException |
| 36 |
* |
| 37 |
* @internal since V8.8.0 |
| 38 |
*/ |
| 39 |
public static function parse(ParserState $oParserState) |
| 40 |
{ |
| 41 |
$oAnchor = $oParserState->anchor(); |
| 42 |
$sIdentifier = ''; |
| 43 |
for ($i = 0; $i < 3; $i++) { |
| 44 |
$sChar = $oParserState->parseCharacter(true); |
| 45 |
if ($sChar === null) { |
| 46 |
break; |
| 47 |
} |
| 48 |
$sIdentifier .= $sChar; |
| 49 |
} |
| 50 |
$bUseUrl = $oParserState->streql($sIdentifier, 'url'); |
| 51 |
if ($bUseUrl) { |
| 52 |
$oParserState->consumeWhiteSpace(); |
| 53 |
$oParserState->consume('('); |
| 54 |
} else { |
| 55 |
$oAnchor->backtrack(); |
| 56 |
} |
| 57 |
$oParserState->consumeWhiteSpace(); |
| 58 |
$oResult = new URL(CSSString::parse($oParserState), $oParserState->currentLine()); |
| 59 |
if ($bUseUrl) { |
| 60 |
$oParserState->consumeWhiteSpace(); |
| 61 |
$oParserState->consume(')'); |
| 62 |
} |
| 63 |
return $oResult; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function setURL(CSSString $oURL) |
| 70 |
{ |
| 71 |
$this->oURL = $oURL; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @return CSSString |
| 76 |
*/ |
| 77 |
public function getURL() |
| 78 |
{ |
| 79 |
return $this->oURL; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @return string |
| 84 |
* |
| 85 |
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead. |
| 86 |
*/ |
| 87 |
public function __toString() |
| 88 |
{ |
| 89 |
return $this->render(new OutputFormat()); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @param OutputFormat|null $oOutputFormat |
| 94 |
* |
| 95 |
* @return string |
| 96 |
*/ |
| 97 |
public function render($oOutputFormat) |
| 98 |
{ |
| 99 |
return "url({$this->oURL->render($oOutputFormat)})"; |
| 100 |
} |
| 101 |
} |
| 102 |
|