Repository navigation
Expand file tree
/
Copy pathValidator.py
More file actions
76 lines (52 loc) · 2.53 KB
/
Copy pathValidator.py
File metadata and controls
76 lines (52 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
Copyright (c) 2015, 2019 Tim Savannah under LGPLv3. All Rights Reserved.
See LICENSE (https://gnu.org/licenses/lgpl-3.0.txt) for more information.
Validator - A validating version of the HTML parser (vs the normal 'best effort' parser)
'''
from .Parser import AdvancedHTMLParser
from .Tags import isValidAttributeName
from .exceptions import InvalidCloseException, MissedCloseException, InvalidAttributeNameException
__all__ = ('InvalidCloseException', 'MissedCloseException', 'InvalidAttributeNameException',
'ValidatingAdvancedHTMLParser',
)
class ValidatingAdvancedHTMLParser(AdvancedHTMLParser):
'''
ValidatingAdvancedHTMLParser - A parser which will raise Exceptions for a couple HTML errors that would otherwise cause
an assumption to be made during parsing.
exceptions.InvalidCloseException - The parsed string/file tried to close something it shouldn't have.
exceptions.MissedCloseException - The parsed string/file missed closing an item.
'''
def handle_starttag(self, tagName, attributeList, isSelfClosing=False):
'''
handle_starttag - internal for parsing,
ValidatingAdvancedHTMLParser will run through the attributes list and make sure
none have an invalid name, or will raise an error.
@raises - InvalidAttributeNameException if an attribute name is passed with invalid character(s)
'''
# Iterate over the passed attributes, and validate them.
for (attrName, attrValue) in attributeList:
if isValidAttributeName(attrName) is False:
raise InvalidAttributeNameException(tagName, attrName, attrValue)
# Done validating, feed to parent.
return AdvancedHTMLParser.handle_starttag(self, tagName, attributeList, isSelfClosing)
def handle_endtag(self, tagName):
'''
Internal for parsing
'''
inTag = self._inTag
if len(inTag) == 0:
# Attempted to close, but no open tags
raise InvalidCloseException(tagName, [])
foundIt = False
i = len(inTag) - 1
while i >= 0:
if inTag[i].tagName == tagName:
foundIt = True
break
i -= 1
if not foundIt:
# Attempted to close, but did not match anything
raise InvalidCloseException(tagName, inTag)
if inTag[-1].tagName != tagName:
raise MissedCloseException(tagName, [x for x in inTag[-1 * (i+1): ] ] )
inTag.pop()