Skip to content

Bug fix in calc parsing when the first operand is a negative value #140

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

Merged
merged 2 commits into from
Aug 29, 2018
Merged
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
39 changes: 22 additions & 17 deletions lib/Sabberworm/CSS/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,34 +555,39 @@ private function parseURLValue() {
}

private function parseCalcValue() {
$aOperators = array('+', '-', '*', '/', '(', ')');
$aOperators = array('+', '-', '*', '/');
$sFunction = trim($this->consumeUntil('(', false, true));
$oCalcList = new CalcRuleValueList($this->iLineNo);
$oList = new RuleValueList(',', $this->iLineNo);
$iNestingLevel = 0;
$iLastComponentType = NULL;
while(!$this->comes(')') || $iNestingLevel > 0) {
$this->consumeWhiteSpace();
if (in_array($this->peek(), $aOperators)) {
if (($this->comes('-') || $this->comes('+'))) {
if ($this->peek(1, -1) != ' ' || !($this->comes('- ') || $this->comes('+ '))) {
throw new UnexpectedTokenException(" {$this->peek()} ", $this->peek(1, -1) . $this->peek(2), 'literal', $this->iLineNo);
}
} else if ($this->comes('(')) {
$iNestingLevel++;
} else if ($this->comes(')')) {
$iNestingLevel--;
}
if ($this->comes('(')) {
$iNestingLevel++;
$oCalcList->addListComponent($this->consume(1));
$iLastComponentType = CalcFunction::T_OPERATOR;
} else {
continue;
} else if ($this->comes(')')) {
$iNestingLevel--;
$oCalcList->addListComponent($this->consume(1));
continue;
}
if ($iLastComponentType != CalcFunction::T_OPERAND) {
$oVal = $this->parsePrimitiveValue();
if ($iLastComponentType == CalcFunction::T_OPERAND) {
throw new UnexpectedTokenException(sprintf('Next token was expected to be an operand of type %s. Instead "%s" was found.', implode(', ', $aOperators), $oVal), '', 'custom', $this->iLineNo);
}

$oCalcList->addListComponent($oVal);
$iLastComponentType = CalcFunction::T_OPERAND;
} else {
if (in_array($this->peek(), $aOperators)) {
if (($this->comes('-') || $this->comes('+'))) {
if ($this->peek(1, -1) != ' ' || !($this->comes('- ') || $this->comes('+ '))) {
throw new UnexpectedTokenException(" {$this->peek()} ", $this->peek(1, -1) . $this->peek(2), 'literal', $this->iLineNo);
}
}
$oCalcList->addListComponent($this->consume(1));
$iLastComponentType = CalcFunction::T_OPERATOR;
} else {
throw new UnexpectedTokenException(sprintf('Next token was expected to be an operand of type %s. Instead "%s" was found.', implode(', ', $aOperators), $oVal), '', 'custom', $this->iLineNo);
}
}
}
$oList->addListComponent($oCalcList);
Expand Down
7 changes: 7 additions & 0 deletions tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,17 @@ function testUrlInFile() {
function testCalcInFile() {
$oDoc = $this->parsedStructureForFile('calc', Settings::create()->withMultibyteSupport(true));
$sExpected = 'div {width: calc(100% / 4);}
div {margin-top: calc(-120% - 4px);}
div {height: -webkit-calc(9 / 16 * 100%) !important;width: -moz-calc(( 50px - 50% ) * 2);}';
$this->assertSame($sExpected, $oDoc->render());
}

function testCalcNestedInFile() {
$oDoc = $this->parsedStructureForFile('calc-nested', Settings::create()->withMultibyteSupport(true));
$sExpected = '.test {font-size: calc(( 3 * 4px ) + -2px);top: calc(200px - calc(20 * 3px));}';
$this->assertSame($sExpected, $oDoc->render());
}

function testGridLineNameInFile() {
$oDoc = $this->parsedStructureForFile('grid-linename', Settings::create()->withMultibyteSupport(true));
$sExpected = "div {grid-template-columns: [linename] 100px;}\nspan {grid-template-columns: [linename1 linename2] 100px;}";
Expand Down
4 changes: 4 additions & 0 deletions tests/files/calc-nested.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.test {
font-size: calc((3 * 4px) + -2px);
top: calc(200px - calc(20 * 3px));
}
1 change: 1 addition & 0 deletions tests/files/calc.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
div { width: calc(100% / 4); }
div { margin-top: calc(-120% - 4px); }
div {
height: -webkit-calc(9/16 * 100%)!important;
width: -moz-calc((50px - 50%)*2);
Expand Down