Skip to content

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

@mpdude

Description

@mpdude

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions