From d0c0a4708070ee4641d78b90133e3016e3e7af42 Mon Sep 17 00:00:00 2001 From: IceReaper Date: Mon, 14 Sep 2015 12:38:51 +0200 Subject: [PATCH] RuleSet::addRule() insert position support. --- lib/Sabberworm/CSS/RuleSet/RuleSet.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/Sabberworm/CSS/RuleSet/RuleSet.php b/lib/Sabberworm/CSS/RuleSet/RuleSet.php index d33f9cde..809d9565 100644 --- a/lib/Sabberworm/CSS/RuleSet/RuleSet.php +++ b/lib/Sabberworm/CSS/RuleSet/RuleSet.php @@ -17,12 +17,22 @@ public function __construct() { $this->aRules = array(); } - public function addRule(Rule $oRule) { + public function addRule(Rule $oRule, Rule $oSibling = null, /* boolean */ $bPrepend = false) { $sRule = $oRule->getRule(); if(!isset($this->aRules[$sRule])) { $this->aRules[$sRule] = array(); } - $this->aRules[$sRule][] = $oRule; + + $iPosition = $bPrepend ? 0 : count($this->aRules[$sRule]); + + if ($oSibling !== null) { + $iSiblingPos = array_search($oSibling, $this->aRules[$sRule], true); + if ($iSiblingPos !== false) { + $iPosition = $bPrepend ? $iSiblingPos : $iSiblingPos + 1; + } + } + + array_splice($this->aRules[$sRule], $iPosition, 0, array($oRule)); } /**