Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / cssutils / css / cssproperties.py @ 475

History | View | Annotate | Download (5.06 KB)

1
"""CSS2Properties (partly!) implements DOM Level 2 CSS CSS2Properties used
2
by CSSStyleDeclaration
3

4
TODO: CSS2Properties
5
    If an implementation does implement this interface, it is expected to
6
    understand the specific syntax of the shorthand properties, and apply
7
    their semantics; when the margin property is set, for example, the
8
    marginTop, marginRight, marginBottom and marginLeft properties are
9
    actually being set by the underlying implementation.
10

11
    When dealing with CSS "shorthand" properties, the shorthand properties
12
    should be decomposed into their component longhand properties as
13
    appropriate, and when querying for their value, the form returned
14
    should be the shortest form exactly equivalent to the declarations made
15
    in the ruleset. However, if there is no shorthand declaration that
16
    could be added to the ruleset without changing in any way the rules
17
    already declared in the ruleset (i.e., by adding longhand rules that
18
    were previously not declared in the ruleset), then the empty string
19
    should be returned for the shorthand property.
20

21
    For example, querying for the font property should not return
22
    "normal normal normal 14pt/normal Arial, sans-serif", when
23
    "14pt Arial, sans-serif" suffices. (The normals are initial values, and
24
    are implied by use of the longhand property.)
25

26
    If the values for all the longhand properties that compose a particular
27
    string are the initial values, then a string consisting of all the
28
    initial values should be returned (e.g. a border-width value of
29
    "medium" should be returned as such, not as "").
30

31
    For some shorthand properties that take missing values from other
32
    sides, such as the margin, padding, and border-[width|style|color]
33
    properties, the minimum number of sides possible should be used; i.e.,
34
    "0px 10px" will be returned instead of "0px 10px 0px 10px".
35

36
    If the value of a shorthand property can not be decomposed into its
37
    component longhand properties, as is the case for the font property
38
    with a value of "menu", querying for the values of the component
39
    longhand properties should return the empty string.
40

41
TODO: CSS2Properties DOMImplementation
42
    The interface found within this section are not mandatory. A DOM
43
    application can use the hasFeature method of the DOMImplementation
44
    interface to determine whether it is supported or not. The feature
45
    string for this extended interface listed in this section is "CSS2"
46
    and the version is "2.0".
47

48
"""
49
__all__ = ['CSS2Properties']
50
__docformat__ = 'restructuredtext'
51
__version__ = '$Id$'
52

    
53
import cssutils.profiles
54
import re
55

    
56
class CSS2Properties(object):
57
    """The CSS2Properties interface represents a convenience mechanism
58
    for retrieving and setting properties within a CSSStyleDeclaration.
59
    The attributes of this interface correspond to all the properties
60
    specified in CSS2. Getting an attribute of this interface is
61
    equivalent to calling the getPropertyValue method of the
62
    CSSStyleDeclaration interface. Setting an attribute of this
63
    interface is equivalent to calling the setProperty method of the
64
    CSSStyleDeclaration interface.
65

66
    cssutils actually also allows usage of ``del`` to remove a CSS property
67
    from a CSSStyleDeclaration.
68

69
    This is an abstract class, the following functions need to be present
70
    in inheriting class:
71

72
    - ``_getP``
73
    - ``_setP``
74
    - ``_delP``
75
    """
76
    # actual properties are set after the class definition!
77
    def _getP(self, CSSname): pass
78
    def _setP(self, CSSname, value): pass
79
    def _delP(self, CSSname): pass
80
    
81

    
82
_reCSStoDOMname = re.compile('-[a-z]', re.I)
83
def _toDOMname(CSSname):
84
    """Returns DOMname for given CSSname e.g. for CSSname 'font-style' returns
85
    'fontStyle'.
86
    """
87
    def _doCSStoDOMname2(m): return m.group(0)[1].capitalize()
88
    return _reCSStoDOMname.sub(_doCSStoDOMname2, CSSname)
89

    
90
_reDOMtoCSSname = re.compile('([A-Z])[a-z]+')
91
def _toCSSname(DOMname):
92
    """Return CSSname for given DOMname e.g. for DOMname 'fontStyle' returns
93
    'font-style'.
94
    """
95
    def _doDOMtoCSSname2(m): return '-' + m.group(0).lower()
96
    return _reDOMtoCSSname.sub(_doDOMtoCSSname2, DOMname)
97

    
98
# add list of DOMname properties to CSS2Properties
99
# used for CSSStyleDeclaration to check if allowed properties
100
# but somehow doubled, any better way?
101
CSS2Properties._properties = []
102
for group in cssutils.profiles.properties:
103
    for name in cssutils.profiles.properties[group]:
104
        CSS2Properties._properties.append(_toDOMname(name))
105

    
106

    
107
# add CSS2Properties to CSSStyleDeclaration:
108
def __named_property_def(DOMname):
109
    """
110
    Closure to keep name known in each properties accessor function
111
    DOMname is converted to CSSname here, so actual calls use CSSname.
112
    """
113
    CSSname = _toCSSname(DOMname)
114
    def _get(self): return self._getP(CSSname)
115
    def _set(self, value): self._setP(CSSname, value)
116
    def _del(self): self._delP(CSSname)
117
    return _get, _set, _del
118

    
119
# add all CSS2Properties to CSSStyleDeclaration
120
for DOMname in CSS2Properties._properties:
121
    setattr(CSS2Properties, DOMname,
122
        property(*__named_property_def(DOMname)))