Skip to content

Commit 1495b8b

Browse files
kata198Timothy Savannah
authored andcommitted
Fix regression where AdvancedTag.getAttribute didn't take a second param for default value. This also broke calling '.value' on an AdvancedTag. Fix, with tests.
1 parent fd77384 commit 1495b8b

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

AdvancedHTMLParser/Tags.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,12 @@ def value(self):
593593
'''
594594
return self.getAttribute('value', '')
595595

596-
def getAttribute(self, attrName):
596+
def getAttribute(self, attrName, defaultValue=None):
597597
'''
598598
getAttribute - Gets an attribute on this tag. Be wary using this for classname, maybe use addClass/removeClass. Attribute names are all lowercase.
599599
@return - The attribute value, or None if none exists.
600600
'''
601-
return self.attributes.get(attrName, None)
601+
return self.attributes.get(attrName, defaultValue)
602602

603603
def setAttribute(self, attrName, attrValue):
604604
'''

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* 6.6.4 (unreleased)
2+
- Fix regression where "AdvancedTag.getAttribute" method would not accept a default
3+
(second param).
4+
- Fix calling ".value" on an AdvancedTag to get the "value" attribute (was
5+
broken by previous regression)
6+
7+
18
* 6.6.3 Oct 03 2016
29
- Fix no-value attributes not appearing in html output (like "checked" on an input). Was in attributes, but not in html output.
310

tests/AdvancedHTMLParserTests/test_Attributes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ def test_noValueAttributes(self):
113113
assert 'checked' in tag.attributes
114114
assert 'checked' in tag.outerHTML
115115

116+
def test_valueMethod(self):
117+
parser = AdvancedHTMLParser()
118+
parser.parseStr('<input id="item" type="text" value="hello" />')
119+
120+
tag = parser.getElementById('item')
121+
assert tag.value == 'hello'
122+
123+
def test_attributeDefault(self):
124+
parser = AdvancedHTMLParser()
125+
parser.parseStr('<input id="item" type="text" value="hello" />')
126+
127+
tag = parser.getElementById('item')
128+
assert tag.getAttribute('type', 'bloogity') == 'text'
129+
assert tag.getAttribute('woogity', 'snoogity') == 'snoogity'
130+
116131

117132
if __name__ == '__main__':
118133
pipe = subprocess.Popen('GoodTests.py "%s"' %(sys.argv[0],), shell=True).wait()

0 commit comments

Comments
 (0)