|
1 | 1 | package org.jsoup.nodes; |
2 | 2 |
|
3 | 3 | import org.jsoup.Jsoup; |
| 4 | +import org.jsoup.select.Elements; |
4 | 5 | import org.junit.jupiter.api.Test; |
5 | 6 |
|
6 | 7 | import java.util.List; |
7 | 8 |
|
8 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
9 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | +import static org.junit.jupiter.api.Assertions.*; |
10 | 10 |
|
11 | 11 | public class ElementIT { |
12 | 12 | @Test |
@@ -78,4 +78,45 @@ public void testFastReparentExistingContent() { |
78 | 78 |
|
79 | 79 | assertTrue(runtime <= 10000); |
80 | 80 | } |
| 81 | + |
| 82 | + // These overflow tests take a couple seconds to run, so are in the slow tests |
| 83 | + @Test void hasTextNoOverflow() { |
| 84 | + // hasText() was recursive, so could overflow |
| 85 | + Document doc = new Document("https://example.com/"); |
| 86 | + Element el = doc.body(); |
| 87 | + for (int i = 0; i <= 50000; i++) { |
| 88 | + el = el.appendElement("p"); |
| 89 | + } |
| 90 | + assertFalse(doc.hasText()); |
| 91 | + el.text("Hello"); |
| 92 | + assertTrue(doc.hasText()); |
| 93 | + assertEquals(el.text(), doc.text()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test void dataNoOverflow() { |
| 97 | + // data() was recursive, so could overflow |
| 98 | + Document doc = new Document("https://example.com/"); |
| 99 | + Element el = doc.body(); |
| 100 | + for (int i = 0; i <= 50000; i++) { |
| 101 | + el = el.appendElement("p"); |
| 102 | + } |
| 103 | + Element script = el.appendElement("script"); |
| 104 | + script.text("script"); // holds data nodes, so inserts as data, not text |
| 105 | + assertFalse(script.hasText()); |
| 106 | + assertEquals("script", script.data()); |
| 107 | + assertEquals(el.data(), doc.data()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test void parentsNoOverflow() { |
| 111 | + // parents() was recursive, so could overflow |
| 112 | + Document doc = new Document("https://example.com/"); |
| 113 | + Element el = doc.body(); |
| 114 | + int num = 50000; |
| 115 | + for (int i = 0; i <= num; i++) { |
| 116 | + el = el.appendElement("p"); |
| 117 | + } |
| 118 | + Elements parents = el.parents(); |
| 119 | + assertEquals(num+2, parents.size()); // +2 for html and body |
| 120 | + assertEquals(doc, el.ownerDocument()); |
| 121 | + } |
81 | 122 | } |
0 commit comments