MCPcopy Index your code
hub / github.com/python/cpython / quoteattr

Function quoteattr

Lib/xml/sax/saxutils.py:48–68  ·  view source on GitHub ↗

Escape and quote an attribute value. Escape &, <, and > in a string of data, then quote it for use as an attribute value. The \" character will be escaped as well, if necessary. You can escape other strings of data by passing a dictionary as the optional entities parameter. T

(data, entities={})

Source from the content-addressed store, hash-verified

46 return data.replace("&amp;", "&")
47
48def quoteattr(data, entities={}):
49 """Escape and quote an attribute value.
50
51 Escape &, <, and > in a string of data, then quote it for use as
52 an attribute value. The \" character will be escaped as well, if
53 necessary.
54
55 You can escape other strings of data by passing a dictionary as
56 the optional entities parameter. The keys and values must all be
57 strings; each key will be replaced with its corresponding value.
58 """
59 entities = {**entities, '\n': '&#10;', '\r': '&#13;', '\t':'&#9;'}
60 data = escape(data, entities)
61 if '"' in data:
62 if "'" in data:
63 data = '"%s"' % data.replace('"', "&quot;")
64 else:
65 data = "'%s'" % data
66 else:
67 data = '"%s"' % data
68 return data
69
70
71def _gettextwriter(out, encoding):

Callers 6

test_quoteattr_basicMethod · 0.90
test_single_quoteattrMethod · 0.90
test_double_quoteattrMethod · 0.90
startElementMethod · 0.85
startElementNSMethod · 0.85

Calls 2

escapeFunction · 0.70
replaceMethod · 0.45

Tested by 4

test_quoteattr_basicMethod · 0.72
test_single_quoteattrMethod · 0.72
test_double_quoteattrMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…