PHP-CSS-Parser
PHP-CSS-Parser copied to clipboard
Changes in Rules not persisting
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
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("...");}