Exemplo n.º 1
0
    def __init__(self, name=None, value=None, priority=u'',
                 _mediaQuery=False, parent=None):
        """
        :param name:
            a property name string (will be normalized)
        :param value:
            a property value string
        :param priority:
            an optional priority string which currently must be u'',
            u'!important' or u'important'
        :param _mediaQuery:
            if ``True`` value is optional (used by MediaQuery)
        :param parent:
            the parent object, normally a
            :class:`cssutils.css.CSSStyleDeclaration`
        """
        super(Property, self).__init__()
        self.seqs = [[], None, []]
        self.wellformed = False
        self._mediaQuery = _mediaQuery
        self.parent = parent

        self.__nametoken = None
        self._name = u''
        self._literalname = u''
        self.seqs[1] = PropertyValue(parent=self)
        if name:
            self.name = name
            self.propertyValue = value

        self._priority = u''
        self._literalpriority = u''
        if priority:
            self.priority = priority
Exemplo n.º 2
0
    def setVariable(self, variableName, value):
        """Used to set a variable value within this variable declaration block.

        :param variableName:
            The name of the CSS variable. 
        :param value:
            The new value of the variable, may also be a PropertyValue object.

        :exceptions:
            - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified value has a syntax error and is
              unparsable.
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this declaration is readonly or the property is
              readonly.
        """
        self._checkReadonly()
                
        # check name
        wellformed, seq, store, unused = \
            ProdParser().parse(normalize(variableName),
                               u'variableName',
                               Sequence(PreDef.ident()))
        if not wellformed:
            self._log.error(u'Invalid variableName: %r: %r'
                            % (variableName, value))
        else:
            # check value
            if isinstance(value, PropertyValue):
                v = value 
            else:
                v = PropertyValue(cssText=value, parent=self)
                                
            if not v.wellformed:
                self._log.error(u'Invalid variable value: %r: %r'
                                % (variableName, value))
            else:
                # update seq
                self.seq._readonly = False
                
                variableName = normalize(variableName)
                
                if variableName in self._vars:
                    for i, x in enumerate(self.seq):
                        if x.value[0] == variableName:
                            self.seq.replace(i, 
                                      [variableName, v], 
                                      x.type, 
                                      x.line,
                                      x.col)
                            break
                else:
                    self.seq.append([variableName, v], 'var')                
                self.seq._readonly = True
                self._vars[variableName] = v
Exemplo n.º 3
0
    def _setPropertyValue(self, cssText):
        """
        See css.PropertyValue

        :exceptions:
        - :exc:`~xml.dom.SyntaxErr`:
          Raised if the specified CSS string value has a syntax error
          (according to the attached property) or is unparsable.
        - :exc:`~xml.dom.InvalidModificationErr`:
          TODO: Raised if the specified CSS string value represents a different
          type of values than the values allowed by the CSS property.
        """
        if self._mediaQuery and not cssText:
            self.seqs[1] = PropertyValue(parent=self)
        else:
            self.seqs[1].cssText = cssText
            self.wellformed = self.wellformed and self.seqs[1].wellformed
Exemplo n.º 4
0
    def _setCssText(self, cssText):
        """Setting this attribute will result in the parsing of the new value
        and resetting of all the properties in the declaration block
        including the removal or addition of properties.

        :exceptions:
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this declaration is readonly or a property is readonly.
            - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified CSS string value has a syntax error and
              is unparsable.

        Format::
        
            variableset
            : vardeclaration [ ';' S* vardeclaration ]*
            ;
            
            vardeclaration
            : varname ':' S* term
            ;
            
            varname
            : IDENT S*
            ;
            
            expr
            : [ VARCALL | term ] [ operator [ VARCALL | term ] ]*
            ;

        """
        self._checkReadonly()

        vardeclaration = Sequence(
            PreDef.ident(),
            PreDef.char(u':', u':', toSeq=False),
            #PreDef.S(toSeq=False, optional=True),
            Prod(name=u'term',
                 match=lambda t, v: True,
                 toSeq=lambda t, tokens:
                 (u'value',
                  PropertyValue(itertools.chain([t], tokens), parent=self))))
        prods = Sequence(
            vardeclaration,
            Sequence(PreDef.S(optional=True),
                     PreDef.char(u';', u';', toSeq=False),
                     PreDef.S(optional=True),
                     vardeclaration,
                     minmax=lambda: (0, None)), PreDef.S(optional=True),
            PreDef.char(u';', u';', toSeq=False, optional=True))
        # parse
        wellformed, seq, store, notused = \
            ProdParser().parse(cssText,
                               u'CSSVariableDeclaration',
                               prods)
        if wellformed:
            newseq = self._tempSeq()
            newvars = {}

            # seq contains only name: value pairs plus comments etc
            nameitem = None
            for item in seq:
                if u'IDENT' == item.type:
                    nameitem = item
                elif u'value' == item.type:
                    nname = normalize(nameitem.value)
                    if nname in newvars:
                        # replace var with same name
                        for i, it in enumerate(newseq):
                            if normalize(it.value[0]) == nname:
                                newseq.replace(i, (nameitem.value, item.value),
                                               'var', nameitem.line,
                                               nameitem.col)
                    else:
                        # saved non normalized name for reserialization
                        newseq.append((nameitem.value, item.value), 'var',
                                      nameitem.line, nameitem.col)

#                    newseq.append((nameitem.value, item.value),
#                                  'var',
#                                  nameitem.line, nameitem.col)

                    newvars[nname] = item.value

                else:
                    newseq.appendItem(item)

            self._setSeq(newseq)
            self._vars = newvars
            self.wellformed = True