@@ -92,6 +92,7 @@ class HTMLSerializer(object):
9292resolve_entities = True
9393
9494# miscellaneous options
95+ alphabetical_attributes = False
9596inject_meta_charset = True
9697strip_whitespace = False
9798sanitize = False
@@ -100,7 +101,8 @@ class HTMLSerializer(object):
100101"omit_optional_tags" ,"minimize_boolean_attributes" ,
101102"use_trailing_solidus" ,"space_before_trailing_solidus" ,
102103"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" )
104106
105107def __init__ (self ,** kwargs ):
106108"""Initialize HTMLSerializer.
@@ -143,6 +145,8 @@ def __init__(self, **kwargs):
143145 See `html5lib user documentation`_
144146 omit_optional_tags=True|False
145147 Omit start/end tags that are optional.
148+ alphabetical_attributes=False|True
149+ Reorder attributes to be in alphabetical order.
146150
147151 .. _html5lib user documentation: http://code.google.com/p/html5lib/wiki/UserDocumentation
148152 """
@@ -185,6 +189,12 @@ def serialize(self, treewalker, encoding=None):
185189if self .omit_optional_tags :
186190from ..filters .optionaltags import Filter
187191treewalker = 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+
188198for token in treewalker :
189199type = token ["type" ]
190200if type == "Doctype" :