comparison env/lib/python3.9/site-packages/bleach/_vendor/html5lib/filters/lint.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 from __future__ import absolute_import, division, unicode_literals
2
3 from six import text_type
4
5 from . import base
6 from ..constants import namespaces, voidElements
7
8 from ..constants import spaceCharacters
9 spaceCharacters = "".join(spaceCharacters)
10
11
12 class Filter(base.Filter):
13 """Lints the token stream for errors
14
15 If it finds any errors, it'll raise an ``AssertionError``.
16
17 """
18 def __init__(self, source, require_matching_tags=True):
19 """Creates a Filter
20
21 :arg source: the source token stream
22
23 :arg require_matching_tags: whether or not to require matching tags
24
25 """
26 super(Filter, self).__init__(source)
27 self.require_matching_tags = require_matching_tags
28
29 def __iter__(self):
30 open_elements = []
31 for token in base.Filter.__iter__(self):
32 type = token["type"]
33 if type in ("StartTag", "EmptyTag"):
34 namespace = token["namespace"]
35 name = token["name"]
36 assert namespace is None or isinstance(namespace, text_type)
37 assert namespace != ""
38 assert isinstance(name, text_type)
39 assert name != ""
40 assert isinstance(token["data"], dict)
41 if (not namespace or namespace == namespaces["html"]) and name in voidElements:
42 assert type == "EmptyTag"
43 else:
44 assert type == "StartTag"
45 if type == "StartTag" and self.require_matching_tags:
46 open_elements.append((namespace, name))
47 for (namespace, name), value in token["data"].items():
48 assert namespace is None or isinstance(namespace, text_type)
49 assert namespace != ""
50 assert isinstance(name, text_type)
51 assert name != ""
52 assert isinstance(value, text_type)
53
54 elif type == "EndTag":
55 namespace = token["namespace"]
56 name = token["name"]
57 assert namespace is None or isinstance(namespace, text_type)
58 assert namespace != ""
59 assert isinstance(name, text_type)
60 assert name != ""
61 if (not namespace or namespace == namespaces["html"]) and name in voidElements:
62 assert False, "Void element reported as EndTag token: %(tag)s" % {"tag": name}
63 elif self.require_matching_tags:
64 start = open_elements.pop()
65 assert start == (namespace, name)
66
67 elif type == "Comment":
68 data = token["data"]
69 assert isinstance(data, text_type)
70
71 elif type in ("Characters", "SpaceCharacters"):
72 data = token["data"]
73 assert isinstance(data, text_type)
74 assert data != ""
75 if type == "SpaceCharacters":
76 assert data.strip(spaceCharacters) == ""
77
78 elif type == "Doctype":
79 name = token["name"]
80 assert name is None or isinstance(name, text_type)
81 assert token["publicId"] is None or isinstance(name, text_type)
82 assert token["systemId"] is None or isinstance(name, text_type)
83
84 elif type == "Entity":
85 assert isinstance(token["name"], text_type)
86
87 elif type == "SerializerError":
88 assert isinstance(token["data"], text_type)
89
90 else:
91 assert False, "Unknown token type: %(type)s" % {"type": type}
92
93 yield token