-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathDomTextTest.java
More file actions
347 lines (297 loc) · 13.2 KB
/
DomTextTest.java
File metadata and controls
347 lines (297 loc) · 13.2 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
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
345
346
347
/*
* Copyright (c) 2002-2026 Gargoyle Software Inc.
*
* 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
* https://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.htmlunit.html;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.ArrayList;
import java.util.List;
import org.htmlunit.MockWebConnection;
import org.htmlunit.SimpleWebTestCase;
import org.htmlunit.WebClient;
import org.htmlunit.util.MimeType;
import org.htmlunit.util.StringUtils;
import org.junit.jupiter.api.Test;
/**
* Tests for {@link DomText}.
*
* @author Marc Guillemot
* @author Ahmed Ashour
* @author Rodney Gitzel
* @author Sudhan Moghe
* @author Philip Graf
* @author Ronald Brill
*/
public class DomTextTest extends SimpleWebTestCase {
/**
* Test the clean up of   in strings.
* @throws Exception if the test fails
*/
@Test
public void asText_nbsp() throws Exception {
testPlainText("a b c d e", "a b c d e");
testPlainText("a b c d e", "a b c d e");
testPlainText(" a ", " a ");
testPlainText(" a ", " a ");
testPlainText(" a ", " a ");
}
/**
* Test font formats, as per bug #490.
* See http://sourceforge.net/p/htmlunit/bugs/490/.
*
* @throws Exception if the test fails
*/
@Test
public void asText_fontFormat() throws Exception {
testAsText("a <b>b</b> c", "a b c");
testAsText("a <b>b</b>c", "a bc");
testAsText("a<b>b</b> c", "ab c");
testAsText("a<b>b</b>c", "abc");
// italics and teletype should work the same way
testAsText("a <i>b</i> c", "a b c");
testAsText("a <i>b</i>c", "a bc");
testAsText("a<i>b</i> c", "ab c");
testAsText("a<i>b</i>c", "abc");
testAsText("a <tt>b</tt> c", "a b c");
testAsText("a <tt>b</tt>c", "a bc");
testAsText("a<tt>b</tt> c", "ab c");
testAsText("a<tt>b</tt>c", "abc");
testAsText("a <font>b</font> c", "a b c");
testAsText("a<font>b</font> c", "ab c");
testAsText("a <font>b</font>c", "a bc");
testAsText("a<font>b</font>c", "abc");
testAsText("a <span>b</span> c", "a b c");
testAsText("a<span>b</span> c", "ab c");
testAsText("a <span>b</span>c", "a bc");
testAsText("a<span>b</span>c", "abc");
testAsText("a<b><font><i>b</i></font></b>c", "abc");
testAsText("a<b><font> <i>b</i></font></b>c", "a bc");
}
/**
* This test once tested regression for bug #490 but the expectations have been changed
* as asNormalizedText() should now use new lines when appropriate.
* @throws Exception if the test fails
*/
@Test
public void asNormalizedTextRegression() throws Exception {
String expected = "a\nb\nc";
testAsText("a<ul><li>b</ul>c", expected);
testAsText("a<p>b<br>c", expected);
testAsText("a<table><tr><td>b</td></tr></table>c", expected);
testAsText("a<div>b</div>c", expected);
expected = "a\nb\nb\nc";
testAsText("a<table><tr><td> b </td></tr>\n<tr><td> b </td></tr></table>c", expected);
}
/**
* Checks the HtmlTable* objects themselves.
* @throws Exception if the test fails
*/
@Test
public void asText_table_elements() throws Exception {
final String html = "<table id='table'><tr id='row'><td id='cell'> b </td></tr>\n</table>\n";
final String content = DOCTYPE_HTML + "<html><body><span id='foo'>" + html + "</span></body></html>";
final HtmlPage page = loadPage(content);
assertEquals("b", page.getHtmlElementById("cell").asNormalizedText());
assertEquals("b", page.getHtmlElementById("row").asNormalizedText());
assertEquals("b", page.getHtmlElementById("table").asNormalizedText());
}
private void testPlainText(final String html, final String expectedText) throws Exception {
final String content = DOCTYPE_HTML + "<html><body><span id='foo'>" + html + "</span></body></html>";
final HtmlPage page = loadPage(content);
assertEquals(expectedText, page.asNormalizedText());
final HtmlElement elt = page.getHtmlElementById("foo");
assertEquals(expectedText, elt.asNormalizedText());
final DomNode node = elt.getFirstChild();
assertEquals(expectedText, node.asNormalizedText());
}
private void testAsText(final String html, final String expectedText) throws Exception {
final String content = DOCTYPE_HTML + "<html><body><span id='foo'>" + html + "</span></body></html>";
final HtmlPage page = loadPage(content);
final HtmlElement elt = page.getHtmlElementById("foo");
assertEquals(expectedText, elt.asNormalizedText());
}
/**
* @throws Exception if the test fails
*/
@Test
public void asXml() throws Exception {
final String unicodeString = "\u064A\u0627 \u0644\u064A\u064A\u0644";
final String html = DOCTYPE_HTML
+ "<html>\n"
+ "<head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head>\n"
+ "<body><span id='foo'>" + unicodeString + "</span></body></html>";
final int[] expectedValues = {1610, 1575, 32, 1604, 1610, 1610, 1604};
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(StringUtils.toByteArray(html, UTF_8), 200, "OK", MimeType.TEXT_HTML);
client.setWebConnection(webConnection);
final HtmlPage page = client.getPage(URL_FIRST);
final String xml = page.getHtmlElementById("foo").getFirstChild().asXml();
assertEquals(expectedValues.length, xml.length());
int index = 0;
for (final int expectedValue : expectedValues) {
assertEquals(expectedValue, xml.codePointAt(index++));
}
}
/**
* @throws Exception if the test fails
*/
@Test
public void splitText() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head></head><body>\n"
+ "<br><div id='tag'></div><br></body></html>";
final HtmlPage page = loadPage(html);
final DomNode divNode = page.getElementById("tag");
final DomText node = new DomText(page, "test split");
divNode.insertBefore(node);
final DomNode previousSibling = node.getPreviousSibling();
final DomNode nextSibling = node.getNextSibling();
final DomNode parent = node.getParentNode();
// position among parent's children
final int position = readPositionAmongParentChildren(node);
final DomText newNode = node.splitText(5);
assertSame("new node previous sibling", node, newNode.getPreviousSibling());
assertSame("previous sibling", previousSibling, node.getPreviousSibling());
assertSame("new node next sibling", nextSibling, newNode.getNextSibling());
assertSame("next sibling", newNode, node.getNextSibling());
assertSame("parent", parent, newNode.getParentNode());
assertSame(node, previousSibling.getNextSibling());
assertSame(newNode, nextSibling.getPreviousSibling());
assertEquals(position + 1, readPositionAmongParentChildren(newNode));
}
/**
* @throws Exception if the test fails
*/
@Test
public void splitLastDomText() throws Exception {
final String content = DOCTYPE_HTML
+ "<html><head></head><body>\n"
+ "<br><div id='tag'></div><br></body></html>";
final HtmlPage page = loadPage(content);
final DomNode divNode = page.getElementById("tag");
final DomText firstNode = new DomText(page, "test split");
divNode.appendChild(firstNode);
assertNull(firstNode.getPreviousSibling());
final DomText secondNode = firstNode.splitText(5);
final DomText thirdNode = new DomText(page, "test split");
divNode.appendChild(thirdNode);
assertSame(secondNode, firstNode.getNextSibling());
assertNull(firstNode.getPreviousSibling());
assertSame(firstNode, secondNode.getPreviousSibling());
assertSame(thirdNode, secondNode.getNextSibling());
assertSame(secondNode, thirdNode.getPreviousSibling());
assertNull(thirdNode.getNextSibling());
assertSame(divNode, secondNode.getParentNode());
assertSame(divNode, thirdNode.getParentNode());
assertEquals(0, readPositionAmongParentChildren(firstNode));
assertEquals(1, readPositionAmongParentChildren(secondNode));
assertEquals(2, readPositionAmongParentChildren(thirdNode));
}
/**
* Reads the position of the node among the children of its parent
* @param node the node to look at
* @return the position
*/
private static int readPositionAmongParentChildren(final DomNode node) {
int i = 0;
for (final DomNode child : node.getParentNode().getChildren()) {
if (child == node) {
return i;
}
i++;
}
return -1;
}
/**
* @throws Exception if the test fails
*/
@Test
public void splitText2() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head><title>foo</title><script>\n"
+ " function test() {\n"
+ " var div = document.getElementById('myDiv');\n"
+ " div.appendChild(document.createElement('a'));\n"
+ " var text = document.createTextNode('123456');\n"
+ " div.appendChild(text);\n"
+ " div.appendChild(document.createElement('hr'));\n"
+ " alert(div.childNodes.length);\n"
+ " text.splitText(3);\n"
+ " alert(div.childNodes.length);\n"
+ " alert(div.childNodes.item(2).nodeValue);\n"
+ " }\n"
+ "</script></head><body onload='test()'>\n"
+ " <div id='myDiv'></div>\n"
+ "</body></html>";
final String[] expectedAlerts = {"3", "4", "456"};
final List<String> collectedAlerts = new ArrayList<>();
loadPage(html, collectedAlerts);
assertEquals(expectedAlerts, collectedAlerts);
}
/**
* @throws Exception if an error occurs
*/
@Test
public void setTextContent() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><span id='s'>abc</span></body></html>";
final HtmlPage page = loadPage(html);
final DomText text = (DomText) page.getElementById("s").getFirstChild();
assertEquals("abc", text.getTextContent());
text.setTextContent("xyz");
assertEquals("xyz", text.getTextContent());
assertEquals("xyz", page.asNormalizedText());
}
/**
* Test case for #1366.
* @throws Exception if an error occurs
*/
@Test
public void getTextContentWhitespace() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='s'><b>Hello</b> <b>World</b>!</div></body></html>";
final HtmlPage page = loadPage(html);
final HtmlElement text = page.getHtmlElementById("s");
assertEquals("Hello World!", text.getTextContent());
}
/**
* Tests if {@code getCanonicalXPath()} returns the correct XPath for a text
* node without other text node siblings.
* @throws Exception if an error occurs
*/
@Test
public void getCanonicalXPath_withoutTextSiblings() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><span id='s'>abc</span></body></html>";
final HtmlPage page = loadPage(html);
final DomText text = (DomText) page.getElementById("s").getFirstChild();
assertEquals("/html/body/span/text()", text.getCanonicalXPath());
assertEquals(text, page.getFirstByXPath(text.getCanonicalXPath()));
}
/**
* Tests if {@code getCanonicalXPath()} returns the correct XPath for a text
* node with other text node siblings.
* @throws Exception if an error occurs
*/
@Test
public void getCanonicalXPath_withTextSiblings() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><span id='s'>abc<br/>def</span></body></html>";
final HtmlPage page = loadPage(html);
final DomText text1 = (DomText) page.getElementById("s").getFirstChild();
assertEquals("abc", text1.getData());
assertEquals("/html/body/span/text()[1]", text1.getCanonicalXPath());
assertEquals(text1, page.getFirstByXPath(text1.getCanonicalXPath()));
final DomText text2 = (DomText) page.getElementById("s").getChildNodes().get(2);
assertEquals("def", text2.getData());
assertEquals("/html/body/span/text()[2]", text2.getCanonicalXPath());
assertEquals(text2, page.getFirstByXPath(text2.getCanonicalXPath()));
}
}