Bug 4287: Fix Html.attr property for self-closing tags (e.g. <img ... />)

svn: r16582
This commit is contained in:
Gerald Britton 2011-02-05 18:59:44 +00:00
parent b1ce7f7014
commit bfc6e84e07

View File

@ -270,7 +270,7 @@ class Html(list):
for keyw, arg in keywargs.iteritems(): for keyw, arg in keywargs.iteritems():
if (keyw in ['indent', 'close', 'inline'] and if (keyw in ['indent', 'close', 'inline'] and
arg in [True, False, None]): arg in [True, False, None]):
setattr(self,keyw, arg) setattr(self, keyw, arg)
elif keyw == 'attr': # pass attributes along elif keyw == 'attr': # pass attributes along
attr += ' ' + arg attr += ' ' + arg
elif keyw[-1] == '_': # avoid Python conflicts elif keyw[-1] == '_': # avoid Python conflicts
@ -461,8 +461,14 @@ class Html(list):
:param name: new HTML tag :param name: new HTML tag
""" """
curtag = self.tag curtag = self.tag
# Replace closing tag, if any
if self[-1] == '</%s>' % curtag: if self[-1] == '</%s>' % curtag:
self[-1] = '</%s>' % newtag self[-1] = '</%s>' % newtag
# Replace opening tag
self[0] = self[0].replace('<' + curtag, '<' + newtag) self[0] = self[0].replace('<' + curtag, '<' + newtag)
tag = property(__gettag, __settag) tag = property(__gettag, __settag)
# #
@ -473,7 +479,7 @@ class Html(list):
:rtype: string :rtype: string
:returns: HTML attributes :returns: HTML attributes
""" """
attr = self[0].strip('<!?>').split(None, 1) attr = self[0].strip('<!?/>').split(None, 1)
return attr[1] if len(attr) > 1 else '' return attr[1] if len(attr) > 1 else ''
# #
def __setattr(self, value): def __setattr(self, value):
@ -483,13 +489,24 @@ class Html(list):
:type name: string :type name: string
:param name: new HTML attributes :param name: new HTML attributes
""" """
self[0] = self[0][:len(self.tag)+1] + ' ' + value + self[0][-1:] beg = len(self.tag) + 1
# See if self-closed or normal
end = -2 if self.close is False else -1
self[0] = self[0][:beg] + ' ' + value + self[0][end:]
# #
def __delattr(self): def __delattr(self):
""" """
Removes HTML attributes for this object Removes HTML attributes for this object
""" """
self[0] = '<' + self.tag + '>' self[0] = '<' + self.tag + (
# Set correct closing delimiter(s)
' />' if self.close is False else '>'
)
#
attr = property(__getattr, __setattr, __delattr) attr = property(__getattr, __setattr, __delattr)
# #
def __getinside(self): def __getinside(self):