Skip to content

Commit 605afba

Browse files
authored
Merge pull request rails#55467 from byroot/erb-escape
`ERB::Util.html_escape`: stop trying to tidy bytes
2 parents 67e7f9e + b76da42 commit 605afba

File tree

12 files changed

+18
-38
lines changed

12 files changed

+18
-38
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/erb/util.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def html_escape(s) # :nodoc:
1212
if s.html_safe?
1313
s
1414
else
15-
super(ActiveSupport::Multibyte::Unicode.tidy_bytes(s))
15+
super(s)
1616
end
1717
end
1818
alias :unwrapped_html_escape :html_escape # :nodoc:
@@ -61,7 +61,7 @@ module Util
6161
# html_escape_once('&lt;&lt; Accept & Checkout')
6262
# # => "&lt;&lt; Accept &amp; Checkout"
6363
def html_escape_once(s)
64-
ActiveSupport::Multibyte::Unicode.tidy_bytes(s.to_s).gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE).html_safe
64+
s.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE).html_safe
6565
end
6666

6767
module_function :html_escape_once

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

activesupport/test/core_ext/string_ext_test.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,12 +1102,6 @@ def to_s
11021102
assert_equal expected, ERB::Util.html_escape(string)
11031103
end
11041104

1105-
test "ERB::Util.html_escape should correctly handle invalid UTF-8 strings" do
1106-
string = "\251 <"
1107-
expected = "© &lt;"
1108-
assert_equal expected, ERB::Util.html_escape(string)
1109-
end
1110-
11111105
test "ERB::Util.html_escape should not escape safe strings" do
11121106
string = "<b>hello</b>".html_safe
11131107
assert_equal string, ERB::Util.html_escape(string)
@@ -1121,12 +1115,6 @@ def to_s
11211115
assert_equal escaped_string, ERB::Util.html_escape_once(escaped_string)
11221116
end
11231117

1124-
test "ERB::Util.html_escape_once should correctly handle invalid UTF-8 strings" do
1125-
string = "\251 <"
1126-
expected = "© &lt;"
1127-
assert_equal expected, ERB::Util.html_escape_once(string)
1128-
end
1129-
11301118
test "ERB::Util.xml_name_escape should escape unsafe characters for XML names" do
11311119
unsafe_char = ">"
11321120
safe_char = "Á"

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

0 commit comments

Comments
 (0)