CSSFunction.php
2 years ago
CSSString.php
4 years ago
CalcFunction.php
2 years ago
CalcRuleValueList.php
4 years ago
Color.php
2 years ago
LineName.php
4 years ago
PrimitiveValue.php
4 years ago
RuleValueList.php
4 years ago
Size.php
2 years ago
URL.php
2 years ago
Value.php
2 years ago
ValueList.php
4 years ago
index.php
3 years ago
CSSString.php
67 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Sabberworm\CSS\Value; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Sabberworm\CSS\OutputFormat; |
| 5 | use MailPoetVendor\Sabberworm\CSS\Parsing\ParserState; |
| 6 | use MailPoetVendor\Sabberworm\CSS\Parsing\SourceException; |
| 7 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 8 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 9 | class CSSString extends PrimitiveValue |
| 10 | { |
| 11 | private $sString; |
| 12 | public function __construct($sString, $iLineNo = 0) |
| 13 | { |
| 14 | $this->sString = $sString; |
| 15 | parent::__construct($iLineNo); |
| 16 | } |
| 17 | public static function parse(ParserState $oParserState) |
| 18 | { |
| 19 | $sBegin = $oParserState->peek(); |
| 20 | $sQuote = null; |
| 21 | if ($sBegin === "'") { |
| 22 | $sQuote = "'"; |
| 23 | } elseif ($sBegin === '"') { |
| 24 | $sQuote = '"'; |
| 25 | } |
| 26 | if ($sQuote !== null) { |
| 27 | $oParserState->consume($sQuote); |
| 28 | } |
| 29 | $sResult = ""; |
| 30 | $sContent = null; |
| 31 | if ($sQuote === null) { |
| 32 | // Unquoted strings end in whitespace or with braces, brackets, parentheses |
| 33 | while (!\preg_match('/[\\s{}()<>\\[\\]]/isu', $oParserState->peek())) { |
| 34 | $sResult .= $oParserState->parseCharacter(\false); |
| 35 | } |
| 36 | } else { |
| 37 | while (!$oParserState->comes($sQuote)) { |
| 38 | $sContent = $oParserState->parseCharacter(\false); |
| 39 | if ($sContent === null) { |
| 40 | throw new SourceException("Non-well-formed quoted string {$oParserState->peek(3)}", $oParserState->currentLine()); |
| 41 | } |
| 42 | $sResult .= $sContent; |
| 43 | } |
| 44 | $oParserState->consume($sQuote); |
| 45 | } |
| 46 | return new CSSString($sResult, $oParserState->currentLine()); |
| 47 | } |
| 48 | public function setString($sString) |
| 49 | { |
| 50 | $this->sString = $sString; |
| 51 | } |
| 52 | public function getString() |
| 53 | { |
| 54 | return $this->sString; |
| 55 | } |
| 56 | public function __toString() |
| 57 | { |
| 58 | return $this->render(new OutputFormat()); |
| 59 | } |
| 60 | public function render(OutputFormat $oOutputFormat) |
| 61 | { |
| 62 | $sString = \addslashes($this->sString); |
| 63 | $sString = \str_replace("\n", '\\A', $sString); |
| 64 | return $oOutputFormat->getStringQuotingType() . $sString . $oOutputFormat->getStringQuotingType(); |
| 65 | } |
| 66 | } |
| 67 |