| 1 |
<?php |
| 2 |
|
| 3 |
namespace Automattic\WooCommerce\Vendor\Sabberworm\CSS\Value; |
| 4 |
|
| 5 |
use Automattic\WooCommerce\Vendor\Sabberworm\CSS\OutputFormat; |
| 6 |
use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\ParserState; |
| 7 |
use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\SourceException; |
| 8 |
use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 9 |
use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 10 |
|
| 11 |
/** |
| 12 |
* This class is a wrapper for quoted strings to distinguish them from keywords. |
| 13 |
* |
| 14 |
* `CSSString`s always output with double quotes. |
| 15 |
*/ |
| 16 |
class CSSString extends PrimitiveValue |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $sString; |
| 22 |
|
| 23 |
/** |
| 24 |
* @param string $sString |
| 25 |
* @param int $iLineNo |
| 26 |
*/ |
| 27 |
public function __construct($sString, $iLineNo = 0) |
| 28 |
{ |
| 29 |
$this->sString = $sString; |
| 30 |
parent::__construct($iLineNo); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @return CSSString |
| 35 |
* |
| 36 |
* @throws SourceException |
| 37 |
* @throws UnexpectedEOFException |
| 38 |
* @throws UnexpectedTokenException |
| 39 |
* |
| 40 |
* @internal since V8.8.0 |
| 41 |
*/ |
| 42 |
public static function parse(ParserState $oParserState) |
| 43 |
{ |
| 44 |
$sBegin = $oParserState->peek(); |
| 45 |
$sQuote = null; |
| 46 |
if ($sBegin === "'") { |
| 47 |
$sQuote = "'"; |
| 48 |
} elseif ($sBegin === '"') { |
| 49 |
$sQuote = '"'; |
| 50 |
} |
| 51 |
if ($sQuote !== null) { |
| 52 |
$oParserState->consume($sQuote); |
| 53 |
} |
| 54 |
$sResult = ""; |
| 55 |
$sContent = null; |
| 56 |
if ($sQuote === null) { |
| 57 |
// Unquoted strings end in whitespace or with braces, brackets, parentheses |
| 58 |
while (!preg_match('/[\\s{}()<>\\[\\]]/isu', $oParserState->peek())) { |
| 59 |
$sResult .= $oParserState->parseCharacter(false); |
| 60 |
} |
| 61 |
} else { |
| 62 |
while (!$oParserState->comes($sQuote)) { |
| 63 |
$sContent = $oParserState->parseCharacter(false); |
| 64 |
if ($sContent === null) { |
| 65 |
throw new SourceException( |
| 66 |
"Non-well-formed quoted string {$oParserState->peek(3)}", |
| 67 |
$oParserState->currentLine() |
| 68 |
); |
| 69 |
} |
| 70 |
$sResult .= $sContent; |
| 71 |
} |
| 72 |
$oParserState->consume($sQuote); |
| 73 |
} |
| 74 |
return new CSSString($sResult, $oParserState->currentLine()); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param string $sString |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function setString($sString) |
| 83 |
{ |
| 84 |
$this->sString = $sString; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @return string |
| 89 |
*/ |
| 90 |
public function getString() |
| 91 |
{ |
| 92 |
return $this->sString; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return string |
| 97 |
* |
| 98 |
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead. |
| 99 |
*/ |
| 100 |
public function __toString() |
| 101 |
{ |
| 102 |
return $this->render(new OutputFormat()); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @param OutputFormat|null $oOutputFormat |
| 107 |
* |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
public function render($oOutputFormat) |
| 111 |
{ |
| 112 |
$sString = addslashes($this->sString); |
| 113 |
$sString = str_replace("\n", '\A', $sString); |
| 114 |
return $oOutputFormat->getStringQuotingType() . $sString . $oOutputFormat->getStringQuotingType(); |
| 115 |
} |
| 116 |
} |
| 117 |
|