Permalink
Browse files

MarkupSafe now provides a escape_silent method as well.

  • Loading branch information...
1 parent 4fa38b6 commit 3051422b114d4292fef31c1e2660498c5b7d2353 @mitsuhiko mitsuhiko committed Aug 12, 2010
Showing with 46 additions and 5 deletions.
  1. +15 −0 README.rst
  2. +3 −3 markupsafe/__init__.py
  3. +9 −0 markupsafe/_native.py
  4. +12 −0 markupsafe/_speedups.c
  5. +6 −1 markupsafe/tests.py
  6. +1 −1 setup.py
View
@@ -31,3 +31,18 @@ the `__html__` function:
Markup(u'<strong>Nice</strong>')
>>> Markup(Foo())
Markup(u'<strong>Nice</strong>')
+
+Since MarkupSafe 0.10 there is now also a separate escape function
+called `escape_silent` that returns an empty string for `None` for
+consistency with other systems that return empty strings for `None`
+when escaping (for instance Pylons' webhelpers).
+
+If you also want to use this for the escape method of the Markup
+object, you can create your own subclass that does that::
+
+ from markupsafe import Markup, escape_silent as escape
+
+ class SilentMarkup(Markup):
+ @classmethod
+ def escape(cls, s):
+ return cls(escape(s))
View
@@ -12,7 +12,7 @@
from itertools import imap
-__all__ = ['Markup', 'soft_unicode', 'escape']
+__all__ = ['Markup', 'soft_unicode', 'escape', 'escape_silent']
_striptags_re = re.compile(r'(<!--.*?-->|<[^>]*>)')
@@ -220,6 +220,6 @@ def __init__(self, obj):
# we have to import it down here as the speedups and native
# modules imports the markup type which is define above.
try:
- from markupsafe._speedups import escape, soft_unicode
+ from markupsafe._speedups import escape, escape_silent, soft_unicode
except ImportError:
- from markupsafe._native import escape, soft_unicode
+ from markupsafe._native import escape, escape_silent, soft_unicode
View
@@ -27,6 +27,15 @@ def escape(s):
)
+def escape_silent(s):
+ """Like :func:`escape` but converts `None` into an empty
+ markup string.
+ """
+ if s is None:
+ return Markup()
+ return escape(s)
+
+
def soft_unicode(s):
"""Make a string unicode if it isn't already. That way a markup
string is not converted back to unicode.
View
@@ -160,6 +160,15 @@ escape(PyObject *self, PyObject *text)
static PyObject*
+escape_silent(PyObject *self, PyObject *text)
+{
+ if (text != Py_None)
+ return escape(self, text);
+ return PyObject_CallFunctionObjArgs(markup, NULL);
+}
+
+
+static PyObject*
soft_unicode(PyObject *self, PyObject *s)
{
if (!PyUnicode_Check(s))
@@ -179,6 +188,9 @@ static PyMethodDef module_methods[] = {
"Convert the characters &, <, >, ', and \" in string s to HTML-safe\n"
"sequences. Use this if you need to display text that might contain\n"
"such characters in HTML. Marks return value as markup string."},
+ {"escape_silent", (PyCFunction)escape_silent, METH_O,
+ "escape_silent(s) -> markup\n\n"
+ "Like escape but converts None to an empty string."},
{"soft_unicode", (PyCFunction)soft_unicode, METH_O,
"soft_unicode(object) -> string\n\n"
"Make a string unicode if it isn't already. That way a markup\n"
View
@@ -1,6 +1,6 @@
import gc
import unittest
-from markupsafe import Markup, escape
+from markupsafe import Markup, escape, escape_silent
class MarkupTestCase(unittest.TestCase):
@@ -45,6 +45,11 @@ def test_all_set(self):
for item in markup.__all__:
getattr(markup, item)
+ def test_escape_silent(self):
+ assert escape_silent(None) == Markup()
+ assert escape(None) == Markup(None)
+ assert escape_silent('<foo>') == Markup(u'&lt;foo&gt;')
+
class MarkupLeakTestCase(unittest.TestCase):
View
@@ -63,7 +63,7 @@ def run_setup(with_binary):
features['speedups'] = speedups
setup(
name='MarkupSafe',
- version='0.9.3',
+ version='0.10',
url='http://dev.pocoo.org/',
license='BSD',
author='Armin Ronacher',

0 comments on commit 3051422

Please sign in to comment.