From 74b51ac1e5fb76250251a66d8d326baaaf1f1cee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Sat, 26 Oct 2002 14:50:45 +0000 Subject: Patch #613256: Add nescape method to xml.sax.saxutils. --- Doc/lib/xmlsaxutils.tex | 11 +++++++++++ Lib/test/output/test_sax | 5 ++++- Lib/test/test_sax.py | 14 +++++++++++++- Lib/xml/sax/saxutils.py | 32 ++++++++++++++++++++++++++------ Misc/ACKS | 1 + Misc/NEWS | 3 +++ 6 files changed, 58 insertions(+), 8 deletions(-) diff --git a/Doc/lib/xmlsaxutils.tex b/Doc/lib/xmlsaxutils.tex index 6ab8d4a..91f25a4 100644 --- a/Doc/lib/xmlsaxutils.tex +++ b/Doc/lib/xmlsaxutils.tex @@ -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{\&}, \character{\<}, and \character{\>} + 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 diff --git a/Lib/test/output/test_sax b/Lib/test/output/test_sax index 8aa5a77..cfb56cb 100644 --- a/Lib/test/output/test_sax +++ b/Lib/test/output/test_sax @@ -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 diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index 1200329..3c5b11a 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -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", {"å" : "å"}) == "Hei på deg" +# ===== unescape + +def test_unescape_basic(): + return unescape("Donald Duck & Co") == "Donald Duck & Co" + +def test_unescape_all(): + return unescape("<Donald Duck & Co>") == "" + +def test_unescape_extra(): + return unescape("Hei på deg", {"å" : "å"}) == "Hei på deg" + # ===== quoteattr def test_quoteattr_basic(): 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("&", "&") - 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. diff --git a/Misc/ACKS b/Misc/ACKS index 6f17773..901bb45 100644 --- 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 diff --git a/Misc/NEWS b/Misc/NEWS index a0ead8a..1e83389 100644 --- 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 -- cgit v0.12