Skip to content

Commit 616f83c

Browse files
committed
Do similar hack to style as was done with className on SpecialAttributesDict. Add tests which were failing before but now passing.
1 parent 78fb713 commit 616f83c

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

AdvancedHTMLParser/SpecialAttributes.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def setdefault(self, *args, **kwargs):
112112

113113
def _handleClassAttr(self):
114114
'''
115-
_handleClassAttr - Hack to ensure "class" shows up in attributes when classes are set,
115+
_handleClassAttr - Hack to ensure "class" and "style" show up in attributes when classes are set,
116116
and doesn't when no classes are present on associated tag.
117117
118118
TODO: I don't like this hack.
@@ -125,6 +125,15 @@ def _handleClassAttr(self):
125125
except:
126126
pass
127127

128+
styleAttr = self.tag.style
129+
if styleAttr.isEmpty() is False:
130+
dict.__setitem__(self, "style", styleAttr)
131+
else:
132+
try:
133+
dict.__delitem__(self, "style")
134+
except:
135+
pass
136+
128137
def items(self):
129138
# Intercept and apply the "class" attribute hack
130139
self._handleClassAttr()
@@ -508,6 +517,7 @@ def _ensureHtmlAttribute(self):
508517
else: #if 'style' not in tagAttributes.keys():
509518
tagAttributes._direct_set('style', self)
510519

520+
511521
def isEmpty(self):
512522
'''
513523
isEmpty - Check if this is an "empty" style (no attributes set)

tests/AdvancedHTMLParserTests/test_Style.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,5 +334,77 @@ def test_styleCopy(self):
334334
assert 'float: left' not in str(tag1) , 'Expected update to oldStyle to not change html representation of tag with different style. str(tag1) = ' + str(tag1)
335335

336336

337+
def test_htmlAttrPresent(self):
338+
'''
339+
test_htmlAttrPresent - Test that html attribute is present when set, and not when not.
340+
'''
341+
tag = AdvancedHTMLParser.AdvancedTag('div')
342+
343+
assert "style=" not in str(tag) , 'Expected style to not be in tag html. Got: ' + str(tag)
344+
345+
assert 'style' not in tag.attributes , "Expected style to not be in tag attributes."
346+
347+
tagCopy = tag.cloneNode()
348+
349+
tag.style = 'display: block; float: left'
350+
351+
tagHTML = str(tag)
352+
353+
assert 'style="' in tagHTML and 'display: block' in tagHTML and 'float: left' in tagHTML , "Expected style to be set properly on tag, but was not. Got: " + tagHTML
354+
355+
tag.style = ''
356+
tagHTML = str(tag)
357+
358+
assert "style" not in tagHTML , "Expected after clearing style via tag.style='' that the attribute was gone. Got: " + tagHTML
359+
360+
assert str(tag.getAttribute("style")) == "" , "Expected after clearing style via tag.style='' that attribute was gone."
361+
362+
tag = tagCopy
363+
364+
tagCopy = tag.cloneNode()
365+
366+
assert str(tag.style) == '' , 'Expected cloned node to not have linked style to its parent'
367+
368+
tag.setAttribute("style", "display: block; float: right")
369+
370+
tagHTML = str(tag)
371+
372+
assert 'style="' in tagHTML and 'display: block' in tagHTML and 'float: right' in tagHTML , "Expected style to be set properly on tag with setAttribute('style', ...) , but was not. Got: " + tagHTML
373+
374+
tag.setAttribute('style', 'display: block')
375+
tagHTML = str(tag)
376+
assert 'style="' in tagHTML and 'display: block' in tagHTML and 'float: right' not in tagHTML , "Expected style to be set properly on tag after modification by setAttribute('style', ...) , but was not. Got: " + tagHTML
377+
378+
tagCopy = tag.cloneNode()
379+
380+
tag.setAttribute('style', '')
381+
382+
tagHTML = str(tag)
383+
384+
assert "style" not in tagHTML , "Expected setAttribute('style', '') to clear style from html, but did not. Got: " + tagHTML
385+
386+
tag = tagCopy
387+
tagCopy = tag.cloneNode()
388+
tagHTML = str(tag)
389+
390+
assert "style" in tagHTML , "Expected cloneNode to retain style, did not."
391+
392+
tag.removeAttribute('style')
393+
tagHTML = str(tag)
394+
395+
assert "style" not in tagHTML , "Expected removeAttribute('style') to clear style from html, but did not. Got: " + tagHTML
396+
397+
398+
tag = tagCopy
399+
400+
401+
tag.attributes['style'] = ''
402+
tagHTML = str(tag)
403+
404+
assert "style" not in tagHTML , "Expected tag.attributes['style'] = '' to clear stlye from html, but did not. Got: " + tagHTML
405+
406+
assert str(tag.style) == '' , 'Expected tag.attributes["style"] = '' to clear style attribute, but tag.style returned: ' + str(tag.style)
407+
408+
337409
if __name__ == '__main__':
338410
sys.exit(subprocess.Popen('GoodTests.py -n1 "%s" %s' %(sys.argv[0], ' '.join(['"%s"' %(arg.replace('"', '\\"'), ) for arg in sys.argv[1:]]) ), shell=True).wait())

0 commit comments

Comments
 (0)