|
14 | 14 | spaceCharacters = "".join(spaceCharacters)
|
15 | 15 |
|
16 | 16 | quoteAttributeSpec = re.compile("[" + spaceCharacters + "\"'=<>`]")
|
| 17 | +quoteAttributeLegacy = re.compile("[\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n" |
| 18 | + "\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15" |
| 19 | + "\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" |
| 20 | + "\x20\x2f\x60\xa0\u1680\u180e\u180f\u2000" |
| 21 | + "\u2001\u2002\u2003\u2004\u2005\u2006\u2007" |
| 22 | + "\u2008\u2009\u200a\u2028\u2029\u202f\u205f" |
| 23 | + "\u3000]") |
17 | 24 |
|
18 | 25 | try:
|
19 | 26 | from codecs import register_error, xmlcharrefreplace_errors
|
@@ -75,7 +82,7 @@ def htmlentityreplace_errors(exc):
|
75 | 82 | class HTMLSerializer(object):
|
76 | 83 |
|
77 | 84 | # attribute quoting options
|
78 |
| - quote_attr_values = False |
| 85 | + quote_attr_values = "legacy" |
79 | 86 | quote_char = '"'
|
80 | 87 | use_best_quote_char = True
|
81 | 88 |
|
@@ -111,9 +118,9 @@ def __init__(self, **kwargs):
|
111 | 118 | inject_meta_charset=True|False
|
112 | 119 | Whether it insert a meta element to define the character set of the
|
113 | 120 | document.
|
114 |
| - quote_attr_values=True|False |
| 121 | + quote_attr_values="legacy"|"spec"|True |
115 | 122 | Whether to quote attribute values that don't require quoting
|
116 |
| - per HTML5 parsing rules. |
| 123 | + per legacy browser behaviour, HTML authoring rules, or always. |
117 | 124 | quote_char=u'"'|u"'"
|
118 | 125 | Use given quote character for attribute quoting. Default is to
|
119 | 126 | use double quote unless attribute value contains a double quote,
|
@@ -242,10 +249,10 @@ def serialize(self, treewalker, encoding=None):
|
242 | 249 | (k not in booleanAttributes.get(name, tuple())
|
243 | 250 | and k not in booleanAttributes.get("", tuple())):
|
244 | 251 | yield self.encodeStrict("=")
|
245 |
| - if self.quote_attr_values: |
| 252 | + if self.quote_attr_values or len(v) == 0: |
246 | 253 | quote_attr = True
|
247 |
| - else: |
248 |
| - quote_attr = len(v) == 0 or quoteAttributeSpec.search(v) |
| 254 | + elif : |
| 255 | + quoteAttributeSpec.search(v) |
249 | 256 | v = v.replace("&", "&")
|
250 | 257 | if self.escape_lt_in_attrs:
|
251 | 258 | v = v.replace("<", "<")
|
|
0 commit comments