Skip to content

Commit

Permalink
Improve performance and flexibility of Rack::Utils.escape_html
Browse files Browse the repository at this point in the history
  • Loading branch information
manveru committed Jun 13, 2010
1 parent 4bfb6fc commit b4d0dc7
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/rack/utils.rb
Expand Up @@ -128,13 +128,18 @@ def build_nested_query(value, prefix = nil)
end
module_function :build_nested_query

ESCAPE_HTML = {
"&" => "&",
"<" => "&lt;",
">" => "&gt;",
"'" => "&#39;",
'"' => "&quot;",
}
ESCAPE_HTML_PATTERN = Regexp.union(ESCAPE_HTML.keys)

# Escape ampersands, brackets and quotes to their HTML/XML entities.
def escape_html(string)
string.to_s.gsub("&", "&amp;").
gsub("<", "&lt;").
gsub(">", "&gt;").
gsub("'", "&#39;").
gsub('"', "&quot;")
string.to_s.gsub(ESCAPE_HTML_PATTERN){|c| ESCAPE_HTML[c] }
end
module_function :escape_html

Expand Down

3 comments on commit b4d0dc7

@sferik
Copy link
Contributor

@sferik sferik commented on b4d0dc7 Apr 26, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks in Ruby 1.8.6 with an error on line 138: in `union': can't convert Array into String (TypeError)

@manveru
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was fixed with bf4593a.

@sferik
Copy link
Contributor

@sferik sferik commented on b4d0dc7 Apr 27, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently it was not :( http://travis-ci.org/pengwynn/octokit/builds/6156 (note: it's using rack 1.2.2, which includes bf4593a).

Please sign in to comment.