comparison env/lib/python3.9/site-packages/bleach/_vendor/html5lib/filters/alphabeticalattributes.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 from __future__ import absolute_import, division, unicode_literals
2
3 from . import base
4
5 from collections import OrderedDict
6
7
8 def _attr_key(attr):
9 """Return an appropriate key for an attribute for sorting
10
11 Attributes have a namespace that can be either ``None`` or a string. We
12 can't compare the two because they're different types, so we convert
13 ``None`` to an empty string first.
14
15 """
16 return (attr[0][0] or ''), attr[0][1]
17
18
19 class Filter(base.Filter):
20 """Alphabetizes attributes for elements"""
21 def __iter__(self):
22 for token in base.Filter.__iter__(self):
23 if token["type"] in ("StartTag", "EmptyTag"):
24 attrs = OrderedDict()
25 for name, value in sorted(token["data"].items(),
26 key=_attr_key):
27 attrs[name] = value
28 token["data"] = attrs
29 yield token