Re: HTML_CSS -> howto parse?

From: Date: Mon, 10 May 2004 17:43:04 +0000
Subject: Re: HTML_CSS -> howto parse?
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-29057@lists.php.net to get a copy of this message
hi, It seems you did not understood my problem, or I did not explain well since I am not very good in ingleesh :) (Or perhaps I did not understood your answer too;) I don't want to create CSS code, I want to read CSS code. What I have to do is to get in a var, the CSS code that is in the head of the document, then remove it (or it will produce an error), and then to put each css code block in each tag of the document in theirs 'style' attribute. I had a quick look at HTML_CSS and it seems it is only designed to create CSS code and not to read it yet. Am I wrong? Here is an exemple, I have to transform the code of the file "what_I_have.svg" to a string "$what_I_want" before to feed IM with it. Cheers -- ================(file->"what_I_have.svg")== <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [ <!ATTLIST svg xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink"> ]> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1" x="0.00000000" y="0.00000000" width="500.000000" height="500.000000" id="svg548"> <defs id="defs550"> <style type='text/css'><![CDATA[ #path557 { font-size: 12; fill: #be8980; fill-rule: evenodd; stroke-width: 1pt; } #path556 { font-size: 12; fill: #a0c180; fill-rule: evenodd; stroke: #7d797a; stroke-width: 7.625; } ]]></style> </defs> <path d="M 113.588 551.237 L 484.421 551.237 L 299.004 130.292 L 113.588 551.237 z " transform="matrix(0.791946,0.000000,0.000000,0.791946,-43.18425,-36.36806)" id="path557" /> <path d="M 611.372 456.024 C 611.372 587.88 310.697 587.88 310.697 456.024 C 310.697 324.168 611.372 324.168 611.372 456.024 z " transform="matrix(0.791946,0.000000,0.000000,0.791946,-36.50262,13.74443)" id="path556" /> </svg> <!-- vim:cindent sw=2 ts=2 sts=2 et fdm=marker --> ================(var->"$what_I_want")== <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [ <!ATTLIST svg xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink"> ]> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1" x="0.00000000" y="0.00000000" width="500.000000" height="500.000000" id="svg548"> <defs id="defs550" /> <path d="M 113.588 551.237 L 484.421 551.237 L 299.004 130.292 L 113.588 551.237 z " transform="matrix(0.791946,0.000000,0.000000,0.791946,-43.18425,-36.36806)" style="font-size:12;fill:#be8980;fill-rule:evenodd;stroke-width:1pt;" id="path557" /> <path d="M 611.372 456.024 C 611.372 587.88 310.697 587.88 310.697 456.024 C 310.697 324.168 611.372 324.168 611.372 456.024 z " transform="matrix(0.791946,0.000000,0.000000,0.791946,-36.50262,13.74443)" style="font-size:12;fill:#a0c180;fill-rule:evenodd;stroke:#7d797a;stroke-width:7.625;" id="path556" /> </svg> ================ -- > >It is for my ImageMagick->Conjure class, as IM does not support CSS yet, > >I need to replace all class tags by the style tag with the css code to put > >each style included (and also add the css code for id tags but without > >removing it, or perhaps removing it since it is just a replacement in a > >buffer before giving it to ImageMagick in a blob). > > Why not add a $css object to your class, using an instance of > HTML_CSS... it provides full CSS support. Currently it has problems with > inheritance, but I'm planning on fixing that next week. Give it a try. > > >So I am searching a Pear package to parse the CSS code into a PHP > > variable. Is there one? > >And even perhaps if it exists a class to parse the document and look for > >matching tags with the css code? > >(I don't think there is yet this, but have to ask to be sure.) > > Try this: > > $css = new HTML_CSS(array()); // add whatever attribs you want. > $css->parseString($string); > // or > // $css->parseFile($filename); > > There are a few quirks in parseString (which is also called by > parseFile) due to the fact that all groups are moved to initial > position, which can cause problems. As I mentioned before, I'm going to > work on that this coming week. > > You can use the regular output options: toString() and display(). You > can also define the mime an character encoding, etc. and access each > selector individually (even retrieving values, etc.). > > Give it a spin :-)

« previous php.pear.dev (#29057) next »