PHP-CSS-Parser icon indicating copy to clipboard operation
PHP-CSS-Parser copied to clipboard

RuleSet::addRule does not behave as expected

Open FMCorz opened this issue 8 years ago • 1 comments

Adding a rule before only works when a rule with identical name has already been added. This does not work for me as I'm looking at automatically adding prefixed rules before the original one.

Here is a testcase: https://github.com/sabberworm/PHP-CSS-Parser/compare/master...moodlehq:addrule-before-testcase

The problem mainly comes from the fact that rules are stored in an associative array. That also is a problem when renaming the rule to another one, where it remains under the key it was initially added, but that's for another issue.

FMCorz avatar Aug 01 '16 10:08 FMCorz

@sabberworm @FMCorz Any update on this, I am facing same issue. Since we need this fix, we fixed it in our fork by adding a condition to addRule method.

if (!is_null($oSibling)) {
	$sRule = $oSibling->getRule();
}

After change:

public function addRule(Rule $oRule, Rule $oSibling = null) {
	$sRule = $oRule->getRule();

	if (!is_null($oSibling)) {
		$sRule = $oSibling->getRule();
	}

	if(!isset($this->aRules[$sRule])) {
		$this->aRules[$sRule] = array();
	}

	$iPosition = count($this->aRules[$sRule]);

	if ($oSibling !== null) {
		$iSiblingPos = array_search($oSibling, $this->aRules[$sRule], true);
		if ($iSiblingPos !== false) {
			$iPosition = $iSiblingPos;
		}
	}

	array_splice($this->aRules[$sRule], $iPosition, 0, array($oRule));
}

reardestani avatar Nov 03 '18 10:11 reardestani