Skip to content

Commit 40a910c

Browse files
authored
Merge pull request rails#44580 from davekaro/fix_svg_elements
Ensure SVG elements are closed.
2 parents 0a5839f + 3814826 commit 40a910c

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

actionview/lib/action_view/helpers/tag_helper.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class TagBuilder # :nodoc:
4646
include OutputSafetyHelper
4747

4848
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
49+
SVG_SELF_CLOSING_ELEMENTS = %i(animate animateMotion animateTransform circle ellipse line path polygon polyline rect set stop use view).to_set
5050

5151
def initialize(view_context)
5252
@view_context = view_context
@@ -67,8 +67,9 @@ def p(*arguments, **options, &block)
6767

6868
def tag_string(name, content = nil, escape_attributes: true, **options, &block)
6969
content = @view_context.capture(self, &block) if block_given?
70-
if (HTML_VOID_ELEMENTS.include?(name) || SVG_VOID_ELEMENTS.include?(name)) && content.nil?
71-
"<#{name.to_s.dasherize}#{tag_options(options, escape_attributes)}>".html_safe
70+
self_closing = SVG_SELF_CLOSING_ELEMENTS.include?(name)
71+
if (HTML_VOID_ELEMENTS.include?(name) || self_closing) && content.nil?
72+
"<#{name.to_s.dasherize}#{tag_options(options, escape_attributes)}#{self_closing ? " />" : ">"}".html_safe
7273
else
7374
content_tag_string(name.to_s.dasherize, content || "", options, escape_attributes)
7475
end

actionview/test/template/tag_helper_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +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") }
24+
assert_equal "<svg><use href=\"#cool-icon\" /></svg>", tag.svg { tag.use("href" => "#cool-icon") }
2525
end
2626

2727
def test_tag_builder_void_tag_with_forced_content

0 commit comments

Comments
 (0)