Skip to content

Correctly parse HTML5 void elements #141

Description

@nidico

(I'm unsure whether parsing "normal HTML" - and not only "custom HTML tags" - is in scope of the library. As a user I expected to be able to parse "normal HTML" as it is, so I opened this issue; if it's not in scope, feel free to close!)

HTML5 elements are not necessarily valid XML, e.g.

  • HTML void elements such as <br> must not have a closing tag (here: </br>).
  • Above MDN document even states that - in general, "Self-closing tags (<tag />) do not exist in HTML." - but they can be added to void elements in order to be XHTML compliant. Something like <p /> isn't valid HTML5 though!

In contrast, HtmlParser requires all "HTML" to be valid XML.

In practice this means that:

  • Void elements currently require a closing tag (e.g. <br><br />) or a self-closing tag (<br /), just (<br>) doesn't work.
  • Non-void elements are currently allowed to be self-closing (e.g. <p />) despite this being non-valid.

My pragmatic suggestion would be to

  • fix the former, i.e. assume void elements always self-close -> treat <br> as <br />
  • and tolerate the latter, as <p /> is still valid XHTML and nobody would write this "by accident" anyway -> leave it as it is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions