-
Notifications
You must be signed in to change notification settings - Fork 112
/
Html5Printer.java
289 lines (275 loc) · 9.19 KB
/
Html5Printer.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2017 The Closure Rules Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.jsoup.nodes;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
/**
* HTML5 document serializer.
*
* <p>This printer prints HTML documents in HTML5 style, where optional tags are omitted. This
* printer is particularly suitable to the printing of HTML documents that have additional documents
* inlined within them, which is the case with HTML imports.
*
* <h3>Implementation Notes</h3>
*
* <p>The rules implemented by this class are defined by the <a
* href="https://www.w3.org/TR/html5/syntax.html#optional-tags">W3C HTML5 Specification</a>.
*
* <p>This class is sealed within the jsoup package as a stopgap until we can upstream a patch that
* makes {@link Node#outerHtmlHead(Appendable, int, org.jsoup.nodes.Document.OutputSettings)} and
* {@link Node#outerHtmlTail(Appendable, int, org.jsoup.nodes.Document.OutputSettings)} publicly
* visible. Otherwise it would not be possible for us to efficiently serialize HTML documents
* without rewriting this functionality ourselves.
*/
public final class Html5Printer {
private static final ImmutableSet<String> CHILDREN_THAT_MAKE_BODY_OPEN_MANDATORY =
ImmutableSet.of("meta", "link", "script", "style", "template");
private static final ImmutableSet<String> SIBLINGS_THAT_MAKE_P_CLOSE_OPTIONAL =
ImmutableSet.of(
"address",
"article",
"aside",
"blockquote",
"div",
"dl",
"fieldset",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"hgroup",
"hr",
"main",
"nav",
"ol",
"p",
"pre",
"section",
"table",
"ul");
/** Turns {@code document} into HTML text. */
public static String stringify(Document document) throws IOException {
StringBuilder result = new StringBuilder(2048);
Html5Printer printer = new Html5Printer(result, document.getOutputSettings());
printer.append(document);
return result.toString();
}
private final Appendable output;
private final Document.OutputSettings settings;
private boolean omittedTableSectionEnd;
private Node currentNode;
private boolean hadHead;
private boolean hadHeadClose;
public Html5Printer(Appendable output, Document.OutputSettings settings) throws IOException {
this.output = checkNotNull(output, "output");
this.settings = checkNotNull(settings, "settings");
output.append("<!doctype html>");
}
/** Stringifies {@code root} recursively to output state. */
public void append(Node root) throws IOException {
currentNode = root;
int depth = 0;
while (currentNode != null) {
emitOpeningTagIfRequiredByHtml5(depth);
if (currentNode.childNodeSize() > 0) {
currentNode = currentNode.childNode(0);
depth++;
} else {
while (currentNode.nextSibling() == null && depth > 0) {
emitClosingTagIfRequiredByHtml5(depth);
currentNode = currentNode.parentNode();
depth--;
}
emitClosingTagIfRequiredByHtml5(depth);
if (currentNode.equals(root)) {
break;
}
currentNode = currentNode.nextSibling();
}
}
}
private void emitOpeningTagIfRequiredByHtml5(int depth) throws IOException {
if (isDocumentOrDoctype()) {
return;
}
if (currentNode instanceof Element
&& currentNode.attributes().size() == 0
&& currentNode.attributes().dataset().isEmpty()) {
switch (currentNode.nodeName()) {
case "html":
if (currentNode.childNodeSize() > 0 && !(currentNode.childNode(0) instanceof Comment)) {
return;
}
break;
case "head":
if (hadHead) {
return;
}
hadHead = true;
if (currentNode.childNodeSize() == 0 || currentNode.childNode(0) instanceof Element) {
return;
}
break;
case "body":
if (currentNode.childNodeSize() == 0) {
return;
}
Node firstChild = currentNode.childNode(0);
if (firstChild instanceof Comment
|| CHILDREN_THAT_MAKE_BODY_OPEN_MANDATORY.contains(firstChild.nodeName())
|| startsWithWhitespace(firstChild)) {
break;
}
return;
case "tbody":
if (currentNode.childNodeSize() > 0
&& currentNode.childNode(0).nodeName().equals("tr")
&& !omittedTableSectionEnd) {
return;
}
break;
default:
// ignored
}
}
// This method is package-private. We intend to beseech jsoup's author to make it public.
currentNode.outerHtmlHead(output, depth, settings);
}
private void emitClosingTagIfRequiredByHtml5(int depth) throws IOException {
omittedTableSectionEnd = false;
if (isDocumentOrDoctype()) {
return;
}
Node nextSibling = currentNode.nextSibling();
if (currentNode instanceof Element) {
switch (currentNode.nodeName()) {
case "html":
case "body":
if (!(nextSibling instanceof Comment)) {
return;
}
break;
case "head":
if (hadHeadClose) {
return;
}
hadHeadClose = true;
if (!(nextSibling instanceof Comment && startsWithWhitespace(nextSibling))) {
return;
}
break;
case "li":
if (nextSibling == null || nextSibling.nodeName().equals("li")) {
return;
}
break;
case "dt":
if (nextSibling != null
&& (nextSibling.nodeName().equals("dt") || nextSibling.nodeName().equals("dd"))) {
return;
}
break;
case "dd":
if (nextSibling == null
|| nextSibling.nodeName().equals("dt")
|| nextSibling.nodeName().equals("dd")) {
return;
}
break;
case "p":
if (!currentNode.parent().nodeName().equals("a")
|| nextSibling == null
|| SIBLINGS_THAT_MAKE_P_CLOSE_OPTIONAL.contains(nextSibling.nodeName())) {
return;
}
break;
case "rb":
case "rt":
case "rtc":
case "rp":
if (nextSibling == null
|| nextSibling.nodeName().equals("rb")
|| nextSibling.nodeName().equals("rtc")
|| nextSibling.nodeName().equals("rp")
|| (nextSibling.nodeName().equals("rt") && !currentNode.nodeName().equals("rtc"))) {
return;
}
break;
case "optgroup":
if (nextSibling == null || nextSibling.nodeName().equals("optgroup")) {
return;
}
break;
case "option":
if (nextSibling == null
|| nextSibling.nodeName().equals("option")
|| nextSibling.nodeName().equals("optgroup")) {
return;
}
break;
case "thead":
case "tbody":
if (nextSibling == null
|| nextSibling.nodeName().equals("tbody")
|| nextSibling.nodeName().equals("tfoot")) {
omittedTableSectionEnd = true;
return;
}
break;
case "tfoot":
if (nextSibling == null || nextSibling.nodeName().equals("tbody")) {
omittedTableSectionEnd = true;
return;
}
break;
case "tr":
if (nextSibling == null || nextSibling.nodeName().equals("tr")) {
return;
}
break;
case "td":
case "th":
if (nextSibling == null
|| nextSibling.nodeName().equals("tr")
|| nextSibling.nodeName().equals("th")) {
return;
}
break;
default:
// ignored
}
}
// This method is package-private. We intend to beseech jsoup's author to make it public.
currentNode.outerHtmlTail(output, depth, settings);
}
private boolean isDocumentOrDoctype() {
return currentNode instanceof DocumentType || currentNode instanceof Document;
}
private static boolean startsWithWhitespace(Node child) {
if (child instanceof TextNode) {
String firstText = ((TextNode) child).text();
if (!firstText.isEmpty() && CharMatcher.whitespace().matches(firstText.charAt(0))) {
return true;
}
}
return false;
}
}