Logo Search packages:      
Sourcecode: calibre version File versions

def cssutils::css::cssmediarule::CSSMediaRule::_setCssText (   self,
  cssText 
) [private]

:param cssText:
    a parseable string or a tuple of (cssText, dict-of-namespaces)
:Exceptions:
    - :exc:`~xml.dom.NamespaceErr`:
      Raised if a specified selector uses an unknown namespace
      prefix.
    - :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.

Reimplemented from cssutils::css::cssrule::CSSRule.

Definition at line 70 of file cssmediarule.py.

00070                                   :
        """
        :param cssText:
            a parseable string or a tuple of (cssText, dict-of-namespaces)
        :Exceptions:
            - :exc:`~xml.dom.NamespaceErr`:
              Raised if a specified selector uses an unknown namespace
              prefix.
            - :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(CSSMediaRule, self)._setCssText(cssText)
        
        # might be (cssText, namespaces)
        cssText, namespaces = self._splitNamespacesOff(cssText)
        try:
            # use parent style sheet ones if available
            namespaces = self.parentStyleSheet.namespaces
        except AttributeError:
            pass
        
        tokenizer = self._tokenize2(cssText)
        attoken = self._nexttoken(tokenizer, None)
        if self._type(attoken) != self._prods.MEDIA_SYM:
            self._log.error(u'CSSMediaRule: No CSSMediaRule found: %s' %
                self._valuestr(cssText),
                error=xml.dom.InvalidModificationErr)
        else:
            # media "name"? { cssRules }
            
            # media
            wellformed = True
            mediatokens, end = self._tokensupto2(tokenizer, 
                                            mediaqueryendonly=True,
                                            separateEnd=True)        
            if u'{' == self._tokenvalue(end) or self._prods.STRING == self._type(end):
                newmedia = cssutils.stylesheets.MediaList()
                newmedia.mediaText = mediatokens
            
            # name (optional)
            name = None
            nameseq = self._tempSeq()
            if self._prods.STRING == self._type(end):
                name = self._stringtokenvalue(end)
                # TODO: for now comments are lost after name
                nametokens, end = self._tokensupto2(tokenizer, 
                                                blockstartonly=True,
                                                separateEnd=True)
                wellformed, expected = self._parse(None, nameseq, nametokens, {})
                if not wellformed:
                    self._log.error(u'CSSMediaRule: Syntax Error: %s' % 
                                    self._valuestr(cssText))
                    

            # check for {
            if u'{' != self._tokenvalue(end):
                self._log.error(u'CSSMediaRule: No "{" found: %s' % 
                                self._valuestr(cssText))
                return
            
            # cssRules
            cssrulestokens, braceOrEOF = self._tokensupto2(tokenizer, 
                                               mediaendonly=True,
                                               separateEnd=True)
            nonetoken = self._nexttoken(tokenizer, None)
            if 'EOF' == self._type(braceOrEOF):
                # HACK!!!
                # TODO: Not complete, add EOF to rule and } to @media
                cssrulestokens.append(braceOrEOF)
                braceOrEOF = ('CHAR', '}', 0, 0)
                self._log.debug(u'CSSMediaRule: Incomplete, adding "}".', 
                                token=braceOrEOF, neverraise=True)

            if u'}' != self._tokenvalue(braceOrEOF):
                self._log.error(u'CSSMediaRule: No "}" found.', 
                                token=braceOrEOF)
            elif nonetoken:
                self._log.error(u'CSSMediaRule: Trailing content found.',
                                token=nonetoken)
            else:                
                # for closures: must be a mutable
                newcssrules = [] #cssutils.css.CSSRuleList()
                new = {'wellformed': True }
                
                def ruleset(expected, seq, token, tokenizer):
                    rule = cssutils.css.CSSStyleRule(parentRule=self)
                    rule.cssText = (self._tokensupto2(tokenizer, token), 
                                    namespaces)
                    if rule.wellformed:
                        rule._parentStyleSheet=self.parentStyleSheet
                        seq.append(rule)
                    return expected
        
                def atrule(expected, seq, token, tokenizer):
                    # TODO: get complete rule!
                    tokens = self._tokensupto2(tokenizer, token)
                    atval = self._tokenvalue(token)
                    if atval in ('@charset ', '@font-face', '@import', '@namespace', 
                                 '@page', '@media'):
                        self._log.error(
                            u'CSSMediaRule: This rule is not allowed in CSSMediaRule - ignored: %s.'
                                % self._valuestr(tokens),
                                token = token, 
                                error=xml.dom.HierarchyRequestErr)
                    else:
                        rule = cssutils.css.CSSUnknownRule(parentRule=self, 
                                           parentStyleSheet=self.parentStyleSheet)
                        rule.cssText = tokens
                        if rule.wellformed:
                            seq.append(rule)
                    return expected
                
                def COMMENT(expected, seq, token, tokenizer=None):
                    seq.append(cssutils.css.CSSComment([token]))
                    return expected
                
                tokenizer = (t for t in cssrulestokens) # TODO: not elegant!
                wellformed, expected = self._parse(braceOrEOF, 
                                                   newcssrules, 
                                                   tokenizer, {
                                                     'COMMENT': COMMENT,
                                                     'CHARSET_SYM': atrule,
                                                     'FONT_FACE_SYM': atrule,
                                                     'IMPORT_SYM': atrule,
                                                     'NAMESPACE_SYM': atrule,
                                                     'PAGE_SYM': atrule,
                                                     'MEDIA_SYM': atrule,
                                                     'ATKEYWORD': atrule
                                                   }, 
                                                   default=ruleset,
                                                   new=new)
                
                # no post condition                    
                if newmedia.wellformed and wellformed:
                    # keep reference
                    self._media.mediaText = newmedia.mediaText  
                    self.name = name
                    self._setSeq(nameseq)
                    del self._cssRules[:]
                    for r in newcssrules:
                        self._cssRules.append(r)
        
    cssText = property(_getCssText, _setCssText,


Generated by  Doxygen 1.6.0   Back to index