actionview 7.2.3.1 → 8.0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +135 -109
  3. data/lib/action_view/dependency_tracker/erb_tracker.rb +36 -27
  4. data/lib/action_view/dependency_tracker/ruby_tracker.rb +2 -19
  5. data/lib/action_view/dependency_tracker/wildcard_resolver.rb +32 -0
  6. data/lib/action_view/dependency_tracker.rb +1 -0
  7. data/lib/action_view/gem_version.rb +4 -4
  8. data/lib/action_view/helpers/atom_feed_helper.rb +0 -2
  9. data/lib/action_view/helpers/cache_helper.rb +8 -0
  10. data/lib/action_view/helpers/date_helper.rb +3 -3
  11. data/lib/action_view/helpers/form_helper.rb +76 -75
  12. data/lib/action_view/helpers/form_options_helper.rb +25 -22
  13. data/lib/action_view/helpers/form_tag_helper.rb +20 -17
  14. data/lib/action_view/helpers/rendering_helper.rb +160 -50
  15. data/lib/action_view/helpers/sanitize_helper.rb +12 -1
  16. data/lib/action_view/helpers/tag_helper.rb +39 -43
  17. data/lib/action_view/helpers/tags/base.rb +11 -9
  18. data/lib/action_view/helpers/tags/check_box.rb +2 -2
  19. data/lib/action_view/helpers/tags/collection_check_boxes.rb +4 -3
  20. data/lib/action_view/helpers/tags/file_field.rb +4 -1
  21. data/lib/action_view/helpers/tags/label.rb +3 -10
  22. data/lib/action_view/helpers/tags/radio_button.rb +1 -1
  23. data/lib/action_view/helpers/tags/select_renderer.rb +1 -1
  24. data/lib/action_view/helpers/tags/text_area.rb +1 -1
  25. data/lib/action_view/helpers/tags/text_field.rb +1 -1
  26. data/lib/action_view/helpers/text_helper.rb +23 -7
  27. data/lib/action_view/helpers/url_helper.rb +2 -4
  28. data/lib/action_view/layouts.rb +1 -1
  29. data/lib/action_view/record_identifier.rb +1 -1
  30. data/lib/action_view/render_parser/prism_render_parser.rb +13 -1
  31. data/lib/action_view/render_parser/ripper_render_parser.rb +10 -1
  32. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +5 -1
  33. data/lib/action_view/renderer/streaming_template_renderer.rb +8 -2
  34. data/lib/action_view/rendering.rb +2 -3
  35. data/lib/action_view/template/handlers/erb/erubi.rb +1 -1
  36. data/lib/action_view/template/raw_file.rb +4 -0
  37. data/lib/action_view/template/resolver.rb +0 -1
  38. data/lib/action_view/template.rb +9 -4
  39. metadata +19 -26
@@ -1,32 +1,140 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # :markup: markdown
4
+
3
5
  module ActionView
4
6
  module Helpers # :nodoc:
5
- # = Action View \Rendering \Helpers
7
+ # # Action View Rendering Helpers
6
8
  #
7
- # Implements methods that allow rendering from a view context.
8
- # In order to use this module, all you need is to implement
9
- # view_renderer that returns an ActionView::Renderer object.
9
+ # Implements methods that allow rendering from a view context. In order to use
10
+ # this module, all you need is to implement view_renderer that returns an
11
+ # ActionView::Renderer object.
10
12
  module RenderingHelper
11
- # Returns the result of a render that's dictated by the options hash. The primary options are:
13
+ # Renders a template and returns the result.
14
+ #
15
+ # Pass the template to render as the first argument. This is shorthand
16
+ # syntax for partial rendering, so the template filename should be
17
+ # prefixed with an underscore. The partial renderer looks for the partial
18
+ # template in the directory of the calling template first.
19
+ #
20
+ # <% # app/views/posts/new.html.erb %>
21
+ # <%= render "form" %>
22
+ # # => renders app/views/posts/_form.html.erb
23
+ #
24
+ # Use the complete view path to render a partial from another directory.
25
+ #
26
+ # <% # app/views/posts/show.html.erb %>
27
+ # <%= render "comments/form" %>
28
+ # # => renders app/views/comments/_form.html.erb
29
+ #
30
+ # Without the rendering mode, the second argument can be a Hash of local
31
+ # variable assignments for the template.
32
+ #
33
+ # <% # app/views/posts/new.html.erb %>
34
+ # <%= render "form", post: Post.new %>
35
+ # # => renders app/views/posts/_form.html.erb
36
+ #
37
+ # If the first argument responds to `render_in`, the template will be rendered
38
+ # by calling `render_in` with the current view context.
39
+ #
40
+ # class Greeting
41
+ # def render_in(view_context)
42
+ # view_context.render html: "<h1>Hello, World</h1>"
43
+ # end
44
+ #
45
+ # def format
46
+ # :html
47
+ # end
48
+ # end
49
+ #
50
+ # <%= render Greeting.new %>
51
+ # # => "<h1>Hello, World</h1>"
52
+ #
53
+ # #### Rendering Mode
54
+ #
55
+ # Pass the rendering mode as first argument to override it.
56
+ #
57
+ # `:partial`
58
+ # : See ActionView::PartialRenderer for details.
59
+ #
60
+ # <%= render partial: "form", locals: { post: Post.new } %>
61
+ # # => renders app/views/posts/_form.html.erb
62
+ #
63
+ # `:file`
64
+ # : Renders the contents of a file. This option should **not** be used with
65
+ # unsanitized user input.
66
+ #
67
+ # <%= render file: "/path/to/some/file" %>
68
+ # # => renders /path/to/some/file
69
+ #
70
+ # `:inline`
71
+ # : Renders an ERB template string.
72
+ #
73
+ # <% name = "World" %>
74
+ # <%= render inline: "<h1>Hello, <%= name %>!</h1>" %>
75
+ # # => renders "<h1>Hello, World!</h1>"
76
+ #
77
+ # `:body`
78
+ # : Renders the provided text, and sets the format as `:text`.
79
+ #
80
+ # <%= render body: "Hello, World!" %>
81
+ # # => renders "Hello, World!"
82
+ #
83
+ # `:plain`
84
+ # : Renders the provided text, and sets the format as `:text`.
85
+ #
86
+ # <%= render plain: "Hello, World!" %>
87
+ # # => renders "Hello, World!"
88
+ #
89
+ # `:html`
90
+ # : Renders the provided HTML string, and sets the format as
91
+ # `:html`. If the string is not `html_safe?`, performs HTML escaping on
92
+ # the string before rendering.
93
+ #
94
+ # <%= render html: "<h1>Hello, World!</h1>".html_safe %>
95
+ # # => renders "<h1>Hello, World!</h1>"
96
+ #
97
+ # <%= render html: "<h1>Hello, World!</h1>" %>
98
+ # # => renders "&lt;h1&gt;Hello, World!&lt;/h1&gt;"
99
+ #
100
+ # `:renderable`
101
+ # : Renders the provided object by calling `render_in` with the current view
102
+ # context. The format is determined by calling `format` on the
103
+ # renderable if it responds to `format`, falling back to `:html` by
104
+ # default.
105
+ #
106
+ # <%= render renderable: Greeting.new %>
107
+ # # => renders "<h1>Hello, World</h1>"
108
+ #
109
+ #
110
+ # #### Options
111
+ #
112
+ # `:locals`
113
+ # : Hash of local variable assignments for the template.
114
+ #
115
+ # <%= render inline: "<h1>Hello, <%= name %>!</h1>", locals: { name: "World" } %>
116
+ # # => renders "<h1>Hello, World!</h1>"
117
+ #
118
+ # `:formats`
119
+ # : Override the current format to render a template for a different format.
120
+ #
121
+ # <% # app/views/posts/show.html.erb %>
122
+ # <%= render template: "posts/content", formats: [:text] %>
123
+ # # => renders app/views/posts/content.text.erb
12
124
  #
13
- # * <tt>:partial</tt> - See ActionView::PartialRenderer.
14
- # * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add +:locals+ to pass in those.
15
- # * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller.
16
- # * <tt>:plain</tt> - Renders the text passed in out. Setting the content
17
- # type as <tt>text/plain</tt>.
18
- # * <tt>:html</tt> - Renders the HTML safe string passed in out, otherwise
19
- # performs HTML escape on the string first. Setting the content type as
20
- # <tt>text/html</tt>.
21
- # * <tt>:body</tt> - Renders the text passed in, and inherits the content
22
- # type of <tt>text/plain</tt> from ActionDispatch::Response object.
125
+ # `:variants`
126
+ # : Render a template for a different variant.
23
127
  #
24
- # If no <tt>options</tt> hash is passed or if <tt>:update</tt> is specified, then:
128
+ # <% # app/views/posts/show.html.erb %>
129
+ # <%= render template: "posts/content", variants: [:tablet] %>
130
+ # # => renders app/views/posts/content.html+tablet.erb
25
131
  #
26
- # If an object responding to +render_in+ is passed, +render_in+ is called on the object,
27
- # passing in the current view context.
132
+ # `:handlers`
133
+ # : Render a template for a different handler.
28
134
  #
29
- # Otherwise, a partial is rendered using the second parameter as the locals hash.
135
+ # <% # app/views/posts/show.html.erb %>
136
+ # <%= render template: "posts/content", handlers: [:builder] %>
137
+ # # => renders app/views/posts/content.html.builder
30
138
  def render(options = {}, locals = {}, &block)
31
139
  case options
32
140
  when Hash
@@ -47,52 +155,54 @@ module ActionView
47
155
  end
48
156
 
49
157
  # Overrides _layout_for in the context object so it supports the case a block is
50
- # passed to a partial. Returns the contents that are yielded to a layout, given a
51
- # name or a block.
158
+ # passed to a partial. Returns the contents that are yielded to a layout, given
159
+ # a name or a block.
52
160
  #
53
- # You can think of a layout as a method that is called with a block. If the user calls
54
- # <tt>yield :some_name</tt>, the block, by default, returns <tt>content_for(:some_name)</tt>.
55
- # If the user calls simply +yield+, the default block returns <tt>content_for(:layout)</tt>.
161
+ # You can think of a layout as a method that is called with a block. If the user
162
+ # calls `yield :some_name`, the block, by default, returns
163
+ # `content_for(:some_name)`. If the user calls simply `yield`, the default block
164
+ # returns `content_for(:layout)`.
56
165
  #
57
166
  # The user can override this default by passing a block to the layout:
58
167
  #
59
- # # The template
60
- # <%= render layout: "my_layout" do %>
61
- # Content
62
- # <% end %>
168
+ # # The template
169
+ # <%= render layout: "my_layout" do %>
170
+ # Content
171
+ # <% end %>
63
172
  #
64
- # # The layout
65
- # <html>
66
- # <%= yield %>
67
- # </html>
173
+ # # The layout
174
+ # <html>
175
+ # <%= yield %>
176
+ # </html>
68
177
  #
69
- # In this case, instead of the default block, which would return <tt>content_for(:layout)</tt>,
70
- # this method returns the block that was passed in to <tt>render :layout</tt>, and the response
178
+ # In this case, instead of the default block, which would return `content_for(:layout)`,
179
+ # this method returns the block that was passed in to `render :layout`, and the response
71
180
  # would be
72
181
  #
73
- # <html>
74
- # Content
75
- # </html>
182
+ # <html>
183
+ # Content
184
+ # </html>
76
185
  #
77
- # Finally, the block can take block arguments, which can be passed in by +yield+:
186
+ # Finally, the block can take block arguments, which can be passed in by
187
+ # `yield`:
78
188
  #
79
- # # The template
80
- # <%= render layout: "my_layout" do |customer| %>
81
- # Hello <%= customer.name %>
82
- # <% end %>
189
+ # # The template
190
+ # <%= render layout: "my_layout" do |customer| %>
191
+ # Hello <%= customer.name %>
192
+ # <% end %>
83
193
  #
84
- # # The layout
85
- # <html>
86
- # <%= yield Struct.new(:name).new("David") %>
87
- # </html>
194
+ # # The layout
195
+ # <html>
196
+ # <%= yield Struct.new(:name).new("David") %>
197
+ # </html>
88
198
  #
89
- # In this case, the layout would receive the block passed into <tt>render :layout</tt>,
199
+ # In this case, the layout would receive the block passed into `render :layout`,
90
200
  # and the struct specified would be passed into the block as an argument. The result
91
201
  # would be
92
202
  #
93
- # <html>
94
- # Hello David
95
- # </html>
203
+ # <html>
204
+ # Hello David
205
+ # </html>
96
206
  #
97
207
  def _layout_for(*args, &block)
98
208
  name = args.first
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/core_ext/string/output_safety"
3
4
  require "rails-html-sanitizer"
4
5
 
5
6
  module ActionView
@@ -24,9 +25,18 @@ module ActionView
24
25
  #
25
26
  # Custom sanitization rules can also be provided.
26
27
  #
28
+ # <b>Warning</b>: Adding disallowed tags or attributes to the allowlists may introduce
29
+ # vulnerabilities into your application. Please rely on the default allowlists whenever
30
+ # possible, because they are curated to maintain security and safety. If you think that the
31
+ # default allowlists should be expanded, please {open an issue on the rails-html-sanitizer
32
+ # project}[https://github.com/rails/rails-html-sanitizer/issues].
33
+ #
27
34
  # Please note that sanitizing user-provided text does not guarantee that the
28
35
  # resulting markup is valid or even well-formed.
29
36
  #
37
+ # The output is markup, which is safe only as element content. Do not use it in an attribute
38
+ # value; use +strip_tags+ or a tag helper such as <tt>tag.a(title: ...)</tt> instead.
39
+ #
30
40
  # ==== Options
31
41
  #
32
42
  # [+:tags+]
@@ -131,7 +141,8 @@ module ActionView
131
141
  # strip_tags("> A quote from Smith & Wesson")
132
142
  # # => &gt; A quote from Smith &amp; Wesson
133
143
  def strip_tags(html)
134
- self.class.full_sanitizer.sanitize(html)&.html_safe
144
+ stripped = self.class.full_sanitizer.sanitize(html)
145
+ ERB::Util.html_escape_once(stripped) if stripped
135
146
  end
136
147
 
137
148
  # Strips all link tags from +html+ leaving just the link text.
@@ -4,7 +4,6 @@ require "active_support/code_generator"
4
4
  require "active_support/core_ext/enumerable"
5
5
  require "active_support/core_ext/string/output_safety"
6
6
  require "active_support/core_ext/string/inflections"
7
- require "set"
8
7
  require "action_view/helpers/capture_helper"
9
8
  require "action_view/helpers/output_safety_helper"
10
9
 
@@ -48,48 +47,36 @@ module ActionView
48
47
  include CaptureHelper
49
48
  include OutputSafetyHelper
50
49
 
51
- def self.define_element(name, code_generator:, method_name: name.to_s.underscore)
52
- code_generator.define_cached_method(method_name, namespace: :tag_builder) do |batch|
53
- batch.push(<<~RUBY) unless instance_methods.include?(method_name.to_sym)
54
- def #{method_name}(content = nil, escape: true, **options, &block)
55
- tag_string("#{name}", content, options, escape: escape, &block)
56
- end
57
- RUBY
50
+ def self.define_element(name, code_generator:, method_name: name)
51
+ return if method_defined?(name)
52
+
53
+ code_generator.class_eval do |batch|
54
+ batch << "\n" <<
55
+ "def #{method_name}(content = nil, escape: true, **options, &block)" <<
56
+ " tag_string(#{name.inspect}, content, options, escape: escape, &block)" <<
57
+ "end"
58
58
  end
59
59
  end
60
60
 
61
- def self.define_void_element(name, code_generator:, method_name: name.to_s.underscore)
62
- code_generator.define_cached_method(method_name, namespace: :tag_builder) do |batch|
63
- batch.push(<<~RUBY)
64
- def #{method_name}(content = nil, escape: true, **options, &block)
65
- if content || block
66
- ActionView.deprecator.warn <<~TEXT
67
- Putting content inside a void element (#{name}) is invalid
68
- according to the HTML5 spec, and so it is being deprecated
69
- without replacement. In Rails 8.0, passing content as a
70
- positional argument will raise, and using a block will have
71
- no effect.
72
- TEXT
73
- tag_string("#{name}", content, options, escape: escape, &block)
74
- else
75
- self_closing_tag_string("#{name}", options, escape, ">")
76
- end
77
- end
78
- RUBY
61
+ def self.define_void_element(name, code_generator:, method_name: name)
62
+ code_generator.class_eval do |batch|
63
+ batch << "\n" <<
64
+ "def #{method_name}(escape: true, **options, &block)" <<
65
+ " self_closing_tag_string(#{name.inspect}, options, escape, '>')" <<
66
+ "end"
79
67
  end
80
68
  end
81
69
 
82
- def self.define_self_closing_element(name, code_generator:, method_name: name.to_s.underscore)
83
- code_generator.define_cached_method(method_name, namespace: :tag_builder) do |batch|
84
- batch.push(<<~RUBY)
85
- def #{method_name}(content = nil, escape: true, **options, &block)
86
- if content || block
87
- tag_string("#{name}", content, options, escape: escape, &block)
88
- else
89
- self_closing_tag_string("#{name}", options, escape)
90
- end
91
- end
92
- RUBY
70
+ def self.define_self_closing_element(name, code_generator:, method_name: name)
71
+ code_generator.class_eval do |batch|
72
+ batch << "\n" <<
73
+ "def #{method_name}(content = nil, escape: true, **options, &block)" <<
74
+ " if content || block" <<
75
+ " tag_string(#{name.inspect}, content, options, escape: escape, &block)" <<
76
+ " else" <<
77
+ " self_closing_tag_string(#{name.inspect}, options, escape)" <<
78
+ " end" <<
79
+ "end"
93
80
  end
94
81
  end
95
82
 
@@ -110,8 +97,8 @@ module ActionView
110
97
  define_void_element :wbr, code_generator: code_generator
111
98
 
112
99
  define_self_closing_element :animate, code_generator: code_generator
113
- define_self_closing_element :animateMotion, code_generator: code_generator
114
- define_self_closing_element :animateTransform, code_generator: code_generator
100
+ define_self_closing_element :animateMotion, code_generator: code_generator, method_name: :animate_motion
101
+ define_self_closing_element :animateTransform, code_generator: code_generator, method_name: :animate_transform
115
102
  define_self_closing_element :circle, code_generator: code_generator
116
103
  define_self_closing_element :ellipse, code_generator: code_generator
117
104
  define_self_closing_element :line, code_generator: code_generator
@@ -265,18 +252,27 @@ module ActionView
265
252
  options.each_pair do |key, value|
266
253
  next if key.blank?
267
254
 
255
+ name = Symbol === key ? key.name : key.to_s
256
+ next if name.blank?
257
+
268
258
  type = TAG_TYPES[key]
269
259
  if type == :data && value.is_a?(Hash)
270
260
  value.each_pair do |k, v|
271
261
  next if k.blank? || v.nil?
272
262
 
263
+ nested_name = Symbol === k ? k.name : k.to_s
264
+ next if nested_name.blank?
265
+
273
266
  output << sep
274
- output << prefix_tag_option(key, k, v, escape)
267
+ output << prefix_tag_option(name, nested_name, v, escape)
275
268
  end
276
269
  elsif type == :aria && value.is_a?(Hash)
277
270
  value.each_pair do |k, v|
278
271
  next if k.blank? || v.nil?
279
272
 
273
+ nested_name = Symbol === k ? k.name : k.to_s
274
+ next if nested_name.blank?
275
+
280
276
  case v
281
277
  when Array, Hash
282
278
  tokens = TagHelper.build_tag_values(v)
@@ -288,16 +284,16 @@ module ActionView
288
284
  end
289
285
 
290
286
  output << sep
291
- output << prefix_tag_option(key, k, v, escape)
287
+ output << prefix_tag_option(name, nested_name, v, escape)
292
288
  end
293
289
  elsif type == :boolean
294
290
  if value
295
291
  output << sep
296
- output << boolean_tag_option(key)
292
+ output << boolean_tag_option(name)
297
293
  end
298
294
  elsif !value.nil?
299
295
  output << sep
300
- output << tag_option(key, value, escape)
296
+ output << tag_option(name, value, escape)
301
297
  end
302
298
  end
303
299
  output unless output.empty?
@@ -80,30 +80,32 @@ module ActionView
80
80
  end
81
81
  end
82
82
 
83
- def add_default_name_and_id_for_value(tag_value, options)
83
+ def add_default_name_and_field_for_value(tag_value, options, field = "id")
84
84
  if tag_value.nil?
85
- add_default_name_and_id(options)
85
+ add_default_name_and_field(options, field)
86
86
  else
87
- specified_id = options["id"]
88
- add_default_name_and_id(options)
87
+ specified_field = options[field]
88
+ add_default_name_and_field(options, field)
89
89
 
90
- if specified_id.blank? && options["id"].present?
91
- options["id"] += "_#{sanitized_value(tag_value)}"
90
+ if specified_field.blank? && options[field].present?
91
+ options[field] += "_#{sanitized_value(tag_value)}"
92
92
  end
93
93
  end
94
94
  end
95
+ alias_method :add_default_name_and_id_for_value, :add_default_name_and_field_for_value
95
96
 
96
- def add_default_name_and_id(options)
97
+ def add_default_name_and_field(options, field = "id")
97
98
  index = name_and_id_index(options)
98
99
  options["name"] = options.fetch("name") { tag_name(options["multiple"], index) }
99
100
 
100
101
  if generate_ids?
101
- options["id"] = options.fetch("id") { tag_id(index, options.delete("namespace")) }
102
+ options[field] = options.fetch(field) { tag_id(index, options.delete("namespace")) }
102
103
  if namespace = options.delete("namespace")
103
- options["id"] = options["id"] ? "#{namespace}_#{options['id']}" : namespace
104
+ options[field] = options[field] ? "#{namespace}_#{options[field]}" : namespace
104
105
  end
105
106
  end
106
107
  end
108
+ alias_method :add_default_name_and_id, :add_default_name_and_field
107
109
 
108
110
  def tag_name(multiple = false, index = nil)
109
111
  @template_object.field_name(@object_name, sanitized_method_name, multiple: multiple, index: index)
@@ -21,10 +21,10 @@ module ActionView
21
21
  options["checked"] = "checked" if input_checked?(options)
22
22
 
23
23
  if options["multiple"]
24
- add_default_name_and_id_for_value(@checked_value, options)
24
+ add_default_name_and_field_for_value(@checked_value, options)
25
25
  options.delete("multiple")
26
26
  else
27
- add_default_name_and_id(options)
27
+ add_default_name_and_field(options)
28
28
  end
29
29
 
30
30
  include_hidden = options.delete("include_hidden") { true }
@@ -10,12 +10,13 @@ module ActionView
10
10
  include FormOptionsHelper
11
11
 
12
12
  class CheckBoxBuilder < Builder # :nodoc:
13
- def check_box(extra_html_options = {})
13
+ def checkbox(extra_html_options = {})
14
14
  html_options = extra_html_options.merge(@input_html_options)
15
15
  html_options[:multiple] = true
16
16
  html_options[:skip_default_ids] = false
17
- @template_object.check_box(@object_name, @method_name, html_options, @value, nil)
17
+ @template_object.checkbox(@object_name, @method_name, html_options, @value, nil)
18
18
  end
19
+ alias_method :check_box, :checkbox
19
20
  end
20
21
 
21
22
  def render(&block)
@@ -24,7 +25,7 @@ module ActionView
24
25
 
25
26
  private
26
27
  def render_component(builder)
27
- builder.check_box + builder.label
28
+ builder.checkbox + builder.label
28
29
  end
29
30
 
30
31
  def hidden_field_name
@@ -6,8 +6,11 @@ module ActionView
6
6
  class FileField < TextField # :nodoc:
7
7
  def render
8
8
  include_hidden = @options.delete(:include_hidden)
9
+ if @options[:accept].is_a?(Array)
10
+ @options[:accept] = @options[:accept].join(",")
11
+ end
9
12
  options = @options.stringify_keys
10
- add_default_name_and_id(options)
13
+ add_default_name_and_field(options)
11
14
 
12
15
  if options["multiple"] && include_hidden
13
16
  hidden_field_for_multiple_file(options) + super
@@ -48,18 +48,11 @@ module ActionView
48
48
  def render(&block)
49
49
  options = @options.stringify_keys
50
50
  tag_value = options.delete("value")
51
- name_and_id = options.dup
52
51
 
53
- if name_and_id["for"]
54
- name_and_id["id"] = name_and_id["for"]
55
- else
56
- name_and_id.delete("id")
57
- end
58
-
59
- add_default_name_and_id_for_value(tag_value, name_and_id)
52
+ add_default_name_and_field_for_value(tag_value, options, "for")
60
53
  options.delete("index")
54
+ options.delete("name")
61
55
  options.delete("namespace")
62
- options["for"] = name_and_id["id"] unless options.key?("for")
63
56
 
64
57
  builder = LabelBuilder.new(@template_object, @object_name, @method_name, @object, tag_value)
65
58
 
@@ -71,7 +64,7 @@ module ActionView
71
64
  render_component(builder)
72
65
  end
73
66
 
74
- label_tag(name_and_id["id"], content, options)
67
+ label_tag(options["for"], content, options)
75
68
  end
76
69
 
77
70
  private
@@ -18,7 +18,7 @@ module ActionView
18
18
  options["type"] = "radio"
19
19
  options["value"] = @tag_value
20
20
  options["checked"] = "checked" if input_checked?(options)
21
- add_default_name_and_id_for_value(@tag_value, options)
21
+ add_default_name_and_field_for_value(@tag_value, options)
22
22
  tag("input", options)
23
23
  end
24
24
 
@@ -11,7 +11,7 @@ module ActionView
11
11
  html_options[prop.to_s] = options.delete(prop) if options.key?(prop) && !html_options.key?(prop.to_s)
12
12
  end
13
13
 
14
- add_default_name_and_id(html_options)
14
+ add_default_name_and_field(html_options)
15
15
 
16
16
  if placeholder_required?(html_options)
17
17
  raise ArgumentError, "include_blank cannot be false for a required field." if options[:include_blank] == false
@@ -10,7 +10,7 @@ module ActionView
10
10
 
11
11
  def render
12
12
  options = @options.stringify_keys
13
- add_default_name_and_id(options)
13
+ add_default_name_and_field(options)
14
14
 
15
15
  if size = options.delete("size")
16
16
  options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
@@ -13,7 +13,7 @@ module ActionView
13
13
  options["size"] = options["maxlength"] unless options.key?("size")
14
14
  options["type"] ||= field_type
15
15
  options["value"] = options.fetch("value") { value_before_type_cast } unless field_type == "file"
16
- add_default_name_and_id(options)
16
+ add_default_name_and_field(options)
17
17
  tag("input", options)
18
18
  end
19
19
 
@@ -143,8 +143,15 @@ module ActionView
143
143
  #
144
144
  # [+:highlighter+]
145
145
  # The highlighter string. Uses <tt>\1</tt> as the placeholder for a
146
- # phrase, similar to +String#sub+. Defaults to <tt>"<mark>\1</mark>"</tt>.
147
- # This option is ignored if a block is specified.
146
+ # phrase. Defaults to <tt>"<mark>\1</mark>"</tt>. The numbered placeholders
147
+ # <tt>\0</tt> to <tt>\9</tt> are expanded; other +String#sub+ replacement escapes
148
+ # are not. The phrase is HTML escaped, quotes included. This option
149
+ # is ignored if a block is specified.
150
+ #
151
+ # WARNING: Put a placeholder only in element content or inside a
152
+ # quoted attribute value. HTML escaping does not make the phrase safe
153
+ # in an unquoted attribute value, in a URL, JavaScript or CSS
154
+ # context, or in an attribute whose value is HTML, such as +srcdoc+.
148
155
  #
149
156
  # [+:sanitize+]
150
157
  # Whether to sanitize +text+ before highlighting. Defaults to true.
@@ -163,8 +170,8 @@ module ActionView
163
170
  # highlight('You searched for: rails', ['for', 'rails'], highlighter: '<em>\1</em>')
164
171
  # # => "You searched <em>for</em>: <em>rails</em>"
165
172
  #
166
- # highlight('You searched for: rails', 'rails', highlighter: '<a href="search?q=\1">\1</a>')
167
- # # => "You searched for: <a href=\"search?q=rails\">rails</a>"
173
+ # highlight('You searched for: rails', 'rails', highlighter: '<mark title="\1">\1</mark>')
174
+ # # => "You searched for: <mark title=\"rails\">rails</mark>"
168
175
  #
169
176
  # highlight('You searched for: rails', 'rails') { |match| link_to(search_path(q: match)) }
170
177
  # # => "You searched for: <a href=\"search?q=rails\">rails</a>"
@@ -179,14 +186,23 @@ module ActionView
179
186
  else
180
187
  patterns = Array(phrases).map { |phrase| Regexp === phrase ? phrase : Regexp.escape(phrase) }
181
188
  pattern = /(#{patterns.join("|")})/i
189
+ # The text after a tag begins with the ">" that closes it. Leave it
190
+ # in the segment so phrases can see it as context, but keep it out
191
+ # of the match so the highlighter can't take the place of the ">".
192
+ pattern_after_tag = /(?!\A)#{pattern}/
182
193
  highlighter = options.fetch(:highlighter, '<mark>\1</mark>') unless block
183
194
 
184
- text.scan(/<[^>]*|[^<]+/).each do |segment|
195
+ text.scan(/<[^>]*|[^<]+/).each_with_index do |segment, index|
185
196
  if !segment.start_with?("<")
197
+ segment_pattern = index > 0 ? pattern_after_tag : pattern
198
+
186
199
  if block
187
- segment.gsub!(pattern, &block)
200
+ segment.gsub!(segment_pattern, &block)
188
201
  else
189
- segment.gsub!(pattern, highlighter)
202
+ segment.gsub!(segment_pattern) do
203
+ match = Regexp.last_match
204
+ highlighter.gsub(/\\(\d)/) { ERB::Util.html_escape_once(match[$1.to_i]) }
205
+ end
190
206
  end
191
207
  end
192
208
  end.join