提交 3d453b40 编写于 作者: P Pat Allan

Make ActionView frozen string literal friendly.

Plus a couple of related ActionPack patches.
上级 dd491b24
...@@ -24,7 +24,7 @@ def process_action(event) ...@@ -24,7 +24,7 @@ def process_action(event)
exception_class_name = payload[:exception].first exception_class_name = payload[:exception].first
status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name) status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
end end
message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round}ms" message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round}ms".dup
message << " (#{additions.join(" | ".freeze)})" unless additions.empty? message << " (#{additions.join(" | ".freeze)})" unless additions.empty?
message << "\n\n" if defined?(Rails.env) && Rails.env.development? message << "\n\n" if defined?(Rails.env) && Rails.env.development?
......
...@@ -18,7 +18,7 @@ def self.normalize_path(path) ...@@ -18,7 +18,7 @@ def self.normalize_path(path)
path.squeeze!("/".freeze) path.squeeze!("/".freeze)
path.sub!(%r{/+\Z}, "".freeze) path.sub!(%r{/+\Z}, "".freeze)
path.gsub!(/(%[a-f0-9]{2})/) { $1.upcase } path.gsub!(/(%[a-f0-9]{2})/) { $1.upcase }
path = "/" if path == "".freeze path = "/".dup if path == "".freeze
path.force_encoding(encoding) path.force_encoding(encoding)
path path
end end
......
...@@ -79,7 +79,7 @@ def normalize_argument_to_redirection(fragment) ...@@ -79,7 +79,7 @@ def normalize_argument_to_redirection(fragment)
def generate_response_message(expected, actual = @response.response_code) def generate_response_message(expected, actual = @response.response_code)
"Expected response to be a <#{code_with_name(expected)}>,"\ "Expected response to be a <#{code_with_name(expected)}>,"\
" but was a <#{code_with_name(actual)}>" " but was a <#{code_with_name(actual)}>"
.concat(location_if_redirected).concat(response_body_if_short) .dup.concat(location_if_redirected).concat(response_body_if_short)
end end
def response_body_if_short def response_body_if_short
......
...@@ -1007,7 +1007,7 @@ def build_select(type, select_options_as_html) ...@@ -1007,7 +1007,7 @@ def build_select(type, select_options_as_html)
select_options[:disabled] = "disabled" if @options[:disabled] select_options[:disabled] = "disabled" if @options[:disabled]
select_options[:class] = css_class_attribute(type, select_options[:class], @options[:with_css_classes]) if @options[:with_css_classes] select_options[:class] = css_class_attribute(type, select_options[:class], @options[:with_css_classes]) if @options[:with_css_classes]
select_html = "\n" select_html = "\n".dup
select_html << content_tag("option".freeze, "", value: "") + "\n" if @options[:include_blank] select_html << content_tag("option".freeze, "", value: "") + "\n" if @options[:include_blank]
select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt] select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt]
select_html << select_options_as_html select_html << select_options_as_html
...@@ -1089,7 +1089,7 @@ def input_id_from_type(type) ...@@ -1089,7 +1089,7 @@ def input_id_from_type(type)
# Given an ordering of datetime components, create the selection HTML # Given an ordering of datetime components, create the selection HTML
# and join them with their appropriate separators. # and join them with their appropriate separators.
def build_selects_from_types(order) def build_selects_from_types(order)
select = "" select = "".dup
first_visible = order.find { |type| !@options[:"discard_#{type}"] } first_visible = order.find { |type| !@options[:"discard_#{type}"] }
order.reverse_each do |type| order.reverse_each do |type|
separator = separator(type) unless type == first_visible # don't add before first visible field separator = separator(type) unless type == first_visible # don't add before first visible field
......
...@@ -95,7 +95,7 @@ def translate(key, options = {}) ...@@ -95,7 +95,7 @@ def translate(key, options = {})
raise e if raise_error raise e if raise_error
keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope]) keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope])
title = "translation missing: #{keys.join('.')}" title = "translation missing: #{keys.join('.')}".dup
interpolations = options.except(:default, :scope) interpolations = options.except(:default, :scope)
if interpolations.any? if interpolations.any?
......
...@@ -14,7 +14,7 @@ def initialize ...@@ -14,7 +14,7 @@ def initialize
def render_template(event) def render_template(event)
info do info do
message = " Rendered #{from_rails_root(event.payload[:identifier])}" message = " Rendered #{from_rails_root(event.payload[:identifier])}".dup
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
message << " (#{event.duration.round(1)}ms)" message << " (#{event.duration.round(1)}ms)"
end end
...@@ -22,7 +22,7 @@ def render_template(event) ...@@ -22,7 +22,7 @@ def render_template(event)
def render_partial(event) def render_partial(event)
info do info do
message = " Rendered #{from_rails_root(event.payload[:identifier])}" message = " Rendered #{from_rails_root(event.payload[:identifier])}".dup
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
message << " (#{event.duration.round(1)}ms)" message << " (#{event.duration.round(1)}ms)"
message << " #{cache_message(event.payload)}" unless event.payload[:cache_hit].nil? message << " #{cache_message(event.payload)}" unless event.payload[:cache_hit].nil?
......
...@@ -329,7 +329,7 @@ def locals_code ...@@ -329,7 +329,7 @@ def locals_code
locals = locals.grep(/\A@?(?![A-Z0-9])(?:[[:alnum:]_]|[^\0-\177])+\z/) locals = locals.grep(/\A@?(?![A-Z0-9])(?:[[:alnum:]_]|[^\0-\177])+\z/)
# Double assign to suppress the dreaded 'assigned but unused variable' warning # Double assign to suppress the dreaded 'assigned but unused variable' warning
locals.each_with_object("") { |key, code| code << "#{key} = #{key} = local_assigns[:#{key}];" } locals.each_with_object("".dup) { |key, code| code << "#{key} = #{key} = local_assigns[:#{key}];" }
end end
def method_name def method_name
......
...@@ -104,7 +104,7 @@ def setup_with_controller ...@@ -104,7 +104,7 @@ def setup_with_controller
# empty string ensures buffer has UTF-8 encoding as # empty string ensures buffer has UTF-8 encoding as
# new without arguments returns ASCII-8BIT encoded buffer like String#new # new without arguments returns ASCII-8BIT encoded buffer like String#new
@output_buffer = ActiveSupport::SafeBuffer.new "" @output_buffer = ActiveSupport::SafeBuffer.new ""
@rendered = "" @rendered = "".dup
make_test_case_available_to_view! make_test_case_available_to_view!
say_no_to_protect_against_forgery! say_no_to_protect_against_forgery!
......
...@@ -20,7 +20,7 @@ def to_s ...@@ -20,7 +20,7 @@ def to_s
private private
def query(path, exts, _, _) def query(path, exts, _, _)
query = "" query = "".dup
EXTENSIONS.each_key do |ext| EXTENSIONS.each_key do |ext|
query << "(" << exts[ext].map { |e| e && Regexp.escape(".#{e}") }.join("|") << "|)" query << "(" << exts[ext].map { |e| e && Regexp.escape(".#{e}") }.join("|") << "|)"
end end
......
...@@ -55,7 +55,7 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut ...@@ -55,7 +55,7 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
private private
def hidden_fields(method = nil) def hidden_fields(method = nil)
txt = %{<input name="utf8" type="hidden" value="&#x2713;" />} txt = %{<input name="utf8" type="hidden" value="&#x2713;" />}.dup
if method && !%w(get post).include?(method.to_s) if method && !%w(get post).include?(method.to_s)
txt << %{<input name="_method" type="hidden" value="#{method}" />} txt << %{<input name="_method" type="hidden" value="#{method}" />}
...@@ -65,7 +65,7 @@ def hidden_fields(method = nil) ...@@ -65,7 +65,7 @@ def hidden_fields(method = nil)
end end
def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil) def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil)
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if multipart txt << %{ enctype="multipart/form-data"} if multipart
txt << %{ data-remote="true"} if remote txt << %{ data-remote="true"} if remote
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class
......
body = "" body = "".dup
body << ["Hello", "from", "Ruby", "code"].join(" ") body << ["Hello", "from", "Ruby", "code"].join(" ")
...@@ -194,7 +194,7 @@ class ScrollsController < ActionController::Base ...@@ -194,7 +194,7 @@ class ScrollsController < ActionController::Base
FEEDS["provide_builder"] = <<-'EOT' FEEDS["provide_builder"] = <<-'EOT'
# we pass in the new_xml to the helper so it doesn't # we pass in the new_xml to the helper so it doesn't
# call anything on the original builder # call anything on the original builder
new_xml = Builder::XmlMarkup.new(:target=>'') new_xml = Builder::XmlMarkup.new(:target=>''.dup)
atom_feed(:xml => new_xml) do |feed| atom_feed(:xml => new_xml) do |feed|
feed.title("My great blog!") feed.title("My great blog!")
feed.updated(@scrolls.first.created_at) feed.updated(@scrolls.first.created_at)
......
...@@ -16,7 +16,7 @@ def hidden_fields(options = {}) ...@@ -16,7 +16,7 @@ def hidden_fields(options = {})
method = options[:method] method = options[:method]
skip_enforcing_utf8 = options.fetch(:skip_enforcing_utf8, false) skip_enforcing_utf8 = options.fetch(:skip_enforcing_utf8, false)
"".tap do |txt| "".dup.tap do |txt|
unless skip_enforcing_utf8 unless skip_enforcing_utf8
txt << %{<input name="utf8" type="hidden" value="&#x2713;" />} txt << %{<input name="utf8" type="hidden" value="&#x2713;" />}
end end
...@@ -32,7 +32,7 @@ def form_text(action = "http://www.example.com", local: false, **options) ...@@ -32,7 +32,7 @@ def form_text(action = "http://www.example.com", local: false, **options)
method = method.to_s == "get" ? "get" : "post" method = method.to_s == "get" ? "get" : "post"
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if enctype txt << %{ enctype="multipart/form-data"} if enctype
txt << %{ data-remote="true"} unless local txt << %{ data-remote="true"} unless local
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class
...@@ -2194,9 +2194,9 @@ def hidden_fields(options = {}) ...@@ -2194,9 +2194,9 @@ def hidden_fields(options = {})
method = options[:method] method = options[:method]
if options.fetch(:skip_enforcing_utf8, false) if options.fetch(:skip_enforcing_utf8, false)
txt = "" txt = "".dup
else else
txt = %{<input name="utf8" type="hidden" value="&#x2713;" />} txt = %{<input name="utf8" type="hidden" value="&#x2713;" />}.dup
end end
if method && !%w(get post).include?(method.to_s) if method && !%w(get post).include?(method.to_s)
...@@ -2207,7 +2207,7 @@ def hidden_fields(options = {}) ...@@ -2207,7 +2207,7 @@ def hidden_fields(options = {})
end end
def form_text(action = "/", id = nil, html_class = nil, local = nil, multipart = nil, method = nil) def form_text(action = "/", id = nil, html_class = nil, local = nil, multipart = nil, method = nil)
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if multipart txt << %{ enctype="multipart/form-data"} if multipart
txt << %{ data-remote="true"} unless local txt << %{ data-remote="true"} unless local
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class
......
...@@ -3446,9 +3446,9 @@ def hidden_fields(options = {}) ...@@ -3446,9 +3446,9 @@ def hidden_fields(options = {})
method = options[:method] method = options[:method]
if options.fetch(:enforce_utf8, true) if options.fetch(:enforce_utf8, true)
txt = %{<input name="utf8" type="hidden" value="&#x2713;" />} txt = %{<input name="utf8" type="hidden" value="&#x2713;" />}.dup
else else
txt = "" txt = "".dup
end end
if method && !%w(get post).include?(method.to_s) if method && !%w(get post).include?(method.to_s)
...@@ -3459,7 +3459,7 @@ def hidden_fields(options = {}) ...@@ -3459,7 +3459,7 @@ def hidden_fields(options = {})
end end
def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil) def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil)
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if multipart txt << %{ enctype="multipart/form-data"} if multipart
txt << %{ data-remote="true"} if remote txt << %{ data-remote="true"} if remote
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class
......
...@@ -14,7 +14,7 @@ def hidden_fields(options = {}) ...@@ -14,7 +14,7 @@ def hidden_fields(options = {})
method = options[:method] method = options[:method]
enforce_utf8 = options.fetch(:enforce_utf8, true) enforce_utf8 = options.fetch(:enforce_utf8, true)
"".tap do |txt| "".dup.tap do |txt|
if enforce_utf8 if enforce_utf8
txt << %{<input name="utf8" type="hidden" value="&#x2713;" />} txt << %{<input name="utf8" type="hidden" value="&#x2713;" />}
end end
...@@ -30,7 +30,7 @@ def form_text(action = "http://www.example.com", options = {}) ...@@ -30,7 +30,7 @@ def form_text(action = "http://www.example.com", options = {})
method = method.to_s == "get" ? "get" : "post" method = method.to_s == "get" ? "get" : "post"
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if enctype txt << %{ enctype="multipart/form-data"} if enctype
txt << %{ data-remote="true"} if remote txt << %{ data-remote="true"} if remote
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class
......
...@@ -20,8 +20,8 @@ def test_escape_javascript ...@@ -20,8 +20,8 @@ def test_escape_javascript
assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos')) assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos'))
assert_equal %(backslash\\\\test), escape_javascript(%(backslash\\test)) assert_equal %(backslash\\\\test), escape_javascript(%(backslash\\test))
assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags)) assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags))
assert_equal %(unicode &#x2028; newline), escape_javascript(%(unicode \342\200\250 newline).force_encoding(Encoding::UTF_8).encode!) assert_equal %(unicode &#x2028; newline), escape_javascript(%(unicode \342\200\250 newline).dup.force_encoding(Encoding::UTF_8).encode!)
assert_equal %(unicode &#x2029; newline), escape_javascript(%(unicode \342\200\251 newline).force_encoding(Encoding::UTF_8).encode!) assert_equal %(unicode &#x2029; newline), escape_javascript(%(unicode \342\200\251 newline).dup.force_encoding(Encoding::UTF_8).encode!)
assert_equal %(dont <\\/close> tags), j(%(dont </close> tags)) assert_equal %(dont <\\/close> tags), j(%(dont </close> tags))
end end
......
...@@ -439,7 +439,7 @@ def test_render_fallbacks_to_erb_for_unknown_types ...@@ -439,7 +439,7 @@ def test_render_fallbacks_to_erb_for_unknown_types
end end
CustomHandler = lambda do |template| CustomHandler = lambda do |template|
"@output_buffer = ''\n" \ "@output_buffer = ''.dup\n" \
"@output_buffer << 'source: #{template.source.inspect}'\n" "@output_buffer << 'source: #{template.source.inspect}'\n"
end end
...@@ -583,7 +583,7 @@ def test_render_layout_with_object ...@@ -583,7 +583,7 @@ def test_render_layout_with_object
def test_render_with_passing_couple_extensions_to_one_register_template_handler_function_call def test_render_with_passing_couple_extensions_to_one_register_template_handler_function_call
ActionView::Template.register_template_handler :foo1, :foo2, CustomHandler ActionView::Template.register_template_handler :foo1, :foo2, CustomHandler
assert_equal @view.render(inline: "Hello, World!", type: :foo1), @view.render(inline: "Hello, World!", type: :foo2) assert_equal @view.render(inline: "Hello, World!".dup, type: :foo1), @view.render(inline: "Hello, World!".dup, type: :foo2)
ensure ensure
ActionView::Template.unregister_template_handler :foo1, :foo2 ActionView::Template.unregister_template_handler :foo1, :foo2
end end
......
...@@ -17,7 +17,7 @@ def render_body(options) ...@@ -17,7 +17,7 @@ def render_body(options)
def buffered_render(options) def buffered_render(options)
body = render_body(options) body = render_body(options)
string = "" string = "".dup
body.each do |piece| body.each do |piece|
string << piece string << piece
end end
......
...@@ -53,7 +53,7 @@ def my_buffer ...@@ -53,7 +53,7 @@ def my_buffer
end end
def new_template(body = "<%= hello %>", details = { format: :html }) def new_template(body = "<%= hello %>", details = { format: :html })
ActionView::Template.new(body, "hello template", details.fetch(:handler) { ERBHandler }, { virtual_path: "hello" }.merge!(details)) ActionView::Template.new(body.dup, "hello template", details.fetch(:handler) { ERBHandler }, { virtual_path: "hello" }.merge!(details))
end end
def render(locals = {}) def render(locals = {})
......
...@@ -11,7 +11,7 @@ def setup ...@@ -11,7 +11,7 @@ def setup
end end
def test_concat def test_concat
self.output_buffer = "foo" self.output_buffer = "foo".dup
assert_equal "foobar", concat("bar") assert_equal "foobar", concat("bar")
assert_equal "foobar", output_buffer assert_equal "foobar", output_buffer
end end
...@@ -104,8 +104,8 @@ def test_truncate_with_options_hash ...@@ -104,8 +104,8 @@ def test_truncate_with_options_hash
end end
def test_truncate_multibyte def test_truncate_multibyte
assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding(Encoding::UTF_8), assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".dup.force_encoding(Encoding::UTF_8),
truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding(Encoding::UTF_8), length: 10) truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".dup.force_encoding(Encoding::UTF_8), length: 10)
end end
def test_truncate_does_not_modify_the_options_hash def test_truncate_does_not_modify_the_options_hash
...@@ -325,7 +325,7 @@ def test_excerpt_with_omission ...@@ -325,7 +325,7 @@ def test_excerpt_with_omission
end end
def test_excerpt_with_utf8 def test_excerpt_with_utf8
assert_equal("...\357\254\203ciency could not be...".force_encoding(Encoding::UTF_8), excerpt("That's why e\357\254\203ciency could not be helped".force_encoding(Encoding::UTF_8), "could", radius: 8)) assert_equal("...\357\254\203ciency could not be...".dup.force_encoding(Encoding::UTF_8), excerpt("That's why e\357\254\203ciency could not be helped".dup.force_encoding(Encoding::UTF_8), "could", radius: 8))
end end
def test_excerpt_does_not_modify_the_options_hash def test_excerpt_does_not_modify_the_options_hash
......
...@@ -535,7 +535,7 @@ def test_current_page_with_escaped_params ...@@ -535,7 +535,7 @@ def test_current_page_with_escaped_params
def test_current_page_with_escaped_params_with_different_encoding def test_current_page_with_escaped_params_with_different_encoding
@request = request_for_url("/") @request = request_for_url("/")
@request.stub(:path, "/category/administra%c3%a7%c3%a3o".force_encoding(Encoding::ASCII_8BIT)) do @request.stub(:path, "/category/administra%c3%a7%c3%a3o".dup.force_encoding(Encoding::ASCII_8BIT)) do
assert current_page?(controller: "foo", action: "category", category: "administração") assert current_page?(controller: "foo", action: "category", category: "administração")
assert current_page?("http://www.example.com/category/administra%c3%a7%c3%a3o") assert current_page?("http://www.example.com/category/administra%c3%a7%c3%a3o")
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册