How to get from a rule to a property.

123 views
Skip to first unread message

Dave Pawson

unread,
Jun 8, 2009, 5:53:23 AM6/8/09
to CSSUtils
I want to check each property as being valid (compared to being 'wellformed')

I can't see how to get from a rule to a set of properties?

This is my code so far.

Any help appreciated.

I want to get to
http://cssutils.googlecode.com/svn/trunk/docs/html/docs/css.html#cssutils.css.Property.cssText

a 'CSSStyleDeclaration/ think.

TIA, DaveP


#class cssutils.css.CSSRuleList(*ignored)
#
#
def checkrules(sheet):
nrules = sheet.cssRules.length
for item in range(0 , nrules):
rule = sheet.cssRules[item]
print rule.type, rule.typeString,
print rule.cssText


#
# Check a list of stylesheets
#
def ckSS(list):
for item in list:
parser=cssutils.CSSParser()
sheet = parser.parseFile(item,'utf-8')
print "******************* Checking %s **********************" % item
checkrules(sheet)
#
# List of stylesheets
#
ssList=['../css/f.css','../css/t.cake.css','../css/t.comments.css','../css/t.generic.css',]

ckSS(ssList)


--
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
http://www.dpawson.co.uk

David King

unread,
Jun 8, 2009, 2:36:55 PM6/8/09
to Dave Pawson, CSSUtils
> I want to check each property as being valid (compared to being
> 'wellformed')

Take a look at http://code.reddit.com/browser/r2/r2/lib/cssfilter.py#L236
, we do this at reddit

see

unread,
Jun 8, 2009, 2:45:23 PM6/8/09
to cssutils
hi Dave,
simple example:

>>> import cssutils
>>> s = cssutils.parseString('@charset "ascii";a { color: red; left: 0} b { colo
r: green}')
>>> for rule in s.cssRules: # all rules
... if rule.type == rule.STYLE_RULE:
... for property in rule.style:
... print property
...
<cssutils.css.property.Property object name=u'color' value=u'red'
priority=u'' v
alid=True at 0x2cdad10>
<cssutils.css.property.Property object name=u'left' value=u'0'
priority=u'' vali
d=True at 0x2cdae30>
<cssutils.css.property.Property object name=u'color' value=u'green'
priority=u''
valid=True at 0x2cdaa30>
>>>

OR

>>> s.cssRules[1].style.getProperties() # for 1st stylerule only
[cssutils.css.Property(name=u'color', value=u'red', priority=u''),
cssutils.css.
Property(name=u'left', value=u'0', priority=u'')]
>>>

A few points to note:
- most objects are iterable, so you can simply iterate over all
cssRules, all properties in a CSSStyleDeclaration
- check if you got a style rule at all (you may also need to check
@media or @page rules)
- a CSSStyleRule has a CSSStyleDeclaration as property ``style`` which
in turn has access to all containing properties
- a simplified object structure of a sheet:
+ CSSStyleSheet
+ .cssRules (a CSSRuleList object)
+ CSSCharsetRule
+ [...]
+ CSSStyleRule
+ .style [CSSStyleDeclaration]
+ Property objects
+ .name + .value [CSSValue etc]
+ .priority

- best place in the docs to look for you problem might be
http://cssutils.googlecode.com/svn/trunk/docs/html/docs/css.html#style-related-classes-cssstyledeclaration-property-cssvalue-etc

hope this helps.

Chris

Dave Pawson

unread,
Jun 9, 2009, 2:37:02 AM6/9/09
to David King, CSSUtils
Hi David.

2009/6/8 David King <dk...@ketralnis.com>:

>cssText¶
(DOM) The parsable textual representation.

That seems to work on type string?
Is this

cssText¶
(DOM) The parsable textual representation.
Am I right in assuming this is the 'internal' model of the stylesheet
used for processing, as apposed to some other model?
I.e. I should be aiming for this rather than an array or struct of rules?

regards

David King

unread,
Jun 9, 2009, 3:06:18 AM6/9/09
to Dave Pawson, CSSUtils
>> Take a look at http://code.reddit.com/browser/r2/r2/lib/cssfilter.py#L236
>> ,
>> we do this at reddit
>>
> cssText (DOM) The parsable textual representation.
> That seems to work on type string? [...]

> Am I right in assuming this is the 'internal' model of the stylesheet
> used for processing, as apposed to some other model?
> I.e. I should be aiming for this rather than an array or struct of
> rules?

We only use the cssText to validate URLs (we don't allow off-site
URLs). For everything else, we use the .valid and .wellformed
properties of rules and properties, like <http://code.reddit.com/browser/r2/r2/lib/cssfilter.py#L280
> and <http://code.reddit.com/browser/r2/r2/lib/cssfilter.py#L198>
(esp. the valid_value, which actually checks the "good"ness of a given
value). We iterate over the rules at <http://code.reddit.com/browser/r2/r2/lib/cssfilter.py#L271
> ("for rule in parsed.cssRules")

Dave Pawson

unread,
Jun 9, 2009, 3:26:05 AM6/9/09
to David King, CSSUtils
2009/6/9 David King <dk...@ketralnis.com>:

> We only use the cssText to validate URLs (we don't allow off-site URLs).

Sensible precaution I guess.


For
> everything else, we use the .valid and .wellformed properties of rules and
> properties,

I was expecting 'wellformed' to be a boolean... but it seems not to be?
I tried if wellformed and never found the else clause.
Valid and wellformed I took to be similar to XML well-formedness and
validity... Mmm :-)

like
> <http://code.reddit.com/browser/r2/r2/lib/cssfilter.py#L280> and
> <http://code.reddit.com/browser/r2/r2/lib/cssfilter.py#L198> (esp. the
> valid_value, which actually checks the "good"ness of a given value).  We
> iterate over the rules at
> <http://code.reddit.com/browser/r2/r2/lib/cssfilter.py#L271> ("for rule in
> parsed.cssRules")


Thanks for the links.

Dave Pawson

unread,
Jun 9, 2009, 4:06:16 AM6/9/09
to CSSUtils
Working now, I have the property and a cssutils.css.CSSRule

http://cssutils.googlecode.com/svn/trunk/docs/html/docs/css.html#cssutils.css.CSSRule

Doesn't have any access to the selector, other than via the cssText method which
dumps the entire rule?

Is there a simpler way to get to the selector please?


TIA

see

unread,
Jun 9, 2009, 1:29:35 PM6/9/09
to cssutils
> cssText¶
>     (DOM) The parsable textual representation.
> Am I right in assuming this is the 'internal' model of the stylesheet
> used for processing, as apposed to some other model?
> I.e. I should be aiming for this rather than an array or struct of rules?

The spec sounds great here (cssText) but just means a string yes ;)

So almost any object of a parsed sheet has a cssText to reserialize
the "thing", e.g. a rule, a styledeclaration or a property or value.
There are a few exceptions but they are basically the same: Selector
has selectorText instead of cssText.

see

unread,
Jun 9, 2009, 1:33:59 PM6/9/09
to cssutils
> I was expecting 'wellformed' to be a boolean... but it seems not to be?
> I tried if wellformed and never found the else clause.
> Valid and wellformed I took to be similar to XML well-formedness and
> validity... Mmm :-)

in cssutils they aim to be a bit like that. Actually well-formed is
almost never False so not that useful really...
And only in the current alpha releases valid does make more sense. The
whole validation stuff has been rewritten as you may now defined your
own properties with valid (or invalid) values.

Dave Pawson

unread,
Jun 9, 2009, 1:35:58 PM6/9/09
to see, cssutils
2009/6/9 see <cth...@gmail.com>:


Tks Chris.

def checkrules(sheet):
nrules = sheet.cssRules.length
for item in range(0 , nrules):

rule = sheet.cssRules[item] #class cssutils.css.CSSRule
if rule.type == rule.STYLE_RULE:
print rule.selectorText, "{"
for property in rule.style:
print "\t",property.name, " \t:",
print "\t",property.value
if property.valid:
pass
else:
print "Invalid property"
sys.exit(2)
print "}"
print "\t\t-----"

Seems to do what I want, show the rule (and barf at any invalidity :-)
which is what I wanted .... need (with the errors I make in writing CSS).

I've been fighting quirks mode| 'strict' / compliant mode today!
that really is silly!

Dave Pawson

unread,
Jun 9, 2009, 1:37:24 PM6/9/09
to cssutils
2009/6/9 see <cth...@gmail.com>:

>
>> I was expecting 'wellformed' to be a boolean... but it seems not to be?
>> I tried if wellformed and never found the else clause.
>> Valid and wellformed I took to be similar to XML well-formedness and
>> validity... Mmm :-)
>
> in cssutils they aim to be a bit like that. Actually well-formed is
> almost never False so not that useful really...

?Nothing said in the documentation?


> And only in the current alpha releases valid does make more sense. The
> whole validation stuff has been rewritten as you may now defined your
> own properties with valid (or invalid) values.

Interesting. Where is that documented please?

see

unread,
Jun 9, 2009, 1:37:43 PM6/9/09
to cssutils
> http://cssutils.googlecode.com/svn/trunk/docs/html/docs/css.html#cssu...
>
> Doesn't have any access to the selector, other than via the cssText method which
> dumps the entire rule?
>
> Is there a simpler way to get to the selector please?

check http://cssutils.googlecode.com/svn/trunk/docs/html/docs/css.html#cssstylerule

CSSStyleRule has besides .selectorText also a .selectorList which in
turn has Selector objects for each selector.

>>> import cssutils
>>> s = cssutils.parseString('a,b {color: red}')
>>> r = s.cssRules[0]
>>> print r.selectorList
<cssutils.css.SelectorList object selectorText=u'a, b'
_namespaces=<cssutils.uti
l._Namespaces object at 0x028EE310> at 0x28ee6b0>
>>> for s in r.selectorList:
... print s
...
<cssutils.css.Selector object selectorText=u'a' specificity=(0, 0, 0,
1) _namesp
aces=cssutils.util._SimpleNamespaces({}) at 0x28ee670>
<cssutils.css.Selector object selectorText=u'b' specificity=(0, 0, 0,
1) _namesp
aces=cssutils.util._SimpleNamespaces({}) at 0x28ee6f0>

see also http://cssutils.googlecode.com/svn/trunk/docs/html/docs/css.html#selectorlist

see

unread,
Jun 9, 2009, 1:48:31 PM6/9/09
to cssutils


On Jun 9, 7:37 pm, Dave Pawson <dave.paw...@gmail.com> wrote:
> 2009/6/9 see <cthe...@gmail.com>:
>
>
>
> >> I was expecting 'wellformed' to be a boolean... but it seems not to be?
> >> I tried if wellformed and never found the else clause.
> >> Valid and wellformed I took to be similar to XML well-formedness and
> >> validity... Mmm :-)
>
> > in cssutils they aim to be a bit like that. Actually well-formed is
> > almost never False so not that useful really...
>
> ?Nothing said in the documentation?
>
> > And only in the current alpha releases valid does make more sense. The
> > whole validation stuff has been rewritten as you may now defined your
> > own properties with valid (or invalid) values.
>
> Interesting. Where is that documented please?

http://cssutils.googlecode.com/svn/trunk/docs/html/index.html :)

There is a change history but I guess there still are a few things
undocumented. But regarding the time I have to spare on the lib I
think it is not that bad. Also most things can be tried on the console
just by looking at e.g. dir(object)
Reply all
Reply to author
Forward
0 new messages