Method: Escape.html_text

Defined in:
lib/escape.rb

.html_text(str) ⇒ Object

Escape.html_text escapes a string appropriate for HTML text using character references.

It escapes 3 characters:

  • ‘&’ to ‘&’

  • ‘<’ to ‘&lt;’

  • ‘>’ to ‘&gt;’

Escape.html_text("abc") #=> "abc"
Escape.html_text("a & b < c > d") #=> "a &amp; b &lt; c &gt; d"

This function is not appropriate for escaping HTML element attribute because quotes are not escaped.



218
219
220
# File 'lib/escape.rb', line 218

def html_text(str)
  str.gsub(/[&<>]/) {|ch| HTML_TEXT_ESCAPE_HASH[ch] }
end