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