mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Patch #613256: Add nescape method to xml.sax.saxutils.
This commit is contained in:
parent
edb6bff67f
commit
74b51ac1e5
6 changed files with 58 additions and 8 deletions
|
@ -12,20 +12,40 @@ try:
|
|||
except AttributeError:
|
||||
_StringTypes = [types.StringType]
|
||||
|
||||
def __dict_replace(s, d):
|
||||
"""Replace substrings of a string using a dictionary."""
|
||||
for key, value in d.items():
|
||||
s = s.replace(key, value)
|
||||
return s
|
||||
|
||||
def escape(data, entities={}):
|
||||
"""Escape &, <, and > in a string of data.
|
||||
|
||||
|
||||
You can escape other strings of data by passing a dictionary as
|
||||
the optional entities parameter. The keys and values must all be
|
||||
strings; each key will be replaced with its corresponding value.
|
||||
"""
|
||||
|
||||
# must do ampersand first
|
||||
data = data.replace("&", "&")
|
||||
data = data.replace("<", "<")
|
||||
data = data.replace(">", ">")
|
||||
for chars, entity in entities.items():
|
||||
data = data.replace(chars, entity)
|
||||
return data
|
||||
data = __dict_replace(data, {"<" : "<",
|
||||
">" : ">",
|
||||
})
|
||||
return __dict_replace(data, entities)
|
||||
|
||||
def unescape(data, entities={}):
|
||||
"""Unescape &, <, and > in a string of data.
|
||||
|
||||
You can unescape other strings of data by passing a dictionary as
|
||||
the optional entities parameter. The keys and values must all be
|
||||
strings; each key will be replaced with its corresponding value.
|
||||
"""
|
||||
data = __dict_replace(data, {"<" : "<",
|
||||
">" : ">",
|
||||
})
|
||||
# must do ampersand last
|
||||
data = data.replace("&", "&")
|
||||
return __dict_replace(data, entities)
|
||||
|
||||
def quoteattr(data, entities={}):
|
||||
"""Escape and quote an attribute value.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue