Skip to content

Commit 11cc1cb

Browse files
committed
Added support for the unit/unit syntax found in font: and border-radius: rules.
Closes MyIntervals#11 as fixed. Closes MyIntervals#17 as fixed. Closes MyIntervals#16 as fixed.
1 parent 921fea2 commit 11cc1cb

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

CSSParser.php

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,33 @@ private function parseRule() {
223223
private function parseValue() {
224224
$aResult = array();
225225
do {
226-
$this->consumeWhiteSpace();
227-
if(is_numeric($this->peek()) || $this->comes('-') || $this->comes('.')) {
228-
$aResult[] = $this->parseNumericValue();
229-
} else if($this->comes('#') || $this->comes('rgb') || $this->comes('hsl')) {
230-
$aResult[] = $this->parseColorValue();
231-
} else if($this->comes('url')){
232-
$aResult[] = $this->parseURLValue();
233-
} else if($this->comes("'") || $this->comes('"')){
234-
$aResult[] = $this->parseStringValue();
235-
} else {
236-
$aResult[] = $this->parseIdentifier();
237-
}
238-
$this->consumeWhiteSpace();
226+
$aResult[] = $this->parseSingleValue();
239227
} while($this->comes(',') && is_string($this->consume(',')));
240228

241229
return $aResult;
242230
}
231+
232+
private function parseSingleValue() {
233+
$oValue = null;
234+
$this->consumeWhiteSpace();
235+
if(is_numeric($this->peek()) || (($this->comes('-') || $this->comes('.')) && is_numeric($this->peek(1, 1)))) {
236+
$oValue = $this->parseNumericValue();
237+
} else if($this->comes('#') || $this->comes('rgb') || $this->comes('hsl')) {
238+
$oValue = $this->parseColorValue();
239+
} else if($this->comes('url')){
240+
$oValue = $this->parseURLValue();
241+
} else if($this->comes("'") || $this->comes('"')){
242+
$oValue = $this->parseStringValue();
243+
} else {
244+
$oValue = $this->parseIdentifier();
245+
}
246+
$this->consumeWhiteSpace();
247+
if($this->comes('/')) {
248+
$this->consume('/');
249+
$oValue = new CSSSlashedValue($oValue, $this->parseSingleValue());
250+
}
251+
return $oValue;
252+
}
243253

244254
private function parseNumericValue() {
245255
$sSize = '';
@@ -990,3 +1000,33 @@ public function __toString() {
9901000
return "url({$this->oURL->__toString()})";
9911001
}
9921002
}
1003+
1004+
class CSSSlashedValue extends CSSValue {
1005+
private $oValue1;
1006+
private $oValue2;
1007+
1008+
public function __construct($oValue1, $oValue2) {
1009+
$this->oValue1 = $oValue1;
1010+
$this->oValue2 = $oValue2;
1011+
}
1012+
1013+
public function getValue1() {
1014+
return $this->oValue1;
1015+
}
1016+
1017+
public function getValue2() {
1018+
return $this->oValue2;
1019+
}
1020+
1021+
public function __toString() {
1022+
$oValue1 = $this->oValue1;
1023+
$oValue2 = $this->oValue2;
1024+
if($oValue1 instanceof CSSValue) {
1025+
$oValue1 = $oValue1->__toString();
1026+
}
1027+
if($oValue2 instanceof CSSValue) {
1028+
$oValue2 = $oValue2->__toString();
1029+
}
1030+
return "$oValue1/$oValue2";
1031+
}
1032+
}

tests/CSSParserTests.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ function testCssFiles() {
1717
if(strrpos($sFileName, '.css') !== strlen($sFileName)-strlen('.css')) {
1818
continue;
1919
}
20+
if(strpos($sFileName, '-') === 0) {
21+
//Either a file which SHOULD fail or a future test of a as-of-now missing feature
22+
continue;
23+
}
2024
$oParser = new CSSParser(file_get_contents($sDirectory.DIRECTORY_SEPARATOR.$sFileName));
2125
try {
2226
$oParser->parse()->__toString();
@@ -158,6 +162,11 @@ function testManipulation() {
158162
}
159163
$this->assertSame('#header {margin: 10px 2em 1cm 2%;color: red !important;}body {color: green;}', $oDoc->__toString());
160164
}
165+
166+
function testSlashedValues() {
167+
$oDoc = $this->parsedStructureForFile('slashed');
168+
$this->assertSame('.test {font: 12px/1.5;border-radius: 5px 10px 5px 10px/10px 5px 10px 5px;}', $oDoc->__toString());
169+
}
161170

162171
function parsedStructureForFile($sFileName) {
163172
$sFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR."$sFileName.css";

tests/files/slashed.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.test {
2+
font: 12px/1.5;
3+
border-radius: 5px 10px 5px 10px / 10px 5px 10px 5px;
4+
}

0 commit comments

Comments
 (0)