9
9
spaceCharacters = "" .join (spaceCharacters )
10
10
11
11
12
- class LintError (Exception ):
13
- pass
14
-
15
-
16
12
class Filter (_base .Filter ):
17
13
def __iter__ (self ):
18
14
open_elements = []
@@ -21,73 +17,56 @@ def __iter__(self):
21
17
if type in ("StartTag" , "EmptyTag" ):
22
18
namespace = token ["namespace" ]
23
19
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"
36
29
if type == "StartTag" :
37
30
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 )
49
37
50
38
elif type == "EndTag" :
51
39
namespace = token ["namespace" ]
52
40
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 != ""
61
45
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 )
66
50
67
51
elif type == "Comment" :
68
52
pass
69
53
70
54
elif type in ("Characters" , "SpaceCharacters" ):
71
55
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 != ""
76
58
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 ) == ""
80
60
81
61
elif type == "Doctype" :
82
62
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 )
85
64
# XXX: what to do with token["data"] ?
86
65
87
66
elif type in ("ParseError" , "SerializeError" ):
88
67
pass
89
68
90
69
else :
91
- raise LintError ( "Unknown token type: %(type)s" % {"type" : type })
70
+ assert False , "Unknown token type: %(type)s" % {"type" : type }
92
71
93
72
yield token
0 commit comments