22
33class CGI
44
5- # Extends `#escape_html` to support escape modes. By default all strings
6- # are escaped on `&`, `>` and `<`. Add the `:nonstandard` mode to omit
7- # this conversion.
5+ # Extended HTML/XHTML escaping with mode support. Unlike Ruby's built-in
6+ # `CGI.escape_html`, this supports additional escape modes.
87 #
9- # If no mode is given then the `:default` mode is used.
10- #
11- # Available modes include:
8+ # Available modes:
129 # * `:quote` - escapes single and double quotes
1310 # * `:newlines` - escapes newline characters (\r and \n)
1411 # * `:ampersand` - escapes the ampersand sign
1512 # * `:brackets` - escapes less-than and greater-than signs
1613 # * `:default` - escapes double quotes
1714 #
15+ # By default all strings are escaped on `&`, `>`, `<` and `"`.
16+ #
1817 # @example
19- # escape_html ("<tag>") #=> "<tag>"
20- # escape_html ("Example\nString", :newlines) #=> "Example String"
21- # escape_html ("\"QUOTE\"", false) #=> "\"QUOTE\""
18+ # CGI.escape_xhtml ("<tag>") #=> "<tag>"
19+ # CGI.escape_xhtml ("Example\nString", :newlines) #=> "Example String"
20+ # CGI.escape_xhtml ("\"QUOTE\"", false) #=> "\"QUOTE\""
2221 #
23- def self . escape_html ( string , *modes )
24- modes << :defualt if modes . empty?
22+ def self . escape_xhtml ( string , *modes )
23+ modes << :default if modes . empty?
2524
2625 unless modes . include? ( :nonstandard )
2726 string = string . gsub ( /&/ , '&' ) . gsub ( />/ , '>' ) . gsub ( /</ , '<' )
@@ -32,7 +31,7 @@ def self.escape_html(string, *modes)
3231 case mode
3332 when :quote , :quotes
3433 string . gsub ( %r|"| , '"' ) . gsub ( %r|'| , ''' )
35- when :newlines , :newlines
34+ when :newlines
3635 string . gsub ( /[\r \n ]+/ , ' ' )
3736 when :ampersand
3837 string . gsub ( /&/ , '&' )
@@ -41,23 +40,13 @@ def self.escape_html(string, *modes)
4140 when :default , true
4241 string . gsub ( /\" / , '"' )
4342 when false
43+ string
4444 else
45- raise ArgumentError , "unrecognized HTML escape mode -- #{ node } "
45+ raise ArgumentError , "unrecognized HTML escape mode -- #{ mode } "
4646 end
4747 end
48- end
4948
50- class << self
51- # @deprecated
52- alias :escapeHTML :escape_html
53- end
54-
55- if RUBY_VERSION < '1.9'
56- class << self
57- alias :unescape_html :unescapeHTML
58- alias :escape_element :escapeElement
59- alias :unescape_element :unescapeElement
60- end
49+ string
6150 end
6251
6352end
0 commit comments