From 075b0e62b15497a7c32ce84c3d24bcb7d0561954 Mon Sep 17 00:00:00 2001 From: Jonathan Hedley Date: Mon, 23 Jan 2023 15:08:11 +1100 Subject: [PATCH] Moved to a traversor vs recursion to gather data() Fixes #1864 --- src/main/java/org/jsoup/nodes/Element.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jsoup/nodes/Element.java b/src/main/java/org/jsoup/nodes/Element.java index 1b5a18fb29..4c53cfe1d7 100644 --- a/src/main/java/org/jsoup/nodes/Element.java +++ b/src/main/java/org/jsoup/nodes/Element.java @@ -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); }