Index: dist/src/Doc/lib/xmlsaxutils.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/xmlsaxutils.tex,v retrieving revision 1.3 diff -c -r1.3 xmlsaxutils.tex *** dist/src/Doc/lib/xmlsaxutils.tex 10 Aug 2001 22:14:17 -0000 1.3 --- dist/src/Doc/lib/xmlsaxutils.tex 15 Oct 2002 19:36:08 -0000 *************** *** 22,27 **** --- 22,36 ---- 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. + \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: dist/src/Lib/test/test_sax.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_sax.py,v retrieving revision 1.22 diff -c -r1.22 test_sax.py *** dist/src/Lib/test/test_sax.py 12 Sep 2002 17:02:01 -0000 1.22 --- dist/src/Lib/test/test_sax.py 15 Oct 2002 19:36:12 -0000 *************** *** 8,14 **** 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.expatreader import create_parser from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl from cStringIO import StringIO --- 8,15 ---- 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, unescape, quoteattr, \ ! XMLFilterBase from xml.sax.expatreader import create_parser from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl from cStringIO import StringIO *************** *** 69,74 **** --- 70,86 ---- 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 Index: dist/src/Lib/xml/sax/saxutils.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/xml/sax/saxutils.py,v retrieving revision 1.16 diff -c -r1.16 saxutils.py *** dist/src/Lib/xml/sax/saxutils.py 7 Aug 2001 19:14:46 -0000 1.16 --- dist/src/Lib/xml/sax/saxutils.py 15 Oct 2002 19:36:16 -0000 *************** *** 12,31 **** except AttributeError: _StringTypes = [types.StringType] 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. """ data = data.replace("&", "&") ! data = data.replace("<", "<") ! data = data.replace(">", ">") ! for chars, entity in entities.items(): ! data = data.replace(chars, entity) ! return data def quoteattr(data, entities={}): """Escape and quote an attribute value. --- 12,51 ---- 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 = __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.