Skip to content

Commit ebd4aaa

Browse files
author
Frederic Massart
committed
Support for overriding all contents and rules
1 parent dcfbc81 commit ebd4aaa

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
@@ -45,6 +45,17 @@ public function remove($oItemToRemove) {
4545
return false;
4646
}
4747

48+
/**
49+
* Set the contents.
50+
* @param array $aContents Objects to set as content.
51+
*/
52+
public function setContents(array $aContents) {
53+
$this->aContents = array();
54+
foreach ($aContents as $content) {
55+
$this->append($content);
56+
}
57+
}
58+
4859
/**
4960
* Removes a declaration block from the CSS list if it matches all given selectors.
5061
* @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
@@ -53,7 +53,18 @@ public function getRules($mRule = null) {
5353
}
5454
return $aResult;
5555
}
56-
56+
57+
/**
58+
* Override all the rules of this set.
59+
* @param array $aRules The rules to override with.
60+
*/
61+
public function setRules(array $aRules) {
62+
$this->aRules = array();
63+
foreach ($aRules as $rule) {
64+
$this->addRule($rule);
65+
}
66+
}
67+
5768
/**
5869
* 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.
5970
* @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)