Skip to content

Commit 705b34a

Browse files
author
Andreas Sandberg
committed
Revert "Merge branch 'master' of https://github.com/sabberworm/PHP-CSS-Parser"
This reverts commit b0d875a, reversing changes made to 9aa6e03.
1 parent b0d875a commit 705b34a

35 files changed

+1843
-2243
lines changed

lib/Sabberworm/CSS/Parser.php renamed to CSSParser.php

Lines changed: 172 additions & 181 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 222 additions & 239 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/CSSProperties.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/**
4+
* Class representing an @import rule.
5+
*/
6+
class CSSImport {
7+
private $oLocation;
8+
private $sMediaQuery;
9+
10+
public function __construct(CSSURL $oLocation, $sMediaQuery) {
11+
$this->oLocation = $oLocation;
12+
$this->sMediaQuery = $sMediaQuery;
13+
}
14+
15+
public function setLocation($oLocation) {
16+
$this->oLocation = $oLocation;
17+
}
18+
19+
public function getLocation() {
20+
return $this->oLocation;
21+
}
22+
23+
public function __toString() {
24+
return "@import ".$this->oLocation->__toString().($this->sMediaQuery === null ? '' : ' '.$this->sMediaQuery).';';
25+
}
26+
}
27+
28+
/**
29+
* Class representing an @charset rule.
30+
* The following restrictions apply:
31+
* • May not be found in any CSSList other than the CSSDocument.
32+
* • May only appear at the very top of a CSSDocument’s contents.
33+
* • Must not appear more than once.
34+
*/
35+
class CSSCharset {
36+
private $sCharset;
37+
38+
public function __construct($sCharset) {
39+
$this->sCharset = $sCharset;
40+
}
41+
42+
public function setCharset($sCharset) {
43+
$this->sCharset = $sCharset;
44+
}
45+
46+
public function getCharset() {
47+
return $this->sCharset;
48+
}
49+
50+
public function __toString() {
51+
return "@charset {$this->sCharset->__toString()};";
52+
}
53+
}
54+
55+
/**
56+
* Class representing a single CSS selector. Selectors have to be split by the comma prior to being passed into this class.
57+
*/
58+
class CSSSelector {
59+
const
60+
NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX = '/
61+
(\.[\w]+) # classes
62+
|
63+
\[(\w+) # attributes
64+
|
65+
(\:( # pseudo classes
66+
link|visited|active
67+
|hover|focus
68+
|lang
69+
|target
70+
|enabled|disabled|checked|indeterminate
71+
|root
72+
|nth-child|nth-last-child|nth-of-type|nth-last-of-type
73+
|first-child|last-child|first-of-type|last-of-type
74+
|only-child|only-of-type
75+
|empty|contains
76+
))
77+
/ix',
78+
ELEMENTS_AND_PSEUDO_ELEMENTS_RX = '/
79+
((^|[\s\+\>\~]+)[\w]+ # elements
80+
|
81+
\:{1,2}( # pseudo-elements
82+
after|before
83+
|first-letter|first-line
84+
|selection
85+
)
86+
)/ix';
87+
88+
private $sSelector;
89+
private $iSpecificity;
90+
91+
public function __construct($sSelector, $bCalculateSpecificity = false) {
92+
$this->setSelector($sSelector);
93+
if($bCalculateSpecificity) {
94+
$this->getSpecificity();
95+
}
96+
}
97+
98+
public function getSelector() {
99+
return $this->sSelector;
100+
}
101+
102+
public function setSelector($sSelector) {
103+
$this->sSelector = trim($sSelector);
104+
$this->iSpecificity = null;
105+
}
106+
107+
public function __toString() {
108+
return $this->getSelector();
109+
}
110+
111+
public function getSpecificity() {
112+
if($this->iSpecificity === null) {
113+
$a = 0;
114+
/// @todo should exclude \# as well as "#"
115+
$aMatches;
116+
$b = substr_count($this->sSelector, '#');
117+
$c = preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->sSelector, $aMatches);
118+
$d = preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->sSelector, $aMatches);
119+
$this->iSpecificity = ($a*1000) + ($b*100) + ($c*10) + $d;
120+
}
121+
return $this->iSpecificity;
122+
}
123+
}
124+

lib/CSSRule.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
/**
4+
* CSSRuleSets contains CSSRule objects which always have a key and a value.
5+
* In CSS, CSSRules are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];”
6+
*/
7+
class CSSRule {
8+
private $sRule;
9+
private $mValue;
10+
private $bIsImportant;
11+
12+
public function __construct($sRule) {
13+
$this->sRule = $sRule;
14+
$this->mValue = null;
15+
$this->bIsImportant = false;
16+
}
17+
18+
public function setRule($sRule) {
19+
$this->sRule = $sRule;
20+
}
21+
22+
public function getRule() {
23+
return $this->sRule;
24+
}
25+
26+
public function getValue() {
27+
return $this->mValue;
28+
}
29+
30+
public function setValue($mValue) {
31+
$this->mValue = $mValue;
32+
}
33+
34+
/**
35+
* @deprecated Old-Style 2-dimensional array given. Retained for (some) backwards-compatibility. Use setValue() instead and wrapp the value inside a CSSRuleValueList if necessary.
36+
*/
37+
public function setValues($aSpaceSeparatedValues) {
38+
$oSpaceSeparatedList = null;
39+
if(count($aSpaceSeparatedValues) > 1) {
40+
$oSpaceSeparatedList = new CSSRuleValueList(' ');
41+
}
42+
foreach($aSpaceSeparatedValues as $aCommaSeparatedValues) {
43+
$oCommaSeparatedList = null;
44+
if(count($aCommaSeparatedValues) > 1) {
45+
$oCommaSeparatedList = new CSSRuleValueList(',');
46+
}
47+
foreach($aCommaSeparatedValues as $mValue) {
48+
if(!$oSpaceSeparatedList && !$oCommaSeparatedList) {
49+
$this->mValue = $mValue;
50+
return $mValue;
51+
}
52+
if($oCommaSeparatedList) {
53+
$oCommaSeparatedList->addListComponent($mValue);
54+
} else {
55+
$oSpaceSeparatedList->addListComponent($mValue);
56+
}
57+
}
58+
if(!$oSpaceSeparatedList) {
59+
$this->mValue = $oCommaSeparatedList;
60+
return $oCommaSeparatedList;
61+
} else {
62+
$oSpaceSeparatedList->addListComponent($oCommaSeparatedList);
63+
}
64+
}
65+
$this->mValue = $oSpaceSeparatedList;
66+
return $oSpaceSeparatedList;
67+
}
68+
69+
/**
70+
* @deprecated Old-Style 2-dimensional array returned. Retained for (some) backwards-compatibility. Use getValue() instead and check for the existance of a (nested set of) CSSValueList object(s).
71+
*/
72+
public function getValues() {
73+
if(!$this->mValue instanceof CSSRuleValueList) {
74+
return array(array($this->mValue));
75+
}
76+
if($this->mValue->getListSeparator() === ',') {
77+
return array($this->mValue->getListComponents());
78+
}
79+
$aResult = array();
80+
foreach($this->mValue->getListComponents() as $mValue) {
81+
if(!$mValue instanceof CSSRuleValueList || $mValue->getListSeparator() !== ',') {
82+
$aResult[] = array($mValue);
83+
continue;
84+
}
85+
if($this->mValue->getListSeparator() === ' ' || count($aResult) === 0) {
86+
$aResult[] = array();
87+
}
88+
foreach($mValue->getListComponents() as $mValue) {
89+
$aResult[count($aResult)-1][] = $mValue;
90+
}
91+
}
92+
return $aResult;
93+
}
94+
95+
/**
96+
* Adds a value to the existing value. Value will be appended if a CSSRuleValueList exists of the given type. Otherwise, the existing value will be wrapped by one.
97+
*/
98+
public function addValue($mValue, $sType = ' ') {
99+
if(!is_array($mValue)) {
100+
$mValue = array($mValue);
101+
}
102+
if(!$this->mValue instanceof CSSRuleValueList || $this->mValue->getListSeparator() !== $sType) {
103+
$mCurrentValue = $this->mValue;
104+
$this->mValue = new CSSRuleValueList($sType);
105+
if($mCurrentValue) {
106+
$this->mValue->addListComponent($mCurrentValue);
107+
}
108+
}
109+
foreach($mValue as $mValueItem) {
110+
$this->mValue->addListComponent($mValueItem);
111+
}
112+
}
113+
114+
public function setIsImportant($bIsImportant) {
115+
$this->bIsImportant = $bIsImportant;
116+
}
117+
118+
public function getIsImportant() {
119+
return $this->bIsImportant;
120+
}
121+
122+
public function __toString() {
123+
$sResult = "{$this->sRule}: ";
124+
if($this->mValue instanceof CSSValue) { //Can also be a CSSValueList
125+
$sResult .= $this->mValue->__toString();
126+
} else {
127+
$sResult .= $this->mValue;
128+
}
129+
if($this->bIsImportant) {
130+
$sResult .= ' !important';
131+
}
132+
$sResult .= ';';
133+
return $sResult;
134+
}
135+
}

0 commit comments

Comments
 (0)