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). Variables are available via variables which is a CSSVariablesDeclaration and contains all available variables including the ones defined in imported style sheets. Changes to this object do NOT change the parent sheet!
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]* ]*
Add rule to style sheet at appropriate position. Same as insertRule(rule, inOrder=True).
Not used in cssutils yet.
Textual representation of the stylesheet (a byte string).
Delete rule at index from the style sheet.
Parameters: | 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 : |
|
Encoding set in CSSCharsetRule or if None resulting in default utf-8 encoding being used.
If the style sheet is a linked style sheet, the value of this attribute is its location. For inline style sheets, the value of this attribute is None. See the href attribute definition for the LINK element in HTML 4.0, and the href pseudo-attribute for the XML style sheet processing instruction.
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 : |
|
All Namespaces used in this CSSStyleSheet.
Not used in cssutils yet.
A ref to an @import rule if it is imported, else None.
For style sheet languages that support the concept of style sheet inclusion, this attribute represents the including style sheet, if one exists. If the style sheet is a top-level style sheet, or the style sheet language does not support inclusion, the value of this attribute is None.
Internal readonly attribute, DO NOT USE!
Set the cssutils global Serializer used for all output.
Set a Preference of CSSSerializer used for output. See cssutils.serialize.Preferences for possible preferences to be set.
This specifies the style sheet language for this style sheet. The style sheet language is specified as a content type (e.g. text/css). The content type is often specified in the ownerNode. Also see the type attribute definition for the LINK element in HTML 4.0, and the type pseudo-attribute for the XML style sheet processing instruction. For CSS this is always text/css.
A cssutils.css.CSSVariablesDeclaration containing all available variables in this CSSStyleSheet including the ones defined in imported sheets.
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.
cssutils.css.CSSCharsetRule (not used in CSSOM anymore)
cssutils.css.CSSComment - not in the offical spec, Value has changed in 0.9.7a3
cssutils.css.MarginRule - experimental rule not in the offical spec
cssutils.css.CSSNamespaceRule, Value has changed in 0.9.7a3 due to a change in the CSSOM spec.
cssutils.css.CSSUnknownRule (not used in CSSOM anymore)
cssutils.css.CSSVariablesRule - experimental rule not in the offical spec
Normalized keyword of an @rule (e.g. @import).
(DOM) The parsable textual representation of the rule. This reflects the current state of the rule and not its initial value.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
Internal readonly attribute, DO NOT USE!
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
If the rule is wellformed.
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!
Implemented in class using a CSSRuleList only.
Implemented in class using a CSSRuleList only.
Raises ValueError if the value is not present.
L.insert(index, object) – insert object before index
(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.
(DOM) The number of CSSRules in the list.
Raises IndexError if list is empty or index is out of range.
L.remove(value) – remove first occurrence of value. Raises ValueError if the value is not present.
L.reverse() – reverse IN PLACE
Yield the rules which have the given type only, one of the constants defined in cssutils.css.CSSRule.
L.sort(cmp=None, key=None, reverse=False) – stable sort IN PLACE; cmp(x, y) -> -1, 0, 1
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";
Normalized keyword of an @rule (e.g. @import).
(DOM) The parsable textual representation.
(DOM)The encoding information used in this @charset rule.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
Internal readonly attribute, DO NOT USE!
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
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.
Normalized keyword of an @rule (e.g. @import).
(DOM) The parsable textual representation of this rule.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
Internal readonly attribute, DO NOT USE!
(DOM) The declaration-block of this rule set, a CSSStyleDeclaration.
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
CSSFontFace is valid if properties font-family and src are set and all properties are valid.
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*
;
Normalized keyword of an @rule (e.g. @import).
(DOM) The parsable textual representation of this rule.
Location of the style sheet to be imported.
An optional name for the imported sheet.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
Internal readonly attribute, DO NOT USE!
(readonly) The style sheet referred to by this rule.
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
Depending on if media is used at all.
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*;
Add rule to page rule. Same as insertRule(rule).
Normalized keyword of an @rule (e.g. @import).
(DOM) The parsable textual representation of this rule.
Delete the rule at index from rules cssRules.
Parameters: | index – The index of the rule to be removed from the rules cssRules 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. |
---|---|
Exceptions : |
|
Implements base insertRule.
An optional name for this media rule.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
Internal readonly attribute, DO NOT USE!
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
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
;
Normalized keyword of an @rule (e.g. @import).
(DOM) The parsable textual representation of this rule.
URI (handled as simple string) of the defined namespace.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
Prefix used for the defined namespace.
Internal readonly attribute, DO NOT USE!
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
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* IDENT? pseudo_page? S*
'{' S* [ declaration | margin ]? [ ';' S* [ declaration | margin ]? ]* '}' S*
;
pseudo_page :
':' [ "left" | "right" | "first" ]
;
margin :
margin_sym S* '{' declaration [ ';' S* declaration? ]* '}' S*
;
margin_sym :
TOPLEFTCORNER_SYM |
TOPLEFT_SYM |
TOPCENTER_SYM |
TOPRIGHT_SYM |
TOPRIGHTCORNER_SYM |
BOTTOMLEFTCORNER_SYM |
BOTTOMLEFT_SYM |
BOTTOMCENTER_SYM |
BOTTOMRIGHT_SYM |
BOTTOMRIGHTCORNER_SYM |
LEFTTOP_SYM |
LEFTMIDDLE_SYM |
LEFTBOTTOM_SYM |
RIGHTTOP_SYM |
RIGHTMIDDLE_SYM |
RIGHTBOTTOM_SYM
;
cssRules contains a list of MarginRule objects.
Add rule to page rule. Same as insertRule(rule).
Normalized keyword of an @rule (e.g. @import).
(DOM) The parsable textual representation of this rule.
Delete the rule at index from rules cssRules.
Parameters: | index – The index of the rule to be removed from the rules cssRules 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. |
---|---|
Exceptions : |
|
Implements base insertRule.
Return list of all set margins (MarginRule).
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
(DOM) The parsable textual representation of the page selector for the rule.
Internal readonly attribute, DO NOT USE!
Specificity of this page rule (READONLY). Tuple of (f, g, h) where:
- if the page selector has a named page, f=1; else f=0
- if the page selector has a ‘:first’ pseudo-class, g=1; else g=0
- if the page selector has a ‘:left’ or ‘:right’ pseudo-class, h=1; else h=0
(DOM) The declaration-block of this rule set, a CSSStyleDeclaration.
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
A margin at-rule consists of an ATKEYWORD that identifies the margin box (e.g. '@top-left‘) and a block of declarations (said to be in the margin context).
Format:
margin :
margin_sym S* '{' declaration [ ';' S* declaration? ]* '}' S*
;
margin_sym :
TOPLEFTCORNER_SYM |
TOPLEFT_SYM |
TOPCENTER_SYM |
TOPRIGHT_SYM |
TOPRIGHTCORNER_SYM |
BOTTOMLEFTCORNER_SYM |
BOTTOMLEFT_SYM |
BOTTOMCENTER_SYM |
BOTTOMRIGHT_SYM |
BOTTOMRIGHTCORNER_SYM |
LEFTTOP_SYM |
LEFTMIDDLE_SYM |
LEFTBOTTOM_SYM |
RIGHTTOP_SYM |
RIGHTMIDDLE_SYM |
RIGHTBOTTOM_SYM
;
e.g.:
@top-left {
content: "123";
}
Margin area of parent CSSPageRule. margin and atkeyword are both normalized @keyword of the @rule.
(DOM) The parsable textual representation.
Margin area of parent CSSPageRule. margin and atkeyword are both normalized @keyword of the @rule.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
Internal readonly attribute, DO NOT USE!
(DOM) The declaration-block of this rule set.
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
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*
;
Normalized keyword of an @rule (e.g. @import).
(DOM) The parsable textual representation of this rule.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
The SelectorList of this rule.
(DOM) The textual representation of the selector for the rule set.
Internal readonly attribute, DO NOT USE!
(DOM) The declaration-block of this rule set.
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
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.
Format:
variables
VARIABLES_SYM S* medium [ COMMA S* medium ]* LBRACE S*
variableset* '}' S*
;
for variableset see cssutils.css.CSSVariablesDeclaration
Media are not implemented. Reason is that cssutils is using CSS variables in a kind of preprocessing and therefor no media information is available at this stage. For now do not use media!
Example:
@variables {
CorporateLogoBGColor: #fe8d12;
}
div.logoContainer {
background-color: var(CorporateLogoBGColor);
}
Normalized keyword of an @rule (e.g. @import).
(DOM) The parsable textual representation of this rule.
NOT IMPLEMENTED! As cssutils resolves variables during serializing media information is lost.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
Internal readonly attribute, DO NOT USE!
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
NOT IMPLEMTED REALLY (TODO)
(DOM) The variables of this rule set, a cssutils.css.CSSVariablesDeclaration.
Represents a CSS comment (cssutils only).
Format:
/*...*/
Normalized keyword of an @rule (e.g. @import).
The parsable textual representation of this rule.
The Parent Node of this CSSRule or None.
If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns None.
The style sheet that contains this rule.
Internal readonly attribute, DO NOT USE!
The type of this rule, as defined by a CSSRule type constant.
Descriptive name of this rule’s type.
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).