Skip to content

Commit 6e2247e

Browse files
authored
Merge pull request rails#43232 from le0pard/fix_use_tag
Support svg unpaired tags in tag helper
2 parents 98fda3d + 6b7ff4f commit 6e2247e

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

actionview/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
## Rails 7.0.0.alpha2 (September 15, 2021) ##
22

3-
* No changes.
3+
* Support svg unpaired tags for `tag` helper.
4+
5+
tag.svg { tag.use('href' => "#cool-icon") }
6+
# => <svg><use href="#cool-icon"></svg>
7+
8+
*Oleksii Vasyliev*
49

510

611
## Rails 7.0.0.alpha1 (September 15, 2021) ##

actionview/lib/action_view/helpers/tag_helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class TagBuilder # :nodoc:
4545
include CaptureHelper
4646
include OutputSafetyHelper
4747

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

5051
def initialize(view_context)
5152
@view_context = view_context
@@ -66,7 +67,7 @@ def p(*arguments, **options, &block)
6667

6768
def tag_string(name, content = nil, escape_attributes: true, **options, &block)
6869
content = @view_context.capture(self, &block) if block_given?
69-
if VOID_ELEMENTS.include?(name) && content.nil?
70+
if (HTML_VOID_ELEMENTS.include?(name) || SVG_VOID_ELEMENTS.include?(name)) && content.nil?
7071
"<#{name.to_s.dasherize}#{tag_options(options, escape_attributes)}>".html_safe
7172
else
7273
content_tag_string(name.to_s.dasherize, content || "", options, escape_attributes)

actionview/test/template/tag_helper_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def test_tag_builder
2121
def test_tag_builder_void_tag
2222
assert_equal "<br>", tag.br
2323
assert_equal "<br class=\"some_class\">", tag.br(class: "some_class")
24+
assert_equal "<svg><use href=\"#cool-icon\"></svg>", tag.svg { tag.use("href" => "#cool-icon") }
2425
end
2526

2627
def test_tag_builder_void_tag_with_forced_content

0 commit comments

Comments
 (0)