From e3e2c6b536fcad8da8d18351e6c5a750626f9922 Mon Sep 17 00:00:00 2001 From: Jonathan Hedley Date: Mon, 23 Jan 2023 15:30:41 +1100 Subject: [PATCH] Removed recursion from accumulateParents --- src/main/java/org/jsoup/nodes/Element.java | 4 ++-- src/test/java/org/jsoup/nodes/ElementTest.java | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jsoup/nodes/Element.java b/src/main/java/org/jsoup/nodes/Element.java index 4c53cfe1d7..6f50900e6d 100644 --- a/src/main/java/org/jsoup/nodes/Element.java +++ b/src/main/java/org/jsoup/nodes/Element.java @@ -276,9 +276,9 @@ public Elements parents() { private static void accumulateParents(Element el, Elements parents) { Element parent = el.parent(); - if (parent != null && !parent.isNode("#root")) { + while (parent != null && !parent.isNode("#root")) { parents.add(parent); - accumulateParents(parent, parents); + parent = parent.parent(); } } diff --git a/src/test/java/org/jsoup/nodes/ElementTest.java b/src/test/java/org/jsoup/nodes/ElementTest.java index d569e42dfd..bc17127113 100644 --- a/src/test/java/org/jsoup/nodes/ElementTest.java +++ b/src/test/java/org/jsoup/nodes/ElementTest.java @@ -256,6 +256,10 @@ public void testGetParents() { assertEquals("div", parents.get(1).tagName()); assertEquals("body", parents.get(2).tagName()); assertEquals("html", parents.get(3).tagName()); + + Element orphan = new Element("p"); + Elements none = orphan.parents(); + assertEquals(0, none.size()); } @Test