hijacking cssutils parser

19 views
Skip to first unread message

Yoann Aubineau

unread,
Feb 3, 2009, 11:38:02 AM2/3/09
to cssutils
Hi folks,

I'm using cssutils to parse a file whose syntax is very like css. It
works like a charm. Thank you for such a great piece of code. :)

However I sometimes encounter this slightly different syntax:

#selector {
/* this property's value is a dictionary */
values: {
name1: value1,
name2: value2
}
}

I know it's not css-valid and that cssutils is not supposed to parse
such syntax. But I would like to know if you guys could set me on
rails to hyjack it, so it eventually does parse this special case.

I've messed around with cssutils.profiles already but it seems I
should go deeper into the tokenizer, whose grammar appears to be hard-
coded, unfortunately.

So, here where I am. Any tips ?

Cheers,
Yoann

see

unread,
Feb 3, 2009, 3:58:49 PM2/3/09
to cssutils
hi,
not sure if that is very easy but you have to change the CSSValue
class in css/cssvalue.py as that is the class that handles the parsing
at this point.

When a CSSValue is set CSS._setCssText(...) is called and handles the
actual parsing. In the current trunk (or I also think in 0.9.6a0 but
am not sure) this method encounters the starting { of the dictionary
but cannot find a match in the defined ``valueprods`` definition.

Unfortunately this is just the area which has changed in the last
release and may or may not change again. Defining the parsing as it is
done here should later be done in all classes but this was the prove
of concept and I fear is not complete yet

but let me know if that helps

Thanks
Chris

Walter D?örwald

unread,
Feb 4, 2009, 6:01:52 AM2/4/09
to see, cssutils
see wrote:

> hi,
> not sure if that is very easy but you have to change the CSSValue
> class in css/cssvalue.py as that is the class that handles the parsing
> at this point.
>
> When a CSSValue is set CSS._setCssText(...) is called and handles the
> actual parsing. In the current trunk (or I also think in 0.9.6a0 but
> am not sure) this method encounters the starting { of the dictionary
> but cannot find a match in the defined ``valueprods`` definition.
>
> Unfortunately this is just the area which has changed in the last
> release and may or may not change again. Defining the parsing as it is
> done here should later be done in all classes but this was the prove
> of concept and I fear is not complete yet
>
> but let me know if that helps

Also you could build a parser yourself. There are various options for
this in Python. I've been using the spark module for that

http://pages.cpsc.ucalgary.ca/~aycock/spark/

which makes it suprisingly simple to create parsers.

Servus,
Walter

Yoann Aubineau

unread,
Feb 5, 2009, 4:16:37 AM2/5/09
to Walter D?örwald, see, cssutils
Thanks guys for your valuable inputs.

I might try to override CSSValue to bend _setCssText setter to my
needs. But man, this is a huge method ! Otherwise, I'm afraid I'll
have to write my own parser with pyparsing or spark, as Walter
suggested.

I'll let you know if I end up with anything working.

2009/2/4 Walter D?örwald <doerw...@googlemail.com>:
Reply all
Reply to author
Forward
0 new messages