Skip to content

Parse escaped URL and Calc functions correctly #174

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

Closed
Closed
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
10 changes: 9 additions & 1 deletion lib/Sabberworm/CSS/Value/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ public static function parseIdentifierOrFunction(ParserState $oParserState, $bIg
if ($oParserState->comes('(')) {
$oParserState->consume('(');
$aArguments = Value::parseValue($oParserState, array('=', ' ', ','));
$sResult = new CSSFunction($sResult, $aArguments, ',', $oParserState->currentLine());

if ($sResult == 'url') {
$sResult = new URL($aArguments, $oParserState->currentLine());
} else if ($sResult == 'calc') {
$sResult = new CalcFunction($sResult, $aArguments, ',', $oParserState->currentLine());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure we support -webkit-calc, -moz-calc, etc here too.

} else {
$sResult = new CSSFunction($sResult, $aArguments, ',', $oParserState->currentLine());
}

Comment on lines +72 to +79
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we unify this with the logic from parsePrimitiveValue (i.e. drop the duplicate code and URL::parse and CalcFunction::parse)?

$oParserState->consume(')');
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,14 @@ function testMicrosoftFilterParsing() {
$sExpected = ".test {filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=\"#80000000\",endColorstr=\"#00000000\",GradientType=1);}";
$this->assertSame($sExpected, $oDoc->render());
}

function testEscapedSpecialCaseTokens() {
$oDoc = $this->parsedStructureForFile('escaped-tokens');
$contents = $oDoc->getContents();
$rules = $contents[0]->getRules();
$urlRule = $rules[0];
$calcRule = $rules[1];
$this->assertEquals(true, is_a($urlRule->getValue(), '\Sabberworm\CSS\Value\URL'));
$this->assertEquals(true, is_a($calcRule->getValue(), '\Sabberworm\CSS\Value\CalcFunction'));
}
}
7 changes: 7 additions & 0 deletions tests/files/escaped-tokens.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Special case function-like tokens, with an escape backslash followed by a non-newline and non-hex digit character, should be parsed as the appropriate \Sabberworm\CSS\Value\ type
*/
body {
background: u\rl("//example.org/picture.jpg");
height: ca\lc(100% - 1px);
}