3 namespace Sabberworm\CSS\Value
;
5 use Sabberworm\CSS\Parsing\ParserState
;
6 use Sabberworm\CSS\Parsing\SourceException
;
8 class CSSString
extends PrimitiveValue
{
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();
20 if ($sBegin === "'") {
22 } else if ($sBegin === '"') {
25 if ($sQuote !== null) {
26 $oParserState->consume($sQuote);
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);
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();