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 / cssvariablesrule.py @ 475

History | View | Annotate | Download (7.8 KB)

1
"""CSSVariables implements (and only partly) experimental 
2
`CSS Variables <http://disruptive-innovations.com/zoo/cssvariables/>`_
3
"""
4
__all__ = ['CSSVariablesRule']
5
__docformat__ = 'restructuredtext'
6
__version__ = '$Id: cssfontfacerule.py 1818 2009-07-30 21:39:00Z cthedot $'
7

    
8
from cssvariablesdeclaration import CSSVariablesDeclaration
9
import cssrule
10
import cssutils
11
import xml.dom
12

    
13
class CSSVariablesRule(cssrule.CSSRule):
14
    """
15
    The CSSVariablesRule interface represents a @variables rule within a CSS
16
    style sheet. The @variables rule is used to specify variables.
17

18
    cssutils uses a :class:`~cssutils.css.CSSVariablesDeclaration`  to
19
    represent the variables.
20
    
21
    Format::
22
    
23
        variables
24
            VARIABLES_SYM S* medium [ COMMA S* medium ]* LBRACE S* 
25
            variableset* '}' S*
26
            ;
27
        
28
    for variableset see :class:`cssutils.css.CSSVariablesDeclaration`
29
         
30
    **Media are not implemented. Reason is that cssutils is using CSS
31
    variables in a kind of preprocessing and therefor no media information
32
    is available at this stage. For now do not use media!**
33
    
34
    Example::
35
    
36
        @variables {
37
          CorporateLogoBGColor: #fe8d12;
38
        }
39
        
40
        div.logoContainer {
41
          background-color: var(CorporateLogoBGColor);
42
        }
43
    """
44
    def __init__(self, mediaText=None, variables=None, parentRule=None, 
45
                 parentStyleSheet=None, readonly=False):
46
        """
47
        If readonly allows setting of properties in constructor only.
48
        """
49
        super(CSSVariablesRule, self).__init__(parentRule=parentRule, 
50
                                              parentStyleSheet=parentStyleSheet)
51
        self._atkeyword = u'@variables'
52
        
53
        # dummy
54
        self._media = cssutils.stylesheets.MediaList(mediaText, 
55
                                                     readonly=readonly)
56
        
57
        if variables:
58
            self.variables = variables
59
        else: 
60
            self.variables = CSSVariablesDeclaration(parentRule=self)
61

    
62
        self._readonly = readonly
63
        
64
    def __repr__(self):
65
        return u"cssutils.css.%s(mediaText=%r, variables=%r)" % (
66
                self.__class__.__name__, 
67
                self._media.mediaText,
68
                self.variables.cssText)
69

    
70
    def __str__(self):
71
        return u"<cssutils.css.%s object mediaText=%r variables=%r valid=%r " \
72
               u"at 0x%x>" % (self.__class__.__name__,
73
                              self._media.mediaText,
74
                              self.variables.cssText,
75
                              self.valid,
76
                              id(self))
77

    
78
    def _getCssText(self):
79
        """Return serialized property cssText."""
80
        return cssutils.ser.do_CSSVariablesRule(self)
81

    
82
    def _setCssText(self, cssText):
83
        """
84
        :exceptions:
85
            - :exc:`~xml.dom.SyntaxErr`:
86
              Raised if the specified CSS string value has a syntax error and
87
              is unparsable.
88
            - :exc:`~xml.dom.InvalidModificationErr`:
89
              Raised if the specified CSS string value represents a different
90
              type of rule than the current one.
91
            - :exc:`~xml.dom.HierarchyRequestErr`:
92
              Raised if the rule cannot be inserted at this point in the
93
              style sheet.
94
            - :exc:`~xml.dom.NoModificationAllowedErr`:
95
              Raised if the rule is readonly.
96
              
97
        Format::
98
        
99
            variables
100
            : VARIABLES_SYM S* medium [ COMMA S* medium ]* LBRACE S* 
101
              variableset* '}' S*
102
            ;
103
            
104
            variableset
105
            : LBRACE S* vardeclaration [ ';' S* vardeclaration ]* '}' S*
106
            ;
107
        """
108
        super(CSSVariablesRule, self)._setCssText(cssText)
109

    
110
        tokenizer = self._tokenize2(cssText)
111
        attoken = self._nexttoken(tokenizer, None)
112
        if self._type(attoken) != self._prods.VARIABLES_SYM:
113
            self._log.error(u'CSSVariablesRule: No CSSVariablesRule found: %s' %
114
                            self._valuestr(cssText),
115
                            error=xml.dom.InvalidModificationErr)
116
        else:
117
            newVariables = CSSVariablesDeclaration(parentRule=self)
118
            ok = True
119
            
120
            beforetokens, brace = self._tokensupto2(tokenizer, 
121
                                                    blockstartonly=True,
122
                                                    separateEnd=True)            
123
            if self._tokenvalue(brace) != u'{':
124
                ok = False
125
                self._log.error(u'CSSVariablesRule: No start { of variable '
126
                                u'declaration found: %r' 
127
                                % self._valuestr(cssText), brace)
128
            
129
            # parse stuff before { which should be comments and S only
130
            new = {'wellformed': True}
131
            newseq = self._tempSeq()#[]
132
            
133
            beforewellformed, expected = self._parse(expected=':',
134
                seq=newseq, tokenizer=self._tokenize2(beforetokens),
135
                productions={})
136
            ok = ok and beforewellformed and new['wellformed']
137
    
138
            variablestokens, braceorEOFtoken = self._tokensupto2(tokenizer, 
139
                                                             blockendonly=True,
140
                                                             separateEnd=True)
141

    
142
            val, type_ = self._tokenvalue(braceorEOFtoken), \
143
                         self._type(braceorEOFtoken)
144
            if val != u'}' and type_ != 'EOF':
145
                ok = False
146
                self._log.error(u'CSSVariablesRule: No "}" after variables '
147
                                u'declaration found: %r'
148
                                % self._valuestr(cssText))
149
                
150
            nonetoken = self._nexttoken(tokenizer)
151
            if nonetoken:
152
                ok = False
153
                self._log.error(u'CSSVariablesRule: Trailing content found.',
154
                                token=nonetoken)
155

    
156
            if 'EOF' == type_:
157
                # add again as variables needs it
158
                variablestokens.append(braceorEOFtoken)
159
            # SET but may raise:
160
            newVariables.cssText = variablestokens
161

    
162
            if ok:
163
                # contains probably comments only upto {
164
                self._setSeq(newseq)
165
                self.variables = newVariables
166

    
167
    cssText = property(_getCssText, _setCssText,
168
                       doc=u"(DOM) The parsable textual representation of this "
169
                           u"rule.")
170

    
171
    media = property(doc=u"NOT IMPLEMENTED! As cssutils resolves variables "\
172
                         u"during serializing media information is lost.")
173

    
174
    def _setVariables(self, variables):
175
        """
176
        :param variables:
177
            a CSSVariablesDeclaration or string
178
        """
179
        self._checkReadonly()
180
        if isinstance(variables, basestring):
181
            self._variables = CSSVariablesDeclaration(cssText=variables, 
182
                                                      parentRule=self)
183
        else:
184
            variables._parentRule = self
185
            self._variables = variables
186

    
187
    variables = property(lambda self: self._variables, _setVariables,
188
                         doc=u"(DOM) The variables of this rule set, a "
189
                             u":class:`cssutils.css.CSSVariablesDeclaration`.")
190

    
191
    type = property(lambda self: self.VARIABLES_RULE, 
192
                    doc=u"The type of this rule, as defined by a CSSRule "
193
                        u"type constant.")
194

    
195
    valid = property(lambda self: True, doc='NOT IMPLEMTED REALLY (TODO)')
196
    
197
    # constant but needed:
198
    wellformed = property(lambda self: True)