Skip to content

Commit

Permalink
Parse escape quoted values
Browse files Browse the repository at this point in the history
  • Loading branch information
petkodimitrov committed Mar 28, 2024
1 parent eda7c24 commit aa00e4c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/Value/CSSString.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
class CSSString extends PrimitiveValue
{
const PARSE_QUOTE_STRINGS = ['"', "'", '\"', "\'"];

/**
* @var string
*/
Expand All @@ -40,11 +42,12 @@ public function __construct($sString, $iLineNo = 0)
public static function parse(ParserState $oParserState)
{
$sBegin = $oParserState->peek();
if ($sBegin === '\\') {
$sBegin = $oParserState->peek(2);
}
$sQuote = null;
if ($sBegin === "'") {
$sQuote = "'";
} elseif ($sBegin === '"') {
$sQuote = '"';
if (in_array($sBegin, self::PARSE_QUOTE_STRINGS)) {
$sQuote = $sBegin;
}
if ($sQuote !== null) {
$oParserState->consume($sQuote);
Expand Down
33 changes: 26 additions & 7 deletions src/Value/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
abstract class Value implements Renderable
{
const PARSE_TERMINATE_STRINGS = ['}',';','!',')', '\\'];
const PARSE_QUOTE_STRINGS = ['"', "'", '\"', "\'"];

/**
* @var int
*/
Expand Down Expand Up @@ -41,12 +44,7 @@ public static function parseValue(ParserState $oParserState, array $aListDelimit
$aStack = [];
$oParserState->consumeWhiteSpace();
//Build a list of delimiters and parsed values
while (
!($oParserState->comes('}') || $oParserState->comes(';') || $oParserState->comes('!')
|| $oParserState->comes(')')
|| $oParserState->comes('\\')
|| $oParserState->isEnd())
) {
while (self::continueParsing($oParserState)) {
if (count($aStack) > 0) {
$bFoundDelimiter = false;
foreach ($aListDelimiters as $sDelimiter) {
Expand Down Expand Up @@ -154,7 +152,10 @@ public static function parsePrimitiveValue(ParserState $oParserState)
$oValue = Size::parse($oParserState);
} elseif ($oParserState->comes('#') || $oParserState->comes('rgb', true) || $oParserState->comes('hsl', true)) {
$oValue = Color::parse($oParserState);
} elseif ($oParserState->comes("'") || $oParserState->comes('"')) {
} elseif (
in_array($oParserState->peek(), self::PARSE_QUOTE_STRINGS)
|| in_array($oParserState->peek(2), self::PARSE_QUOTE_STRINGS)
) {
$oValue = CSSString::parse($oParserState);
} elseif ($oParserState->comes("progid:") && $oParserState->getSettings()->bLenientParsing) {
$oValue = self::parseMicrosoftFilter($oParserState);
Expand Down Expand Up @@ -209,4 +210,22 @@ public function getLineNo()
{
return $this->iLineNo;
}

/**
* @return bool
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
private static function continueParsing(ParserState $oParserState)
{
if ($oParserState->isEnd()) {
return false;
}
$sPeekOne = $oParserState->peek();
if ($sPeekOne === '\\') {
return in_array($oParserState->peek(2), self::PARSE_QUOTE_STRINGS);
}
return !in_array($sPeekOne, self::PARSE_TERMINATE_STRINGS);
}
}

0 comments on commit aa00e4c

Please sign in to comment.