Beispiel #1
0
    def _setCssText(self, cssText):
        """
        :exceptions:
            - :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.
              
        Format::
        
            variables
            : VARIABLES_SYM S* medium [ COMMA S* medium ]* LBRACE S* variableset* '}' S*
            ;
            
            variableset
            : LBRACE S* vardeclaration [ ';' S* vardeclaration ]* '}' S*
            ;
        """
        super(CSSVariablesRule, self)._setCssText(cssText)

        tokenizer = self._tokenize2(cssText)
        attoken = self._nexttoken(tokenizer, None)
        if self._type(attoken) != self._prods.VARIABLES_SYM:
            self._log.error(u'CSSVariablesRule: No CSSVariablesRule found: %s' %
                self._valuestr(cssText),
                error=xml.dom.InvalidModificationErr)
        else:
            # save if parse goes wrong
            oldvariables = CSSVariablesDeclaration()
            oldvariables._absorb(self.variables)
            
            ok = True
            beforetokens, brace = self._tokensupto2(tokenizer, 
                                                    blockstartonly=True,
                                                    separateEnd=True)            
            if self._tokenvalue(brace) != u'{':
                ok = False
                self._log.error(
                    u'CSSVariablesRule: No start { of variable declaration found: %r' %
                    self._valuestr(cssText), brace)
            
            # parse stuff before { which should be comments and S only
            new = {'wellformed': True}
            newseq = self._tempSeq()#[]
            
            beforewellformed, expected = self._parse(expected=':',
                seq=newseq, tokenizer=self._tokenize2(beforetokens),
                productions={})
            ok = ok and beforewellformed and new['wellformed']
    
            variablestokens, braceorEOFtoken = self._tokensupto2(tokenizer, 
                                                             blockendonly=True,
                                                             separateEnd=True)

            val, typ = self._tokenvalue(braceorEOFtoken), self._type(braceorEOFtoken)
            if val != u'}' and typ != 'EOF':
                ok = False
                self._log.error(
                    u'CSSVariablesRule: No "}" after variables declaration found: %r' %
                    self._valuestr(cssText))
                
            nonetoken = self._nexttoken(tokenizer)
            if nonetoken:
                ok = False
                self._log.error(u'CSSVariablesRule: Trailing content found.',
                                token=nonetoken)

            if 'EOF' == typ:
                # add again as variables needs it
                variablestokens.append(braceorEOFtoken)
            # may raise:
            self.variables.cssText = variablestokens

            if ok:
                # contains probably comments only upto {
                self._setSeq(newseq)
            else:
                # RESET
                self.variables._absorb(oldvariables)