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

Problems with Elements class array methods updating the DOM #2100

Open
821938089 opened this issue Jan 10, 2024 · 8 comments
Open

Problems with Elements class array methods updating the DOM #2100

821938089 opened this issue Jan 10, 2024 · 8 comments

Comments

@821938089
Copy link
Contributor

Since 1.17.1 the array method of the Elements class will update the DOM, this behavior is not what I expected and could potentially cause some problems (e.g. accidentally breaking the DOM).

To be honest, I don't think modifying arrays should update the DOM, this behavior seems counter-intuitive to me.

If this cannot be changed back, I suggest adding an ElementList class, which is equivalent to the previous Elements class.
The inheritance could be ArrayList -> ElementList -> Elements.
The two classes provide a method to convert between them.

@jhy
Copy link
Owner

jhy commented Jul 1, 2024

I don't think I follow what circumstances would break the DOM? What action are you doing that you are modifying the Elements but don't want those modification to go back to the DOM?

Perhaps there could be a detach() method on Elements that decoupled.

@jhy jhy added the needs-more-info More information is needed from the reporter to progress the issue label Jul 1, 2024
@821938089
Copy link
Contributor Author

A reading app that uses a custom set of rule executors to extract book information, chapter lists, content, etc. from the website.

In the internal processing of the rule executor, Elements is used to store the acquired elements, and sometimes the array needs to be cleared and re-added.

The new version of jsoup will delete the elements in the dom at the same time after clearing the array, which will cause the dom to be destroyed.

@jhy jhy removed the needs-more-info More information is needed from the reporter to progress the issue label Jul 1, 2024
@jhy
Copy link
Owner

jhy commented Jul 1, 2024

OK, I guess I'm not clear on why you'd clear and re-add elements vs using a different variable / re-select. Hard to suggest another approach without more detail. But as far as I can guess, a detach() method would be suitable, right? Could potentially by used in combination with clone().

@821938089
Copy link
Contributor Author

What does it mean to use different variables?
Re-select is not possible, it must use custom rule executor to select.

I wish there was a global switch that controlled this behavior so I didn't have to modify the code everywhere.
For example Jsoup.enableElementsMutableDom(false)

There is another problem, some third-party libraries that rely on jsoup will also have this problem, such as: JsoupXpath, these libraries also perform similar operations as me.

@jhy
Copy link
Owner

jhy commented Jul 1, 2024

Can you link me the example in JsoupXpath so I can see the context?

@821938089
Copy link
Contributor Author

Hi, any updates here?
Is it possible to provide a global switch?

@jhy
Copy link
Owner

jhy commented Jul 15, 2024

@821938089 when I have an update I will definitely post it here.

Currently I am leaning towards a method like deselect() which will remove the Element from the Elements array, but not chain that to removing it from the DOM. I generally prefer explicit code than configuration switches. It may be a little more work to update existing code, but it will be clearer for other users in the future. And, Find Usages on remove() will make it easy to inspect and update the changes required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants