Merge branch 'MDL-65744' of git://github.com/Chocolate-lightning/moodle
[moodle.git] / lib / php-css-parser / Value / CSSString.php
blob9f9c050e97c89b35edc9299e15b8d2b2198cb4ce
1 <?php
3 namespace Sabberworm\CSS\Value;
5 use Sabberworm\CSS\Parsing\ParserState;
6 use Sabberworm\CSS\Parsing\SourceException;
8 class CSSString extends PrimitiveValue {
10 private $sString;
12 public function __construct($sString, $iLineNo = 0) {
13 $this->sString = $sString;
14 parent::__construct($iLineNo);
17 public static function parse(ParserState $oParserState) {
18 $sBegin = $oParserState->peek();
19 $sQuote = null;
20 if ($sBegin === "'") {
21 $sQuote = "'";
22 } else if ($sBegin === '"') {
23 $sQuote = '"';
25 if ($sQuote !== null) {
26 $oParserState->consume($sQuote);
28 $sResult = "";
29 $sContent = null;
30 if ($sQuote === null) {
31 // Unquoted strings end in whitespace or with braces, brackets, parentheses
32 while (!preg_match('/[\\s{}()<>\\[\\]]/isu', $oParserState->peek())) {
33 $sResult .= $oParserState->parseCharacter(false);
35 } else {
36 while (!$oParserState->comes($sQuote)) {
37 $sContent = $oParserState->parseCharacter(false);
38 if ($sContent === null) {
39 throw new SourceException("Non-well-formed quoted string {$oParserState->peek(3)}", $oParserState->currentLine());
41 $sResult .= $sContent;
43 $oParserState->consume($sQuote);
45 return new CSSString($sResult, $oParserState->currentLine());
48 public function setString($sString) {
49 $this->sString = $sString;
52 public function getString() {
53 return $this->sString;
56 public function __toString() {
57 return $this->render(new \Sabberworm\CSS\OutputFormat());
60 public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
61 $sString = addslashes($this->sString);
62 $sString = str_replace("\n", '\A', $sString);
63 return $oOutputFormat->getStringQuotingType() . $sString . $oOutputFormat->getStringQuotingType();