|
1 | | -from gettext import gettext |
2 | | -_ = gettext |
3 | | - |
4 | | -import _base |
5 | | -from constants import cdataElements, rcdataElements, voidElements |
6 | | - |
7 | | -from constants import spaceCharacters |
8 | | -spaceCharacters = u"".join(spaceCharacters) |
9 | | - |
10 | | -class LintError(Exception): pass |
11 | | - |
12 | | -class Filter(_base.Filter): |
13 | | - def __iter__(self): |
14 | | - open_elements = [] |
15 | | - contentModelFlag = "PCDATA" |
16 | | - for token in _base.Filter.__iter__(self): |
17 | | - type = token["type"] |
18 | | - if type in ("StartTag", "EmptyTag"): |
19 | | - name = token["name"] |
20 | | - if contentModelFlag != "PCDATA": |
21 | | - raise LintError(_("StartTag not in PCDATA content model flag: %s") % name) |
22 | | - if not isinstance(name, unicode): |
23 | | - raise LintError(_(u"Tag name is not a string: %r") % name) |
24 | | - if not name: |
25 | | - raise LintError(_(u"Empty tag name")) |
26 | | - if type == "StartTag" and name in voidElements: |
27 | | - raise LintError(_(u"Void element reported as StartTag token: %s") % name) |
28 | | - elif type == "EmptyTag" and name not in voidElements: |
29 | | - raise LintError(_(u"Non-void element reported as EmptyTag token: %s") % token["name"]) |
30 | | - if type == "StartTag": |
31 | | - open_elements.append(name) |
32 | | - for name, value in token["data"]: |
33 | | - if not isinstance(name, unicode): |
34 | | - raise LintError(_("Attribute name is not a string: %r") % name) |
35 | | - if not name: |
36 | | - raise LintError(_(u"Empty attribute name")) |
37 | | - if not isinstance(value, unicode): |
38 | | - raise LintError(_("Attribute value is not a string: %r") % value) |
39 | | - if name in cdataElements: |
40 | | - contentModelFlag = "CDATA" |
41 | | - elif name in rcdataElements: |
42 | | - contentModelFlag = "RCDATA" |
43 | | - elif name == "plaintext": |
44 | | - contentModelFlag = "PLAINTEXT" |
45 | | - |
46 | | - elif type == "EndTag": |
47 | | - name = token["name"] |
48 | | - if not isinstance(name, unicode): |
49 | | - raise LintError(_(u"Tag name is not a string: %r") % name) |
50 | | - if not name: |
51 | | - raise LintError(_(u"Empty tag name")) |
52 | | - if name in voidElements: |
53 | | - raise LintError(_(u"Void element reported as EndTag token: %s") % name) |
54 | | - start_name = open_elements.pop() |
55 | | - if start_name != name: |
56 | | - raise LintError(_(u"EndTag (%s) does not match StartTag (%s)") % (name, start_name)) |
57 | | - contentModelFlag = "PCDATA" |
58 | | - |
59 | | - elif type == "Comment": |
60 | | - if contentModelFlag != "PCDATA": |
61 | | - raise LintError(_("Comment not in PCDATA content model flag")) |
62 | | - |
63 | | - elif type in ("Characters", "SpaceCharacters"): |
64 | | - data = token["data"] |
65 | | - if not isinstance(data, unicode): |
66 | | - raise LintError(_("Attribute name is not a string: %r") % data) |
67 | | - if not data: |
68 | | - raise LintError(_(u"%s token with empty data") % type) |
69 | | - if type == "SpaceCharacters": |
70 | | - data = data.strip(spaceCharacters) |
71 | | - if data: |
72 | | - raise LintError(_(u"Non-space character(s) found in SpaceCharacters token: ") % data) |
73 | | - |
74 | | - elif type == "Doctype": |
75 | | - name = token["name"] |
76 | | - if contentModelFlag != "PCDATA": |
77 | | - raise LintError(_("Doctype not in PCDATA content model flag: %s") % name) |
78 | | - if not isinstance(name, unicode): |
79 | | - raise LintError(_(u"Tag name is not a string: %r") % name) |
80 | | - if not name: |
81 | | - raise LintError(_(u"Empty tag name")) |
82 | | - # XXX: what to do with token["data"] ? |
83 | | - |
84 | | - elif type in ("ParseError", "SerializeError"): |
85 | | - pass |
86 | | - |
87 | | - else: |
88 | | - raise LintError(_(u"Unknown token type: %s") % type) |
89 | | - |
90 | | - yield token |
| 1 | +from gettext import gettext |
| 2 | +_ = gettext |
| 3 | + |
| 4 | +import _base |
| 5 | +from constants import cdataElements, rcdataElements, voidElements |
| 6 | + |
| 7 | +from constants import spaceCharacters |
| 8 | +spaceCharacters = u"".join(spaceCharacters) |
| 9 | + |
| 10 | +class LintError(Exception): pass |
| 11 | + |
| 12 | +class Filter(_base.Filter): |
| 13 | + def __iter__(self): |
| 14 | + open_elements = [] |
| 15 | + contentModelFlag = "PCDATA" |
| 16 | + for token in _base.Filter.__iter__(self): |
| 17 | + type = token["type"] |
| 18 | + if type in ("StartTag", "EmptyTag"): |
| 19 | + name = token["name"] |
| 20 | + if contentModelFlag != "PCDATA": |
| 21 | + raise LintError(_("StartTag not in PCDATA content model flag: %s") % name) |
| 22 | + if not isinstance(name, unicode): |
| 23 | + raise LintError(_(u"Tag name is not a string: %r") % name) |
| 24 | + if not name: |
| 25 | + raise LintError(_(u"Empty tag name")) |
| 26 | + if type == "StartTag" and name in voidElements: |
| 27 | + raise LintError(_(u"Void element reported as StartTag token: %s") % name) |
| 28 | + elif type == "EmptyTag" and name not in voidElements: |
| 29 | + raise LintError(_(u"Non-void element reported as EmptyTag token: %s") % token["name"]) |
| 30 | + if type == "StartTag": |
| 31 | + open_elements.append(name) |
| 32 | + for name, value in token["data"]: |
| 33 | + if not isinstance(name, unicode): |
| 34 | + raise LintError(_("Attribute name is not a string: %r") % name) |
| 35 | + if not name: |
| 36 | + raise LintError(_(u"Empty attribute name")) |
| 37 | + if not isinstance(value, unicode): |
| 38 | + raise LintError(_("Attribute value is not a string: %r") % value) |
| 39 | + if name in cdataElements: |
| 40 | + contentModelFlag = "CDATA" |
| 41 | + elif name in rcdataElements: |
| 42 | + contentModelFlag = "RCDATA" |
| 43 | + elif name == "plaintext": |
| 44 | + contentModelFlag = "PLAINTEXT" |
| 45 | + |
| 46 | + elif type == "EndTag": |
| 47 | + name = token["name"] |
| 48 | + if not isinstance(name, unicode): |
| 49 | + raise LintError(_(u"Tag name is not a string: %r") % name) |
| 50 | + if not name: |
| 51 | + raise LintError(_(u"Empty tag name")) |
| 52 | + if name in voidElements: |
| 53 | + raise LintError(_(u"Void element reported as EndTag token: %s") % name) |
| 54 | + start_name = open_elements.pop() |
| 55 | + if start_name != name: |
| 56 | + raise LintError(_(u"EndTag (%s) does not match StartTag (%s)") % (name, start_name)) |
| 57 | + contentModelFlag = "PCDATA" |
| 58 | + |
| 59 | + elif type == "Comment": |
| 60 | + if contentModelFlag != "PCDATA": |
| 61 | + raise LintError(_("Comment not in PCDATA content model flag")) |
| 62 | + |
| 63 | + elif type in ("Characters", "SpaceCharacters"): |
| 64 | + data = token["data"] |
| 65 | + if not isinstance(data, unicode): |
| 66 | + raise LintError(_("Attribute name is not a string: %r") % data) |
| 67 | + if not data: |
| 68 | + raise LintError(_(u"%s token with empty data") % type) |
| 69 | + if type == "SpaceCharacters": |
| 70 | + data = data.strip(spaceCharacters) |
| 71 | + if data: |
| 72 | + raise LintError(_(u"Non-space character(s) found in SpaceCharacters token: ") % data) |
| 73 | + |
| 74 | + elif type == "Doctype": |
| 75 | + name = token["name"] |
| 76 | + if contentModelFlag != "PCDATA": |
| 77 | + raise LintError(_("Doctype not in PCDATA content model flag: %s") % name) |
| 78 | + if not isinstance(name, unicode): |
| 79 | + raise LintError(_(u"Tag name is not a string: %r") % name) |
| 80 | + if not name: |
| 81 | + raise LintError(_(u"Empty tag name")) |
| 82 | + # XXX: what to do with token["data"] ? |
| 83 | + |
| 84 | + elif type in ("ParseError", "SerializeError"): |
| 85 | + pass |
| 86 | + |
| 87 | + else: |
| 88 | + raise LintError(_(u"Unknown token type: %s") % type) |
| 89 | + |
| 90 | + yield token |
0 commit comments