Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaped quotes parsing #646

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -38,11 +40,12 @@ public function __construct($sString, $iLineNo = 0)
public static function parse(ParserState $oParserState): CSSString
{
$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
35 changes: 27 additions & 8 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,13 +44,8 @@ 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())
) {
if (\count($aStack) > 0) {
while (self::continueParsing($oParserState)) {
if (count($aStack) > 0) {
$bFoundDelimiter = false;
foreach ($aListDelimiters as $sDelimiter) {
if ($oParserState->comes($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 @@ -214,4 +215,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);
}
}
31 changes: 31 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,37 @@ public function lonelyImport(): void
self::assertSame($sExpected, $oDoc->render());
}

/**
* @test
*/
public function parseForEscapedQuotes()
{
$preParseCss = sprintf(
"%s%s%s%s%s%s%s",
'.fonts-first {font-family: Roboto, "Fira Mono", \"Liberation Serif\";}',
PHP_EOL,
".font-second {font-family: Roboto, 'Fira Mono', \'Liberation Serif\';}",
PHP_EOL,
'.bgpic-first {background-image: url(\"pic.webp\");}',
PHP_EOL,
".bgpic-second {background-image: url(\'pic.webp\');}"
);
$expectedCss = sprintf(
"%s%s%s%s%s%s%s",
'.fonts-first {font-family: Roboto,"Fira Mono","Liberation Serif";}',
PHP_EOL,
'.font-second {font-family: Roboto,"Fira Mono","Liberation Serif";}',
PHP_EOL,
'.bgpic-first {background-image: url("pic.webp");}',
PHP_EOL,
'.bgpic-second {background-image: url("pic.webp");}'
);
$parser = new Parser($preParseCss);
$document = $parser->parse();
$postParseCss = $document->render();
self::assertEquals($expectedCss, $postParseCss);
}

public function escapedSpecialCaseTokens(): void
{
$oDoc = $this->parsedStructureForFile('escaped-tokens');
Expand Down
Loading