| 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 is a wrapper for quoted strings to distinguish them from keywords. |
| 15 |
* |
| 16 |
* `CSSString`s always output with double quotes. |
| 17 |
*/ |
| 18 |
class CSSString extends PrimitiveValue |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $string; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param int<1, max>|null $lineNumber |
| 27 |
*/ |
| 28 |
public function __construct(string $string, ?int $lineNumber = null) |
| 29 |
{ |
| 30 |
$this->string = $string; |
| 31 |
parent::__construct($lineNumber); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @throws SourceException |
| 36 |
* @throws UnexpectedEOFException |
| 37 |
* @throws UnexpectedTokenException |
| 38 |
* |
| 39 |
* @internal since V8.8.0 |
| 40 |
*/ |
| 41 |
public static function parse(ParserState $parserState): CSSString |
| 42 |
{ |
| 43 |
$begin = $parserState->peek(); |
| 44 |
$quote = null; |
| 45 |
if ($begin === "'") { |
| 46 |
$quote = "'"; |
| 47 |
} elseif ($begin === '"') { |
| 48 |
$quote = '"'; |
| 49 |
} |
| 50 |
if ($quote !== null) { |
| 51 |
$parserState->consume($quote); |
| 52 |
} |
| 53 |
$result = ''; |
| 54 |
$content = null; |
| 55 |
if ($quote === null) { |
| 56 |
// Unquoted strings end in whitespace or with braces, brackets, parentheses |
| 57 |
while (\preg_match('/[\\s{}()<>\\[\\]]/isu', $parserState->peek()) !== 1) { |
| 58 |
$result .= $parserState->parseCharacter(false); |
| 59 |
} |
| 60 |
} else { |
| 61 |
while (!$parserState->comes($quote)) { |
| 62 |
$content = $parserState->parseCharacter(false); |
| 63 |
if ($content === null) { |
| 64 |
throw new SourceException( |
| 65 |
"Non-well-formed quoted string {$parserState->peek(3)}", |
| 66 |
$parserState->currentLine() |
| 67 |
); |
| 68 |
} |
| 69 |
$result .= $content; |
| 70 |
} |
| 71 |
$parserState->consume($quote); |
| 72 |
} |
| 73 |
return new CSSString($result, $parserState->currentLine()); |
| 74 |
} |
| 75 |
|
| 76 |
public function setString(string $string): void |
| 77 |
{ |
| 78 |
$this->string = $string; |
| 79 |
} |
| 80 |
|
| 81 |
public function getString(): string |
| 82 |
{ |
| 83 |
return $this->string; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return non-empty-string |
| 88 |
*/ |
| 89 |
public function render(OutputFormat $outputFormat): string |
| 90 |
{ |
| 91 |
$string = \addslashes($this->string); |
| 92 |
$string = \str_replace("\n", '\\A', $string); |
| 93 |
return $outputFormat->getStringQuotingType() . $string . $outputFormat->getStringQuotingType(); |
| 94 |
} |
| 95 |
} |
| 96 |
|