Skip to content

Commit 90534d9

Browse files
committed
Add spaces between operators in the calc functions
1 parent 04d6e21 commit 90534d9

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

lib/Sabberworm/CSS/OutputFormat.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class OutputFormat {
4141
// This is what’s printed after the comma of value lists
4242
public $sSpaceBeforeListArgumentSeparator = '';
4343
public $sSpaceAfterListArgumentSeparator = '';
44+
45+
// This is what’s printed after the operators in a calc function
46+
public $sSpaceBeforeCalcListArgumentSeparator = ' ';
47+
public $sSpaceAfterCalcListArgumentSeparator = ' ';
4448

4549
public $sSpaceBeforeOpeningBrace = ' ';
4650

@@ -219,6 +223,14 @@ public function spaceAfterListArgumentSeparator($sSeparator) {
219223
return $this->space('AfterListArgumentSeparator', $sSeparator);
220224
}
221225

226+
public function spaceBeforeCalcListArgumentSeparator($sSeparator) {
227+
return $this->space('BeforeCalcListArgumentSeparator', $sSeparator);
228+
}
229+
230+
public function spaceAfterCalcListArgumentSeparator($sSeparator) {
231+
return $this->space('AfterCalcListArgumentSeparator', $sSeparator);
232+
}
233+
222234
public function spaceBeforeOpeningBrace() {
223235
return $this->space('BeforeOpeningBrace');
224236
}
@@ -286,4 +298,4 @@ private function prepareSpace($sSpaceString) {
286298
private function indent() {
287299
return str_repeat($this->oFormat->sIndentation, $this->oFormat->level());
288300
}
289-
}
301+
}

lib/Sabberworm/CSS/Parser.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Sabberworm\CSS\CSSList\AtRuleBlockList;
1515
use Sabberworm\CSS\RuleSet\DeclarationBlock;
1616
use Sabberworm\CSS\Value\CSSFunction;
17+
use Sabberworm\CSS\Value\CalcFunction;
1718
use Sabberworm\CSS\Value\RuleValueList;
19+
use Sabberworm\CSS\Value\CalcRuleValueList;
1820
use Sabberworm\CSS\Value\Size;
1921
use Sabberworm\CSS\Value\Color;
2022
use Sabberworm\CSS\Value\URL;
@@ -372,7 +374,7 @@ private function parseRule() {
372374
return $oRule;
373375
}
374376

375-
private function parseValue($aListDelimiters) {
377+
private function parseValue($aListDelimiters, $ruleValueListClass = '\Sabberworm\CSS\Value\RuleValueList') {
376378
$aStack = array();
377379
$this->consumeWhiteSpace();
378380
//Build a list of delimiters and parsed values
@@ -408,7 +410,7 @@ private function parseValue($aListDelimiters) {
408410
break;
409411
}
410412
}
411-
$oList = new RuleValueList($sDelimiter, $this->iLineNo);
413+
$oList = new $ruleValueListClass($sDelimiter, $this->iLineNo);
412414
for ($i = $iStartPosition - 1; $i - $iStartPosition + 1 < $iLength * 2; $i+=2) {
413415
$oList->addListComponent($aStack[$i]);
414416
}
@@ -527,8 +529,8 @@ private function parseCalcValue() {
527529
$this->consume($func);
528530
$this->consumeWhiteSpace();
529531
$this->consume('(');
530-
$aArguments = $this->parseValue(array('+', '-', '*', '/', ' '));
531-
return new CSSFunction($func, $aArguments, ',', $this->iLineNo);
532+
$aArguments = $this->parseValue(array('+', '-', '*', '/', ' '), '\Sabberworm\CSS\Value\CalcRuleValueList');
533+
return new CalcFunction($func, $aArguments, ',', $this->iLineNo);
532534
}
533535

534536
/**

lib/Sabberworm/CSS/Value/CSSFunction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class CSSFunction extends ValueList {
66

7-
private $sName;
7+
protected $sName;
88

99
public function __construct($sName, $aArguments, $sSeparator = ',', $iLineNo = 0) {
1010
if($aArguments instanceof RuleValueList) {
@@ -37,4 +37,4 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
3737
return "{$this->sName}({$aArguments})";
3838
}
3939

40-
}
40+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Sabberworm\CSS\Value;
4+
5+
class CalcFunction extends CSSFunction {
6+
7+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
8+
$aArguments = $oOutputFormat->implode($oOutputFormat->spaceBeforeCalcListArgumentSeparator($this->sSeparator) . $this->sSeparator . $oOutputFormat->spaceAfterCalcListArgumentSeparator($this->sSeparator), $this->aComponents);
9+
return "{$this->sName}({$aArguments})";
10+
}
11+
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Sabberworm\CSS\Value;
4+
5+
class CalcRuleValueList extends RuleValueList {
6+
7+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
8+
return $oOutputFormat->implode($oOutputFormat->spaceBeforeCalcListArgumentSeparator($this->sSeparator) . $this->sSeparator . $oOutputFormat->spaceAfterCalcListArgumentSeparator($this->sSeparator), $this->aComponents);
9+
}
10+
11+
}

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,8 @@ function testUrlInFile() {
389389

390390
function testCalcInFile() {
391391
$oDoc = $this->parsedStructureForFile('calc', Settings::create()->withMultibyteSupport(true));
392-
$sExpected = 'div {width: calc(100%/4);}
393-
div {height: -webkit-calc(9/16*100%);}';
392+
$sExpected = 'div {width: calc(100% / 4);}
393+
div {height: -webkit-calc(9 / 16 * 100%);}';
394394
$this->assertSame($sExpected, $oDoc->render());
395395
}
396396

0 commit comments

Comments
 (0)