Bug#4287; Html.attr was not closing such elements as: <img ... /> . Thanks Gerald Britton for the patch.

svn: r16587
This commit is contained in:
Rob G. Healey
2011-02-07 07:13:22 +00:00
parent 69f1ec0508
commit 7a9fe58b48

View File

@@ -26,6 +26,8 @@
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
from __future__ import print_function
""" """
HTML operations. HTML operations.
@@ -95,18 +97,6 @@ _START_CLOSE = set([
'param' 'param'
]) ])
#------------------------------------------------------------------------
#
# Helper functions.
#
#------------------------------------------------------------------------
def print_(line):
"""
Print function
"""
print line
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #
# Html class. # Html class.
@@ -280,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
@@ -383,7 +373,7 @@ class Html(list):
# #
iterkeys = itervalues = iteritems = __iter__ iterkeys = itervalues = iteritems = __iter__
# #
def write(self, method=print_, indent='\t', tabs=''): def write(self, method=print, indent='\t', tabs=''):
""" """
Output function: performs an insertion-order tree traversal Output function: performs an insertion-order tree traversal
and calls supplied method for each item found. and calls supplied method for each item found.
@@ -471,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)
# #
@@ -483,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):
@@ -493,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):