Merge pull request #44580 from davekaro/fix_svg_elements

Ensure SVG elements are closed.
This commit is contained in:
Rafael Mendonça França 2022-02-28 18:58:36 -05:00 committed by GitHub
commit 40a910c9e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 4 deletions

View File

@ -46,7 +46,7 @@ module ActionView
include OutputSafetyHelper
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
SVG_SELF_CLOSING_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
@ -67,8 +67,9 @@ module ActionView
def tag_string(name, content = nil, escape_attributes: true, **options, &block)
content = @view_context.capture(self, &block) if block_given?
if (HTML_VOID_ELEMENTS.include?(name) || SVG_VOID_ELEMENTS.include?(name)) && content.nil?
"<#{name.to_s.dasherize}#{tag_options(options, escape_attributes)}>".html_safe
self_closing = SVG_SELF_CLOSING_ELEMENTS.include?(name)
if (HTML_VOID_ELEMENTS.include?(name) || self_closing) && content.nil?
"<#{name.to_s.dasherize}#{tag_options(options, escape_attributes)}#{self_closing ? " />" : ">"}".html_safe
else
content_tag_string(name.to_s.dasherize, content || "", options, escape_attributes)
end

View File

@ -21,7 +21,7 @@ class TagHelperTest < ActionView::TestCase
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") }
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