Skip to content

Commit 0643a61

Browse files
committed
Support for overriding all contents and rules
2 parents 42e7432 + ebd4aaa commit 0643a61

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ public function remove($oItemToRemove) {
4848
return false;
4949
}
5050

51+
/**
52+
* Set the contents.
53+
* @param array $aContents Objects to set as content.
54+
*/
55+
public function setContents(array $aContents) {
56+
$this->aContents = array();
57+
foreach ($aContents as $content) {
58+
$this->append($content);
59+
}
60+
}
61+
5162
/**
5263
* Removes a declaration block from the CSS list if it matches all given selectors.
5364
* @param array|string $mSelector The selectors to match.

lib/Sabberworm/CSS/RuleSet/RuleSet.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@ public function getRules($mRule = null) {
5656
}
5757
return $aResult;
5858
}
59-
59+
60+
/**
61+
* Override all the rules of this set.
62+
* @param array $aRules The rules to override with.
63+
*/
64+
public function setRules(array $aRules) {
65+
$this->aRules = array();
66+
foreach ($aRules as $rule) {
67+
$this->addRule($rule);
68+
}
69+
}
70+
6071
/**
6172
* Returns all rules matching the given pattern and returns them in an associative array with the rule’s name as keys. This method exists mainly for backwards-compatibility and is really only partially useful.
6273
* @param (string) $mRule pattern to search for. If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are returned as well as one matching the pattern with the dash excluded. passing a Rule behaves like calling getRules($mRule->getRule()).
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Sabberworm\CSS\CSSList;
4+
5+
use Sabberworm\CSS\Parser;
6+
7+
class DocumentTest extends \PHPUnit_Framework_TestCase {
8+
9+
public function testOverrideContents() {
10+
$sCss = '.thing { left: 10px; }';
11+
$oParser = new Parser($sCss);
12+
$oDoc = $oParser->parse();
13+
$aContents = $oDoc->getContents();
14+
$this->assertCount(1, $aContents);
15+
16+
$sCss2 = '.otherthing { right: 10px; }';
17+
$oParser2 = new Parser($sCss);
18+
$oDoc2 = $oParser2->parse();
19+
$aContents2 = $oDoc2->getContents();
20+
21+
$oDoc->setContents(array($aContents[0], $aContents2[0]));
22+
$aFinalContents = $oDoc->getContents();
23+
$this->assertCount(2, $aFinalContents);
24+
}
25+
26+
}

tests/Sabberworm/CSS/RuleSet/DeclarationBlockTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sabberworm\CSS\RuleSet;
44

55
use Sabberworm\CSS\Parser;
6+
use Sabberworm\CSS\Rule\Rule;
67

78
class DeclarationBlockTest extends \PHPUnit_Framework_TestCase {
89

@@ -205,4 +206,22 @@ public function createBackgroundShorthandProvider() {
205206
);
206207
}
207208

209+
public function testOverrideRules() {
210+
$sCss = '.wrapper { left: 10px; text-align: left; }';
211+
$oParser = new Parser($sCss);
212+
$oDoc = $oParser->parse();
213+
$oRule = new Rule('right');
214+
$oRule->setValue('-10px');
215+
$aContents = $oDoc->getContents();
216+
$oWrapper = $aContents[0];
217+
218+
$this->assertCount(2, $oWrapper->getRules());
219+
$aContents[0]->setRules(array($oRule));
220+
221+
$aRules = $oWrapper->getRules();
222+
$this->assertCount(1, $aRules);
223+
$this->assertEquals('right', $aRules[0]->getRule());
224+
$this->assertEquals('-10px', $aRules[0]->getValue());
225+
}
226+
208227
}

0 commit comments

Comments
 (0)