annotate markup/filters.py @ 109:2de3f9d84a1c

Reorder the conditional branches in the serializers so that the more common event kinds are on top.
author cmlenz
date Fri, 28 Jul 2006 17:34:18 +0000
parents f648152df7fd
children e815c2c07572
rev   line source
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
2 #
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 54
diff changeset
3 # Copyright (C) 2006 Edgewall Software
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
5 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 54
diff changeset
8 # are also available at http://markup.edgewall.org/wiki/License.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
9 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 54
diff changeset
12 # history and logs, available at http://markup.edgewall.org/log/.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
13
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
14 """Implementation of a number of stream filters."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
15
92
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
16 from itertools import chain
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
17 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
18 frozenset
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
19 except NameError:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20 from sets import ImmutableSet as frozenset
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21 import re
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22
92
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
23 from markup.core import Attributes, Markup, Namespace, escape
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
24 from markup.core import END, END_NS, START, START_NS, TEXT
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
25 from markup.path import Path
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
26
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 15
diff changeset
27 __all__ = ['IncludeFilter', 'WhitespaceFilter', 'HTMLSanitizer']
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
28
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
29
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
30 class IncludeFilter(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
31 """Template filter providing (very) basic XInclude support
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
32 (see http://www.w3.org/TR/xinclude/) in templates.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
33 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
34
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
35 NAMESPACE = Namespace('http://www.w3.org/2001/XInclude')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
36
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 15
diff changeset
37 def __init__(self, loader):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
38 """Initialize the filter.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
39
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
40 @param loader: the `TemplateLoader` to use for resolving references to
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
41 external template files
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
42 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
43 self.loader = loader
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
44
12
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
45 def __call__(self, stream, ctxt=None, ns_prefixes=None):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
46 """Filter the stream, processing any XInclude directives it may
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
47 contain.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
48
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
49 @param ctxt: the template context
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
50 @param stream: the markup event stream to filter
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
51 """
12
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
52 from markup.template import Template, TemplateError, TemplateNotFound
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
53
12
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
54 if ns_prefixes is None:
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
55 ns_prefixes = []
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
56 in_fallback = False
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
57 include_href, fallback_stream = None, None
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
58 namespace = self.NAMESPACE
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
59
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
60 for kind, data, pos in stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
61
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
62 if kind is START and not in_fallback and data[0] in namespace:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
63 tag, attrib = data
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
64 if tag.localname == 'include':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
65 include_href = attrib.get('href')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
66 elif tag.localname == 'fallback':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
67 in_fallback = True
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
68 fallback_stream = []
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
69
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
70 elif kind is END and data in namespace:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
71 if data.localname == 'include':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
72 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
73 if not include_href:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
74 raise TemplateError('Include misses required '
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
75 'attribute "href"')
21
eca77129518a * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 18
diff changeset
76 template = self.loader.load(include_href,
eca77129518a * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 18
diff changeset
77 relative_to=pos[0])
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 15
diff changeset
78 for event in template.generate(ctxt):
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 15
diff changeset
79 yield event
13
bf9de5a4c896 Match directives should now also be applied when included indirectly.
cmlenz
parents: 12
diff changeset
80
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
81 except TemplateNotFound:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
82 if fallback_stream is None:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
83 raise
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
84 for event in fallback_stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
85 yield event
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
86
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
87 include_href = None
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
88 fallback_stream = None
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 15
diff changeset
89
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
90 elif data.localname == 'fallback':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
91 in_fallback = False
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
92
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
93 elif in_fallback:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
94 fallback_stream.append((kind, data, pos))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
95
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
96 elif kind is START_NS and data[1] == namespace:
12
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
97 ns_prefixes.append(data[0])
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
98
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
99 elif kind is END_NS and data in ns_prefixes:
12
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
100 ns_prefixes.pop()
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
101
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
102 else:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
103 yield kind, data, pos
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
104
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
105
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
106 class WhitespaceFilter(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
107 """A filter that removes extraneous white space from the stream.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
108
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
109 TODO:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
110 * Support for xml:space
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
111 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
112 _TRAILING_SPACE = re.compile('[ \t]+(?=\n)')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
113 _LINE_COLLAPSE = re.compile('\n{2,}')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
114
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
115 def __call__(self, stream, ctxt=None):
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
116 trim_trailing_space = self._TRAILING_SPACE.sub
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
117 collapse_lines = self._LINE_COLLAPSE.sub
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
118 mjoin = Markup('').join
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
119
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
120 textbuf = []
92
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
121 for kind, data, pos in chain(stream, [(None, None, None)]):
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
122 if kind is TEXT:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
123 textbuf.append(data)
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
124 else:
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
125 if textbuf:
92
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
126 if len(textbuf) > 1:
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
127 output = Markup(collapse_lines('\n',
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
128 trim_trailing_space('',
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
129 mjoin(textbuf, escape_quotes=False))))
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
130 del textbuf[:]
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
131 yield TEXT, output, pos
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
132 else:
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
133 output = escape(collapse_lines('\n',
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
134 trim_trailing_space('',
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
135 textbuf.pop())), quotes=False)
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
136 yield TEXT, output, pos
93
f648152df7fd Minor bugfi x follow-up to [97]: don't yield the terminator event from the whitespace filter.
cmlenz
parents: 92
diff changeset
137 if kind is not None:
f648152df7fd Minor bugfi x follow-up to [97]: don't yield the terminator event from the whitespace filter.
cmlenz
parents: 92
diff changeset
138 yield kind, data, pos
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
139
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
140
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
141 class HTMLSanitizer(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
142 """A filter that removes potentially dangerous HTML tags and attributes
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
143 from the stream.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
144 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
145
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
146 _SAFE_TAGS = frozenset(['a', 'abbr', 'acronym', 'address', 'area', 'b',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
147 'big', 'blockquote', 'br', 'button', 'caption', 'center', 'cite',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
148 'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
149 'em', 'fieldset', 'font', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
150 'hr', 'i', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'map',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
151 'menu', 'ol', 'optgroup', 'option', 'p', 'pre', 'q', 's', 'samp',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
152 'select', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'table',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
153 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'tr', 'tt', 'u',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
154 'ul', 'var'])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
155
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
156 _SAFE_ATTRS = frozenset(['abbr', 'accept', 'accept-charset', 'accesskey',
15
f083101b8e8a Port HTML sanitizer fix from trac:changeset:3417.
cmlenz
parents: 14
diff changeset
157 'action', 'align', 'alt', 'axis', 'bgcolor', 'border', 'cellpadding',
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
158 'cellspacing', 'char', 'charoff', 'charset', 'checked', 'cite', 'class',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
159 'clear', 'cols', 'colspan', 'color', 'compact', 'coords', 'datetime',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
160 'dir', 'disabled', 'enctype', 'for', 'frame', 'headers', 'height',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
161 'href', 'hreflang', 'hspace', 'id', 'ismap', 'label', 'lang',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
162 'longdesc', 'maxlength', 'media', 'method', 'multiple', 'name',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
163 'nohref', 'noshade', 'nowrap', 'prompt', 'readonly', 'rel', 'rev',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
164 'rows', 'rowspan', 'rules', 'scope', 'selected', 'shape', 'size',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
165 'span', 'src', 'start', 'style', 'summary', 'tabindex', 'target',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
166 'title', 'type', 'usemap', 'valign', 'value', 'vspace', 'width'])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
167 _URI_ATTRS = frozenset(['action', 'background', 'dynsrc', 'href', 'lowsrc',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
168 'src'])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
169 _SAFE_SCHEMES = frozenset(['file', 'ftp', 'http', 'https', 'mailto', None])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
170
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
171 def __call__(self, stream, ctxt=None):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
172 waiting_for = None
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
173
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
174 for kind, data, pos in stream:
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
175 if kind is START:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
176 if waiting_for:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
177 continue
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
178 tag, attrib = data
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
179 if tag not in self._SAFE_TAGS:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
180 waiting_for = tag
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
181 continue
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
182
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
183 new_attrib = []
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
184 for attr, value in attrib:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
185 if attr not in self._SAFE_ATTRS:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
186 continue
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
187 elif attr in self._URI_ATTRS:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
188 # Don't allow URI schemes such as "javascript:"
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
189 if self._get_scheme(value) not in self._SAFE_SCHEMES:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
190 continue
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
191 elif attr == 'style':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
192 # Remove dangerous CSS declarations from inline styles
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
193 decls = []
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
194 for decl in filter(None, value.split(';')):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
195 is_evil = False
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
196 if 'expression' in decl:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
197 is_evil = True
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
198 for m in re.finditer(r'url\s*\(([^)]+)', decl):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
199 if self._get_scheme(m.group(1)) not in self._SAFE_SCHEMES:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
200 is_evil = True
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
201 break
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
202 if not is_evil:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
203 decls.append(decl.strip())
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
204 if not decls:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
205 continue
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
206 value = '; '.join(decls)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
207 new_attrib.append((attr, value))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
208
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
209 yield kind, (tag, new_attrib), pos
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
210
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
211 elif kind is END:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
212 tag = data
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
213 if waiting_for:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
214 if waiting_for == tag:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
215 waiting_for = None
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
216 else:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
217 yield kind, data, pos
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
218
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
219 else:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
220 if not waiting_for:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
221 yield kind, data, pos
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
222
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
223 def _get_scheme(self, text):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
224 if ':' not in text:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
225 return None
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
226 chars = [char for char in text.split(':', 1)[0] if char.isalnum()]
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
227 return ''.join(chars).lower()
Copyright (C) 2012-2017 Edgewall Software