Commits

Ben Bangert committed 7036f28 Merge

Comments (0)

Files changed (2)

webhelpers/html/builder.py

 
 """
 import re
-from cgi import escape as cgi_escape
 from urllib import quote as url_escape
 from UserDict import DictMixin
 try:
 except NameError:
     from sets import Set as set
 
+from webhelpers.util import cgi_escape
+
 __all__ = ["HTML", "escape", "literal", "url_escape", "lit_sub"]
 
 class UnfinishedTag(object):

webhelpers/util.py

 from UserDict import DictMixin
 from xml.sax.saxutils import XMLGenerator
 
+def cgi_escape(s, quote=False):
+    """Replace special characters '&', '<' and '>' by SGML entities.
+
+    This is a slightly more efficient version of the cgi.escape by
+    using 'in' membership to test if the replace is needed.
+
+    """
+    if '&' in s:
+        s = s.replace("&", "&amp;") # Must be done first!
+    if '<' in s:
+        s = s.replace("<", "&lt;")
+    if '>' in s:
+        s = s.replace(">", "&gt;")
+    if quote:
+        s = s.replace('"', "&quot;")
+    return s
+
 def html_escape(s):
     """HTML-escape a string or object.
     
             s = unicode(s)
         else:
             s = str(s)
-    s = cgi.escape(s, True)
+    s = cgi_escape(s, True)
     if isinstance(s, unicode):
         s = s.encode('ascii', 'xmlcharrefreplace')
     return s
         return self.fn(*(self.args + args), **d)
 
 class SimplerXMLGenerator(XMLGenerator):
-    def addQuickElement(self, name, contents=None, attrs={}):
+    def addQuickElement(self, name, contents=None, attrs=None):
         """Add an element with no children."""
+        if attrs is None:
+            attrs = {}
         self.startElement(name, attrs)
         if contents is not None:
             self.characters(contents)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.