Method: Escape.html_attr
- Defined in:
- lib/escape.rb
.html_attr(str) ⇒ Object
Escape.html_attr encodes a string as a double-quoted HTML attribute using character references.
Escape.html_attr("abc") #=> "\"abc\""
Escape.html_attr("a&b") #=> "\"a&b\""
Escape.html_attr("ab&<>\"c") #=> "\"ab&<>"c\""
Escape.html_attr("a'c") #=> "\"a'c\""
It escapes 4 characters:
-
‘&’ to ‘&’
-
‘<’ to ‘<’
-
‘>’ to ‘>’
-
‘“’ to ‘"’
244 245 246 |
# File 'lib/escape.rb', line 244 def html_attr(str) '"' + str.gsub(/[&<>"]/) {|ch| HTML_ATTR_ESCAPE_HASH[ch] } + '"' end |