def _setCSSValue(self, cssText): """ See css.CSSValue :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] = CSSValue(parent=self) else: try: self.seqs[1].cssText = cssText except xml.dom.SyntaxErr, e: v = CSSValue(parent=self) # try parsing for CSSValue (simply raise if not) # might have been e.g. a CSSVariable before v.cssText = cssText self.seqs[1] = v self.wellformed = self.wellformed and self.seqs[1].wellformed
def _setCSSValue(self, cssText): """ see css.CSSValue DOMException on setting? - SYNTAX_ERR: (self) Raised if the specified CSS string value has a syntax error (according to the attached property) or is unparsable. - TODO: INVALID_MODIFICATION_ERR: 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] = CSSValue() else: if not self.seqs[1]: self.seqs[1] = CSSValue() cssvalue = self.seqs[1] cssvalue._propertyName = self.name cssvalue.cssText = cssText if cssvalue._value and cssvalue.wellformed: self.seqs[1] = cssvalue self.valid = self.valid and cssvalue.valid self.wellformed = self.wellformed and cssvalue.wellformed
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] = CSSValue(parent=self) if name: self.name = name self.cssValue = value self._priority = u'' self._literalpriority = u'' if priority: self.priority = priority
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 CSSValue 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, CSSValue): v = value else: v = CSSValue(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
def _setCSSValue(self, cssText): """ See css.CSSValue :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] = CSSValue() else: if not self.seqs[1]: self.seqs[1] = CSSValue() cssvalue = self.seqs[1] cssvalue.cssText = cssText if cssvalue.wellformed: #cssvalue._value and self.seqs[1] = cssvalue self.wellformed = self.wellformed and cssvalue.wellformed
def __init__(self, name=None, value=None, priority=None, _mediaQuery=False): """ inits property name a property name string value a property value string priority an optional priority string _mediaQuery boolean if True value is optional as used by MediaQuery objects """ super(Property, self).__init__() self.seqs = [[], None, []] self.valid = False self.wellformed = False self._mediaQuery = _mediaQuery if name: self.name = name else: self._name = u'' self.normalname = u'' if value: self.cssValue = value else: self.seqs[1] = CSSValue() self.priority = priority
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', CSSValue(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