Merge pull request #50737 from skipkayhil/hm-further-optimize-tag-builder

Optimize TagBuilder tag generation
This commit is contained in:
Jean Boussier 2024-01-18 19:19:27 +01:00 committed by GitHub
commit 4816684c85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 8 deletions

@ -52,7 +52,7 @@ def self.define_element(name, code_generator:, method_name: name.to_s.underscore
code_generator.define_cached_method(method_name, namespace: :tag_builder) do |batch|
batch.push(<<~RUBY) unless instance_methods.include?(method_name.to_sym)
def #{method_name}(content = nil, escape: true, **options, &block)
tag_string(#{name.inspect}, content, escape: escape, **options, &block)
tag_string("#{name}", content, options, escape: escape, &block)
end
RUBY
end
@ -70,7 +70,7 @@ def #{method_name}(content = nil, escape: true, **options, &block)
positional argument will raise, and using a block will have
no effect.
TEXT
tag_string("#{name}", content, escape: escape, **options, &block)
tag_string("#{name}", content, options, escape: escape, &block)
else
self_closing_tag_string("#{name}", options, escape, ">")
end
@ -84,7 +84,7 @@ def self.define_self_closing_element(name, code_generator:, method_name: name.to
batch.push(<<~RUBY)
def #{method_name}(content = nil, escape: true, **options, &block)
if content || block
tag_string("#{name}", content, escape: escape, **options, &block)
tag_string("#{name}", content, options, escape: escape, &block)
else
self_closing_tag_string("#{name}", options, escape)
end
@ -239,7 +239,7 @@ def attributes(attributes)
tag_options(attributes.to_h).to_s.strip.html_safe
end
def tag_string(name, content = nil, escape: true, **options, &block)
def tag_string(name, content = nil, options, escape: true, &block)
content = @view_context.capture(self, &block) if block
content_tag_string(name, content, options, escape)
@ -252,7 +252,7 @@ def self_closing_tag_string(name, options, escape = true, tag_suffix = " />")
def content_tag_string(name, content, options, escape = true)
tag_options = tag_options(options, escape) if options
if escape
if escape && content.present?
content = ERB::Util.unwrapped_html_escape(content)
end
"<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name]}#{content}</#{name}>".html_safe
@ -334,12 +334,12 @@ def respond_to_missing?(*args)
true
end
def method_missing(called, ...)
def method_missing(called, *args, escape: true, **options, &block)
name = called.name.dasherize
TagHelper.ensure_valid_html5_tag_name(name)
tag_string(name, ...)
tag_string(name, *args, options, escape: escape, &block)
end
end

@ -45,7 +45,7 @@ def test_tag_builder_self_closing_tag
end
def test_tag_builder_self_closing_tag_with_content
assert_equal "<svg><circle><desc>A circle</desc></circle></svg>", tag.svg { tag.circle { tag.desc "A circle" } }
assert_equal "<svg><circle r=\"5\"><desc>A circle</desc></circle></svg>", tag.svg { tag.circle(r: "5") { tag.desc "A circle" } }
end
def test_tag_builder_defines_methods_to_build_html_elements