[lxml-dev] CSS and lxml

Stefan Behnel stefan_ml at behnel.de
Mon Sep 17 16:37:05 CEST 2007


Frederik Elwert wrote:
> Am Montag, den 17.09.2007, 15:07 +0200 schrieb Stefan Behnel:
>> 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.

If you don't want to alter the tree, you can use a dict to map Elements to a
style dict. However, note that Elements are not currently weak referenceable,
so you'd have to make sure the trees are discarded after use.


>> 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.

Agreed. However, you can't store anything in Elements that is not reflected by
the underlying tree, as Element objects (which are actually just proxies) can
be garbage collected while the tree stays alive.

You can also store style information in the tree under a separate namespace.


> 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!

I don't think cssselect.py uses any 2.0 specific features. Copying it over to
1.3 (or into your own code base) should work as a temporary solution.

Stefan



More information about the lxml-dev mailing list