~n-muench/+junk/cssutils

« back to all changes in this revision

Viewing changes to src/cssutils/css/cssunknownrule.py

  • Committer: Nate Muench
  • Date: 2010-09-28 00:18:28 UTC
  • Revision ID: nowiwilldestroyabydos@gmail.com-20100928001828-70392l841d8fkqdv
Initial Import, Version 0.9.7b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""CSSUnknownRule implements DOM Level 2 CSS CSSUnknownRule."""
 
2
__all__ = ['CSSUnknownRule']
 
3
__docformat__ = 'restructuredtext'
 
4
__version__ = '$Id: cssunknownrule.py 1949 2010-03-26 22:16:33Z cthedot $'
 
5
 
 
6
import cssrule
 
7
import cssutils
 
8
import xml.dom
 
9
 
 
10
class CSSUnknownRule(cssrule.CSSRule):
 
11
    """
 
12
    Represents an at-rule not supported by this user agent, so in 
 
13
    effect all other at-rules not defined in cssutils.
 
14
 
 
15
    Format::
 
16
 
 
17
        @xxx until ';' or block {...}
 
18
    """
 
19
    def __init__(self, cssText=u'', parentRule=None, 
 
20
                 parentStyleSheet=None, readonly=False):
 
21
        """
 
22
        :param cssText:
 
23
            of type string
 
24
        """
 
25
        super(CSSUnknownRule, self).__init__(parentRule=parentRule, 
 
26
                                             parentStyleSheet=parentStyleSheet)
 
27
        self._atkeyword = None
 
28
        if cssText:
 
29
            self.cssText = cssText
 
30
 
 
31
        self._readonly = readonly
 
32
 
 
33
    def __repr__(self):
 
34
        return u"cssutils.css.%s(cssText=%r)" % (
 
35
                self.__class__.__name__,
 
36
                self.cssText)
 
37
        
 
38
    def __str__(self):
 
39
        return u"<cssutils.css.%s object cssText=%r at 0x%x>" % (
 
40
                self.__class__.__name__,
 
41
                self.cssText,
 
42
                id(self))
 
43
 
 
44
    def _getCssText(self):
 
45
        """Return serialized property cssText."""
 
46
        return cssutils.ser.do_CSSUnknownRule(self)
 
47
 
 
48
    def _setCssText(self, cssText):
 
49
        """
 
50
        :exceptions:
 
51
            - :exc:`~xml.dom.SyntaxErr`:
 
52
              Raised if the specified CSS string value has a syntax error and
 
53
              is unparsable.
 
54
            - :exc:`~xml.dom.InvalidModificationErr`:
 
55
              Raised if the specified CSS string value represents a different
 
56
              type of rule than the current one.
 
57
            - :exc:`~xml.dom.HierarchyRequestErr`:
 
58
              Raised if the rule cannot be inserted at this point in the
 
59
              style sheet.
 
60
            - :exc:`~xml.dom.NoModificationAllowedErr`:
 
61
              Raised if the rule is readonly.
 
62
        """
 
63
        super(CSSUnknownRule, self)._setCssText(cssText)
 
64
        tokenizer = self._tokenize2(cssText)
 
65
        attoken = self._nexttoken(tokenizer, None)
 
66
        if not attoken or self._type(attoken) != self._prods.ATKEYWORD:
 
67
            self._log.error(u'CSSUnknownRule: No CSSUnknownRule found: %s' %
 
68
                self._valuestr(cssText),
 
69
                error=xml.dom.InvalidModificationErr)
 
70
        else:
 
71
            # for closures: must be a mutable
 
72
            new = {'nesting': [], # {} [] or ()
 
73
                   'wellformed': True
 
74
                   }
 
75
 
 
76
            def CHAR(expected, seq, token, tokenizer=None):
 
77
                type_, val, line, col = token
 
78
                if expected != 'EOF':
 
79
                    if val in u'{[(':
 
80
                        new['nesting'].append(val)
 
81
                    elif val in u'}])':
 
82
                        opening = {u'}': u'{', u']': u'[', u')': u'('}[val]
 
83
                        try:
 
84
                            if new['nesting'][-1] == opening:
 
85
                                new['nesting'].pop()
 
86
                            else:
 
87
                                raise IndexError()
 
88
                        except IndexError:
 
89
                            new['wellformed'] = False
 
90
                            self._log.error(u'CSSUnknownRule: Wrong nesting of '
 
91
                                            u'{, [ or (.', token=token)
 
92
    
 
93
                    if val in u'};' and not new['nesting']:
 
94
                        expected = 'EOF' 
 
95
    
 
96
                    seq.append(val, type_, line=line, col=col)
 
97
                    return expected
 
98
                else:
 
99
                    new['wellformed'] = False
 
100
                    self._log.error(u'CSSUnknownRule: Expected end of rule.',
 
101
                                    token=token)
 
102
                    return expected
 
103
 
 
104
            def FUNCTION(expected, seq, token, tokenizer=None):
 
105
                # handled as opening (
 
106
                type_, val, line, col = token
 
107
                val = self._tokenvalue(token)
 
108
                if expected != 'EOF':
 
109
                    new['nesting'].append(u'(')
 
110
                    seq.append(val, type_, line=line, col=col)
 
111
                    return expected
 
112
                else:
 
113
                    new['wellformed'] = False
 
114
                    self._log.error(u'CSSUnknownRule: Expected end of rule.',
 
115
                                    token=token)
 
116
                    return expected                
 
117
 
 
118
            def EOF(expected, seq, token, tokenizer=None):
 
119
                "close all blocks and return 'EOF'"
 
120
                for x in reversed(new['nesting']):
 
121
                    closing = {u'{': u'}', u'[': u']', u'(': u')'}[x]
 
122
                    seq.append(closing, closing)
 
123
                new['nesting'] = []
 
124
                return 'EOF'
 
125
                
 
126
            def INVALID(expected, seq, token, tokenizer=None):
 
127
                # makes rule invalid
 
128
                self._log.error(u'CSSUnknownRule: Bad syntax.',
 
129
                                token=token, error=xml.dom.SyntaxErr)
 
130
                new['wellformed'] = False
 
131
                return expected
 
132
 
 
133
            def STRING(expected, seq, token, tokenizer=None):
 
134
                type_, val, line, col = token
 
135
                val = self._stringtokenvalue(token)
 
136
                if expected != 'EOF':
 
137
                    seq.append(val, type_, line=line, col=col)
 
138
                    return expected
 
139
                else:
 
140
                    new['wellformed'] = False
 
141
                    self._log.error(u'CSSUnknownRule: Expected end of rule.',
 
142
                                    token=token)
 
143
                    return expected                
 
144
 
 
145
            def URI(expected, seq, token, tokenizer=None):
 
146
                type_, val, line, col = token
 
147
                val = self._uritokenvalue(token)
 
148
                if expected != 'EOF':
 
149
                    seq.append(val, type_, line=line, col=col)
 
150
                    return expected
 
151
                else:
 
152
                    new['wellformed'] = False
 
153
                    self._log.error(u'CSSUnknownRule: Expected end of rule.',
 
154
                                    token=token)
 
155
                    return expected                
 
156
 
 
157
            def default(expected, seq, token, tokenizer=None):
 
158
                type_, val, line, col = token
 
159
                if expected != 'EOF':
 
160
                    seq.append(val, type_, line=line, col=col)
 
161
                    return expected
 
162
                else:
 
163
                    new['wellformed'] = False
 
164
                    self._log.error(u'CSSUnknownRule: Expected end of rule.',
 
165
                                    token=token)
 
166
                    return expected                
 
167
 
 
168
            # unknown : ATKEYWORD S* ... ; | }
 
169
            newseq = self._tempSeq()
 
170
            wellformed, expected = self._parse(expected=None,
 
171
                seq=newseq, tokenizer=tokenizer,
 
172
                productions={'CHAR': CHAR,
 
173
                             'EOF': EOF,
 
174
                             'FUNCTION': FUNCTION,
 
175
                             'INVALID': INVALID,
 
176
                             'STRING': STRING,
 
177
                             'URI': URI,
 
178
                             'S': default # overwrite default default!
 
179
                }, 
 
180
                default=default,
 
181
                new=new)
 
182
 
 
183
            # wellformed set by parse
 
184
            wellformed = wellformed and new['wellformed']
 
185
            
 
186
            # post conditions
 
187
            if expected != 'EOF':
 
188
                wellformed = False
 
189
                self._log.error(u'CSSUnknownRule: No ending ";" or "}" found: '
 
190
                                u'%r' % self._valuestr(cssText))
 
191
            elif new['nesting']:
 
192
                wellformed = False
 
193
                self._log.error(u'CSSUnknownRule: Unclosed "{", "[" or "(": %r'
 
194
                                % self._valuestr(cssText))
 
195
 
 
196
            # set all
 
197
            if wellformed:
 
198
                self.atkeyword = self._tokenvalue(attoken)
 
199
                self._setSeq(newseq)
 
200
 
 
201
    cssText = property(fget=_getCssText, fset=_setCssText,
 
202
                       doc=u"(DOM) The parsable textual representation.")
 
203
    
 
204
    type = property(lambda self: self.UNKNOWN_RULE, 
 
205
                    doc=u"The type of this rule, as defined by a CSSRule "
 
206
                        u"type constant.")
 
207
    
 
208
    wellformed = property(lambda self: bool(self.atkeyword))
 
209
    
 
 
b'\\ No newline at end of file'