Repository navigation
Expand file tree
/
Copy path15.HTML5 Parsing.htm
More file actions
128 lines (128 loc) · 6.22 KB
/
Copy path15.HTML5 Parsing.htm
File metadata and controls
128 lines (128 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<html>
<head>
<title>HTML5 Parsing</title>
<link rel="Stylesheet" href="StyleSheet" />
</head>
<body>
<h1>HTML5 Parsing
</h1>
<blockquote>
<p>
The html is parsed using a real <b>HTML5 tokenizer</b> (from the
<a href="https://github.com/jstedfast/HtmlKit">HtmlKit</a> library) instead of a hand-rolled
scanner, so the pages below - all of which are <i>intentionally malformed</i> - render
correctly instead of breaking the layout. Every example shows the raw (broken) html
source followed by the actual <b>live rendering</b> of that exact same markup.
</p>
<hr />
<h2>Optional / omitted end tags
</h2>
<p>
Real world html is full of tags whose closing tag was never written. The parser now
follows the <a href="https://html.spec.whatwg.org/dev/dom.html#concept-element-tag-omission">
HTML5 end-tag omission rules</a> and automatically closes them at the right
place, instead of nesting everything into one deep, broken tree.
</p>
<div class="comment">Source:</div>
<pre><p>First paragraph, never closed
<p>Second paragraph, never closed
<p>Third paragraph, never closed</pre>
<div class="comment">Live result:</div>
<div class="example">
<p>First paragraph, never closed
<p>Second paragraph, never closed
<p>Third paragraph, never closed
</div>
<hr />
<h2>Tables with missing <tr>/<td> end tags
</h2>
<p>
The same omission rules apply to table rows and cells, so a table written without a
single closing <code></tr></code> or <code></td></code> still comes out as
a proper grid.
</p>
<div class="comment">Source:</div>
<pre><table border="1" cellpadding="4">
<tr><td>A<td>B
<tr><td>C<td>D
</table></pre>
<div class="comment">Live result:</div>
<div class="example">
<table border="1" cellpadding="4">
<tr><td>A<td>B
<tr><td>C<td>D
</table>
</div>
<hr />
<h2>Anonymous table boxes
</h2>
<p>
Per the <a href="https://www.w3.org/TR/CSS2/tables.html#anonymous-boxes">CSS 2.1 anonymous
table object rules</a>, a stray <code><td></code> with no <code><tr></code>
(or even no <code><table></code>) around it is automatically wrapped in the missing
row/table boxes, rather than producing a broken or invisible layout.
</p>
<div class="comment">Source:</div>
<pre><table border="1"><td>Anonymous row generated around me</td></table></pre>
<div class="comment">Live result:</div>
<div class="example">
<table border="1"><td>Anonymous row generated around me</td></table>
</div>
<hr />
<h2>Comments containing ">"
</h2>
<p>
A real tokenizer knows where a comment actually ends, so a stray <code>></code> character
inside a comment no longer truncates it early or corrupts the rest of the page.
</p>
<div class="comment">Source:</div>
<pre><!-- a comment that contains a > character right here -->
<p>This paragraph renders normally right after it.</p></pre>
<div class="comment">Live result:</div>
<div class="example">
<!-- a comment that contains a > character right here -->
<p>This paragraph renders normally right after it.</p>
</div>
<hr />
<h2><script> content is not mistaken for markup
</h2>
<p>
<code><script></code> content is tokenized as raw text (this renderer does not execute
script, so the code itself is not shown), which means <code><</code> and <code>></code>
characters used for comparisons inside the script can no longer be misread as the start
of a nested tag and swallow the rest of the document.
</p>
<div class="comment">Source:</div>
<pre><script>
if (1 < 2 && 3 > 2) {
runDemo('<b>looks like a tag</b>, but is just a string');
}
</script>
<p>This paragraph still renders correctly right after the script block.</p></pre>
<div class="comment">Live result:</div>
<div class="example">
<script>
if (1 < 2 && 3 > 2) {
runDemo('<b>looks like a tag</b>, but is just a string');
}
</script>
<p>This paragraph still renders correctly right after the script block.</p>
</div>
<hr />
<h2><noscript> content is parsed as real markup
</h2>
<p>
Per the HTML5 tokenizer spec, <code><noscript></code> content is normally tokenized
as raw text too. Since this renderer never runs script, its <code><noscript></code>
content is specifically re-parsed as nested html, so tags placed inside it render as real
elements instead of showing up as literal text.
</p>
<div class="comment">Source:</div>
<pre><noscript><p>This is <b>real, nested markup</b> inside noscript.</p></noscript></pre>
<div class="comment">Live result:</div>
<div class="example">
<noscript><p>This is <b>real, nested markup</b> inside noscript.</p></noscript>
</div>
</blockquote>
</body>
</html>