MCPcopy
hub / github.com/django/django / flatatt

Function flatatt

django/forms/utils.py:20–42  ·  view source on GitHub ↗

Convert a dictionary of attributes to a single string. The returned string will contain a leading space followed by key="value", XML-style pairs. In the case of a boolean value, the key will appear without a value. It is assumed that the keys do not need to be XML-escaped. If th

(attrs)

Source from the content-addressed store, hash-verified

18
19
20def flatatt(attrs):
21 """
22 Convert a dictionary of attributes to a single string.
23 The returned string will contain a leading space followed by key="value",
24 XML-style pairs. In the case of a boolean value, the key will appear
25 without a value. It is assumed that the keys do not need to be
26 XML-escaped. If the passed dictionary is empty, then return an empty
27 string.
28
29 The result is passed through 'mark_safe' (by way of 'format_html_join').
30 """
31 key_value_attrs = []
32 boolean_attrs = []
33 for attr, value in attrs.items():
34 if isinstance(value, bool):
35 if value:
36 boolean_attrs.append((attr,))
37 elif value is not None:
38 key_value_attrs.append((attr, value))
39
40 return format_html_join("", ' {}="{}"', sorted(key_value_attrs)) + format_html_join(
41 "", " {}", sorted(boolean_attrs)
42 )
43
44
45class RenderableMixin:

Callers 5

label_tagMethod · 0.90
__str__Method · 0.90
renderMethod · 0.90
test_flatattMethod · 0.90

Calls 3

format_html_joinFunction · 0.90
itemsMethod · 0.45
appendMethod · 0.45

Tested by 2

test_flatattMethod · 0.72