Skip to content

Commit c07ef01

Browse files
transclaude
andcommitted
Rename CGI.escape_html to CGI.escape_xhtml
Avoids overriding Ruby's built-in CGI.escape_html. Also fixes two bugs in the original: :defualt typo and 'node' instead of 'mode' in error. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0db401b commit c07ef01

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

lib/standard/facets/cgi/escape_html.rb

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22

33
class 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>") #=> "&lt;tag&gt;"
20-
# escape_html("Example\nString", :newlines) #=> "Example&#13;&#10;String"
21-
# escape_html("\"QUOTE\"", false) #=> "\"QUOTE\""
18+
# CGI.escape_xhtml("<tag>") #=> "&lt;tag&gt;"
19+
# CGI.escape_xhtml("Example\nString", :newlines) #=> "Example&#13;&#10;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(/&/, '&amp;').gsub(/>/, '&gt;').gsub(/</, '&lt;')
@@ -32,7 +31,7 @@ def self.escape_html(string, *modes)
3231
case mode
3332
when :quote, :quotes
3433
string.gsub(%r|"|,'&quot;').gsub(%r|'|,'&#39;')
35-
when :newlines, :newlines
34+
when :newlines
3635
string.gsub(/[\r\n]+/,'&#13;&#10;')
3736
when :ampersand
3837
string.gsub(/&/, '&amp;')
@@ -41,23 +40,13 @@ def self.escape_html(string, *modes)
4140
when :default, true
4241
string.gsub(/\"/, '&quot;')
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

6352
end

0 commit comments

Comments
 (0)