Skip to content

Commit

Permalink
Adding attr_escape to deal with quotes when embedding values in HTML …
Browse files Browse the repository at this point in the history
…attributes safely.
  • Loading branch information
eliast authored and jehiah committed Apr 7, 2010
1 parent 002ba2c commit 70347b6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tornado/escape.py
Expand Up @@ -46,11 +46,13 @@ def xhtml_escape(value):
"""Escapes a string so it is valid within XML or XHTML."""
return utf8(xml.sax.saxutils.escape(value, {'"': """}))


def xhtml_unescape(value):
"""Un-escapes an XML-escaped string."""
return re.sub(r"&(#?)(\w+?);", _convert_entity, _unicode(value))

def attr_escape(data):
"""Similar to xhtml_escape(), but also prepares data to be used as an attribute value."""
return utf8(xml.sax.saxutils.quoteattr(data))

def json_encode(value):
"""JSON-encodes the given Python object."""
Expand Down
1 change: 1 addition & 0 deletions tornado/template.py
Expand Up @@ -116,6 +116,7 @@ def generate(self, **kwargs):
"""Generate this template with the given arguments."""
namespace = {
"escape": escape.xhtml_escape,
"attr_escape": escape.attr_escape,
"url_escape": escape.url_escape,
"json_encode": escape.json_encode,
"squeeze": escape.squeeze,
Expand Down

0 comments on commit 70347b6

Please sign in to comment.