Using phpQuery to inject <style></style> or external CSS links into inline style="" attributes

32 views
Skip to first unread message

Beau D. Simensen

unread,
Apr 6, 2009, 2:50:14 PM4/6/09
to phpQuery
I've asked a question on Stackoverflow and I think that phpQuery might
be a good place to start on answering. Please check Stackoverflow as
the color coded code might be easier to read than here.

http://stackoverflow.com/questions/720313/what-is-the-best-way-to-process-external-css-stylesheets-and-inject-into-inline-c/720321

Basically, given input:

<style type="text/css">
a {
color: #a8a8a8;
}
.foo {
font-weight: bolder;
}
</style>
<a href="http://blah/blah/blah">click here</a>
<a href="http://foo/foo/foo" class="foo">click here FOO</a>

... I'd like for the following HTML to be output:

<a href="http://blah/blah/blah" style="color: #a8a8a8;">click
here</a>
<a href="http://foo/foo/foo" style="color: #a8a8a8; font-weight:
bolder;">click here FOO</a>

I'd like to have the option of specifying the style information using
<style></style> tags as well as potentially by link tags.

I'm sure I can write this myself with the help of phpQuery, but if
someone has already done something similar to this I'd consider using
that instead.

Thanks!

Tobiasz Cudnik

unread,
Apr 6, 2009, 5:27:47 PM4/6/09
to phpQuery
CSS style-sheets parsing isn't implemented. Although planned in some
sort in QueryTemplates project[1]. Helpful here would be jQuery CSS
methods, which are still not implemented[2].

[1] http://code.google.com/p/querytemplates/issues/detail?id=15
[2] http://code.google.com/p/phpquery/issues/detail?id=36

On Apr 6, 8:50 pm, "Beau D. Simensen" <simen...@gmail.com> wrote:
> I've asked a question on Stackoverflow and I think that phpQuery might
> be a good place to start on answering. Please check Stackoverflow as
> the color coded code might be easier to read than here.
>
> http://stackoverflow.com/questions/720313/what-is-the-best-way-to-pro...

Beau D. Simensen

unread,
Apr 6, 2009, 6:00:55 PM4/6/09
to phpQuery
If I implement something like this using phpQuery, would there be a
place in phpQuery for it to live or would it be better for me to
create another project and just use phpQuery to do the job? I could
possibly try to tackle the missing CSS functionality.

Tobiasz Cudnik

unread,
Apr 8, 2009, 3:51:51 AM4/8/09
to phpQuery
I've created a ticket #121 dedicated to this issue
http://code.google.com/p/phpquery/issues/detail?id=121

You can send patch as comment to this issue or attach it on this
group. Best place for it would be a separate plugin (but not script)
particularly it's static namespace. Important thing is also optional
ability to use new data() method to store properties and not pollute
markup. Than this would be possible:

phpQuery::parseStyleSheets($document);
$hasWidth = in_array('width',
array_keys($document['p.my-p']->data('css'))
);
// but this will also be usable
print $document['p.my-p'];

You can read more about plugins here:
http://code.google.com/p/phpquery/wiki/PluginsServerSide

For a quick testing you can use phpQuery::extend() method to add new
methods, which is not yet documented.
Reply all
Reply to author
Forward
0 new messages