/usr/share/pyshared/cssutils/css/cssstyledeclaration.py is in python-cssutils 0.9.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | """CSSStyleDeclaration implements DOM Level 2 CSS CSSStyleDeclaration and
extends CSS2Properties
see
http://www.w3.org/TR/1998/REC-CSS2-19980512/syndata.html#parsing-errors
Unknown properties
------------------
User agents must ignore a declaration with an unknown property.
For example, if the style sheet is::
H1 { color: red; rotation: 70minutes }
the user agent will treat this as if the style sheet had been::
H1 { color: red }
Cssutils gives a message about any unknown properties but
keeps any property (if syntactically correct).
Illegal values
--------------
User agents must ignore a declaration with an illegal value. For example::
IMG { float: left } /* correct CSS2 */
IMG { float: left here } /* "here" is not a value of 'float' */
IMG { background: "red" } /* keywords cannot be quoted in CSS2 */
IMG { border-width: 3 } /* a unit must be specified for length values */
A CSS2 parser would honor the first rule and ignore the rest, as if the
style sheet had been::
IMG { float: left }
IMG { }
IMG { }
IMG { }
Cssutils again will issue a message (WARNING in this case) about invalid
CSS2 property values.
TODO:
This interface is also used to provide a read-only access to the
computed values of an element. See also the ViewCSS interface.
- return computed values and not literal values
- simplify unit pairs/triples/quadruples
2px 2px 2px 2px -> 2px for border/padding...
- normalize compound properties like:
background: no-repeat left url() #fff
-> background: #fff url() no-repeat left
"""
__all__ = ['CSSStyleDeclaration', 'Property']
__docformat__ = 'restructuredtext'
__version__ = '$Id$'
from cssproperties import CSS2Properties
from property import Property
import cssutils
import xml.dom
class CSSStyleDeclaration(CSS2Properties, cssutils.util.Base2):
"""The CSSStyleDeclaration class represents a single CSS declaration
block. This class may be used to determine the style properties
currently set in a block or to set style properties explicitly
within the block.
While an implementation may not recognize all CSS properties within
a CSS declaration block, it is expected to provide access to all
specified properties in the style sheet through the
CSSStyleDeclaration interface.
Furthermore, implementations that support a specific level of CSS
should correctly handle CSS shorthand properties for that level. For
a further discussion of shorthand properties, see the CSS2Properties
interface.
Additionally the CSS2Properties interface is implemented.
$css2propertyname
All properties defined in the CSS2Properties class are available
as direct properties of CSSStyleDeclaration with their respective
DOM name, so e.g. ``fontStyle`` for property 'font-style'.
These may be used as::
>>> style = CSSStyleDeclaration(cssText='color: red')
>>> style.color = 'green'
>>> print style.color
green
>>> del style.color
>>> print style.color
<BLANKLINE>
Format::
[Property: Value Priority?;]* [Property: Value Priority?]?
"""
def __init__(self, cssText=u'', parentRule=None, readonly=False,
validating=None):
"""
:param cssText:
Shortcut, sets CSSStyleDeclaration.cssText
:param parentRule:
The CSS rule that contains this declaration block or
None if this CSSStyleDeclaration is not attached to a CSSRule.
:param readonly:
defaults to False
:param validating:
a flag defining if this sheet should be validated on change.
Defaults to None, which means defer to the parent stylesheet.
"""
super(CSSStyleDeclaration, self).__init__()
self._parentRule = parentRule
self.validating = validating
self.cssText = cssText
self._readonly = readonly
def __contains__(self, nameOrProperty):
"""Check if a property (or a property with given name) is in style.
:param name:
a string or Property, uses normalized name and not literalname
"""
if isinstance(nameOrProperty, Property):
name = nameOrProperty.name
else:
name = self._normalize(nameOrProperty)
return name in self.__nnames()
def __iter__(self):
"""Iterator of set Property objects with different normalized names."""
def properties():
for name in self.__nnames():
yield self.getProperty(name)
return properties()
def keys(self):
"""Analoguous to standard dict returns property names which are set in
this declaration."""
return list(self.__nnames())
def __getitem__(self, CSSName):
"""Retrieve the value of property ``CSSName`` from this declaration.
``CSSName`` will be always normalized.
"""
return self.getPropertyValue(CSSName)
def __setitem__(self, CSSName, value):
"""Set value of property ``CSSName``. ``value`` may also be a tuple of
(value, priority), e.g. style['color'] = ('red', 'important')
``CSSName`` will be always normalized.
"""
priority = None
if isinstance(value, tuple):
value, priority = value
return self.setProperty(CSSName, value, priority)
def __delitem__(self, CSSName):
"""Delete property ``CSSName`` from this declaration.
If property is not in this declaration return u'' just like
removeProperty.
``CSSName`` will be always normalized.
"""
return self.removeProperty(CSSName)
def __setattr__(self, n, v):
"""Prevent setting of unknown properties on CSSStyleDeclaration
which would not work anyway. For these
``CSSStyleDeclaration.setProperty`` MUST be called explicitly!
TODO:
implementation of known is not really nice, any alternative?
"""
known = ['_tokenizer', '_log', '_ttypes',
'_seq', 'seq', 'parentRule', '_parentRule', 'cssText',
'valid', 'wellformed', 'validating',
'_readonly', '_profiles', '_validating']
known.extend(CSS2Properties._properties)
if n in known:
super(CSSStyleDeclaration, self).__setattr__(n, v)
else:
raise AttributeError(u'Unknown CSS Property, '
u'``CSSStyleDeclaration.setProperty("%s", '
u'...)`` MUST be used.' % n)
def __repr__(self):
return u"cssutils.css.%s(cssText=%r)" % (
self.__class__.__name__,
self.getCssText(separator=u' '))
def __str__(self):
return u"<cssutils.css.%s object length=%r (all: %r) at 0x%x>" % (
self.__class__.__name__,
self.length,
len(self.getProperties(all=True)),
id(self))
def __nnames(self):
"""Return iterator for all different names in order as set
if names are set twice the last one is used (double reverse!)
"""
names = []
for item in reversed(self.seq):
val = item.value
if isinstance(val, Property) and not val.name in names:
names.append(val.name)
return reversed(names)
# overwritten accessor functions for CSS2Properties' properties
def _getP(self, CSSName):
"""(DOM CSS2Properties) Overwritten here and effectively the same as
``self.getPropertyValue(CSSname)``.
Parameter is in CSSname format ('font-style'), see CSS2Properties.
Example::
>>> style = CSSStyleDeclaration(cssText='font-style:italic;')
>>> print style.fontStyle
italic
"""
return self.getPropertyValue(CSSName)
def _setP(self, CSSName, value):
"""(DOM CSS2Properties) Overwritten here and effectively the same as
``self.setProperty(CSSname, value)``.
Only known CSS2Properties may be set this way, otherwise an
AttributeError is raised.
For these unknown properties ``setPropertyValue(CSSname, value)``
has to be called explicitly.
Also setting the priority of properties needs to be done with a
call like ``setPropertyValue(CSSname, value, priority)``.
Example::
>>> style = CSSStyleDeclaration()
>>> style.fontStyle = 'italic'
>>> # or
>>> style.setProperty('font-style', 'italic', '!important')
"""
self.setProperty(CSSName, value)
# TODO: Shorthand ones
def _delP(self, CSSName):
"""(cssutils only) Overwritten here and effectively the same as
``self.removeProperty(CSSname)``.
Example::
>>> style = CSSStyleDeclaration(cssText='font-style:italic;')
>>> del style.fontStyle
>>> print style.fontStyle
<BLANKLINE>
"""
self.removeProperty(CSSName)
def children(self):
"""Generator yielding any known child in this declaration including
*all* properties, comments or CSSUnknownrules.
"""
for item in self._seq:
yield item.value
def _getCssText(self):
"""Return serialized property cssText."""
return cssutils.ser.do_css_CSSStyleDeclaration(self)
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.
"""
self._checkReadonly()
tokenizer = self._tokenize2(cssText)
# for closures: must be a mutable
new = {'wellformed': True}
def ident(expected, seq, token, tokenizer=None):
# a property
tokens = self._tokensupto2(tokenizer, starttoken=token,
semicolon=True)
if self._tokenvalue(tokens[-1]) == u';':
tokens.pop()
property = Property(parent=self)
property.cssText = tokens
if property.wellformed:
seq.append(property, 'Property')
else:
self._log.error(u'CSSStyleDeclaration: Syntax Error in '
u'Property: %s' % self._valuestr(tokens))
# does not matter in this case
return expected
def unexpected(expected, seq, token, tokenizer=None):
# error, find next ; or } to omit upto next property
ignored = self._tokenvalue(token) + self._valuestr(
self._tokensupto2(tokenizer,
propertyvalueendonly=True))
self._log.error(u'CSSStyleDeclaration: Unexpected token, ignoring '
'upto %r.' % ignored,token)
# does not matter in this case
return expected
def char(expected, seq, token, tokenizer=None):
# a standalone ; or error...
if self._tokenvalue(token) == u';':
self._log.info(u'CSSStyleDeclaration: Stripped standalone semicolon'
u': %s' % self._valuestr([token]), neverraise=True)
return expected
else:
return unexpected(expected, seq, token, tokenizer)
# [Property: Value;]* Property: Value?
newseq = self._tempSeq()
wellformed, expected = self._parse(expected=None,
seq=newseq, tokenizer=tokenizer,
productions={'IDENT': ident, 'CHAR': char},
default=unexpected)
# wellformed set by parse
for item in newseq:
item.value._parent = self
# do not check wellformed as invalid things are removed anyway
self._setSeq(newseq)
cssText = property(_getCssText, _setCssText,
doc=u"(DOM) A parsable textual representation of the "
u"declaration block excluding the surrounding curly "
u"braces.")
def getCssText(self, separator=None):
"""
:returns:
serialized property cssText, each property separated by
given `separator` which may e.g. be ``u''`` to be able to use
cssText directly in an HTML style attribute. ``;`` is part of
each property (except the last one) and **cannot** be set with
separator!
"""
return cssutils.ser.do_css_CSSStyleDeclaration(self, separator)
def _setParentRule(self, parentRule):
self._parentRule = parentRule
# for x in self.children():
# x.parent = self
parentRule = property(lambda self: self._parentRule, _setParentRule,
doc="(DOM) The CSS rule that contains this declaration block or "
"None if this CSSStyleDeclaration is not attached to a CSSRule.")
def getProperties(self, name=None, all=False):
"""
:param name:
optional `name` of properties which are requested.
Only properties with this **always normalized** `name` are returned.
If `name` is ``None`` all properties are returned (at least one for
each set name depending on parameter `all`).
:param all:
if ``False`` (DEFAULT) only the effective properties are returned.
If name is given a list with only one property is returned.
if ``True`` all properties including properties set multiple times
with different values or priorities for different UAs are returned.
The order of the properties is fully kept as in the original
stylesheet.
:returns:
a list of :class:`~cssutils.css.Property` objects set in
this declaration.
"""
if name and not all:
# single prop but list
p = self.getProperty(name)
if p:
return [p]
else:
return []
elif not all:
# effective Properties in name order
return [self.getProperty(name) for name in self.__nnames()]
else:
# all properties or all with this name
nname = self._normalize(name)
properties = []
for item in self.seq:
val = item.value
if isinstance(val, Property) and (
(bool(nname) == False) or (val.name == nname)):
properties.append(val)
return properties
def getProperty(self, name, normalize=True):
"""
:param name:
of the CSS property, always lowercase (even if not normalized)
:param normalize:
if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
escapes) so "color", "COLOR" or "C\olor" will all be equivalent
If ``False`` may return **NOT** the effective value but the
effective for the unnormalized name.
:returns:
the effective :class:`~cssutils.css.Property` object.
"""
nname = self._normalize(name)
found = None
for item in reversed(self.seq):
val = item.value
if isinstance(val, Property):
if (normalize and nname == val.name) or name == val.literalname:
if val.priority:
return val
elif not found:
found = val
return found
def getPropertyCSSValue(self, name, normalize=True):
"""
:param name:
of the CSS property, always lowercase (even if not normalized)
:param normalize:
if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
escapes) so "color", "COLOR" or "C\olor" will all be equivalent
If ``False`` may return **NOT** the effective value but the
effective for the unnormalized name.
:returns:
:class:`~cssutils.css.CSSValue`, the value of the effective
property if it has been explicitly set for this declaration block.
(DOM)
Used to retrieve the object representation of the value of a CSS
property if it has been explicitly set within this declaration
block. Returns None if the property has not been set.
(This method returns None if the property is a shorthand
property. Shorthand property values can only be accessed and
modified as strings, using the getPropertyValue and setProperty
methods.)
**cssutils currently always returns a CSSValue if the property is
set.**
for more on shorthand properties see
http://www.dustindiaz.com/css-shorthand/
"""
nname = self._normalize(name)
if nname in self._SHORTHANDPROPERTIES:
self._log.info(u'CSSValue for shorthand property "%s" should be '
u'None, this may be implemented later.' %
nname, neverraise=True)
p = self.getProperty(name, normalize)
if p:
return p.cssValue
else:
return None
def getPropertyValue(self, name, normalize=True):
"""
:param name:
of the CSS property, always lowercase (even if not normalized)
:param normalize:
if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
escapes) so "color", "COLOR" or "C\olor" will all be equivalent
If ``False`` may return **NOT** the effective value but the
effective for the unnormalized name.
:returns:
the value of the effective property if it has been explicitly set
for this declaration block. Returns the empty string if the
property has not been set.
"""
p = self.getProperty(name, normalize)
if p:
return p.value
else:
return u''
def getPropertyPriority(self, name, normalize=True):
"""
:param name:
of the CSS property, always lowercase (even if not normalized)
:param normalize:
if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
escapes) so "color", "COLOR" or "C\olor" will all be equivalent
If ``False`` may return **NOT** the effective value but the
effective for the unnormalized name.
:returns:
the priority of the effective CSS property (e.g. the
"important" qualifier) if the property has been explicitly set in
this declaration block. The empty string if none exists.
"""
p = self.getProperty(name, normalize)
if p:
return p.priority
else:
return u''
def removeProperty(self, name, normalize=True):
"""
(DOM)
Used to remove a CSS property if it has been explicitly set within
this declaration block.
:param name:
of the CSS property
:param normalize:
if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
escapes) so "color", "COLOR" or "C\olor" will all be equivalent.
The effective Property value is returned and *all* Properties
with ``Property.name == name`` are removed.
If ``False`` may return **NOT** the effective value but the
effective for the unnormalized `name` only. Also only the
Properties with the literal name `name` are removed.
:returns:
the value of the property if it has been explicitly set for
this declaration block. Returns the empty string if the property
has not been set or the property name does not correspond to a
known CSS property
:exceptions:
- :exc:`~xml.dom.NoModificationAllowedErr`:
Raised if this declaration is readonly or the property is
readonly.
"""
self._checkReadonly()
r = self.getPropertyValue(name, normalize=normalize)
newseq = self._tempSeq()
if normalize:
# remove all properties with name == nname
nname = self._normalize(name)
for item in self.seq:
if not (isinstance(item.value, Property)
and item.value.name == nname):
newseq.appendItem(item)
else:
# remove all properties with literalname == name
for item in self.seq:
if not (isinstance(item.value, Property)
and item.value.literalname == name):
newseq.appendItem(item)
self._setSeq(newseq)
return r
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))
def item(self, index):
"""(DOM) Retrieve the properties that have been explicitly set in
this declaration block. The order of the properties retrieved using
this method does not have to be the order in which they were set.
This method can be used to iterate over all properties in this
declaration block.
:param index:
of the property to retrieve, negative values behave like
negative indexes on Python lists, so -1 is the last element
:returns:
the name of the property at this ordinal position. The
empty string if no property exists at this position.
**ATTENTION:**
Only properties with different names are counted. If two
properties with the same name are present in this declaration
only the effective one is included.
:meth:`item` and :attr:`length` work on the same set here.
"""
names = list(self.__nnames())
try:
return names[index]
except IndexError:
return u''
length = property(lambda self: len(list(self.__nnames())),
doc=u"(DOM) The number of distinct properties that have "
u"been explicitly in this declaration block. The "
u"range of valid indices is 0 to length-1 inclusive. "
u"These are properties with a different ``name`` "
u"only. :meth:`item` and :attr:`length` work on the "
u"same set here.")
def _getValidating(self):
try:
# CSSParser.parseX() sets validating of stylesheet
return self.parentRule.parentStyleSheet.validating
except AttributeError:
# CSSParser.parseStyle() sets validating of declaration
if self._validating is not None:
return self._validating
# default
return True
def _setValidating(self, validating):
self._validating = validating
validating = property(_getValidating, _setValidating,
doc=u"If ``True`` this declaration validates "
u"contained properties. The parent StyleSheet "
u"validation setting does *always* win though so "
u"even if validating is True it may not validate "
u"if the StyleSheet defines else!")
|