Skip to content

Commit

Permalink
Merge pull request #43232 from le0pard/fix_use_tag
Browse files Browse the repository at this point in the history
Support svg unpaired tags in tag helper
  • Loading branch information
rafaelfranca committed Sep 20, 2021
2 parents 98fda3d + 6b7ff4f commit 6e2247e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion actionview/CHANGELOG.md
@@ -1,6 +1,11 @@
## Rails 7.0.0.alpha2 (September 15, 2021) ##

* No changes.
* Support svg unpaired tags for `tag` helper.

tag.svg { tag.use('href' => "#cool-icon") }
# => <svg><use href="#cool-icon"></svg>

*Oleksii Vasyliev*


## Rails 7.0.0.alpha1 (September 15, 2021) ##
Expand Down
5 changes: 3 additions & 2 deletions actionview/lib/action_view/helpers/tag_helper.rb
Expand Up @@ -45,7 +45,8 @@ class TagBuilder # :nodoc:
include CaptureHelper
include OutputSafetyHelper

VOID_ELEMENTS = %i(area base br col embed hr img input keygen link meta param source track wbr).to_set
HTML_VOID_ELEMENTS = %i(area base br col circle embed hr img input keygen link meta param source track wbr).to_set
SVG_VOID_ELEMENTS = %i(animate animateMotion animateTransform circle ellipse line path polygon polyline rect set stop use view).to_set

def initialize(view_context)
@view_context = view_context
Expand All @@ -66,7 +67,7 @@ def p(*arguments, **options, &block)

def tag_string(name, content = nil, escape_attributes: true, **options, &block)
content = @view_context.capture(self, &block) if block_given?
if VOID_ELEMENTS.include?(name) && content.nil?
if (HTML_VOID_ELEMENTS.include?(name) || SVG_VOID_ELEMENTS.include?(name)) && content.nil?
"<#{name.to_s.dasherize}#{tag_options(options, escape_attributes)}>".html_safe
else
content_tag_string(name.to_s.dasherize, content || "", options, escape_attributes)
Expand Down
1 change: 1 addition & 0 deletions actionview/test/template/tag_helper_test.rb
Expand Up @@ -21,6 +21,7 @@ def test_tag_builder
def test_tag_builder_void_tag
assert_equal "<br>", tag.br
assert_equal "<br class=\"some_class\">", tag.br(class: "some_class")
assert_equal "<svg><use href=\"#cool-icon\"></svg>", tag.svg { tag.use("href" => "#cool-icon") }
end

def test_tag_builder_void_tag_with_forced_content
Expand Down

0 comments on commit 6e2247e

Please sign in to comment.