99spaceCharacters = "" .join (spaceCharacters )
1010
1111
12- class LintError (Exception ):
13- pass
14-
15-
1612class Filter (_base .Filter ):
1713def __iter__ (self ):
1814open_elements = []
@@ -21,73 +17,56 @@ def __iter__(self):
2117if type in ("StartTag" ,"EmptyTag" ):
2218namespace = token ["namespace" ]
2319name = 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"
3629if type == "StartTag" :
3730open_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
5038elif type == "EndTag" :
5139namespace = token ["namespace" ]
5240name = 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 != ""
6145if (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
6751elif type == "Comment" :
6852pass
6953
7054elif type in ("Characters" ,"SpaceCharacters" ):
7155data = 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 != ""
7658if 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
8161elif type == "Doctype" :
8262name = 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
8766elif type in ("ParseError" ,"SerializeError" ):
8867pass
8968
9069else :
91- raise LintError ( "Unknown token type: %(type)s" % {"type" :type })
70+ assert False , "Unknown token type: %(type)s" % {"type" :type }
9271
9372yield token