Skip to content

Commit

Permalink
Use the incoming node's parent if outgoing has already been removed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jhy committed Nov 25, 2024
1 parent df404cf commit 1a91aac
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* In the `TreeBuilder`, the `onNodeInserted()` and `onNodeClosed()` events are now also fired for the outermost /
root `Document` node. This enables source position tracking on the Document node (which was previously unset). And
it also enables the node traversor to see the outer Document node. [2182](https://github.com/jhy/jsoup/pull/2182)
* Selected Elements can now be position swapped inline using
`Elements#set()`. [2212](https://github.com/jhy/jsoup/issues/2212)

### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jsoup/nodes/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ void nodelistChanged() {
*/
public void replaceWith(Node in) {
Validate.notNull(in);
if (parentNode == null) parentNode = in.parentNode; // allows old to have been temp removed before replacing
Validate.notNull(parentNode);
parentNode.replaceChild(this, in);
}
Expand Down
5 changes: 1 addition & 4 deletions src/test/java/org/jsoup/nodes/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,8 @@ public void handlesAbsOnProtocolessAbsoluteUris() {
assertEquals("One <em>foo</em> three", p.html());
}

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

0 comments on commit 1a91aac

Please sign in to comment.