Python3: fix few htmllib problems

This commit is contained in:
Petr Dlouhý 2018-01-05 22:48:46 +01:00 committed by Chris Mayo
parent 205ceb6805
commit 2daf685633

View file

@ -19,6 +19,8 @@ Default HTML parser handler classes.
"""
import sys
from builtins import bytes, str as str_text
from builtins import chr
class HtmlPrinter (object):
@ -43,7 +45,8 @@ class HtmlPrinter (object):
@type attrs: tuple
@return: None
"""
print >> self.fd, self.mem, attrs
self.fd.write(self.mem)
self.fd.write(str_text(attrs))
def __getattr__ (self, name):
"""
@ -201,14 +204,18 @@ def quote_attrval (s):
"""
res = []
for c in s:
if ord(c) <= 127:
try: # Python 2
ord_c = ord(c)
except TypeError:
ord_c = c
if ord_c <= 127:
# ASCII
if c == u'&':
res.append(u"&amp;")
elif c == u'"':
res.append(u"&quot;")
else:
res.append(c)
res.append(chr(ord_c))
else:
res.append(u"&#%d;" % ord(c))
res.append(u"&#%d;" % ord_c)
return u"".join(res)