Logo Search packages:      
Sourcecode: calibre version File versions


00001 """CSSStyleRule implements DOM Level 2 CSS CSSStyleRule."""
__all__ = ['CSSStyleRule']
__docformat__ = 'restructuredtext'
__version__ = '$Id: cssstylerule.py 1815 2009-07-29 16:51:58Z cthedot $'

from cssstyledeclaration import CSSStyleDeclaration
from selectorlist import SelectorList
import cssrule
import cssutils
import xml.dom

00012 class CSSStyleRule(cssrule.CSSRule):
    """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.
        : selector [ COMMA S* selector ]*
        LBRACE S* declaration [ ';' S* declaration ]* '}' S*
00023     def __init__(self, selectorText=None, style=None, parentRule=None, 
                 parentStyleSheet=None, readonly=False):
                string parsed into selectorList
                string parsed into CSSStyleDeclaration for this CSSStyleRule
                if True allows setting of properties in constructor only
        super(CSSStyleRule, self).__init__(parentRule=parentRule, 

        self._selectorList = SelectorList(parentRule=self)
        self._style = CSSStyleDeclaration(parentRule=self)
        if selectorText:
            self.selectorText = selectorText            
        if style:
            self.style = style

        self._readonly = readonly

    def __repr__(self):
        if self._namespaces:
            st = (self.selectorText, self._namespaces)
            st = self.selectorText 
        return "cssutils.css.%s(selectorText=%r, style=%r)" % (
                self.__class__.__name__, st, self.style.cssText)

    def __str__(self):
        return "<cssutils.css.%s object selector=%r style=%r _namespaces=%r at 0x%x>" % (
                self.__class__.__name__, self.selectorText, self.style.cssText,
                self._namespaces, id(self))

00059     def _getCssText(self):
        """Return serialized property cssText."""
        return cssutils.ser.do_CSSStyleRule(self)

00063     def _setCssText(self, cssText):
        :param cssText:
            a parseable string or a tuple of (cssText, dict-of-namespaces)
            - :exc:`~xml.dom.NamespaceErr`:
              Raised if the specified selector uses an unknown namespace
            - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified CSS string value has a syntax error and
              is unparsable.
            - :exc:`~xml.dom.InvalidModificationErr`:
              Raised if the specified CSS string value represents a different
              type of rule than the current one.
            - :exc:`~xml.dom.HierarchyRequestErr`:
              Raised if the rule cannot be inserted at this point in the
              style sheet.
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if the rule is readonly.
        super(CSSStyleRule, self)._setCssText(cssText)
        # might be (cssText, namespaces)
        cssText, namespaces = self._splitNamespacesOff(cssText)
            # use parent style sheet ones if available
            namespaces = self.parentStyleSheet.namespaces
        except AttributeError:

        tokenizer = self._tokenize2(cssText)
        selectortokens = self._tokensupto2(tokenizer, blockstartonly=True)
        styletokens = self._tokensupto2(tokenizer, blockendonly=True)
        trail = self._nexttoken(tokenizer)
        if trail:
            self._log.error(u'CSSStyleRule: Trailing content: %s' % 
                            self._valuestr(cssText), token=trail)
        elif not selectortokens:
            self._log.error(u'CSSStyleRule: No selector found: %r' % 
        elif self._tokenvalue(selectortokens[0]).startswith(u'@'):
            self._log.error(u'CSSStyleRule: No style rule: %r' %
            wellformed = True
            testselectorlist, teststyle = None, None
            bracetoken = selectortokens.pop()
            if self._tokenvalue(bracetoken) != u'{':
                wellformed = False
                    u'CSSStyleRule: No start { of style declaration found: %r' %
                    self._valuestr(cssText), bracetoken)
            elif not selectortokens:
                wellformed = False
                self._log.error(u'CSSStyleRule: No selector found: %r.' %
                            self._valuestr(cssText), bracetoken)
            testselectorlist = SelectorList(selectorText=(selectortokens, 

            if not styletokens:
                wellformed = False
                    u'CSSStyleRule: No style declaration or "}" found: %r' %
                braceorEOFtoken = styletokens.pop()
                val, typ = self._tokenvalue(braceorEOFtoken), self._type(braceorEOFtoken)
                if val != u'}' and typ != 'EOF':
                    wellformed = False
                        u'CSSStyleRule: No "}" after style declaration found: %r' %
                    if 'EOF' == typ:
                        # add again as style needs it
                    teststyle = CSSStyleDeclaration(styletokens, parentRule=self)

            if wellformed and testselectorlist and teststyle:
                # known as correct from before
                cssutils.log.enabled = False
                self.style.cssText = styletokens
                self.selectorList.selectorText=(selectortokens, namespaces)
                cssutils.log.enabled = True

    cssText = property(_getCssText, _setCssText,
        doc="(DOM) The parsable textual representation of this rule.")

    def __getNamespaces(self):
        "Uses children namespaces if not attached to a sheet, else the sheet's ones."
            return self.parentStyleSheet.namespaces
        except AttributeError:
            return self.selectorList._namespaces
    _namespaces = property(__getNamespaces, 
                           doc="If this Rule is attached to a CSSStyleSheet "
                               "the namespaces of that sheet are mirrored "
                               "here. While the Rule is not attached the "
                               "namespaces of selectorList are used.""")

00170     def _setSelectorList(self, selectorList):
        :param selectorList: A SelectorList which replaces the current 
            selectorList object
        selectorList._parentRule = self
        self._selectorList = selectorList
    selectorList = property(lambda self: self._selectorList, _setSelectorList,
        doc="The SelectorList of this rule.")

00182     def _setSelectorText(self, selectorText):
        wrapper for cssutils SelectorList object

        :param selectorText: 
            of type string, might also be a comma separated list
            of selectors
            - :exc:`~xml.dom.NamespaceErr`:
              Raised if the specified selector uses an unknown namespace
            - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified CSS string value has a syntax error
              and is unparsable.
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this rule is readonly.
        self._selectorList.selectorText = selectorText

    selectorText = property(lambda self: self._selectorList.selectorText, 
                            doc="(DOM) The textual representation of the "
                                "selector for the rule set.")

00207     def _setStyle(self, style):
        :param style: A string or CSSStyleDeclaration which replaces the 
            current style object.
        if isinstance(style, basestring):
            self._style.cssText = style
            style._parentRule = self
            self._style = style

    style = property(lambda self: self._style, _setStyle,
                     doc="(DOM) The declaration-block of this rule set.")

    type = property(lambda self: self.STYLE_RULE, 
                    doc="The type of this rule, as defined by a CSSRule "
                        "type constant.")

    wellformed = property(lambda self: self.selectorList.wellformed)

Generated by  Doxygen 1.6.0   Back to index