Skip to content

Commit f628385

Browse files
committed
Update characters that need be quoted in attributes in the serializer per spec
This also moves to using re, which seems far cleaner than the reduce-based search previously used.
1 parent e8d1802 commit f628385

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

html5lib/serializer/htmlserializer.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import gettext
55
_ = gettext.gettext
66

7-
try:
8-
from functools import reduce
9-
except ImportError:
10-
pass
7+
import re
118

129
from ..constants import voidElements, booleanAttributes, spaceCharacters
1310
from ..constants import rcdataElements, entities, xmlEntities
@@ -16,6 +13,8 @@
1613

1714
spaceCharacters = "".join(spaceCharacters)
1815

16+
quoteAttributeSpec = re.compile("[" + spaceCharacters + "\"'=<>`]")
17+
1918
try:
2019
from codecs import register_error, xmlcharrefreplace_errors
2120
except ImportError:
@@ -243,11 +242,10 @@ def serialize(self, treewalker, encoding=None):
243242
(k not in booleanAttributes.get(name, tuple())
244243
and k not in booleanAttributes.get("", tuple())):
245244
yield self.encodeStrict("=")
246-
if self.quote_attr_values or not v:
245+
if self.quote_attr_values:
247246
quote_attr = True
248247
else:
249-
quote_attr = reduce(lambda x, y: x or (y in v),
250-
spaceCharacters + ">\"'=", False)
248+
quote_attr = len(v) == 0 or quoteAttributeSpec.search(v)
251249
v = v.replace("&", "&amp;")
252250
if self.escape_lt_in_attrs:
253251
v = v.replace("<", "&lt;")

0 commit comments

Comments
 (0)