Skip to content

Commit 7819fdd

Browse files
committed
Add intermediate CSSBlockList
Now that we have CSSLists that may contain “unreal” declaration blocks (the @Keyframes at-rule), we need to make sure searching for selectors does not accidentally yield the blocks therein.
1 parent 5037bc9 commit 7819fdd

File tree

4 files changed

+81
-63
lines changed

4 files changed

+81
-63
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Sabberworm\CSS\CSSList;
4+
5+
use Sabberworm\CSS\RuleSet\DeclarationBlock;
6+
use Sabberworm\CSS\RuleSet\RuleSet;
7+
use Sabberworm\CSS\Property\Selector;
8+
use Sabberworm\CSS\Rule\Rule;
9+
use Sabberworm\CSS\Value\ValueList;
10+
use Sabberworm\CSS\Value\CSSFunction;
11+
12+
/**
13+
* A CSSBlockList is a CSSList whose DeclarationBlocks are guaranteed to contain valid declaration blocks or at-rules.
14+
* Most CSSLists conform to this category but some at-rules (such as @keyframes) do not.
15+
*/
16+
abstract class CSSBlockList extends CSSList {
17+
protected function allDeclarationBlocks(&$aResult) {
18+
foreach ($this->aContents as $mContent) {
19+
if ($mContent instanceof DeclarationBlock) {
20+
$aResult[] = $mContent;
21+
} else if ($mContent instanceof CSSBlockList) {
22+
$mContent->allDeclarationBlocks($aResult);
23+
}
24+
}
25+
}
26+
27+
protected function allRuleSets(&$aResult) {
28+
foreach ($this->aContents as $mContent) {
29+
if ($mContent instanceof RuleSet) {
30+
$aResult[] = $mContent;
31+
} else if ($mContent instanceof CSSBlockList) {
32+
$mContent->allRuleSets($aResult);
33+
}
34+
}
35+
}
36+
37+
protected function allValues($oElement, &$aResult, $sSearchString = null, $bSearchInFunctionArguments = false) {
38+
if ($oElement instanceof CSSBlockList) {
39+
foreach ($oElement->getContents() as $oContent) {
40+
$this->allValues($oContent, $aResult, $sSearchString, $bSearchInFunctionArguments);
41+
}
42+
} else if ($oElement instanceof RuleSet) {
43+
foreach ($oElement->getRules($sSearchString) as $oRule) {
44+
$this->allValues($oRule, $aResult, $sSearchString, $bSearchInFunctionArguments);
45+
}
46+
} else if ($oElement instanceof Rule) {
47+
$this->allValues($oElement->getValue(), $aResult, $sSearchString, $bSearchInFunctionArguments);
48+
} else if ($oElement instanceof ValueList) {
49+
if ($bSearchInFunctionArguments || !($oElement instanceof CSSFunction)) {
50+
foreach ($oElement->getListComponents() as $mComponent) {
51+
$this->allValues($mComponent, $aResult, $sSearchString, $bSearchInFunctionArguments);
52+
}
53+
}
54+
} else {
55+
//Non-List Value or String (CSS identifier)
56+
$aResult[] = $oElement;
57+
}
58+
}
59+
60+
protected function allSelectors(&$aResult, $sSpecificitySearch = null) {
61+
$aDeclarationBlocks = array();
62+
$this->allDeclarationBlocks($aDeclarationBlocks);
63+
foreach ($aDeclarationBlocks as $oBlock) {
64+
foreach ($oBlock->getSelectors() as $oSelector) {
65+
if ($sSpecificitySearch === null) {
66+
$aResult[] = $oSelector;
67+
} else {
68+
$sComparison = "\$bRes = {$oSelector->getSpecificity()} $sSpecificitySearch;";
69+
eval($sComparison);
70+
if ($bRes) {
71+
$aResult[] = $oSelector;
72+
}
73+
}
74+
}
75+
}
76+
}
77+
78+
}

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
abstract class CSSList {
1717

18-
private $aContents;
18+
protected $aContents;
1919

2020
public function __construct() {
2121
$this->aContents = array();
@@ -72,64 +72,4 @@ public function __toString() {
7272
public function getContents() {
7373
return $this->aContents;
7474
}
75-
76-
protected function allDeclarationBlocks(&$aResult) {
77-
foreach ($this->aContents as $mContent) {
78-
if ($mContent instanceof DeclarationBlock) {
79-
$aResult[] = $mContent;
80-
} else if ($mContent instanceof CSSList) {
81-
$mContent->allDeclarationBlocks($aResult);
82-
}
83-
}
84-
}
85-
86-
protected function allRuleSets(&$aResult) {
87-
foreach ($this->aContents as $mContent) {
88-
if ($mContent instanceof RuleSet) {
89-
$aResult[] = $mContent;
90-
} else if ($mContent instanceof CSSList) {
91-
$mContent->allRuleSets($aResult);
92-
}
93-
}
94-
}
95-
96-
protected function allValues($oElement, &$aResult, $sSearchString = null, $bSearchInFunctionArguments = false) {
97-
if ($oElement instanceof CSSList) {
98-
foreach ($oElement->getContents() as $oContent) {
99-
$this->allValues($oContent, $aResult, $sSearchString, $bSearchInFunctionArguments);
100-
}
101-
} else if ($oElement instanceof RuleSet) {
102-
foreach ($oElement->getRules($sSearchString) as $oRule) {
103-
$this->allValues($oRule, $aResult, $sSearchString, $bSearchInFunctionArguments);
104-
}
105-
} else if ($oElement instanceof Rule) {
106-
$this->allValues($oElement->getValue(), $aResult, $sSearchString, $bSearchInFunctionArguments);
107-
} else if ($oElement instanceof ValueList) {
108-
if ($bSearchInFunctionArguments || !($oElement instanceof CSSFunction)) {
109-
foreach ($oElement->getListComponents() as $mComponent) {
110-
$this->allValues($mComponent, $aResult, $sSearchString, $bSearchInFunctionArguments);
111-
}
112-
}
113-
} else {
114-
//Non-List Value or String (CSS identifier)
115-
$aResult[] = $oElement;
116-
}
117-
}
118-
119-
protected function allSelectors(&$aResult, $sSpecificitySearch = null) {
120-
foreach ($this->getAllDeclarationBlocks() as $oBlock) {
121-
foreach ($oBlock->getSelectors() as $oSelector) {
122-
if ($sSpecificitySearch === null) {
123-
$aResult[] = $oSelector;
124-
} else {
125-
$sComparison = "\$bRes = {$oSelector->getSpecificity()} $sSpecificitySearch;";
126-
eval($sComparison);
127-
if ($bRes) {
128-
$aResult[] = $oSelector;
129-
}
130-
}
131-
}
132-
}
133-
}
134-
13575
}

lib/Sabberworm/CSS/CSSList/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* The root CSSList of a parsed file. Contains all top-level css contents, mostly declaration blocks, but also any @-rules encountered.
77
*/
8-
class Document extends CSSList {
8+
class Document extends CSSBlockList {
99

1010
/**
1111
* Gets all DeclarationBlock objects recursively.

lib/Sabberworm/CSS/CSSList/MediaQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* A CSSList consisting of the CSSList and CSSList objects found in a @media query.
77
*/
8-
class MediaQuery extends CSSList {
8+
class MediaQuery extends CSSBlockList {
99

1010
private $sQuery;
1111

0 commit comments

Comments
 (0)