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

Changes in Rules not persisting

Open Nickolaus opened this issue 8 years ago • 1 comments

I am trying to change an Url inside a font rule, but my changes are not persisted

foreach ($ruleSet->getRules() as $rule) {
                   $value = $rule->getValue();

                    if ($value instanceof URL) {
                        $url = $value->getURL();
                        $newUrlAsString = '...'
                        $newUrl = new CSSString($newUrlAsString);
                        $value->setURL($newUrl);

                        $rule->setValue($value);
                    }
  }

If I dump the RuleSet outside the loop the changes are lost. Also if I clone the rule and remove the old rule from the RuleSet, then commit the changes to the cloned rule, which is then appended to the RuleSet the old url is still in the RuleSet and I can't see any changes

Nickolaus avatar Sep 09 '16 18:09 Nickolaus

For me the following works:

$oParser = new Sabberworm\CSS\Parser(<<<CSS
	.test {
		src: url('test');
	}
CSS
);
$oDoc = $oParser->parse();

$ruleSet = $oDoc->getAllRuleSets()[0];

foreach ($ruleSet->getRules() as $rule) {
                   $value = $rule->getValue();

                    if ($value instanceof \Sabberworm\Css\Value\URL) {
                        $url = $value->getURL();
                        $newUrlAsString = '...';
                        $newUrl = new \Sabberworm\Css\Value\CSSString($newUrlAsString);
                        $value->setURL($newUrl);

                        $rule->setValue($value);
                    }
  }

print($oDoc->render());

outputs:

.test {src: url("...");}

sabberworm avatar Dec 08 '18 10:12 sabberworm