Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22c2b1a

Browse files
committed
Get rid of LintError and just use asserts
All of these properties should always hold per the API, so asserts seem like a good match here.
1 parent e0ea899 commit 22c2b1a

File tree

1 file changed

+28
-49
lines changed

1 file changed

+28
-49
lines changed

html5lib/filters/lint.py

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
spaceCharacters = "".join(spaceCharacters)
1010

1111

12-
class LintError(Exception):
13-
pass
14-
15-
1612
class Filter(_base.Filter):
1713
def __iter__(self):
1814
open_elements = []
@@ -21,73 +17,56 @@ def __iter__(self):
2117
if type in ("StartTag", "EmptyTag"):
2218
namespace = token["namespace"]
2319
name = token["name"]
24-
if namespace is not None and not isinstance(namespace, text_type):
25-
raise LintError("Tag namespace is not a string or None: %(name)r" % {"name": namespace})
26-
if namespace == "":
27-
raise LintError("Empty tag namespace")
28-
if not isinstance(name, text_type):
29-
raise LintError("Tag name is not a string: %(tag)r" % {"tag": name})
30-
if not name:
31-
raise LintError("Empty tag name")
32-
if type == "StartTag" and (not namespace or namespace == namespaces["html"]) and name in voidElements:
33-
raise LintError("Void element reported as StartTag token: %(tag)s" % {"tag": name})
34-
elif type == "EmptyTag" and (not namespace or namespace == namespaces["html"]) and name not in voidElements:
35-
raise LintError("Non-void element reported as EmptyTag token: %(tag)s" % {"tag": token["name"]})
20+
assert namespace is None or isinstance(namespace, text_type)
21+
assert namespace != ""
22+
assert isinstance(name, text_type)
23+
assert name != ""
24+
assert isinstance(token["data"], dict)
25+
if (not namespace or namespace == namespaces["html"]) and name in voidElements:
26+
assert type == "EmptyTag"
27+
else:
28+
assert type == "StartTag"
3629
if type == "StartTag":
3730
open_elements.append((namespace, name))
38-
for (namespace, localname), value in token["data"].items():
39-
if namespace is not None and not isinstance(namespace, text_type):
40-
raise LintError("Attribute namespace is not a string or None: %(name)r" % {"name": namespace})
41-
if namespace == "":
42-
raise LintError("Empty attribute namespace")
43-
if not isinstance(localname, text_type):
44-
raise LintError("Attribute localname is not a string: %(name)r" % {"name": localname})
45-
if not localname:
46-
raise LintError("Empty attribute localname")
47-
if not isinstance(value, text_type):
48-
raise LintError("Attribute value is not a string: %(value)r" % {"value": value})
31+
for (namespace, name), value in token["data"].items():
32+
assert namespace is None or isinstance(namespace, text_type)
33+
assert namespace != ""
34+
assert isinstance(name, text_type)
35+
assert name != ""
36+
assert isinstance(value, text_type)
4937

5038
elif type == "EndTag":
5139
namespace = token["namespace"]
5240
name = token["name"]
53-
if namespace is not None and not isinstance(namespace, text_type):
54-
raise LintError("Tag namespace is not a string or None: %(name)r" % {"name": namespace})
55-
if namespace == "":
56-
raise LintError("Empty tag namespace")
57-
if not isinstance(name, text_type):
58-
raise LintError("Tag name is not a string: %(tag)r" % {"tag": name})
59-
if not name:
60-
raise LintError("Empty tag name")
41+
assert namespace is None or isinstance(namespace, text_type)
42+
assert namespace != ""
43+
assert isinstance(name, text_type)
44+
assert name != ""
6145
if (not namespace or namespace == namespaces["html"]) and name in voidElements:
62-
raise LintError("Void element reported as EndTag token: %(tag)s" % {"tag": name})
63-
start_name = open_elements.pop()
64-
if start_name != (namespace, name):
65-
raise LintError("EndTag (%(end)s) does not match StartTag (%(start)s)" % {"end": name, "start": start_name})
46+
assert False, "Void element reported as EndTag token: %(tag)s" % {"tag": name}
47+
else:
48+
start = open_elements.pop()
49+
assert start == (namespace, name)
6650

6751
elif type == "Comment":
6852
pass
6953

7054
elif type in ("Characters", "SpaceCharacters"):
7155
data = token["data"]
72-
if not isinstance(data, text_type):
73-
raise LintError("Attribute name is not a string: %(name)r" % {"name": data})
74-
if not data:
75-
raise LintError("%(type)s token with empty data" % {"type": type})
56+
assert isinstance(data, text_type)
57+
assert data != ""
7658
if type == "SpaceCharacters":
77-
data = data.strip(spaceCharacters)
78-
if data:
79-
raise LintError("Non-space character(s) found in SpaceCharacters token: %(token)r" % {"token": data})
59+
assert data.strip(spaceCharacters) == ""
8060

8161
elif type == "Doctype":
8262
name = token["name"]
83-
if name is not None and not isinstance(name, text_type):
84-
raise LintError("Tag name is not a string or None: %(tag)r" % {"tag": name})
63+
assert name is None or isinstance(name, text_type)
8564
# XXX: what to do with token["data"] ?
8665

8766
elif type in ("ParseError", "SerializeError"):
8867
pass
8968

9069
else:
91-
raise LintError("Unknown token type: %(type)s" % {"type": type})
70+
assert False, "Unknown token type: %(type)s" % {"type": type}
9271

9372
yield token

0 commit comments

Comments
 (0)