A global object cssutils.profile is used for validation of all properties. It is an instance of cssutils.profiles.Profiles. Add or remove new profile definitions here.
Most important method is cssutils.profiles.Profiles.addProfile() (use cssutils.profile.addProfile) to add new properties to cssutils and the setting of defaultProfiles.
Example of how to add a new profile:
>>> import cssutils
>>> sheet = cssutils.parseString('x { -test-custommacro: x }')
>>> print sheet.cssRules[0].style.getProperties()[0].valid
False
>>> M1 = {
... 'testvalue': 'x'
... }
>>> P1 = {
... '-test-tokenmacro': '({num}{w}){1,2}',
... '-test-macro': '{ident}|{percentage}',
... '-test-custommacro': '{testvalue}',
... # custom validation function
... '-test-funcval': lambda(v): int(v) > 0
... }
>>> cssutils.profile.addProfile('test', P1, M1)
>>> sheet = cssutils.parseString('x { -test-custommacro: x }')
>>> print sheet.cssRules[0].style.getProperties()[0].valid
True
An additional per CSSStyleSheet setting of a profile may be added soon.
Please note: This might change again, but only slightly as it has been refactored in 0.9.6a2.
Two dictionaries which contain macro and property definitions for the predefined property profiles.
Both use the additional macros defined in Profiles._TOKEN_MACROS and Profiles._MACROS which contain basic macros for definition of new properties. Things like ident, name or hexcolor are defined there and may be used in any new property definition as these two macro sets defined in Profiles are added to any custom macro definition given. You may overwrite these basic macros with your own macros or simply define your own macros and use only these.
Use cssutils.profiles.macros if you need any other predefined macro or cssutils.profiles.properties if you want to add any known property to your custom property profile.
All profiles used for validation. cssutils.profile is a preset object of this class and used by all properties for validation.
Predefined profiles are (use propertiesByProfile() to get a list of defined properties):
Predefined macros are:
If you want to redefine any of these macros do this in your custom macros.
Add a new profile with name profile (e.g. ‘CSS level 2’) and the given properties.
Parameters: |
|
---|
Add a list of profiles at once. Useful as if profiles define custom macros these are used in one go. Using addProfile instead my be very slow instead.
Names of profiles to use for validation.To use e.g. the CSS2 profile set cssutils.profile.defaultProfiles = cssutils.profile.CSS_LEVEL_2
All known property names of all profiles.
Names of all profiles in order as defined.
Generator: Yield property names, if no profiles is given all profile’s properties are used.
Parameters: | profiles – a single profile name or a list of names. |
---|
Remove profile or remove all profiles.
If the removed profile used custom macros all remaining profiles are reset to reflect the macro changes. This may be quite an expensive operation!
Parameters: |
|
---|---|
Exceptions : |
|
Check if value is valid for given property name using any profile.
Parameters: |
|
---|---|
Returns: | if the value is valid for the given property name in any profile |
Check if value is valid for given property name returning (valid, profile).
Parameters: |
|
---|---|
Returns: | valid, matching, profiles where valid is if the value is valid for the given property name in any profile, matching==True if it is valid in the given profiles and profiles the profile names for which the value is valid (or [] if not valid at all) |
Example:
>>> cssutils.profile.defaultProfiles = cssutils.profile.CSS_LEVEL_2
>>> print cssutils.profile.validateWithProfile('color', 'rgba(1,1,1,1)')
(True, False, Profiles.CSS3_COLOR)