[lxml-dev] CSS and lxml

Frederik Elwert felwert at uni-bremen.de
Mon Sep 17 16:12:40 CEST 2007


Am Montag, den 17.09.2007, 15:07 +0200 schrieb Stefan Behnel:
> There are a couple of things you have to do here. First, you have to parse
> CSS, which only the cssutils currently do. Then you have to find out which of
> the rules apply to an element which AFAICT is not currently supported at all.

No, cssutils supports only parsing and generating CSS, but not
element-based style selection. And if it would, I guess they'd stick to
xml.dom or something.

> You could do a brute force test and just take all selectors that you find in
> all CSS stylesheets in the document or in external references, to match them
> against the element in question - but that would be quite some overhead.

I thought about that and came to the same conclusion as you do regarding
the overhead.

> On
> the other hand, if style lookup is more frequent than document parsing, you
> can build an inverse index: run through all CSS selectors, find the elements
> they match and store the style content for each of the elements, thus
> aggregating the style properties per element.

This would be quite practical, right. I'm just not sure about where to
store the information.

> You could maybe implement a "cssannotate(stylesheet, tree)" function, which
> would map a stylesheet on a tree by setting (or extending) the "style"
> attributes on each element accordingly. That would come pretty close to what
> you were looking for.

This just had the negative side-effect of changing the tree itself. So
it would only be applicable for read-only-operations, since one wouldn't
want to put all style permanently into style attributes for most use
cases.

Hm, I have to think about this. But it seems that a combination of
lxml.cssselect and cssutils would quite do. Since I don't want to rely
on lxml 2.0 yet, I'd wait for the implementation anyway. Thanks for your
hints!

Regards,
Frederik



More information about the lxml-dev mailing list