Package cssutils :: Package css :: Module cssstylerule |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.css.cssstylerule

      1  """CSSStyleRule implements DOM Level 2 CSS CSSStyleRule.
      2  """
      3  __all__ = ['CSSStyleRule']
      4  __docformat__ = 'restructuredtext'
      5  __author__ = '$LastChangedBy: cthedot $'
      6  __date__ = '$LastChangedDate: 2008-01-02 22:28:39 +0100 (Mi, 02 Jan 2008) $'
      7  __version__ = '$LastChangedRevision: 789 $'
      8
      9  import xml.dom
     10  import cssrule
     11  import cssutils
     12  from selectorlist import SelectorList
     13  from cssstyledeclaration import CSSStyleDeclaration
     14
     15 -class CSSStyleRule(cssrule.CSSRule):
     16      """
     17      The CSSStyleRule object represents a ruleset specified (if any) in a CSS
     18      style sheet. It provides access to a declaration block as well as to the
     19      associated group of selectors.
     20
     21      Properties
     22      ==========
     23      selectorText: of type DOMString
     24          The textual representation of the selector for the rule set. The
     25          implementation may have stripped out insignificant whitespace while
     26          parsing the selector.
     27      style: of type CSSStyleDeclaration, (DOM)
     28          The declaration-block of this rule set.
     29
     30      inherited properties:
     31          - cssText
     32          - parentRule
     33          - parentStyleSheet
     34          - type: STYLE_RULE
     35
     36      cssutils only
     37      -------------
     38      selectorList: of type SelectorList (cssutils only)
     39          A list of all Selector elements for the rule set.
     40
     41      Format
     42      ======
     43      ruleset::
     44
     45          : selector [ COMMA S* selector ]*
     46          LBRACE S* declaration [ ';' S* declaration ]* '}' S*
     47          ;
     48      """
     49      type = cssrule.CSSRule.STYLE_RULE
     50
     51 -    def __init__(self, selectorText=None, style=None, readonly=False):
     52          """
     53          if readonly allows setting of properties in constructor only
     54
     55          selectorText
     56              type string
     57          style
     58              CSSStyleDeclaration for this CSSStyleRule
     59          """
     60          super(CSSStyleRule, self).__init__()
     61
     62          if selectorText:
     63              self.selectorText = selectorText
     64              self.seq.append(self.selectorText)
     65          else:
     66              self._selectorList = SelectorList()
     67              self._selectorList.parentRule = self
     68          if style:
     69              self.style = style
     70              self.seq.append(self.style)
     71          else:
     72              self._style = CSSStyleDeclaration(parentRule=self)
     73
     74          self._readonly = readonly
     75
     76 -    def _getCssText(self):
     77          """
     78          returns serialized property cssText
     79          """
     80          return cssutils.ser.do_CSSStyleRule(self)
     81
     82 -    def _setCssText(self, cssText):
     83          """
     84          DOMException on setting
     85
     86          - SYNTAX_ERR: (self, StyleDeclaration, etc)
     87            Raised if the specified CSS string value has a syntax error and
     88            is unparsable.
     89          - INVALID_MODIFICATION_ERR: (self)
     90            Raised if the specified CSS string value represents a different
     91            type of rule than the current one.
     92          - HIERARCHY_REQUEST_ERR: (CSSStylesheet)
     93            Raised if the rule cannot be inserted at this point in the
     94            style sheet.
     95          - NO_MODIFICATION_ALLOWED_ERR: (CSSRule)
     96            Raised if the rule is readonly.
     97          """
     98          super(CSSStyleRule, self)._setCssText(cssText)
     99
    100          tokenizer = self._tokenize2(cssText)
    101          selectortokens = self._tokensupto2(tokenizer, blockstartonly=True)
    102          styletokens = self._tokensupto2(tokenizer, blockendonly=True)
    103
    104          if not selectortokens or self._tokenvalue(
    105                                          selectortokens[0]).startswith(u'@'):
    106              self._log.error(u'CSSStyleRule: No content or no style rule.',
    107                      error=xml.dom.InvalidModificationErr)
    108
    109          else:
    110              valid = True
    111
    112              bracetoken = selectortokens.pop()
    113              if self._tokenvalue(bracetoken) != u'{':
    114                  valid = False
    115                  self._log.error(
    116                      u'CSSStyleRule: No start { of style declaration found: %r' %
    117                      self._valuestr(cssText), bracetoken)
    118              elif not selectortokens:
    119                  valid = False
    120                  self._log.error(u'CSSStyleRule: No selector found: %r.' %
    121                              self._valuestr(cssText), bracetoken)
    122
    123              newselectorlist = SelectorList(selectorText=selectortokens)
    124
    125              newstyle = CSSStyleDeclaration()
    126              if not styletokens:
    127                  valid = False
    128                  self._log.error(
    129                      u'CSSStyleRule: No style declaration or "}" found: %r' %
    130                      self._valuestr(cssText))
    131              else:
    132                  braceorEOFtoken = styletokens.pop()
    133                  val, typ = self._tokenvalue(braceorEOFtoken), self._type(braceorEOFtoken)
    134                  if val != u'}' and typ != 'EOF':
    135                      valid = False
    136                      self._log.error(
    137                          u'CSSStyleRule: No "}" after style declaration found: %r' %
    138                          self._valuestr(cssText))
    139                  else:
    140                      if 'EOF' == typ:
    141                          # add again as style needs it
    142                          styletokens.append(braceorEOFtoken)
    143                      newstyle.cssText = styletokens
    144
    145              if valid:
    146                  self.valid = True
    147                  self.selectorList = newselectorlist
    148                  self.style = newstyle
    149
    150      cssText = property(_getCssText, _setCssText,
    151          doc="(DOM) The parsable textual representation of the rule.")
    152
    153 -    def _setSelectorList(self, selectorList):
    154          """
    155          (cssutils)
    156          set the SelectorList of this rule
    157
    158          selectorList
    159              instance of SelectorList
    160
    161          DOMException on setting
    162
    163          - NO_MODIFICATION_ALLOWED_ERR:
    164            Raised if this rule is readonly.
    165          """
    166          self._checkReadonly()
    167          self._selectorList = selectorList
    168          self._selectorList.parentRule = self
    169
    170 -    def _getSelectorList(self):
    171          """
    172          (cssutils)
    173          returns the SelectorList of this rule
    174          see selectorText for a textual representation
    175          """
    176          return self._selectorList
    177
    178      selectorList = property(_getSelectorList, _setSelectorList,
    179          doc="The SelectorList of this rule.")
    180
    181 -    def _getSelectorText(self):
    182          """
    183          wrapper for cssutils SelectorList object
    184          """
    185          return self._selectorList.selectorText
    186
    187 -    def _setSelectorText(self, selectorText):
    188          """
    189          wrapper for cssutils SelectorList object
    190
    191          selector
    192              of type string, might also be a comma separated list of
    193              selectors
    194
    195          DOMException on setting
    196
    197          - SYNTAX_ERR: (SelectorList, Selector)
    198            Raised if the specified CSS string value has a syntax error
    199            and is unparsable.
    200          - NO_MODIFICATION_ALLOWED_ERR: (self)
    201            Raised if this rule is readonly.
    202          """
    203          self._checkReadonly()
    204          self._selectorList = SelectorList(selectorText)
    205          self._selectorList.parentRule = self
    206
    207      selectorText = property(_getSelectorText, _setSelectorText,
    208          doc="""(DOM) The textual representation of the selector for the
    209              rule set.""")
    210
    211 -    def _getStyle(self):
    212          return self._style
    213
    214 -    def _setStyle(self, style):
    215          """
    216          style
    217              StyleDeclaration or string
    218          """
    219          self._checkReadonly()
    220          if isinstance(style, basestring):
    221              self._style = CSSStyleDeclaration(parentRule=self, cssText=style)
    222          else:
    223              self._style = style
    224              style.parentRule = self
    225
    226      style = property(_getStyle, _setStyle,
    227          doc="(DOM) The declaration-block of this rule set.")
    228
    229 -    def __repr__(self):
    230          return "cssutils.css.%s(selectorText=%r, style=%r)" % (
    231                  self.__class__.__name__, self.selectorText, self.style.cssText)
    232
    233 -    def __str__(self):
    234          return "<cssutils.css.%s object selector=%r style=%r at 0x%x>" % (
    235                  self.__class__.__name__, self.selectorText, self.style.cssText,
    236                  id(self))
    237