Skip to content

Commit

Permalink
Merge pull request rails#50737 from skipkayhil/hm-further-optimize-ta…
Browse files Browse the repository at this point in the history
…g-builder

Optimize TagBuilder tag generation
  • Loading branch information
byroot committed Jan 18, 2024
2 parents 0a30d2b + 597b56c commit 4816684
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions actionview/lib/action_view/helpers/tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion actionview/test/template/tag_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4816684

Please sign in to comment.