22# Copyright (c) 2015, 2019 Tim Savannah All Rights Rserved under LGPLv3. See LICENSE (https://gnu.org/licenses/lgpl-3.0.txt) for more information.
33
44from .Parser import AdvancedHTMLParser
5+ from .Tags import isValidAttributeName
56
6- from .exceptions import InvalidCloseException , MissedCloseException
7+ from .exceptions import InvalidCloseException , MissedCloseException , InvalidAttributeNameException
78
8- __all__ = ('InvalidCloseException' , 'MissedCloseException' , 'ValidatingAdvancedHTMLParser' )
9+ __all__ = ('InvalidCloseException' , 'MissedCloseException' , 'InvalidAttributeNameException' ,
10+ 'ValidatingAdvancedHTMLParser' ,
11+ )
912
1013class ValidatingAdvancedHTMLParser (AdvancedHTMLParser ):
1114 '''
@@ -16,6 +19,27 @@ class ValidatingAdvancedHTMLParser(AdvancedHTMLParser):
1619 exceptions.MissedCloseException - The parsed string/file missed closing an item.
1720 '''
1821
22+ def handle_starttag (self , tagName , attributeList , isSelfClosing = False ):
23+ '''
24+ handle_starttag - internal for parsing,
25+
26+ ValidatingAdvancedHTMLParser will run through the attributes list and make sure
27+ none have an invalid name, or will raise an error.
28+
29+
30+ @raises - InvalidAttributeNameException if an attribute name is passed with invalid character(s)
31+ '''
32+
33+ # Iterate over the passed attributes, and validate them.
34+ for (attrName , attrValue ) in attributeList :
35+
36+ if isValidAttributeName (attrName ) is False :
37+
38+ raise InvalidAttributeNameException (tagName , attrName , attrValue )
39+
40+ # Done validating, feed to parent.
41+ return AdvancedHTMLParser .handle_starttag (self , tagName , attributeList , isSelfClosing )
42+
1943
2044 def handle_endtag (self , tagName ):
2145 '''
0 commit comments