Classes implementing DOM Level 2 CSS and CSS Module: Namespaces (W3C Working Draft 28 August 2006)
A CSSStyleSheet contains all rules. It consists of the different CSS rules like CSSImportRule, CSSStyleRule etc. It also defines the encoding of the style sheet used for serialization. The encoding might by set with an CSSCharsetRule rule or simpler with the encoding attribute. The serialized sheet may be obtained from cssText. All rules are present in cssRules, a stylesheet iterates on its rules so this might be easier. Namespaces are available via namespaces (do not rely on prefixes, see http://www.w3.org/TR/REC-xml-names/ how these work).
CSSStyleSheet represents a CSS style sheet.
Format:
stylesheet
: [ CHARSET_SYM S* STRING S* ';' ]?
[S|CDO|CDC]* [ import [S|CDO|CDC]* ]*
[ namespace [S|CDO|CDC]* ]* # according to @namespace WD
[ [ ruleset | media | page ] [S|CDO|CDC]* ]*
Delete rule at index from the style sheet.
Parameter: | index – The index of the rule to be removed from the StyleSheet’s rule list. For an index < 0 no IndexSizeErr is raised but rules for normal Python lists are used. E.g. deleteRule(-1) removes the last rule in cssRules. index may also be a CSSRule object which will then be removed from the StyleSheet. |
---|---|
Exceptions: |
|
Used to insert a new rule into the style sheet. The new rule now becomes part of the cascade.
Parameters: |
|
---|---|
Returns: | The index within the style sheet’s rule collection |
Exceptions: |
|
The CSSRuleList object represents an (ordered) list of statements.
The items in the CSSRuleList are accessible via an integral index, starting from 0.
Subclasses a standard Python list so theoretically all standard list methods are available. Setting methods like __init__, append, extend or __setslice__ are added later on instances of this class if so desired. E.g. CSSStyleSheet adds append which is not available in a simple instance of this class!
(DOM) Retrieve a CSS rule by ordinal index. The order in this collection represents the order of the rules in the CSS style sheet. If index is greater than or equal to the number of rules in the list, this returns None.
Returns CSSRule, the style rule at the index position in the CSSRuleList, or None if that is not a valid index.
Abstract base interface for any type of CSS statement. This includes both rule sets and at-rules. An implementation is expected to preserve all rules specified in a CSS style sheet, even if the rule is not recognized by the parser. Unrecognized rules are represented using the CSSUnknownRule interface.
The CSSCharsetRule interface represents an @charset rule in a CSS style sheet. The value of the encoding attribute does not affect the encoding of text data in the DOM objects; this encoding is always UTF-16 (also in Python?). After a stylesheet is loaded, the value of the encoding attribute is the value found in the @charset rule. If there was no @charset in the original document, then no CSSCharsetRule is created. The value of the encoding attribute may also be used as a hint for the encoding used on serialization of the style sheet.
The value of the @charset rule (and therefore of the CSSCharsetRule) may not correspond to the encoding the document actually came in; character encoding information e.g. in an HTTP header, has priority (see CSS document representation) but this is not reflected in the CSSCharsetRule.
This rule is not really needed anymore as setting CSSStyleSheet.encoding is much easier.
Format:
charsetrule:
CHARSET_SYM S* STRING S* ';'
BUT: Only valid format is (single space, double quotes!):
@charset "ENCODING";
Represents a CSS comment (cssutils only).
Format:
/*...*/
Represents an @namespace rule within a CSS style sheet.
The @namespace at-rule declares a namespace prefix and associates it with a given namespace (a string). This namespace prefix can then be used in namespace-qualified names such as those described in the Selectors Module [SELECT] or the Values and Units module [CSS3VAL].
Dealing with these rules directly is not needed anymore, easier is the use of cssutils.css.CSSStyleSheet.namespaces.
Format:
namespace
: NAMESPACE_SYM S* [namespace_prefix S*]? [STRING|URI] S* ';' S*
;
namespace_prefix
: IDENT
;
Represents an @import rule within a CSS style sheet. The @import rule is used to import style rules from other style sheets.
Format:
import
: IMPORT_SYM S*
[STRING|URI] S* [ medium [ COMMA S* medium]* ]? S* STRING? S* ';' S*
;
Objects implementing the CSSMediaRule interface can be identified by the MEDIA_RULE constant. On these objects the type attribute must return the value of that constant.
Format:
: MEDIA_SYM S* medium [ COMMA S* medium ]*
STRING? # the name
LBRACE S* ruleset* '}' S*;
Delete the rule at index from the media block.
Parameter: | index – The index of the rule to be removed from the media block’s rule list. For an index < 0 no IndexSizeErr is raised but rules for normal Python lists are used. E.g. deleteRule(-1) removes the last rule in cssRules. index may also be a CSSRule object which will then be removed from the media block. |
---|---|
Exceptions: |
|
Insert rule into the media block.
Parameters: |
|
---|---|
Returns: | the index within the media block’s rule collection of the newly inserted rule. |
Exceptions: |
|
The CSSFontFaceRule interface represents a @font-face rule in a CSS style sheet. The @font-face rule is used to hold a set of font descriptions.
Format:
font_face
: FONT_FACE_SYM S*
'{' S* declaration [ ';' S* declaration ]* '}' S*
;
cssutils uses a CSSStyleDeclaration to represent the font descriptions. For validation a specific profile is used though were some properties have other valid values than when used in e.g. a CSSStyleRule.
The CSSPageRule interface represents a @page rule within a CSS style sheet. The @page rule is used to specify the dimensions, orientation, margins, etc. of a page box for paged media.
Format:
page
: PAGE_SYM S* pseudo_page? S*
LBRACE S* declaration [ ';' S* declaration ]* '}' S*
;
pseudo_page
: ':' IDENT # :first, :left, :right in CSS 2.1
;
The CSSStyleRule object represents a ruleset specified (if any) in a CSS style sheet. It provides access to a declaration block as well as to the associated group of selectors.
Format:
: selector [ COMMA S* selector ]*
LBRACE S* declaration [ ';' S* declaration ]* '}' S*
;
ALPHA STATUS
The CSSVariablesRule interface represents a @variables rule within a CSS style sheet. The @variables rule is used to specify variables.
cssutils uses a CSSVariablesDeclaration to represent the variables.
Some classes in this package support standard Python idioms like iteration on certain attributes:
>>> import cssutils
>>> sheet = cssutils.css.CSSStyleSheet()
>>> sheet.cssText = '@charset "ascii";a { color: green }'
>>> for rule in sheet:
... print rule
...
<cssutils.css.CSSCharsetRule object encoding='ascii' at 0x2ce7
<cssutils.css.CSSStyleRule object selector=u'a' style=u'color:
s=<cssutils.util._Namespaces object at 0x02CE7B30> at 0x2ce7d3
for in is supported by CSSStyleSheet and CSSMediaRule (iterating over the contained CSSRule objects, CSSStyleDeclaration (over the names of the contained Property objects), CSSValueList (over the CSSValue objects in the list).