summaryrefslogtreecommitdiffstats
path: root/Lib/xml
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2002-10-26 14:50:45 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2002-10-26 14:50:45 (GMT)
commit74b51ac1e5fb76250251a66d8d326baaaf1f1cee (patch)
tree2fd291bbcbfd19c22193e1102b204989ecebeab6 /Lib/xml
parentedb6bff67fdb72718ed656d39ac71317ada37a04 (diff)
downloadcpython-74b51ac1e5fb76250251a66d8d326baaaf1f1cee.zip
cpython-74b51ac1e5fb76250251a66d8d326baaaf1f1cee.tar.gz
cpython-74b51ac1e5fb76250251a66d8d326baaaf1f1cee.tar.bz2
Patch #613256: Add nescape method to xml.sax.saxutils.
Diffstat (limited to 'Lib/xml')
-rw-r--r--Lib/xml/sax/saxutils.py32
1 files changed, 26 insertions, 6 deletions
diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py
index 8a96be6..c369f98 100644
--- a/Lib/xml/sax/saxutils.py
+++ b/Lib/xml/sax/saxutils.py
@@ -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("&", "&amp;")
- data = data.replace("<", "&lt;")
- data = data.replace(">", "&gt;")
- for chars, entity in entities.items():
- data = data.replace(chars, entity)
- return data
+ data = __dict_replace(data, {"<" : "&lt;",
+ ">" : "&gt;",
+ })
+ return __dict_replace(data, entities)
+
+def unescape(data, entities={}):
+ """Unescape &amp;, &lt;, and &gt; 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, {"&lt;" : "<",
+ "&gt;" : ">",
+ })
+ # must do ampersand last
+ data = data.replace("&amp;", "&")
+ return __dict_replace(data, entities)
def quoteattr(data, entities={}):
"""Escape and quote an attribute value.