diff options
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/alphabeticalattributes.py')
-rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/alphabeticalattributes.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/alphabeticalattributes.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/alphabeticalattributes.py new file mode 100644 index 0000000..d9e234a --- /dev/null +++ b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/html5lib/filters/alphabeticalattributes.py | |||
@@ -0,0 +1,29 @@ | |||
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 | ||