[Python-checkins] python/dist/src/Lib/xml/sax saxutils.py,1.16,1.17

loewis@users.sourceforge.net loewis@users.sourceforge.net
Sat, 26 Oct 2002 07:50:47 -0700


Update of /cvsroot/python/python/dist/src/Lib/xml/sax
In directory usw-pr-cvs1:/tmp/cvs-serv29237/Lib/xml/sax

Modified Files:
	saxutils.py 
Log Message:
Patch #613256: Add nescape method to xml.sax.saxutils.


Index: saxutils.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xml/sax/saxutils.py,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** saxutils.py	7 Aug 2001 19:14:46 -0000	1.16
--- saxutils.py	26 Oct 2002 14:50:45 -0000	1.17
***************
*** 13,30 ****
      _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("&", "&amp;")
!     data = data.replace("<", "&lt;")
!     data = data.replace(">", "&gt;")
!     for chars, entity in entities.items():
!         data = data.replace(chars, entity)
!     return data
  
  def quoteattr(data, entities={}):
--- 13,50 ----
      _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 = __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={}):