10000 [Form] Use self-closing `<input />` tags again · Issue #53649 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Use self-closing <input /> tags again #53649

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

Closed
mpdude opened this issue Jan 26, 2024 · 10 comments · Fixed by #53656
Closed

[Form] Use self-closing <input /> tags again #53649

mpdude opened this issue Jan 26, 2024 · 10 comments · Fixed by #53656
Labels

Comments

@mpdude
Copy link
Contributor
mpdude commented Jan 26, 2024

Description

#47715 removed self-closing slashes from <input> elements in the various form themes, i.e. it changed <input ... /> to <input ...>.

My request is to revert this change, or at least to make it configurable (either way opt-in/opt-out/opt-back-in).

The reasons given in #47715 were:

Switching form template from XHTML to HTML5 syntax.
https://validator.w3.org/nu/ says:

Warning: Self-closing tag syntax in text/html documents is widely discouraged; it’s unnecessary and interacts badly with other HTML features (e.g., unquoted attribute values). If you’re using a tool that injects self-closing tag syntax into all void elements, prettier/prettier#5246, then consider switching to a different tool.

The "widely discouraged" reference to https://google.github.io/styleguide/htmlcssguide.html#HTML_Style_Rules does, as of today, state nothing about self-closing tags anymore. And, as of today, the W3 validator merely writes:

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

That's a far less opinionated statement.

The referenced document at https://github.com/validator/validator/wiki/Markup-%C2%BB-Void-elements#trailing-slashes-in-void-element-start-tags-do-not-mark-the-start-tags-as-self-closing goes on nit-picking that that for "void elements" (like <hr> or <input>), the trailing slash does not mark the start tag as self-closing, but instead...

the standard states that you can use a trailing slash in a void-element start tag for literally any purpose except to mark the start tag as self-closing.
So, for example, the following are all acceptable reasons for using a trailing slash in a void-element start tag —
[...]
✅ I use a trailing slash because I run my HTML markup through a formatting tool that’s hardcoded to add trailing slashes to all void-element start tags, without any option for me to prevent the tool from doing that.

The other argument against the slash is a possible mis-interpretation when using unquoted attribute values. That, however, is not an issue for the case we're discussing here (the form themes all using quoted attribute notation).

This now brings me to my reason why I'd like to keep the <input /> style for now:

When you want to use the PHP DOM extension for parsing HTML5, doing DOM transformations and/or using XPath, you can do so under the requisite of writing Polyglot HTML 5. Basically, this describes a set of extra rules to follow to write markup in a way that it has the same meaning to an HTML 5 and an XML parser. PHP DOM functions are based on libxml2, an XML parser.

Writing Polyglot HTML 5 allows to use PHP DOM extension functions to load, inspect, modify and dump HTML 5 documents. Polyglot HTML 5 requires self-closing tags to be used for void elements.

Removing the self-closing trailing slash as in #47715 takes this possibility from users, unless they completely override the form theme or do quirky string-replacements on the content. This is exactly what was made to get the tests green at https://github.com/symfony/symfony/pull/47715/files#r1280400076 - run a preg_replace to use the DOM functions afterwards.

The / may be superfluous and annyoing for purists, but it is officially OK and valid. It allows to keep the PHP DOM use case working.

Some more words on libxml2

The libxml2 library powering the PHP DOM functions has been written 20 years ago, long before the advent of HTML 5. It has no HTML 5 support, and that's probably never going to change. To my knowledge, there is no established, proven, fast alternative available as of today.

There is https://github.com/Masterminds/html5-php, but as a parser written in PHP it cannot compare with the C efficiency of libxml2.

All the problems surrounding libxml2 and its use on HTML 5 are neatly summarized in https://wiki.php.net/rfc/domdocument_html5_parser. That RFC lead to what seems to be HTML 5 support in the DOM extension – but the implementation has been merged into PHP only two months ago.

The comment over at the disputed PR that points to ...loadHTML() is a trap. loadHTML in libxml2 implements logic that tries to recover non-well formed HTML 4 – a technology from the last millenium only the most seasoned among us might remember – into XHTML 1.0. This is not a sane thing to run on HTML 5 input, and the php.net documentation features a red warning box about this.

Example

No response

@carsonbot carsonbot added the Form label Jan 26, 2024
@mpdude
Copy link
Contributor Author
mpdude commented Jan 26, 2024

Inviting @ThomasLandauer as the author of #47715 to the discussion. I'd like to learn more about your use cases and why it was important to you to change this?

@mpdude mpdude changed the title [Form] Add self-closing slash to <input> again [Form] Use self-closing <input /> tags again Jan 26, 2024
@mmarton
Copy link
mmarton commented Jan 27, 2024

sidenote: php8.4 will come with proper html5 support
https://wiki.php.net/rfc/domdocument_html5_parser

@derrabus
Copy link
Member

@mpdude Would the HTML5 parser that will be shipped with PHP 8.4 solve your problem?

@mpdude
Copy link
Contributor Author
mpdude commented Jan 27, 2024

I haven't yet had the opportunity to try it. But after all I read about it and why it was written in the first place, I assume yes.

@mpdude
Copy link
Contributor Author
mpdude commented Jan 27, 2024

Would it be an option to revert the change and postpone it to a Symfony version that requires PHP 8.4?

Could this be reverted as a bug fix, or in a feature release only (since it was added in feature release 6.4 as well)?

@ThomasLandauer
Copy link
Contributor

I opened that PR just because /> looked like a forgotten leftover from the old days of XHTML to me. I posted the validator link cause I had it at hand - not cause it's the ultimate authority in this question.

@mpdude
Copy link
Contributor Author
mpdude commented Jan 27, 2024

What would be an acceptable target branch for a PR that reverts the change?

@fabpot
Copy link
Member
fabpot commented Jan 27, 2024
8000

What would be an acceptable target branch for a PR that reverts the change?

6.4 I suppose

@mpdude
Copy link
Contributor Author
mpdude commented Jan 28, 2024

PR is at #53656

nicolas-grekas added a commit that referenced this issue Jan 29, 2024
…#47715 (mpdude)

This PR was merged into the 6.4 branch.

Discussion
----------

[Form] Use self-closing `<input />` syntax again, reverting #47715

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #53649
| License       | MIT

This PR reverts #47715 and puts the trailing slash back into `<input .../>` markup in the form themes.

The reasons are outlined in detail in #53649. Basically, the trailing slash in void elements like `<input>` is not required in HTML 5, yet it is perfectly valid and compliant with specs.

Writing markup that way is required to parse and process it with an XML parser, like `libxml2` underlying all of the PHP DOM, XML etc. extensions.

HTML 5 capable parser support has been added to PHP in php/php-src#12111 and will be available with PHP 8.4. So, we might want to remove the trailing slashes once Symfony requires at least PHP 8.4.

Commits
-------

8c92200 Revert #47715
@mpdude
Copy link
Contributor Author
mpdude commented Jan 29, 2024

Fixed by #53656

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

Successfully merging a pull request may close this issue.

6 participants
0