Skip to content

Improve/escaped quotes parsing #6

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

Open
wants to merge 2 commits into
base: master
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 @@ -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);
}
}
31 changes: 31 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,37 @@ public function lonelyImport()
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()
{
$oDoc = $this->parsedStructureForFile('escaped-tokens');
Expand Down
Loading