sabberworm / PHP-CSS-Parser

A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output as (optimized) CSS

Home Page:http://www.sabberworm.com/blog/2010/6/10/php-css-parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changes in Rules not persisting

Nickolaus opened this issue · 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

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("...");}