Commit Graph

3109 Commits

Author SHA1 Message Date
Akhil G Krishnan
1677e31240
Fix: simple_format with blank wrapper_tag option returns plain html tag.
By default `simple_format` method returns the text wrapped with `<p>`. But if we explicitly specify
the `wrapper_tag: nil` in the options, it returns the text wrapped with `<></>` tag.

Before:
```ruby
 simple_format("Hello World", {},  { wrapper_tag: nil })
 # <>Hello World</>
```

After:
```ruby
 simple_format("Hello World", {},  { wrapper_tag: nil })
 # <p>Hello World</p>
```

Co-authored-by: Junichi Ito <jit@sonicgarden.jp>
2023-09-05 10:09:40 +00:00
Gannon McGibbon
a7cc807cb9 Fix to_param parameter generation for partial composite keys
Adds tests for url_for use with composite primary key models. Fixes bug
related to new CPK model to_param generation.
2023-08-29 14:04:03 -05:00
Gannon McGibbon
7ea19698b0 Fix form_for id generation for new CPK models
Adds tests for form_for use with composite primary key models. Fixes bug
related to new CPK model dom id generation.
2023-08-28 15:56:25 -05:00
Oscar Romero
9db860f1b4
Document the :ignore_date option on time_select (#49017)
* Document the :ignore_date option on time_select

* split documentation comment into two lines

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
2023-08-23 15:41:53 -04:00
Rafael Mendonça França
ed5af00459
Merge pull request #48998 from Shopify/to_key-supports-composite-primary-key
Support composite identifiers in `to_key`
2023-08-22 13:53:19 -04:00
Nikita Vasilevsky
8a5cf4cf44
Support composite identifiers in to_key
This commit adds support for composite identifiers in `to_key`.
Rails 7.1 adds support for composite primary key which means that
composite primary key models' `#id` method returns an `Array` and
`to_key` needs to avoid double-wrapping the value.
2023-08-22 16:13:23 +00:00
Jean Boussier
40d761c3e1
Merge pull request #48987 from Austio/better-dom-id-error-on-nil
Adds a specific ArgumentError when passing nil to dom_id.
2023-08-22 17:19:45 +02:00
Austin Story
6245c0048a Adds a specific ArgumentError when passing nil to dom_id. Which can happen if you do something like pass a non-existent ivar dom_id(@something_non_existant)
Before this would raise: `NoMethodError: undefined method `to_key' for nil:NilClass`
After it raises `ArumentError: dom_id must be passed a record_or_class as the first parameter, you passed 'nil'`
2023-08-22 17:16:25 +02:00
julianfssen
6a798d079a Add docs for data-turbo-method and data-turbo-confirm for link_to
Rails 7 ships with Turbo enabled by default. Instead of using
`data-method` and `data-confirm`, Turbo now uses `data-turbo-method` and
`data-turbo-confirm` to perform a link visit with the specified HTTP
method and show confirmation dialog respective.

The deprecated legacy options are documented but the new options are
not.

This commit documents the `data-turbo-method` and `data-turbo-confirm`
for the `link_to` method.

The `button_to` documentation has also been updated to reference the
new `link_to` options.
2023-08-16 19:09:17 +08:00
Jean Boussier
770060d93a Handle non-string partial body in ActionView::CollectionCaching
Followup: https://github.com/rails/rails/pull/48645

Some template engines such as `jbuilder` use these Action View primitives
with types other than strings, which breaks a bunch of assumptions.

I wish I could add a test for this, but this is deep in private methods
I don't see a way to cover this.
2023-08-14 11:20:43 +02:00
Mike Dalessio
8a57ba8c0e
Update Action View and Dispatch to use Rails::Dom::Testing helpers
Use the helpers introduced in rails-dom-testing 2.2.0 instead of
managing the HTML parsers as was done in #48523.

See also related #47144 / ad79ed0e
2023-08-03 11:17:38 -04:00
John Hawthorn
6a5efd9777
Merge pull request #48669 from jhawthorn/faster_json_escape
Improve performance of JSON HTML entity escaping
2023-07-31 14:15:44 -07:00
Mike Dalessio
d316d690b9
Fix Action View sanitizer initialization
Using `initializer` in the railtie ran the block before application
initializers. Instead, use `after_initialize` to ensure that
application initializers take effect properly.
2023-07-17 11:20:40 -04:00
Petrik de Heus
0164f1fe04
Merge pull request #48737 from mikepmunroe/mm-template-remove-autolink
Remove RDoc auto-link from Template text
2023-07-15 10:25:10 +02:00
Stefanni Brasil
13d8bc10af Add test for nested content_tags with data attributes outside of erb
The existing content_tag tests document how to use content_tag outside
of erb views with data attributes. But for those who need to use
content_tag with nested content_tags and data attributes outside of
erb views, it can be challenging to find out how to do that.

When I had to do this in a project, it took me longer than I wanted.
Hopefully adding this test with an explicit example of how to achieve that
will others some time.
2023-07-14 15:57:23 -06:00
Mike Munroe
1304d39053 Remove RDoc auto-link from Template text 2023-07-14 10:18:31 -04:00
Guillermo Iguaran
a5fc471b3f
Merge pull request #48667 from igor-drozdov/fix-ruby-keywords-for-action-view-test-case
Add ruby2_keywords to ActionView::TestCase#method_missing
2023-07-06 14:24:49 -07:00
Guillermo Iguaran
d540abd31b
Add parentheses to ruby2_keywords method call
For consistency with all the other usages of the method in the code base.
2023-07-05 23:35:23 -07:00
John Hawthorn
ebe0c4066b Improve performance of JSON HTML entity escaping
Running gsub! 5 times with string arguments seems to be faster than
running it once with a regex and Hash.

When there are matches to the regex (there are characters to escape)
this is faster in part because CRuby will allocate a new match object
and string as a key to lookup in the map hash provided. It's possible
that could be optimized upstream, but at the moment this avoids those
allocations.

Surprisingly (at least to me) this is still much faster when there is no
replacement needed: in my test ~3x faster on a short ~200 byte string,
and ~5x faster on a pre-escaped ~600k twitter.json.
2023-07-05 12:45:31 -07:00
Igor Drozdov
79cdc0c8f2
Add ruby2_keywords to ActionView::TestCase#method_missing
For example RSpec predicate methods rely on method_missing.
Without ruby2_keywords, matchers like be_something(kwarg: 1)
don't work
2023-07-05 19:21:12 +02:00
Jean Boussier
76deaf6362
Merge pull request #48645 from Shopify/action-view-bare-cache-fragments
Fix Action View collection caching to store fragments as bare strings
2023-07-05 12:14:23 +02:00
Jean Boussier
6842e766a1 Fix Action View collection caching to store fragments as bare strings
Ref: https://github.com/rails/rails/issues/48611

Individual fragments have been cached as bare string since forever,
but somehow fragment cached via collection caching were stored
as `ActionView::OutputBuffer` instances.

This is both bad for performance, but also can cause issues on
Rails upgrades if the internal representation of `AV::OutputBuffer`
changes.
2023-07-04 16:37:22 +02:00
Ricardo Trindade
adf80fe807
Update streaming_template_renderer.rb
Remove extra `layout_name` check. At that point in the time layout_time will be defined so there's no need to check for it once again.
2023-07-04 15:21:51 +02:00
Akira Matsuda
bceac79565
Merge branch 'main' into action-view-nested-field-name-calls 2023-07-03 06:15:43 +09:00
Guillermo Iguaran
8a158ed5e3
Merge pull request #48553 from ghiculescu/preload_links_header-invocation
Allow opting in/out of `Link preload` headers when calling `stylesheet_link_tag` or `javascript_include_tag`
2023-06-30 13:40:03 -07:00
Sean Doyle
5cb8678dd6 Don't double-encode nested field_id and field_name index
Closes https://github.com/rails/rails/issues/45483
Closes https://github.com/rails/rails/pull/45523

To quote #45483:

> `field_name` is adding an extra index parameter, the extra `[0]` in
> `parent[children_attributes][0][0][grandchildren_attributes][]`

To resolve that issue, this commit reads its default `field_id` and
`field_name` method's `index:` option directly from the `@options`.
Prior to this commit, that value was read from the `@index` instance
variable.
2023-06-30 09:54:23 -04:00
Rafael Mendonça França
ba4169668b
Don't require an autoloaded constant 2023-06-26 04:18:47 +00:00
Rafael Mendonça França
847a27a3cb
Don't eager load SelectRenderer
This module is included in classes that are already eager loaded, so
we don't need to eager load it.
2023-06-26 04:18:10 +00:00
Rafael Mendonça França
51ec30a88d
Merge pull request #48574 from pinzonjulian/simplify_action_view_bases_helper_logic
Extract Select rendering methods into SelectRenderer module and simplify Action View base helper logic
2023-06-26 00:17:03 -04:00
pinzonjulian
77f3fc118a Move Form Options helper into select rendering classes
The FormOptionsHelper is only needed in classes where a <select> tag is rendered. It was previously included in the Tags::Base class which unnecessarily included it every child Tag class.
2023-06-26 09:39:37 +10:00
pinzonjulian
f19f426c6a Simplify Tags::Base private method logic 2023-06-25 22:44:27 +10:00
pinzonjulian
9e85c2cd05 Extract Select rendering methods into SelectRenderer module
3 methods that originally lived in the Tags::Base class are only used by tags that output a <select> tag. These were extracted into a SelectRenderer module which is now only added to the tags that need it.
2023-06-25 22:44:27 +10:00
zzak
dd89f600f7
🔗 Remove RDoc auto-link from Rails module everywhere 2023-06-23 10:49:30 +09:00
Alex
5b116c5216 Allow opting in/out of Link preload headers when calling stylesheet_link_tag or javascript_include_tag
Fixes https://github.com/rails/rails/issues/48517

```ruby
javascript_include_tag("http://example.com/all.js", preload_links_header: false)

stylesheet_link_tag("http://example.com/all.js", preload_links_header: true)
```
2023-06-22 12:15:22 +10:00
Mike Dalessio
2362f57cd8
Update Action View and Action Dispatch to use HTML5 when available 2023-06-19 16:34:33 -04:00
John Hawthorn
55c3066da3
Merge pull request #48414 from jhawthorn/faster_block_expr
Speed up ERB expression BLOCK_EXPR regex
2023-06-07 09:20:20 -07:00
Jean Boussier
facc260fbb Stop appending preload links once the head is already 1kB
Fix: https://github.com/rails/rails/issues/48402

Some HTTP client and proxies have a 4kiB header limit, but more importantly
including preload links has diminishing returns so it's best to not go overboard

1kB worth of preload links should be plenty.
2023-06-07 11:18:45 +02:00
John Hawthorn
d8e11e9100 Speed up ERB expression BLOCK_EXPR regex
I noticed that during our application's boot we were spending more time
than desirable matching against this BLOCK_EXPR regex. This was slower
than necessary because even though this regex engine only cares about
the end of the string, Ruby's regex engine only works forward, and this
regex would match whitespace anywhere inside the string using two
ambiguous adjacent whitespace repetitions.

We don't expect untrusted code here (this is Ruby in our ERB which we
are going to execute), but with realistic strings this still causes
unnecessary backtracking.

This commit fixes the problem by removing the initial "\s*" (since this
is optional we don't need to capture it, we can just find the match
later if it exists) and replacing the following "\s+" with just "\s"
(similarly we don't care how many whitespace appear in a row, we can
just match later if there is more than one).

Testing against all the ERB expressions in our app this is 10x faster:
from 1.5 seconds to 0.1 seconds.
2023-06-06 16:26:40 -07:00
Rafael Mendonça França
fdb7412fce
Fix test to keep behavior the same after #48397
This test was failing because the parameter `a` in the accept header
is now being correctly extracted as a mime type.

This test was expecting that Rails would extract it an an empty accept
header, that will only happen now if the `q` parameter that has special
meaning in used.
2023-06-05 19:51:27 +00:00
Guillermo Iguaran
f618fa4bfd Fix actionview test to work regardless of capitalization of missing translation message 2023-06-03 18:51:55 -07:00
Jean Boussier
a2f685c7c0 Cleanly fallback when failing to tokenize ERB templates
Fix: https://github.com/rails/rails/issues/48319
Followup: https://github.com/rails/rails/pull/48184
2023-06-01 11:35:31 +02:00
Andriichuk
ef7449c9fe Handle sanitize_options in simple_format helper 2023-05-31 21:11:40 +01:00
Rafael Mendonça França
54de0cb2ee
Merge pull request #48293 from flavorjones/flavorjones-support-html5-sanitizer
Update Action View to use HTML5 standards-compliant sanitizers
2023-05-30 16:00:13 -04:00
zzak
722d4f6582
Unlink Rails constant from READMEs 2023-05-29 15:14:44 +09:00
Mike Dalessio
ce43ac6088
Add support for HTML5 sanitizers
* new config value: action_view.sanitizer_vendor
* SanitizerHelper defaults to Rails::HTML4::Sanitizer
* 7.1 config defaults to Rails::HTML5::Sanitizer if it's supported
2023-05-28 14:00:35 -04:00
Mike Dalessio
500ccaaeea
Update dependency on rails-html-sanitizer to ~>1.6
for HTML5 sanitizer support
2023-05-28 14:00:35 -04:00
Mike Dalessio
8000f4e633
Overhaul SanitizerHelper tests
to avoid testing specific sanitizations and instead:

- unit test SanitizeHelper using a mock vendor and mock sanitizers
- integration test support vendors

Note that we only have one supported vendor today --
Rails::Html::Sanitizer -- but this commit helps prepare the codebase
so we can add another, Rails::HTML5::Sanitizer.

The upstream vendor, rails-html-sanitizer, is adequately testing the
sanitization behavior expected by Rails and so this commit removes all
but the most basic expectations for each sanitizer type.
2023-05-28 14:00:27 -04:00
Mike Dalessio
18dd733c01
Add sanitizer_vendor accessor to ActionView SanitizeHelper 2023-05-28 13:54:32 -04:00
Mike Dalessio
ae8afc821c
Remove tests asserting on allowed tags and attributes
Equivalent tests exist upstream in rails-html-sanitizer, and leaving
these here mean our tests are coupled to r-h-s and are liable to break
as the allowlists evolve.
2023-05-26 19:55:57 -04:00
zzak
073269c9b9
Merge pull request #48288 from zzak/rdoc-fixed-width-namespaces
Use short-form fixed-width RDoc form for namespaces
2023-05-25 07:14:47 +09:00