Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace Sabberworm\CSS\Value;
4
 
5
use Sabberworm\CSS\OutputFormat;
6
use Sabberworm\CSS\Parsing\ParserState;
7
use Sabberworm\CSS\Parsing\SourceException;
8
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
9
use 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
    public static function parse(ParserState $oParserState)
41
    {
42
        $sBegin = $oParserState->peek();
43
        $sQuote = null;
44
        if ($sBegin === "'") {
45
            $sQuote = "'";
46
        } elseif ($sBegin === '"') {
47
            $sQuote = '"';
48
        }
49
        if ($sQuote !== null) {
50
            $oParserState->consume($sQuote);
51
        }
52
        $sResult = "";
53
        $sContent = null;
54
        if ($sQuote === null) {
55
            // Unquoted strings end in whitespace or with braces, brackets, parentheses
56
            while (!preg_match('/[\\s{}()<>\\[\\]]/isu', $oParserState->peek())) {
57
                $sResult .= $oParserState->parseCharacter(false);
58
            }
59
        } else {
60
            while (!$oParserState->comes($sQuote)) {
61
                $sContent = $oParserState->parseCharacter(false);
62
                if ($sContent === null) {
63
                    throw new SourceException(
64
                        "Non-well-formed quoted string {$oParserState->peek(3)}",
65
                        $oParserState->currentLine()
66
                    );
67
                }
68
                $sResult .= $sContent;
69
            }
70
            $oParserState->consume($sQuote);
71
        }
72
        return new CSSString($sResult, $oParserState->currentLine());
73
    }
74
 
75
    /**
76
     * @param string $sString
77
     *
78
     * @return void
79
     */
80
    public function setString($sString)
81
    {
82
        $this->sString = $sString;
83
    }
84
 
85
    /**
86
     * @return string
87
     */
88
    public function getString()
89
    {
90
        return $this->sString;
91
    }
92
 
93
    /**
94
     * @return string
95
     */
96
    public function __toString()
97
    {
98
        return $this->render(new OutputFormat());
99
    }
100
 
101
    /**
102
     * @param OutputFormat|null $oOutputFormat
103
     *
104
     * @return string
105
     */
106
    public function render($oOutputFormat)
107
    {
108
        $sString = addslashes($this->sString);
109
        $sString = str_replace("\n", '\A', $sString);
110
        return $oOutputFormat->getStringQuotingType() . $sString . $oOutputFormat->getStringQuotingType();
111
    }
112
}