1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Ensure tag.with_options({}).p builds a <p>

Prior to this change, the following call raises:

```ruby
with_options(id: "with-options") { |t| t.p "content" }

```

The `ActionView::Helpers::TagHelper::TagBuilder` implementation relies
on `method_missing` to dispatch calls to `tag_string` where the missing
method name is the resulting element's tagName. Unfortunately,
[`Kernel#p` already exists][Kernel#p] and is invoked before
`method_missing` can intervene.

This commit rectifies this by declaring `TagBuilder#p` and overriding
the existent `#p` instance method.

[Kernel#p]: https://ruby-doc.org/core-2.7.2/Kernel.html#method-i-p
This commit is contained in:
Sean Doyle 2020-10-05 18:27:55 -04:00
parent 04dcf56cec
commit af6f4be040
2 changed files with 10 additions and 0 deletions

View file

@ -47,6 +47,10 @@ module ActionView
@view_context = view_context
end
def p(*arguments, **options, &block)
tag_string(:p, *arguments, **options, &block)
end
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?

View file

@ -97,6 +97,12 @@ class TagHelperTest < ActionView::TestCase
tag.p(disabled: true, itemscope: true, multiple: true, readonly: true, allowfullscreen: true, seamless: true, typemustmatch: true, sortable: true, default: true, inert: true, truespeed: true, allowpaymentrequest: true, nomodule: true, playsinline: true)
end
def test_tag_builder_with_options_builds_p_elements
html = tag.with_options(id: "with-options") { |t| t.p("content") }
assert_dom_equal '<p id="with-options">content</p>', html
end
def test_tag_builder_do_not_modify_html_safe_options
html_safe_str = '"'.html_safe
assert_equal "<p value=\"&quot;\" />", tag("p", value: html_safe_str)