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

# Source Code for Module cssutils.css.cssfontfacerule

      1  """CSSFontFaceRule implements DOM Level 2 CSS CSSFontFaceRule.
      2  """
      3  __all__ = ['CSSFontFaceRule']
      4  __docformat__ = 'restructuredtext'
      5  __author__ = '$LastChangedBy: cthedot $'
      6  __date__ = '$LastChangedDate: 2007-10-18 22:44:54 +0200 (Do, 18 Okt 2007) $'
      7  __version__ = '$LastChangedRevision: 507 $'
      8
      9  import xml.dom
     10  import cssrule
     11  import cssutils
     12  from cssstyledeclaration import CSSStyleDeclaration
     13
     14 -class CSSFontFaceRule(cssrule.CSSRule):
     15      """
     16      The CSSFontFaceRule interface represents a @font-face rule in a CSS
     17      style sheet. The @font-face rule is used to hold a set of font
     18      descriptions.
     19
     20      Properties
     21      ==========
     22      cssText: of type DOMString
     23          The parsable textual representation of this rule
     24      style: of type CSSStyleDeclaration
     25          The declaration-block of this rule.
     26
     27      cssutils only
     28      -------------
     29      atkeyword:
     30          the literal keyword used
     31
     32      Inherits properties from CSSRule
     33
     34      Format
     35      ======
     36      ::
     37
     38          font_face
     39            : FONT_FACE_SYM S*
     40              '{' S* declaration [ ';' S* declaration ]* '}' S*
     41            ;
     42      """
     43      type = cssrule.CSSRule.FONT_FACE_RULE
     44
     45 -    def __init__(self, style=None, readonly=False):
     46          """
     47          if readonly allows setting of properties in constructor only
     48
     49          style
     50              CSSStyleDeclaration for this CSSStyleRule
     51          """
     52          super(CSSFontFaceRule, self).__init__()
     53
     54          self.atkeyword = u'@font-face'
     55
     56          if style:
     57              self.style = style
     58              self.seq.append(self.style)
     59          else:
     60              self._style = CSSStyleDeclaration(parentRule=self)
     61
     62          self._readonly = readonly
     63
     64 -    def _getCssText(self):
     65          """
     66          returns serialized property cssText
     67          """
     68          return cssutils.ser.do_CSSFontFaceRule(self)
     69
     70 -    def _setCssText(self, cssText):
     71          """
     72          DOMException on setting
     73
     74          - SYNTAX_ERR: (self, StyleDeclaration)
     75            Raised if the specified CSS string value has a syntax error and
     76            is unparsable.
     77          - INVALID_MODIFICATION_ERR: (self)
     78            Raised if the specified CSS string value represents a different
     79            type of rule than the current one.
     80          - HIERARCHY_REQUEST_ERR: (CSSStylesheet)
     81            Raised if the rule cannot be inserted at this point in the
     82            style sheet.
     83          - NO_MODIFICATION_ALLOWED_ERR: (CSSRule)
     84            Raised if the rule is readonly.
     85          """
     86          super(CSSFontFaceRule, self)._setCssText(cssText)
     87
     88          tokenizer = self._tokenize2(cssText)
     89          attoken = self._nexttoken(tokenizer, None)
     90          if not attoken or u'@font-face' != self._tokenvalue(
     91                                                  attoken, normalize=True):
     92              self._log.error(u'CSSFontFaceRule: No CSSFontFaceRule found: %s' %
     93                  self._valuestr(cssText),
     94                  error=xml.dom.InvalidModificationErr)
     95          else:
     96              valid = True
     97              beforetokens = self._tokensupto2(tokenizer, blockstartonly=True)
     98              try:
     99                  bracetoken = beforetokens.pop()
    100              except IndexError:
    101                  bracetoken = None
    102              if self._tokenvalue(bracetoken) != u'{':
    103                  valid = False
    104                  self._log.error(
    105                      u'CSSFontFaceRule: No start { of style declaration found: %r' %
    106                      self._valuestr(cssText), bracetoken)
    107
    108              # parse stuff before { which should be comments and S only
    109              new = {'valid': True}
    110              newseq = []
    111              beforevalid, expected = self._parse(expected=':',
    112                  seq=newseq, tokenizer=self._tokenize2(beforetokens),
    113                  productions={})
    114              valid = valid and beforevalid and new['valid']
    115
    116              styletokens = self._tokensupto2(tokenizer, blockendonly=True)
    117              newstyle = CSSStyleDeclaration()
    118              if not styletokens:
    119                  valid = False
    120                  self._log.error(
    121                      u'CSSFontFaceRule: No style declaration or "}" found: %r' %
    122                      self._valuestr(cssText))
    123
    124              braceorEOFtoken = styletokens.pop()
    125              val, typ = self._tokenvalue(braceorEOFtoken), self._type(braceorEOFtoken)
    126              if val != u'}' and typ != 'EOF':
    127                  valid = False
    128                  self._log.error(
    129                      u'CSSFontFaceRule: No "}" after style declaration found: %r' %
    130                      self._valuestr(cssText))
    131              else:
    132                  if 'EOF' == typ:
    133                      # add again as style needs it
    134                      styletokens.append(braceorEOFtoken)
    135                  newstyle.cssText = styletokens
    136
    137              if valid:
    138                  self.valid = True
    139                  self.style = newstyle
    140                  self.seq = newseq # contains upto { only
    141
    142      cssText = property(_getCssText, _setCssText,
    143          doc="(DOM) The parsable textual representation of the rule.")
    144
    145 -    def _getStyle(self):
    146          return self._style
    147
    148 -    def _setStyle(self, style):
    149          """
    150          style
    151              StyleDeclaration or string
    152          """
    153          self._checkReadonly()
    154          if isinstance(style, basestring):
    155              self._style = CSSStyleDeclaration(parentRule=self, cssText=style)
    156          else:
    157              self._style = style
    158              style.parentRule = self
    159
    160      style = property(_getStyle, _setStyle,
    161          doc="(DOM) The declaration-block of this rule set.")
    162
    163 -    def __repr__(self):
    164          return "cssutils.css.%s(style=%r)" % (
    165                  self.__class__.__name__, self.style.cssText)
    166
    167 -    def __str__(self):
    168          return "<cssutils.css.%s object style=%r at 0x%x>" % (
    169                  self.__class__.__name__, self.style.cssText, id(self))
    170