Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeafNode.value is not cloned in LeafNode.clone() #1176

Closed
fluorumlabs opened this issue Jan 28, 2019 · 2 comments
Closed

LeafNode.value is not cloned in LeafNode.clone() #1176

fluorumlabs opened this issue Jan 28, 2019 · 2 comments
Labels
bug Confirmed bug that we should fix
Milestone

Comments

@fluorumlabs
Copy link

fluorumlabs commented Jan 28, 2019

Affected version: At least 1.11.3, but most likely 1.11.1 and above

Consider the following example:

/* 1 */ TextNode x = new TextNode("zzz");
/* 2 */ x.attributes(); // this calls LeafNode.ensureAttributes() and creates attributes map
/* 3 */ TextNode y = (TextNode) x.clone();
/* 4 */ y.text("xxx"); // now x.text() is also "xxx"
  • Here, on step 1 we create a new TextNode with a value of zzz.
  • On step 2 ensureAttributes() is called indirectly, which replaces String value with Attributes containing #text = zzz entry.
  • On step 3 we clone the node.
  • If we inspect properties after step 3 we'll notice that x.attributes() and y.attributes() are returning the same instance
  • On step 4 we update content of y, which is "magically" mirrored to x as well.

In 1.10.3 attributes were explicitly cloned in Node.doClone() method, but starting with 1.11.1 attributes were moved to TextNode and then to LeafNode without any clone implementation.

@fluorumlabs
Copy link
Author

fluorumlabs commented Jan 28, 2019

Temporary workaround is to call this before every Document.clone:

/**
 * Temporary workaround for https://github.com/jhy/jsoup/issues/1176
 *
 * @param document
 */
private static void workaroundForJsoupIssue1176(Document document) {
	document.getAllElements().forEach(element -> {
		for (TextNode textNode : element.textNodes()) {
			TextNode flattenedTextNode = new TextNode(textNode.getWholeText());
			textNode.replaceWith(flattenedTextNode);
		}
	});
}

@jhy jhy added the bug Confirmed bug that we should fix label Feb 6, 2020
@jhy jhy added this to the 1.12.2 milestone Feb 6, 2020
@jhy jhy closed this as completed in fb50d96 Feb 6, 2020
@jhy
Copy link
Owner

jhy commented Feb 6, 2020

Thanks! Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug that we should fix
Projects
None yet
Development

No branches or pull requests

2 participants