Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove trailing slash from void HTML elements #51643

Open
ahukkanen opened this issue Apr 23, 2024 · 2 comments · May be fixed by #51646
Open

Remove trailing slash from void HTML elements #51643

ahukkanen opened this issue Apr 23, 2024 · 2 comments · May be fixed by #51646

Comments

@ahukkanen
Copy link

Steps to reproduce

  1. Follow https://guides.rubyonrails.org/getting_started.html up until the generation of the first view of the application
  2. Look at the page source
  3. See void elements using trailing slashes, e.g. <meta name="csrf-param" content="authenticity_token" />
  4. Copy the source of this page (using view-source)
  5. Paste the source to https://validator.w3.org/nu/#textarea
  6. See a lot of "info" level notices for trailing slashes on the void elements

Recently the W3C validator is giving a lot of the following types of "info" level notices for any markup generated by a Rails application:
W3C notices on trailing slashes for void elements

Or in text format:

Trailing slash on void elements has no effect and interacts badly with unquoted attribute values.

Expected behavior

I would expect to be able to generate a page source that would give me a clean W3C HTML validation report without any notices.

While both formats are valid, using the trailing slash is discouraged and causes a lot of noise in the validation report.

Actual behavior

I get a lot of the described notices in the W3C HTML validation report.

If there is a reason (unknown to me) to preserve the current behavior, I would make it configurable in config.action_view to revert to the old behavior.

As far as I know, this originates from these two lines at ActionView::TagHelper:

def self_closing_tag_string(name, options, escape = true, tag_suffix = " />")

"<#{name}#{tag_builder.tag_options(options, escape) if options}#{open ? ">" : " />"}".html_safe

And in addition, there are also few matches at rails-ujs.js and rails-ujs.esm.js:

let formContent = `<input name='_method' value='${method}' type='hidden' />`;
if (csrfParam$1 && csrfToken$1 && !isCrossDomain(href)) {
formContent += `<input name='${csrfParam$1}' value='${csrfToken$1}' type='hidden' />`;
}
formContent += '<input type="submit" />';

let formContent = `<input name='_method' value='${method}' type='hidden' />`;
if (csrfParam$1 && csrfToken$1 && !isCrossDomain(href)) {
formContent += `<input name='${csrfParam$1}' value='${csrfToken$1}' type='hidden' />`;
}
formContent += '<input type="submit" />';

System configuration

Rails version: pretty much any version, e.g. 7.1.3.2

Ruby version: 3.3.0

@Earlopain
Copy link
Contributor

Yes, this is something I noticed a few days ago as well. There are also a few other places where this is the case like error pages. You can utilize a regex search to find most of these cases.

rails-ujs however is deprecated, see #50535 and #50555, so I doubt this will be changed for that.

@ahukkanen ahukkanen linked a pull request Apr 23, 2024 that will close this issue
4 tasks
@ahukkanen
Copy link
Author

ahukkanen commented Apr 23, 2024

I made a PR (#51646) to solve the issue. Will look into the test results to see any unexpected implications, at least the actionview tests seemed to pass after the changes. (edit: failing tests already fixed in the PR)

Most other parts were in tests and I also found one actual place where the trailing slashes are needed (elements inside SVGs) but these seemed to be covered by the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants