/
W3CDomTest.java
344 lines (283 loc) · 16.5 KB
/
W3CDomTest.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package org.jsoup.helper;
import org.jsoup.Jsoup;
import org.jsoup.TextUtil;
import org.jsoup.integration.ParseTest;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
public class W3CDomTest {
private static Document parseXml(String xml, boolean nameSpaceAware) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(nameSpaceAware);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver((publicId, systemId) -> {
if (systemId.contains("about:legacy-compat")) { // <!doctype html>
return new InputSource(new StringReader(""));
} else {
return null;
}
});
Document dom = builder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
dom.normalizeDocument();
return dom;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
@Test
public void simpleConversion() {
String html = "<html><head><title>W3c</title></head><body><p class='one' id=12>Text</p><!-- comment --><invalid>What<script>alert('!')";
org.jsoup.nodes.Document doc = Jsoup.parse(html);
W3CDom w3c = new W3CDom();
Document wDoc = w3c.fromJsoup(doc);
NodeList meta = wDoc.getElementsByTagName("META");
assertEquals(0, meta.getLength());
String out = W3CDom.asString(wDoc, W3CDom.OutputXml());
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><html><head><title>W3c</title></head><body><p class=\"one\" id=\"12\">Text</p><!-- comment --><invalid>What<script>alert('!')</script></invalid></body></html>";
assertEquals(expected, TextUtil.stripNewlines(out));
Document roundTrip = parseXml(out, true);
assertEquals("Text", roundTrip.getElementsByTagName("p").item(0).getTextContent());
// check we can set properties
Map<String, String> properties = W3CDom.OutputXml();
properties.put(OutputKeys.INDENT, "yes");
String furtherOut = W3CDom.asString(wDoc, properties);
assertTrue(furtherOut.length() > out.length()); // wanted to assert formatting, but actual indentation is platform specific so breaks in CI
String furtherExpected =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><html><head><title>W3c</title></head><body><p class=\"one\" id=\"12\">Text</p><!-- comment --><invalid>What<script>alert('!')</script></invalid></body></html>";
assertEquals(furtherExpected, TextUtil.stripNewlines(furtherOut)); // on windows, DOM will write newlines as \r\n
}
@Test
public void convertsGoogle() throws IOException {
File in = ParseTest.getFile("/htmltests/google-ipod.html.gz");
org.jsoup.nodes.Document doc = Jsoup.parse(in, "UTF8");
W3CDom w3c = new W3CDom();
Document wDoc = w3c.fromJsoup(doc);
Node htmlEl = wDoc.getChildNodes().item(1);
assertNull(htmlEl.getNamespaceURI());
assertEquals("html", htmlEl.getLocalName());
assertEquals("html", htmlEl.getNodeName());
DocumentType doctype = wDoc.getDoctype();
Node doctypeNode = wDoc.getChildNodes().item(0);
assertSame(doctype, doctypeNode);
assertEquals("html", doctype.getName());
String xml = W3CDom.asString(wDoc, W3CDom.OutputXml());
assertTrue(xml.contains("ipod"));
Document roundTrip = parseXml(xml, true);
assertEquals("Images", roundTrip.getElementsByTagName("a").item(0).getTextContent());
}
@Test
public void convertsGoogleLocation() throws IOException {
File in = ParseTest.getFile("/htmltests/google-ipod.html.gz");
org.jsoup.nodes.Document doc = Jsoup.parse(in, "UTF8");
W3CDom w3c = new W3CDom();
Document wDoc = w3c.fromJsoup(doc);
String out = w3c.asString(wDoc);
assertEquals(doc.location(), wDoc.getDocumentURI());
}
@Test
public void namespacePreservation() throws IOException {
File in = ParseTest.getFile("/htmltests/namespaces.xhtml");
org.jsoup.nodes.Document jsoupDoc;
jsoupDoc = Jsoup.parse(in, "UTF-8");
Document doc;
org.jsoup.helper.W3CDom jDom = new org.jsoup.helper.W3CDom();
doc = jDom.fromJsoup(jsoupDoc);
Node htmlEl = doc.getChildNodes().item(0);
assertEquals("http://www.w3.org/1999/xhtml", htmlEl.getNamespaceURI());
assertEquals("html", htmlEl.getLocalName());
assertEquals("html", htmlEl.getNodeName());
// inherits default namespace
Node head = htmlEl.getFirstChild().getNextSibling();
assertEquals("http://www.w3.org/1999/xhtml", head.getNamespaceURI());
assertEquals("head", head.getLocalName());
assertEquals("head", head.getNodeName());
Node epubTitle = htmlEl.getChildNodes().item(3).getChildNodes().item(3);
assertEquals("Check", epubTitle.getTextContent());
assertEquals("http://www.idpf.org/2007/ops", epubTitle.getNamespaceURI());
assertEquals("title", epubTitle.getLocalName());
assertEquals("epub:title", epubTitle.getNodeName());
Node xSection = epubTitle.getNextSibling().getNextSibling();
assertEquals("urn:test", xSection.getNamespaceURI());
assertEquals("section", xSection.getLocalName());
assertEquals("x:section", xSection.getNodeName());
// https://github.com/jhy/jsoup/issues/977
// does not keep last set namespace
Node svg = xSection.getNextSibling().getNextSibling();
assertEquals("http://www.w3.org/2000/svg", svg.getNamespaceURI());
assertEquals("svg", svg.getLocalName());
assertEquals("svg", svg.getNodeName());
Node path = svg.getChildNodes().item(1);
assertEquals("http://www.w3.org/2000/svg", path.getNamespaceURI());
assertEquals("path", path.getLocalName());
assertEquals("path", path.getNodeName());
Node clip = path.getChildNodes().item(1);
assertEquals("http://example.com/clip", clip.getNamespaceURI());
assertEquals("clip", clip.getLocalName());
assertEquals("clip", clip.getNodeName());
assertEquals("456", clip.getTextContent());
Node picture = svg.getNextSibling().getNextSibling();
assertEquals("http://www.w3.org/1999/xhtml", picture.getNamespaceURI());
assertEquals("picture", picture.getLocalName());
assertEquals("picture", picture.getNodeName());
Node img = picture.getFirstChild();
assertEquals("http://www.w3.org/1999/xhtml", img.getNamespaceURI());
assertEquals("img", img.getLocalName());
assertEquals("img", img.getNodeName());
}
@Test
public void handlesInvalidAttributeNames() {
String html = "<html><head></head><body style=\"color: red\" \" name\"></body></html>";
org.jsoup.nodes.Document jsoupDoc;
jsoupDoc = Jsoup.parse(html);
Element body = jsoupDoc.select("body").first();
assertTrue(body.hasAttr("\"")); // actually an attribute with key '"'. Correct per HTML5 spec, but w3c xml dom doesn't dig it
assertTrue(body.hasAttr("name\""));
Document w3Doc = W3CDom.convert(jsoupDoc);
String xml = W3CDom.asString(w3Doc, W3CDom.OutputXml());
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><html><head/><body name=\"\" style=\"color: red\"/></html>", xml);
}
@Test
public void htmlInputDocMaintainsHtmlAttributeNames() {
String html = "<!DOCTYPE html><html><head></head><body><p hành=\"1\" hình=\"2\">unicode attr names</p></body></html>";
org.jsoup.nodes.Document jsoupDoc;
jsoupDoc = Jsoup.parse(html);
Document w3Doc = W3CDom.convert(jsoupDoc);
String out = W3CDom.asString(w3Doc, W3CDom.OutputHtml());
String expected = "<!DOCTYPE html SYSTEM \"about:legacy-compat\"><html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body><p hành=\"1\" hình=\"2\">unicode attr names</p></body></html>";
assertEquals(expected, TextUtil.stripNewlines(out));
}
@Test
public void xmlInputDocMaintainsHtmlAttributeNames() {
String html = "<!DOCTYPE html><html><head></head><body><p hành=\"1\" hình=\"2\">unicode attr names coerced</p></body></html>";
org.jsoup.nodes.Document jsoupDoc;
jsoupDoc = Jsoup.parse(html);
jsoupDoc.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml);
Document w3Doc = W3CDom.convert(jsoupDoc);
String out = W3CDom.asString(w3Doc, W3CDom.OutputHtml());
String expected = "<!DOCTYPE html SYSTEM \"about:legacy-compat\"><html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body><p hnh=\"2\">unicode attr names coerced</p></body></html>";
assertEquals(expected, TextUtil.stripNewlines(out));
}
@Test
public void handlesInvalidTagAsText() {
org.jsoup.nodes.Document jsoup = Jsoup.parse("<インセンティブで高収入!>Text <p>More</p>");
Document w3Doc = W3CDom.convert(jsoup);
String xml = W3CDom.asString(w3Doc, W3CDom.OutputXml());
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><html><head/><body><インセンティブで高収入!>Text <p>More</p></body></html>", xml);
}
@Test
public void treatsUndeclaredNamespaceAsLocalName() {
String html = "<fb:like>One</fb:like>";
org.jsoup.nodes.Document doc = Jsoup.parse(html);
Document w3Doc = new W3CDom().fromJsoup(doc);
Node htmlEl = w3Doc.getFirstChild();
assertNull(htmlEl.getNamespaceURI());
assertEquals("html", htmlEl.getLocalName());
assertEquals("html", htmlEl.getNodeName());
Node fb = htmlEl.getFirstChild().getNextSibling().getFirstChild();
assertNull(fb.getNamespaceURI());
assertEquals("like", fb.getLocalName());
assertEquals("fb:like", fb.getNodeName());
}
@Test
public void xmlnsXpathTest() throws XPathExpressionException {
W3CDom w3c = new W3CDom();
String html = "<html><body><div>hello</div></body></html>";
Document dom = w3c.fromJsoup(Jsoup.parse(html));
NodeList nodeList = xpath(dom, "//body");// no ns, so needs no prefix
assertEquals("div", nodeList.item(0).getLocalName());
// default output is namespace aware, so query needs to be as well
html = "<html xmlns='http://www.w3.org/1999/xhtml'><body id='One'><div>hello</div></body></html>";
dom = w3c.fromJsoup(Jsoup.parse(html));
nodeList = xpath(dom, "//body");
assertNull(nodeList); // no matches
dom = w3c.fromJsoup(Jsoup.parse(html));
nodeList = xpath(dom, "//*[local-name()=\"body\"]");
assertNotNull(nodeList);
assertEquals(1, nodeList.getLength());
assertEquals("div", nodeList.item(0).getLocalName());
assertEquals("http://www.w3.org/1999/xhtml", nodeList.item(0).getNamespaceURI());
assertNull(nodeList.item(0).getPrefix());
// get rid of the name space awareness
String xml = w3c.asString(dom);
dom = parseXml(xml, false);
Node item = (Node) xpath(dom, "//body");
assertEquals("body", item.getNodeName());
assertNull(item.getNamespaceURI());
assertNull(item.getPrefix());
// put back, will get zero
dom = parseXml(xml, true);
nodeList = xpath(dom, "//body");
assertNull(nodeList);
}
private NodeList xpath(Document w3cDoc, String query) throws XPathExpressionException {
XPathExpression xpath = XPathFactory.newInstance().newXPath().compile(query);
return ((NodeList) xpath.evaluate(w3cDoc, XPathConstants.NODE));
}
@Test
public void testRoundTripDoctype() {
// TODO - not super happy with this output - but plain DOM doesn't let it out, and don't want to rebuild the writer
// because we have Saxon on the test classpath, the transformer will change to that, and so case may change (e.g. Java base in META, Saxon is meta for HTML)
String base = "<!DOCTYPE html><p>One</p>";
assertEqualsIgnoreCase("<!DOCTYPE html SYSTEM \"about:legacy-compat\"><html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body><p>One</p></body></html>",
output(base, true));
assertEqualsIgnoreCase("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html SYSTEM \"about:legacy-compat\"><html><head/><body><p>One</p></body></html>", output(base, false));
String publicDoc = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
assertEqualsIgnoreCase("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body></body></html>", output(publicDoc, true));
// different impls will have different XML formatting. OpenJDK 13 default gives this: <body /> but others have <body/>, so just check start
assertTrue(output(publicDoc, false).startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC"));
String systemDoc = "<!DOCTYPE html SYSTEM \"exampledtdfile.dtd\">";
assertEqualsIgnoreCase("<!DOCTYPE html SYSTEM \"exampledtdfile.dtd\"><html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body></body></html>", output(systemDoc, true));
assertEqualsIgnoreCase("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html SYSTEM \"exampledtdfile.dtd\"><html><head/><body/></html>", output(systemDoc, false));
String legacyDoc = "<!DOCTYPE html SYSTEM \"about:legacy-compat\">";
assertEqualsIgnoreCase("<!DOCTYPE html SYSTEM \"about:legacy-compat\"><html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body></body></html>", output(legacyDoc, true));
assertEqualsIgnoreCase("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html SYSTEM \"about:legacy-compat\"><html><head/><body/></html>", output(legacyDoc, false));
String noDoctype = "<p>One</p>";
assertEqualsIgnoreCase("<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body><p>One</p></body></html>", output(noDoctype, true));
assertEqualsIgnoreCase("<?xml version=\"1.0\" encoding=\"UTF-8\"?><html><head/><body><p>One</p></body></html>", output(noDoctype, false));
}
private String output(String in, boolean modeHtml) {
org.jsoup.nodes.Document jdoc = Jsoup.parse(in);
Document w3c = W3CDom.convert(jdoc);
Map<String, String> properties = modeHtml ? W3CDom.OutputHtml() : W3CDom.OutputXml();
return TextUtil.normalizeSpaces(W3CDom.asString(w3c, properties));
}
private void assertEqualsIgnoreCase(String want, String have) {
assertEquals(want.toLowerCase(Locale.ROOT), have.toLowerCase(Locale.ROOT));
}
@Test public void convertsElementsAndMaintainsSource() {
org.jsoup.nodes.Document jdoc = Jsoup.parse("<body><div><p>One</div><div><p>Two");
W3CDom w3CDom = new W3CDom();
Element jDiv = jdoc.selectFirst("div");
assertNotNull(jDiv);
Document doc = w3CDom.fromJsoup(jDiv);
Node div = doc.getFirstChild();
assertEquals("div", div.getLocalName());
assertEquals(jDiv, div.getUserData(W3CDom.SourceProperty));
Node textNode = div.getFirstChild().getFirstChild();
assertEquals("One", textNode.getTextContent());
assertEquals(Node.TEXT_NODE, textNode.getNodeType());
org.jsoup.nodes.TextNode jText = (TextNode) jDiv.childNode(0).childNode(0);
assertEquals(jText, textNode.getUserData(W3CDom.SourceProperty));
}
}