Skip to content

Commit b76da42

Browse files
committed
Consistently use ERB::Util.html_escape over CGI.escapeHTML
The ERB method save on duping the string if there's nothing to escape, which is more often than not the case.
1 parent a31ac87 commit b76da42

File tree

10 files changed

+16
-24
lines changed

10 files changed

+16
-24
lines changed

actionpack/test/dispatch/debug_exceptions_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def self.build_app(app, *args)
402402
"action_dispatch.parameter_filter" => [:foo] }
403403
assert_response 500
404404

405-
assert_match(CGI.escape_html({ "foo" => "[FILTERED]" }.inspect[1..-2]), body)
405+
assert_match(ERB::Util.html_escape({ "foo" => "[FILTERED]" }.inspect[1..-2]), body)
406406
end
407407

408408
test "show registered original exception if the last exception is TemplateError" do
@@ -466,7 +466,7 @@ def self.build_app(app, *args)
466466
})
467467
assert_response 500
468468

469-
assert_includes(body, CGI.escapeHTML(PP.pp(params, +"", 200)))
469+
assert_includes(body, ERB::Util.html_escape(PP.pp(params, +"", 200)))
470470
end
471471

472472
test "sets the HTTP charset parameter" do

actionview/lib/action_view/buffers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def <<(value)
4545
@raw_buffer << if value.html_safe?
4646
value
4747
else
48-
CGI.escapeHTML(value)
48+
ERB::Util.unwrapped_html_escape(value)
4949
end
5050
end
5151
self

actionview/lib/action_view/helpers/form_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "cgi/escape"
4-
require "cgi/util" if RUBY_VERSION < "3.5"
53
require "action_view/helpers/date_helper"
64
require "action_view/helpers/url_helper"
75
require "action_view/helpers/form_tag_helper"

actionview/lib/action_view/helpers/form_options_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "cgi/escape"
4-
require "cgi/util" if RUBY_VERSION < "3.5"
53
require "erb"
64
require "active_support/core_ext/string/output_safety"
75
require "active_support/core_ext/array/extract_options"

actionview/lib/action_view/helpers/form_tag_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "cgi/escape"
4-
require "cgi/util" if RUBY_VERSION < "3.5"
53
require "action_view/helpers/content_exfiltration_prevention_helper"
64
require "action_view/helpers/url_helper"
75
require "action_view/helpers/text_helper"

actionview/test/template/form_tag_helper_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def test_text_field_tag_size_symbol
581581

582582
def test_text_field_tag_with_ac_parameters
583583
actual = text_field_tag "title", ActionController::Parameters.new(key: "value")
584-
value = CGI.escapeHTML({ "key" => "value" }.inspect)
584+
value = ERB::Util.html_escape({ "key" => "value" }.inspect)
585585
expected = %(<input id="title" name="title" type="text" value="#{value}" />)
586586
assert_dom_equal expected, actual
587587
end

activesupport/lib/active_support/core_ext/string/output_safety.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ def #{unsafe_method}!(*args, &block) # def gsub!(*args, &block)
196196

197197
private
198198
def explicit_html_escape_interpolated_argument(arg)
199-
(!html_safe? || arg.html_safe?) ? arg : CGI.escapeHTML(arg.to_s)
199+
(!html_safe? || arg.html_safe?) ? arg : ERB::Util.unwrapped_html_escape(arg)
200200
end
201201

202202
def implicit_html_escape_interpolated_argument(arg)
203203
if !html_safe? || arg.html_safe?
204204
arg
205205
else
206-
CGI.escapeHTML(arg.to_str)
206+
ERB::Util.unwrapped_html_escape(arg.to_str)
207207
end
208208
end
209209

railties/lib/rails/info.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "cgi/escape"
4-
require "cgi/util" if RUBY_VERSION < "3.5"
3+
require "active_support/core_ext/erb/util"
54

65
module Rails
76
# This module helps build the runtime properties that are displayed in
@@ -44,11 +43,11 @@ def to_s
4443
def to_html
4544
(+"<table>").tap do |table|
4645
properties.each do |(name, value)|
47-
table << %(<tr><td class="name">#{CGI.escapeHTML(name.to_s)}</td>)
46+
table << %(<tr><td class="name">#{ERB::Util.html_escape(name.to_s)}</td>)
4847
formatted_value = if value.kind_of?(Array)
49-
"<ul>" + value.map { |v| "<li>#{CGI.escapeHTML(v.to_s)}</li>" }.join + "</ul>"
48+
"<ul>" + value.map { |v| "<li>#{ERB::Util.html_escape(v.to_s)}</li>" }.join + "</ul>"
5049
else
51-
CGI.escapeHTML(value.to_s)
50+
ERB::Util.html_escape(value.to_s)
5251
end
5352
table << %(<td class="value">#{formatted_value}</td></tr>)
5453
end

railties/test/rails_info_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_html_includes_middleware
3939
html = Rails::Info.to_html
4040
assert_includes html, '<tr><td class="name">Middleware</td>'
4141
properties.value_for("Middleware").each do |value|
42-
assert_includes html, "<li>#{CGI.escapeHTML(value)}</li>"
42+
assert_includes html, "<li>#{ERB::Util.html_escape(value)}</li>"
4343
end
4444
end
4545

tools/preview_docs.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "erb"
4-
require "cgi/escape"
5-
require "cgi/util" if RUBY_VERSION < "3.5"
3+
require "active_support/core_ext/string/output_safety"
64

75
# How to test:
86
#
@@ -41,9 +39,10 @@ def link_to(name, url)
4139
"<a href=\"#{escape(url)}\">#{escape(name)}</a>"
4240
end
4341

44-
def escape(str)
45-
CGI.escapeHTML(str)
46-
end
42+
private
43+
def escape(str)
44+
ERB::Util.html_escape(str)
45+
end
4746
end
4847

4948
module EnvVars

0 commit comments

Comments
 (0)