Skip to content

Commit b6ce031

Browse files
committed
Add alphabetical_attributes option to the serializer
1 parent 508ab39 commit b6ce031

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

html5lib/serializer/htmlserializer.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class HTMLSerializer(object):
9292
resolve_entities = True
9393

9494
# miscellaneous options
95+
alphabetical_attributes = False
9596
inject_meta_charset = True
9697
strip_whitespace = False
9798
sanitize = 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

105107
def __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):
185189
if self.omit_optional_tags:
186190
from ..filters.optionaltags import Filter
187191
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+
188198
for token in treewalker:
189199
type = token["type"]
190200
if type == "Doctype":

0 commit comments

Comments
 (0)