Skip to content

Commit ea4d5df

Browse files
committed
Added support for CSS functions
Closes MyIntervals#15 as fixed. Closes MyIntervals#12 as fixed.
1 parent 01109f1 commit ea4d5df

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

CSSParser.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private function parseAtRule() {
9292
}
9393
}
9494

95-
private function parseIdentifier() {
95+
private function parseIdentifier($bAllowFunctions = true) {
9696
$sResult = $this->parseCharacter(true);
9797
if($sResult === null) {
9898
throw new Exception("Identifier expected, got {$this->peek(5)}");
@@ -101,6 +101,11 @@ private function parseIdentifier() {
101101
while(($sCharacter = $this->parseCharacter(true)) !== null) {
102102
$sResult .= $sCharacter;
103103
}
104+
if($bAllowFunctions && $this->comes('(')) {
105+
$this->consume('(');
106+
$sResult = new CSSFunction($sResult, $this->parseValue());
107+
$this->consume(')');
108+
}
104109
return $sResult;
105110
}
106111

@@ -297,13 +302,13 @@ private function parseColorValue() {
297302
$aColor = array();
298303
if($this->comes('#')) {
299304
$this->consume('#');
300-
$sValue = $this->parseIdentifier();
305+
$sValue = $this->parseIdentifier(false);
301306
if(mb_strlen($sValue, $this->sCharset) === 3) {
302307
$sValue = $sValue[0].$sValue[0].$sValue[1].$sValue[1].$sValue[2].$sValue[2];
303308
}
304309
$aColor = array('r' => new CSSSize(intval($sValue[0].$sValue[1], 16)), 'g' => new CSSSize(intval($sValue[2].$sValue[3], 16)), 'b' => new CSSSize(intval($sValue[4].$sValue[5], 16)));
305310
} else {
306-
$sColorMode = $this->parseIdentifier();
311+
$sColorMode = $this->parseIdentifier(false);
307312
$this->consumeWhiteSpace();
308313
$this->consume('(');
309314
$iLength = mb_strlen($sColorMode, $this->sCharset);
@@ -1034,3 +1039,26 @@ public function __toString() {
10341039
return "$oValue1/$oValue2";
10351040
}
10361041
}
1042+
1043+
class CSSFunction extends CSSValue {
1044+
private $sName;
1045+
private $aContents;
1046+
1047+
public function __construct($sName, $aContents) {
1048+
$this->sName = $sName;
1049+
$this->aContents = $aContents;
1050+
}
1051+
1052+
public function getName() {
1053+
return $this->sName;
1054+
}
1055+
1056+
public function getContents() {
1057+
return $this->aContents;
1058+
}
1059+
1060+
public function __toString() {
1061+
$sContents = implode(',', $this->aContents);
1062+
return "{$this->sName}({$sContents})";
1063+
}
1064+
}

tests/CSSParserTests.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ function testSlashedValues() {
167167
$oDoc = $this->parsedStructureForFile('slashed');
168168
$this->assertSame('.test {font: 12px/1.5;border-radius: 5px 10px 5px 10px/10px 5px 10px 5px;}', $oDoc->__toString());
169169
}
170+
171+
function testFunctionSyntax() {
172+
$oDoc = $this->parsedStructureForFile('functions');
173+
$this->assertSame('div.main {background-image: linear-gradient(rgb(0, 0, 0),rgb(255, 255, 255));}.collapser::before, .collapser::-moz-before, .collapser::-webkit-before {content: "»";font-size: 1.2em;margin-right: 0.2em;-moz-transition-property: -moz-transform;-moz-transition-duration: 0.2s;-moz-transform-origin: center 60%;}.collapser.expanded::before, .collapser.expanded::-moz-before, .collapser.expanded::-webkit-before {-moz-transform: rotate(90deg);}.collapser + * {height: 0;overflow: hidden;-moz-transition-property: height;-moz-transition-duration: 0.3s;}.collapser.expanded + * {height: auto;}', $oDoc->__toString());
174+
}
170175

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

tests/files/functions.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
div.main { background-image: linear-gradient(#000, #fff) }
2+
.collapser::before,
3+
.collapser::-moz-before,
4+
.collapser::-webkit-before {
5+
content: "»";
6+
font-size: 1.2em;
7+
margin-right: .2em;
8+
-moz-transition-property: -moz-transform;
9+
-moz-transition-duration: .2s;
10+
-moz-transform-origin: center 60%;
11+
}
12+
.collapser.expanded::before,
13+
.collapser.expanded::-moz-before,
14+
.collapser.expanded::-webkit-before { -moz-transform: rotate(90deg) }
15+
.collapser + * {
16+
height: 0;
17+
overflow: hidden;
18+
-moz-transition-property: height;
19+
-moz-transition-duration: .3s;
20+
}
21+
.collapser.expanded + * { height: auto }
22+

0 commit comments

Comments
 (0)