Exemplo n.º 1
0
    def setProperty(self, name, value=None, priority=u"", normalize=True):
        """(DOM) Set a property value and priority within this declaration
        block.

        :param name:
            of the CSS property to set (in W3C DOM the parameter is called
            "propertyName"), always lowercase (even if not normalized)

            If a property with this `name` is present it will be reset.
            
            cssutils also allowed `name` to be a 
            :class:`~cssutils.css.Property` object, all other
            parameter are ignored in this case
        
        :param value:
            the new value of the property, ignored if `name` is a Property.
        :param priority:
            the optional priority of the property (e.g. "important"),
            ignored if `name` is a Property.
        :param normalize:
            if True (DEFAULT) `name` will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent

        :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()

        if isinstance(name, Property):
            newp = name
            name = newp.literalname
        elif not value:
            # empty string or None effectively removed property
            return self.removeProperty(name)
        else:
            newp = Property(name, value, priority)
        if not newp.wellformed:
            self._log.warn(u"Invalid Property: %s: %s %s" % (name, value, priority))
        else:
            nname = self._normalize(name)
            properties = self.getProperties(name, all=(not normalize))
            for property in reversed(properties):
                if normalize and property.name == nname:
                    property.cssValue = newp.cssValue.cssText
                    property.priority = newp.priority
                    break
                elif property.literalname == name:
                    property.cssValue = newp.cssValue.cssText
                    property.priority = newp.priority
                    break
            else:
                newp.parent = self
                self.seq._readonly = False
                self.seq.append(newp, "Property")
                self.seq._readonly = True
Exemplo n.º 2
0
    def setProperty(self, name, value=None, priority=u'', normalize=True):
        """(DOM) Set a property value and priority within this declaration
        block.

        :param name:
            of the CSS property to set (in W3C DOM the parameter is called
            "propertyName"), always lowercase (even if not normalized)

            If a property with this `name` is present it will be reset.
            
            cssutils also allowed `name` to be a 
            :class:`~cssutils.css.Property` object, all other
            parameter are ignored in this case
        
        :param value:
            the new value of the property, ignored if `name` is a Property.
        :param priority:
            the optional priority of the property (e.g. "important"),
            ignored if `name` is a Property.
        :param normalize:
            if True (DEFAULT) `name` will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent

        :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()

        if isinstance(name, Property):
            newp = name
            name = newp.literalname
        else:
            newp = Property(name, value, priority)
        if not newp.wellformed:
            self._log.warn(u'Invalid Property: %s: %s %s' %
                           (name, value, priority))
        else:
            nname = self._normalize(name)
            properties = self.getProperties(name, all=(not normalize))
            for property in reversed(properties):
                if normalize and property.name == nname:
                    property.cssValue = newp.cssValue.cssText
                    property.priority = newp.priority
                    break
                elif property.literalname == name:
                    property.cssValue = newp.cssValue.cssText
                    property.priority = newp.priority
                    break
            else:
                newp.parent = self
                self.seq._readonly = False
                self.seq.append(newp, 'Property')
                self.seq._readonly = True
    def setProperty(self, name, value=None, priority=u'',
                    normalize=True, replace=True):
        """(DOM) Set a property value and priority within this declaration
        block.

        :param name:
            of the CSS property to set (in W3C DOM the parameter is called
            "propertyName"), always lowercase (even if not normalized)

            If a property with this `name` is present it will be reset.

            cssutils also allowed `name` to be a
            :class:`~cssutils.css.Property` object, all other
            parameter are ignored in this case

        :param value:
            the new value of the property, ignored if `name` is a Property.
        :param priority:
            the optional priority of the property (e.g. "important"),
            ignored if `name` is a Property.
        :param normalize:
            if True (DEFAULT) `name` will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent
        :param replace:
            if True (DEFAULT) the given property will replace a present
            property. If False a new property will be added always.
            The difference to `normalize` is that two or more properties with
            the same name may be set, useful for e.g. stuff like::

                background: red;
                background: rgba(255, 0, 0, 0.5);

            which defines the same property but only capable UAs use the last
            property value, older ones use the first value.

        :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()

        if isinstance(name, Property):
            newp = name
            name = newp.literalname
        elif not value:
            # empty string or None effectively removed property
            return self.removeProperty(name)
        else:
            newp = Property(name, value, priority, parent=self)

        if newp.wellformed:
            if replace:
                # check if update
                nname = self._normalize(name)
                properties = self.getProperties(name, all=(not normalize))
                for property in reversed(properties):
                    if normalize and property.name == nname:
                        property.propertyValue = newp.propertyValue.cssText
                        property.priority = newp.priority
                        return
                    elif property.literalname == name:
                        property.propertyValue = newp.propertyValue.cssText
                        property.priority = newp.priority
                        return

            # not yet set or forced omit replace
            newp.parent = self
            self.seq._readonly = False
            self.seq.append(newp, 'Property')
            self.seq._readonly = True

        else:
            self._log.warn(u'Invalid Property: %s: %s %s'
                           % (name, value, priority))
Exemplo n.º 4
0
    def setProperty(self,
                    name,
                    value=None,
                    priority=u'',
                    normalize=True,
                    replace=True):
        """(DOM) Set a property value and priority within this declaration
        block.

        :param name:
            of the CSS property to set (in W3C DOM the parameter is called
            "propertyName"), always lowercase (even if not normalized)

            If a property with this `name` is present it will be reset.

            cssutils also allowed `name` to be a
            :class:`~cssutils.css.Property` object, all other
            parameter are ignored in this case

        :param value:
            the new value of the property, ignored if `name` is a Property.
        :param priority:
            the optional priority of the property (e.g. "important"),
            ignored if `name` is a Property.
        :param normalize:
            if True (DEFAULT) `name` will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent
        :param replace:
            if True (DEFAULT) the given property will replace a present
            property. If False a new property will be added always.
            The difference to `normalize` is that two or more properties with
            the same name may be set, useful for e.g. stuff like::

                background: red;
                background: rgba(255, 0, 0, 0.5);

            which defines the same property but only capable UAs use the last
            property value, older ones use the first value.

        :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()

        if isinstance(name, Property):
            newp = name
            name = newp.literalname
        elif not value:
            # empty string or None effectively removed property
            return self.removeProperty(name)
        else:
            newp = Property(name, value, priority)

        if newp.wellformed:
            if replace:
                # check if update
                nname = self._normalize(name)
                properties = self.getProperties(name, all=(not normalize))
                for property in reversed(properties):
                    if normalize and property.name == nname:
                        property.cssValue = newp.cssValue.cssText
                        property.priority = newp.priority
                        return
                    elif property.literalname == name:
                        property.cssValue = newp.cssValue.cssText
                        property.priority = newp.priority
                        return

            # not yet set or forced omit replace
            newp.parent = self
            self.seq._readonly = False
            self.seq.append(newp, 'Property')
            self.seq._readonly = True

        else:
            self._log.warn(u'Invalid Property: %s: %s %s' %
                           (name, value, priority))