Skip to content

Commit 04d6e21

Browse files
committed
Add support for CSS3 calc
1 parent c3b01ef commit 04d6e21

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ private function parsePrimitiveValue() {
434434
$oValue = $this->parseColorValue();
435435
} else if ($this->comes('url', true)) {
436436
$oValue = $this->parseURLValue();
437+
} else if ($this->comes('calc', true) || $this->comes('-webkit-calc', true)) {
438+
$oValue = $this->parseCalcValue();
437439
} else if ($this->comes("'") || $this->comes('"')) {
438440
$oValue = $this->parseStringValue();
439441
} else if ($this->comes("progid:") && $this->oParserSettings->bLenientParsing) {
@@ -520,6 +522,15 @@ private function parseURLValue() {
520522
return $oResult;
521523
}
522524

525+
private function parseCalcValue() {
526+
$func = $this->comes('calc', true) ? 'calc' : '-webkit-calc';
527+
$this->consume($func);
528+
$this->consumeWhiteSpace();
529+
$this->consume('(');
530+
$aArguments = $this->parseValue(array('+', '-', '*', '/', ' '));
531+
return new CSSFunction($func, $aArguments, ',', $this->iLineNo);
532+
}
533+
523534
/**
524535
* Tests an identifier for a given value. Since identifiers are all keywords, they can be vendor-prefixed. We need to check for these versions too.
525536
*/

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,13 @@ function testUrlInFile() {
387387
$this->assertSame($sExpected, $oDoc->render());
388388
}
389389

390+
function testCalcInFile() {
391+
$oDoc = $this->parsedStructureForFile('calc', Settings::create()->withMultibyteSupport(true));
392+
$sExpected = 'div {width: calc(100%/4);}
393+
div {height: -webkit-calc(9/16*100%);}';
394+
$this->assertSame($sExpected, $oDoc->render());
395+
}
396+
390397
function testUrlInFileMbOff() {
391398
$oDoc = $this->parsedStructureForFile('url', Settings::create()->withMultibyteSupport(false));
392399
$sExpected = 'body {background: #fff url("http://somesite.com/images/someimage.gif") repeat top center;}

tests/files/calc.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
div { width: calc(100% / 4); }
2+
div {
3+
height: -webkit-calc(9/16 * 100%);
4+
}

0 commit comments

Comments
 (0)