Merge pull request #50159 from skipkayhil/hm-deprecate-void-content
Deprecate content for void elements in TagBuilder
This commit is contained in:
commit
d6197c5efc
@ -1,3 +1,7 @@
|
|||||||
|
* Deprecate passing content to void elements when using `tag.br` type tag builders.
|
||||||
|
|
||||||
|
*Hartley McGuire*
|
||||||
|
|
||||||
* Fix the `number_to_human_size` view helper to correctly work with negative numbers.
|
* Fix the `number_to_human_size` view helper to correctly work with negative numbers.
|
||||||
|
|
||||||
*Earlopain*
|
*Earlopain*
|
||||||
|
@ -58,22 +58,39 @@ def #{method_name}(content = nil, escape: true, **options, &block)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.define_void_element(name, code_generator:, method_name: name.to_s.underscore, self_closing: false)
|
def self.define_void_element(name, code_generator:, method_name: name.to_s.underscore)
|
||||||
code_generator.define_cached_method(method_name, namespace: :tag_builder) do |batch|
|
code_generator.define_cached_method(method_name, namespace: :tag_builder) do |batch|
|
||||||
batch.push(<<~RUBY)
|
batch.push(<<~RUBY)
|
||||||
def #{method_name}(content = nil, escape: true, **options, &block)
|
def #{method_name}(content = nil, escape: true, **options, &block)
|
||||||
if content || block
|
if content || block
|
||||||
tag_string(#{name.inspect}, content, escape: escape, **options, &block)
|
ActionView.deprecator.warn <<~TEXT
|
||||||
|
Putting content inside a void element (#{name}) is invalid
|
||||||
|
according to the HTML5 spec, and so it is being deprecated
|
||||||
|
without replacement. In Rails 7.3, passing content as a
|
||||||
|
positional argument will raise, and using a block will have
|
||||||
|
no effect.
|
||||||
|
TEXT
|
||||||
|
tag_string("#{name}", content, escape: escape, **options, &block)
|
||||||
else
|
else
|
||||||
void_tag_string(#{name.inspect}, options, escape, #{self_closing})
|
self_closing_tag_string("#{name}", options, escape, ">")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.define_self_closing_element(name, **options)
|
def self.define_self_closing_element(name, code_generator:, method_name: name.to_s.underscore)
|
||||||
define_void_element(name, self_closing: true, **options)
|
code_generator.define_cached_method(method_name, namespace: :tag_builder) do |batch|
|
||||||
|
batch.push(<<~RUBY)
|
||||||
|
def #{method_name}(content = nil, escape: true, **options, &block)
|
||||||
|
if content || block
|
||||||
|
tag_string("#{name}", content, escape: escape, **options, &block)
|
||||||
|
else
|
||||||
|
self_closing_tag_string("#{name}", options, escape)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ActiveSupport::CodeGenerator.batch(self, __FILE__, __LINE__) do |code_generator|
|
ActiveSupport::CodeGenerator.batch(self, __FILE__, __LINE__) do |code_generator|
|
||||||
@ -228,8 +245,8 @@ def tag_string(name, content = nil, escape: true, **options, &block)
|
|||||||
content_tag_string(name, content, options, escape)
|
content_tag_string(name, content, options, escape)
|
||||||
end
|
end
|
||||||
|
|
||||||
def void_tag_string(name, options, escape = true, self_closing = false)
|
def self_closing_tag_string(name, options, escape = true, tag_suffix = " />")
|
||||||
"<#{name}#{tag_options(options, escape)}#{self_closing ? " />" : ">"}".html_safe
|
"<#{name}#{tag_options(options, escape)}#{tag_suffix}".html_safe
|
||||||
end
|
end
|
||||||
|
|
||||||
def content_tag_string(name, content, options, escape = true)
|
def content_tag_string(name, content, options, escape = true)
|
||||||
|
@ -27,12 +27,16 @@ def test_tag_builder_void_tag
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_tag_builder_void_tag_with_forced_content
|
def test_tag_builder_void_tag_with_forced_content
|
||||||
|
assert_deprecated(ActionView.deprecator) do
|
||||||
assert_equal "<br>some content</br>", tag.br("some content")
|
assert_equal "<br>some content</br>", tag.br("some content")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_tag_builder_void_tag_with_empty_content
|
def test_tag_builder_void_tag_with_empty_content
|
||||||
|
assert_deprecated(ActionView.deprecator) do
|
||||||
assert_equal "<br></br>", tag.br("")
|
assert_equal "<br></br>", tag.br("")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_tag_builder_self_closing_tag
|
def test_tag_builder_self_closing_tag
|
||||||
assert_equal "<svg><use href=\"#cool-icon\" /></svg>", tag.svg { tag.use("href" => "#cool-icon") }
|
assert_equal "<svg><use href=\"#cool-icon\" /></svg>", tag.svg { tag.use("href" => "#cool-icon") }
|
||||||
|
Loading…
Reference in New Issue
Block a user