Skip to content

Commit 9d0e1f0

Browse files
committed
Streamline API, add tests
1 parent ea58a33 commit 9d0e1f0

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/Sabberworm/CSS/RuleSet/RuleSet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ public function getLineNo() {
2929
return $this->iLineNo;
3030
}
3131

32-
public function addRule(Rule $oRule, Rule $oSibling = null, /* boolean */ $bPrepend = false) {
32+
public function addRule(Rule $oRule, Rule $oSibling = null) {
3333
$sRule = $oRule->getRule();
3434
if(!isset($this->aRules[$sRule])) {
3535
$this->aRules[$sRule] = array();
3636
}
3737

38-
$iPosition = $bPrepend ? 0 : count($this->aRules[$sRule]);
38+
$iPosition = count($this->aRules[$sRule]);
3939

4040
if ($oSibling !== null) {
4141
$iSiblingPos = array_search($oSibling, $this->aRules[$sRule], true);
4242
if ($iSiblingPos !== false) {
43-
$iPosition = $bPrepend ? $iSiblingPos : $iSiblingPos + 1;
43+
$iPosition = $iSiblingPos;
4444
}
4545
}
4646

tests/Sabberworm/CSS/RuleSet/DeclarationBlockTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Sabberworm\CSS\Parser;
66
use Sabberworm\CSS\Rule\Rule;
7+
use Sabberworm\CSS\Value\Size;
78

89
class DeclarationBlockTest extends \PHPUnit_Framework_TestCase {
910

@@ -223,5 +224,44 @@ public function testOverrideRules() {
223224
$this->assertEquals('right', $aRules[0]->getRule());
224225
$this->assertEquals('-10px', $aRules[0]->getValue());
225226
}
227+
228+
public function testRuleInsertion() {
229+
$sCss = '.wrapper { left: 10px; text-align: left; }';
230+
$oParser = new Parser($sCss);
231+
$oDoc = $oParser->parse();
232+
$aContents = $oDoc->getContents();
233+
$oWrapper = $aContents[0];
234+
235+
$oFirst = $oWrapper->getRules('left');
236+
$this->assertCount(1, $oFirst);
237+
$oFirst = $oFirst[0];
238+
239+
$oSecond = $oWrapper->getRules('text-');
240+
$this->assertCount(1, $oSecond);
241+
$oSecond = $oSecond[0];
242+
243+
$oBefore = new Rule('left');
244+
$oBefore->setValue(new Size(16, 'em'));
245+
246+
$oMiddle = new Rule('text-align');
247+
$oMiddle->setValue(new Size(1));
248+
249+
$oAfter = new Rule('border-bottom-width');
250+
$oAfter->setValue(new Size(1, 'px'));
251+
252+
$oWrapper->addRule($oAfter);
253+
$oWrapper->addRule($oBefore, $oFirst);
254+
$oWrapper->addRule($oMiddle, $oSecond);
255+
256+
$aRules = $oWrapper->getRules();
257+
258+
$this->assertSame($oBefore, $aRules[0]);
259+
$this->assertSame($oFirst, $aRules[1]);
260+
$this->assertSame($oMiddle, $aRules[2]);
261+
$this->assertSame($oSecond, $aRules[3]);
262+
$this->assertSame($oAfter, $aRules[4]);
263+
264+
$this->assertSame('.wrapper {left: 16em;left: 10px;text-align: 1;text-align: left;border-bottom-width: 1px;}', $oDoc->render());
265+
}
226266

227267
}

0 commit comments

Comments
 (0)