Skip to content

Commit

Permalink
Moved to a traversor vs recursion to gather data()
Browse files Browse the repository at this point in the history
Fixes #1864
  • Loading branch information
jhy committed Jan 23, 2023
1 parent 75bdf8e commit 075b0e6
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -1481,25 +1481,20 @@ public boolean hasText() {
*/
public String data() {
StringBuilder sb = StringUtil.borrowBuilder();

for (Node childNode : childNodes) {
traverse((childNode, depth) -> {
if (childNode instanceof DataNode) {
DataNode data = (DataNode) childNode;
sb.append(data.getWholeData());
} else if (childNode instanceof Comment) {
Comment comment = (Comment) childNode;
sb.append(comment.getData());
} else if (childNode instanceof Element) {
Element element = (Element) childNode;
String elementData = element.data();
sb.append(elementData);
} else if (childNode instanceof CDataNode) {
// this shouldn't really happen because the html parser won't see the cdata as anything special when parsing script.
// but in case another type gets through.
CDataNode cDataNode = (CDataNode) childNode;
sb.append(cDataNode.getWholeText());
}
}
});
return StringUtil.releaseBuilder(sb);
}

Expand Down

0 comments on commit 075b0e6

Please sign in to comment.