summaryrefslogtreecommitdiff
path: root/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/lint.py
diff options
context:
space:
mode:
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/lint.py')
-rw-r--r--venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/lint.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/lint.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/lint.py
new file mode 100644
index 0000000..b5bbd97
--- /dev/null
+++ b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/lint.py
@@ -0,0 +1,93 @@
1from __future__ import absolute_import, division, unicode_literals
2
3from pip._vendor.six import text_type
4
5from . import base
6from ..constants import namespaces, voidElements
7
8from ..constants import spaceCharacters
9spaceCharacters = "".join(spaceCharacters)
10
11
12class 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