~inkscape.dev/inkscape-devlibs64/trunk

« back to all changes in this revision

Viewing changes to python/Lib/site-packages/lxml/html/clean.py

  • Committer: Eduard Braun
  • Date: 2016-10-22 16:51:19 UTC
  • Revision ID: eduard.braun2@gmx.de-20161022165119-9eosgy6lp8j1kzli
Update Python to version 2.7.12

Included modules:
  coverage 4.2
  lxml 3.6.4
  numpy 1.11.2
  scour 0.35
  six 1.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
 
71
71
# All kinds of schemes besides just javascript: that can cause
72
72
# execution:
73
 
_is_javascript_scheme = re.compile(
 
73
_is_image_dataurl = re.compile(
 
74
    r'^data:image/.+;base64', re.I).search
 
75
_is_possibly_malicious_scheme = re.compile(
74
76
    r'(?:javascript|jscript|livescript|vbscript|data|about|mocha):',
75
77
    re.I).search
 
78
def _is_javascript_scheme(s):
 
79
    if _is_image_dataurl(s):
 
80
        return None
 
81
    return _is_possibly_malicious_scheme(s)
 
82
 
76
83
_substitute_whitespace = re.compile(r'[\s\x00-\x08\x0B\x0C\x0E-\x19]+').sub
77
84
# FIXME: should data: be blocked?
78
85
 
88
95
     "descendant-or-self::x:a[normalize-space(@href) and substring(normalize-space(@href),1,1) != '#']"),
89
96
    namespaces={'x':XHTML_NAMESPACE})
90
97
 
 
98
 
91
99
class Cleaner(object):
92
100
    """
93
101
    Instances cleans the document of each of the possible offending
105
113
        Removes any comments.
106
114
 
107
115
    ``style``:
108
 
        Removes any style tags or attributes.
 
116
        Removes any style tags.
 
117
 
 
118
    ``inline_style``
 
119
        Removes any style attributes.  Defaults to the value of the ``style`` option.
109
120
 
110
121
    ``links``:
111
122
        Removes any ``<link>`` tags
184
195
    javascript = True
185
196
    comments = True
186
197
    style = False
 
198
    inline_style = None
187
199
    links = True
188
200
    meta = True
189
201
    page_structure = True
208
220
                raise TypeError(
209
221
                    "Unknown parameter: %s=%r" % (name, value))
210
222
            setattr(self, name, value)
 
223
        if self.inline_style is None and 'inline_style' not in kw:
 
224
            self.inline_style = self.style
211
225
 
212
226
    # Used to lookup the primary URL for a given tag that is up for
213
227
    # removal:
257
271
            kill_tags.add('script')
258
272
        if self.safe_attrs_only:
259
273
            safe_attrs = set(self.safe_attrs)
260
 
            for el in doc.iter():
 
274
            for el in doc.iter(etree.Element):
261
275
                attrib = el.attrib
262
276
                for aname in attrib.keys():
263
277
                    if aname not in safe_attrs:
266
280
            if not (self.safe_attrs_only and
267
281
                    self.safe_attrs == defs.safe_attrs):
268
282
                # safe_attrs handles events attributes itself
269
 
                for el in doc.iter():
 
283
                for el in doc.iter(etree.Element):
270
284
                    attrib = el.attrib
271
285
                    for aname in attrib.keys():
272
286
                        if aname.startswith('on'):
273
287
                            del attrib[aname]
274
288
            doc.rewrite_links(self._remove_javascript_link,
275
289
                              resolve_base_href=False)
276
 
            if not self.style:
277
 
                # If we're deleting style then we don't have to remove JS links
278
 
                # from styles, otherwise...
 
290
            # If we're deleting style then we don't have to remove JS links
 
291
            # from styles, otherwise...
 
292
            if not self.inline_style:
279
293
                for el in _find_styled_elements(doc):
280
294
                    old = el.get('style')
281
295
                    new = _css_javascript_re.sub('', old)
285
299
                        del el.attrib['style']
286
300
                    elif new != old:
287
301
                        el.set('style', new)
 
302
            if not self.style:
288
303
                for el in list(doc.iter('style')):
289
304
                    if el.get('type', '').lower().strip() == 'text/javascript':
290
305
                        el.drop_tree()
307
322
            kill_tags.add(etree.ProcessingInstruction)
308
323
        if self.style:
309
324
            kill_tags.add('style')
 
325
        if self.inline_style:
310
326
            etree.strip_attributes(doc, 'style')
311
327
        if self.links:
312
328
            kill_tags.add('link')