Module: Sinatra::Helpers::Escape::HTML

Extended by:
HTML
Included in:
HTML
Defined in:
lib/sinatra-helpers/escape/html.rb

Overview

HTML escaping helpers.

Usage:

Include the module in your Sinatra application:

helpers ::Sinatra::Helpers::Escape::HTML

Constant Summary collapse

ESCAPE_HTML =

Map special characters to HTML entities.

{
	'&'  .freeze => '&',
	'<'  .freeze => '&lt;',
	'>'  .freeze => '&gt;',
#	'\'' .freeze => '&#x27;',
	'\'' .freeze => '&#39;',
	'"'  .freeze => '&quot;',
}
ESCAPE_HTML_PATTERN =

Regexp search pattern for special characters.

::Regexp.union( *ESCAPE_HTML.keys )

Instance Method Summary collapse

Instance Method Details

#h(str) ⇒ String

Escapes the string so it’s safe to use in HTML.

Parameters:

  • str (String)

    The string to escape.

Returns:

  • (String)

    Escaped version of the string.



40
41
42
43
# File 'lib/sinatra-helpers/escape/html.rb', line 40

def h( str )
	#::Rack::Utils.escape_html( str )
	str.to_s.gsub( ESCAPE_HTML_PATTERN ){ |c| ESCAPE_HTML[c] }
end