Skip to content

Commit 1a91aac

Browse files
committed
Use the incoming node's parent if outgoing has already been removed
Fixes #2212 This won't work for all scenarios - when an element is removed it is de-parented, and so if the order of calls had lead to both elements already being detached, there won't be a parent to re-attach to.
1 parent df404cf commit 1a91aac

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* In the `TreeBuilder`, the `onNodeInserted()` and `onNodeClosed()` events are now also fired for the outermost /
1616
root `Document` node. This enables source position tracking on the Document node (which was previously unset). And
1717
it also enables the node traversor to see the outer Document node. [2182](https://github.com/jhy/jsoup/pull/2182)
18+
* Selected Elements can now be position swapped inline using
19+
`Elements#set()`. [2212](https://github.com/jhy/jsoup/issues/2212)
1820

1921
### Bug Fixes
2022

src/main/java/org/jsoup/nodes/Node.java

+1
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ void nodelistChanged() {
509509
*/
510510
public void replaceWith(Node in) {
511511
Validate.notNull(in);
512+
if (parentNode == null) parentNode = in.parentNode; // allows old to have been temp removed before replacing
512513
Validate.notNull(parentNode);
513514
parentNode.replaceChild(this, in);
514515
}

src/test/java/org/jsoup/nodes/NodeTest.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,8 @@ public void handlesAbsOnProtocolessAbsoluteUris() {
176176
assertEquals("One <em>foo</em> three", p.html());
177177
}
178178

179-
/**
180-
* test case for
181-
* <a href="https://github.com/jhy/jsoup/issues/2212">Issue #2212</a>
182-
*/
183179
@Test public void testReplaceTwice() {
180+
// https://github.com/jhy/jsoup/issues/2212
184181
Document doc = Jsoup.parse("<p><span>Child One</span><span>Child Two</span><span>Child Three</span><span>Child Four</span></p>");
185182
Elements children = doc.select("p").first().children();
186183
// first swap 0 and 1

0 commit comments

Comments
 (0)