@@ -92,6 +92,7 @@ class HTMLSerializer(object):
92
92
resolve_entities = True
93
93
94
94
# miscellaneous options
95
+ alphabetical_attributes = False
95
96
inject_meta_charset = True
96
97
strip_whitespace = False
97
98
sanitize = False
@@ -100,7 +101,8 @@ class HTMLSerializer(object):
100
101
"omit_optional_tags" , "minimize_boolean_attributes" ,
101
102
"use_trailing_solidus" , "space_before_trailing_solidus" ,
102
103
"escape_lt_in_attrs" , "escape_rcdata" , "resolve_entities" ,
103
- "inject_meta_charset" , "strip_whitespace" , "sanitize" )
104
+ "alphabetical_attributes" , "inject_meta_charset" ,
105
+ "strip_whitespace" , "sanitize" )
104
106
105
107
def __init__ (self , ** kwargs ):
106
108
"""Initialize HTMLSerializer.
@@ -143,6 +145,8 @@ def __init__(self, **kwargs):
143
145
See `html5lib user documentation`_
144
146
omit_optional_tags=True|False
145
147
Omit start/end tags that are optional.
148
+ alphabetical_attributes=False|True
149
+ Reorder attributes to be in alphabetical order.
146
150
147
151
.. _html5lib user documentation: http://code.google.com/p/html5lib/wiki/UserDocumentation
148
152
"""
@@ -185,6 +189,12 @@ def serialize(self, treewalker, encoding=None):
185
189
if self .omit_optional_tags :
186
190
from ..filters .optionaltags import Filter
187
191
treewalker = Filter (treewalker )
192
+ # Alphabetical attributes must be last, as other filters
193
+ # could add attributes and alter the order
194
+ if self .alphabetical_attributes :
195
+ from ..filters .alphabeticalattributes import Filter
196
+ treewalker = Filter (treewalker )
197
+
188
198
for token in treewalker :
189
199
type = token ["type" ]
190
200
if type == "Doctype" :
0 commit comments