Skip to content

Commit 2b4f235

Browse files
committed
Fix issues where style could not show up after being set on tag
1 parent 25e4d4b commit 2b4f235

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

AdvancedHTMLParser/SpecialAttributes.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ def _direct_set(self, key, value):
4444

4545
def __setitem__(self, key, value):
4646
if key == 'style':
47-
self.tag.style = StyleAttribute(value)
47+
if not isinstance(value, StyleAttribute):
48+
self.tag.style = StyleAttribute(value, self.tag)
49+
else:
50+
self.tag.style = value
4851
elif key == 'class':
4952

5053
# Ensure when we update the "class" attribute, that we update the list as well.
@@ -220,9 +223,9 @@ class StyleAttribute(object):
220223
StyleAttribute - Represents the "style" field on a tag.
221224
'''
222225

223-
RESERVED_ATTRIBUTES = ('_styleValue', '_styleDict', '_asStr')
226+
RESERVED_ATTRIBUTES = ('_styleValue', '_styleDict', '_asStr', 'tag')
224227

225-
def __init__(self, styleValue):
228+
def __init__(self, styleValue, tag=None):
226229
'''
227230
__init__ - Create a StyleAttribute object.
228231
@@ -233,6 +236,7 @@ def __init__(self, styleValue):
233236

234237
self._styleValue = styleValue
235238
self._styleDict = StyleAttribute.styleToDict(styleValue)
239+
self.tag = tag
236240

237241
def __getattribute__(self, name):
238242
'''
@@ -281,6 +285,9 @@ def __setattr__(self, name, val):
281285
else:
282286
self._styleDict[name] = val
283287

288+
if self.tag and self._styleDict:
289+
self.tag.setAttribute('style', self)
290+
284291
return val
285292

286293
@staticmethod
@@ -452,7 +459,7 @@ def __str__(self):
452459
return self._asStr()
453460

454461
def __repr__(self):
455-
return "%s(%s)" %(self.__class__.__name__, repr(self._asStr()))
462+
return "%s(%s)" %(type(self).__name__, repr(self._asStr()))
456463

457464
def __copy__(self):
458465
return self.__class__(self._asStr())

AdvancedHTMLParser/Tags.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ def __init__(self, tagName, attrList=None, isSelfClosing=False, ownerDocument=No
104104
if isSelfClosing is False and tagName in IMPLICIT_SELF_CLOSING_TAGS:
105105
isSelfClosing = True
106106

107+
styleAttr = StyleAttribute('', self)
108+
107109
rawSet('_attributes', SpecialAttributesDict(self))
108110
rawSet('text', '')
109111
rawSet('blocks', [''])
110112
rawSet('classNames', [])
111-
rawSet('style', StyleAttribute(''))
113+
rawSet('style', styleAttr)
112114

113115
rawSet('isSelfClosing', isSelfClosing)
114116

@@ -145,7 +147,7 @@ def __setattr__(self, name, value):
145147
return value
146148

147149
if name == 'style' and not isinstance(value, StyleAttribute):
148-
value = StyleAttribute(value)
150+
value = StyleAttribute(value, self)
149151

150152
try:
151153
return object.__setattr__(self, name, value)
@@ -853,6 +855,7 @@ def getStartTag(self):
853855
'''
854856
attributeString = []
855857
for name, val in self._attributes.items():
858+
val = str(val)
856859
if val:
857860
val = escapeQuotes(val)
858861
attributeString.append('%s="%s"' %(name, val) )
@@ -1063,7 +1066,11 @@ def setStyle(self, styleName, styleValue):
10631066
10641067
@return - String of current value of "style" after change is made.
10651068
'''
1066-
setattr(self.style, styleName, styleValue)
1069+
if 'style' not in self._attributes:
1070+
self._attributes['style'] = "%s: %s" %(styleName, styleValue)
1071+
else:
1072+
setattr(self._attributes['style'], styleName, styleValue)
1073+
# setattr(self.style, styleName, styleValue)
10671074

10681075
def setStyles(self, styleUpdatesDict):
10691076
'''
@@ -1078,7 +1085,7 @@ def setStyles(self, styleUpdatesDict):
10781085
@return - String of current value of "style" after change is made.
10791086
'''
10801087
for newName, newValue in styleUpdatesDict.items():
1081-
setattr(self.style, newName, newValue)
1088+
self.setStyle(newName, newValue)
10821089

10831090
return self.style
10841091

0 commit comments

Comments
 (0)