by irrational · 4y ago · 0 comments
Furthermore, you need to know about this rule even if you do write `</p>`. If you write this:
<p>
<div>hello</div>
</p>
it is invalid HTML. This is because the paragraph is auto-closed, and then your code is equivalent to this: <p>
</p><div>hello</div>
</p>
resulting in an error because of the extra `</p>` after the `</div>`.They will indeed auto close the (first) paragraph, and also auto open a new <p> element because of the extra closing </p> tag.
Try it out by typing this in your address bar and open the inspector:
data:text/html;charset=utf-8,<!DOCTYPE html><p><div>hello</div></p>
The generated HTML (document.body.parentNode.innerHTML): <head></head><body><p></p><div>hello</div><p></p></body>
Browsers will go out of their way to produce a (valid) DOM from pretty much any string (as per the HTML5 spec). (Almost?) nothing is a syntax error. You won't get a HTML5-compliant browser to show a parse error to the user in HTML (of course, this is not the case in XHTML).edit: sorry niconii, I edited my comment under your feet.
For example, try running the following document through the W3C's HTML validator[1]:
<!DOCTYPE html>
<html lang=en>
<title>Test Document</title>
<p>
<div></div>
</p>
The HTML spec contains a list of all possible parse errors[2].[1] https://validator.w3.org/nu/#textarea
[2] https://html.spec.whatwg.org/multipage/parsing.html#parse-er...
Citing from [1]:
Earlier [SGML] HTML DTDs included the following definition for flow capturing this in a rather straightforward way:
<!ENTITY % flow "%block; | %inline;">
While HTML5 lacks it, a definition for "block content" rises again by subtracting those elements from the "flow content" category that aren't also in the "phrasing content" (inline) category.Nesting of phrasing into flow elements is about the most basic property of the HTML grammar. In SGML, the flow/phrasing hierarchy is expressed by declaring the content model of flow elements as allowing %phrasing;, where %phrasing;, like in earlier HTML DTDs, is substituted into a string such as a|abbr|... containing all phrasing elements as a name group.
For example, the element declaration for the p element is as follows:
<!ELEMENT p - O
(#PCDATA|%phrasing;)*
-(%flow_only;)>
meaning that- the content of p elements can be any sequence of text content (#PCDATA) and phrasing elements, and
- flow content isn't just forbidden in direct child content of p (via admitting %phrasing; elements only), but also isn't admitted anywhere in descendant content of p, as expressed by the -(%flow_only;) SGML exclusion exception, and
- the end-element, but not the start-element tag of p can be omitted, as declared in ps tag omission indicator - O
where the flow_only parameter entity contains block-level elements as described above, ie. flow elements that aren't also phrasing elements.