]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #613256: Add nescape method to xml.sax.saxutils.
authorMartin v. Löwis <martin@v.loewis.de>
Sat, 26 Oct 2002 14:50:45 +0000 (14:50 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sat, 26 Oct 2002 14:50:45 +0000 (14:50 +0000)
Doc/lib/xmlsaxutils.tex
Lib/test/output/test_sax
Lib/test/test_sax.py
Lib/xml/sax/saxutils.py
Misc/ACKS
Misc/NEWS

index 6ab8d4a705d70dc4b3eac14a37f6459891cdbd86..91f25a47db508b5aec6a755aac712533bc549cbb 100644 (file)
@@ -22,6 +22,17 @@ either in direct use, or as base classes.
   strings; each key will be replaced with its corresponding value.
 \end{funcdesc}
 
+\begin{funcdesc}{unescape}{data\optional{, entities}}
+  Unescape \character{\&amp;}, \character{\&lt;}, and \character{\&gt;}
+  in a string of data.
+
+  You can unescape other strings of data by passing a dictionary as the
+  optional \var{entities} parameter.  The keys and values must all be
+  strings; each key will be replaced with its corresponding value.
+
+  \versionadded{2.3}
+\end{funcdesc}
+
 \begin{funcdesc}{quoteattr}{data\optional{, entities}}
   Similar to \function{escape()}, but also prepares \var{data} to be
   used as an attribute value.  The return value is a quoted version of
index 8aa5a77c2f97a98c08d595ff0bce565a63f613fe..cfb56cb238f830647fe3a79cd4cf4139edda47b1 100644 (file)
@@ -29,6 +29,9 @@ Passed test_nsattrs_wattr
 Passed test_quoteattr_basic
 Passed test_single_double_quoteattr
 Passed test_single_quoteattr
+Passed test_unescape_all
+Passed test_unescape_basic
+Passed test_unescape_extra
 Passed test_xmlgen_attr_escape
 Passed test_xmlgen_basic
 Passed test_xmlgen_content
@@ -36,4 +39,4 @@ Passed test_xmlgen_content_escape
 Passed test_xmlgen_ignorable
 Passed test_xmlgen_ns
 Passed test_xmlgen_pi
-37 tests, 0 failures
+40 tests, 0 failures
index 1200329934e86f569ec2bc86ba53f189e3be8a57..3c5b11ab26567757a005bd612aab81f184725119 100644 (file)
@@ -8,7 +8,8 @@ try:
 except SAXReaderNotAvailable:
     # don't try to test this module if we cannot create a parser
     raise ImportError("no XML parsers available")
-from xml.sax.saxutils import XMLGenerator, escape, quoteattr, XMLFilterBase
+from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
+                             XMLFilterBase
 from xml.sax.expatreader import create_parser
 from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
 from cStringIO import StringIO
@@ -70,6 +71,17 @@ def test_escape_all():
 def test_escape_extra():
     return escape("Hei på deg", {"å" : "&aring;"}) == "Hei p&aring; deg"
 
+# ===== unescape
+
+def test_unescape_basic():
+    return unescape("Donald Duck &amp; Co") == "Donald Duck & Co"
+
+def test_unescape_all():
+    return unescape("&lt;Donald Duck &amp; Co&gt;") == "<Donald Duck & Co>"
+
+def test_unescape_extra():
+    return unescape("Hei på deg", {"å" : "&aring;"}) == "Hei p&aring; deg"
+
 # ===== quoteattr
 
 def test_quoteattr_basic():
index 8a96be60e8f106396ed1b717cc48bf51bcae4f43..c369f98fcfe77783a7c0a7020d46e7274fef119f 100644 (file)
@@ -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.
index 6f177734229aed7f0325b790182cb4a109b50f32..901bb45970bd947595ebbced5b81cf08425acff1 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -56,6 +56,7 @@ Pablo Bleyer
 Erik van Blokland
 Finn Bock
 Paul Boddie
+Matthew Boedicker
 David Bolen
 Jurjen Bos
 Peter Bosch
index a0ead8a32a373467406d51d7fa7395866aad35a2..1e833899a1480d67d4273e22b9f65654f5f4ebd1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -352,6 +352,9 @@ Extension modules
 Library
 -------
 
+- xml.sax.saxutils.unescape has been added, to replace entity references
+  with their entity value.
+
 - Queue.Queue.{put,get} now support an optional timeout argument.
 
 - Various features of Tk 8.4 are exposed in Tkinter.py. The multiple