lib/roda/plugins/h.rb in roda-2.23.0 vs lib/roda/plugins/h.rb in roda-2.24.0

- old
+ new

@@ -12,26 +12,40 @@ # # route do |r| # h('<foo>') # end module H - # A Hash of entities and their escaped equivalents, - # to be escaped by h(). - ESCAPE_HTML = { - "&" => "&amp;".freeze, - "<" => "&lt;".freeze, - ">" => "&gt;".freeze, - "'" => "&#x27;".freeze, - '"' => "&quot;".freeze, - }.freeze + begin + require 'cgi/escape' + unless CGI.respond_to?(:escapeHTML) # work around for JRuby 9.1 + CGI = Object.new + CGI.extend(::CGI::Util) + end - # A Regexp of HTML entities to match for escaping. - ESCAPE_HTML_PATTERN = Regexp.union(*ESCAPE_HTML.keys) + module InstanceMethods + # HTML escape the input and return the escaped version. + def h(string) + CGI.escapeHTML(string.to_s) + end + end + rescue LoadError + # A Hash of entities and their escaped equivalents, + # to be escaped by h(). + ESCAPE_HTML = { + "&" => "&amp;".freeze, + "<" => "&lt;".freeze, + ">" => "&gt;".freeze, + "'" => "&#39;".freeze, + '"' => "&quot;".freeze, + }.freeze - module InstanceMethods - # HTML escape the input and return the escaped version. - def h(string) - string.to_s.gsub(ESCAPE_HTML_PATTERN){|c| ESCAPE_HTML[c] } + # A Regexp of HTML entities to match for escaping. + ESCAPE_HTML_PATTERN = Regexp.union(*ESCAPE_HTML.keys) + + module InstanceMethods + def h(string) + string.to_s.gsub(ESCAPE_HTML_PATTERN){|c| ESCAPE_HTML[c] } + end end end end register_plugin(:h, H)