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

HtmlInput changeType new instance causes side effects #718

Open
shapiroronny opened this issue Jan 29, 2024 · 3 comments
Open

HtmlInput changeType new instance causes side effects #718

shapiroronny opened this issue Jan 29, 2024 · 3 comments

Comments

@shapiroronny
Copy link
Contributor

In one of the previous versions a breaking change with HtmlInput.setAttribute was introduced.
Currently, when changing type of HtmlInput a new instance is created and the instance worked on is no longer attached.
Looks like this behavior is intended but causes undesired side effects as the user has absolutely no way knowing they are working on a stale object.

(HtmlElementBuilder is a helper class we created, it basically just creates a StringWebResponse and passes it to to PageCreator)

Example 1 :

@Test
public void testInput() {
        HtmlPage page = HtmlElementBuilder.createPageFromString("<html><head></head><body></body></html>");
        HtmlElement input = (HtmlElement) page.createElement("input");
        input.setAttribute("type", "hidden");
        input.setAttribute("name", "test");
        page.getBody().appendChild(input);

        System.out.println(page.asXml());
}

Which result in this result:

<?xml version="1.0" encoding="UTF-8"?>
<html>
 <head/>
 <body>
   <input type="hidden"/>
 </body>
</html>

Another case:

@Test
public void testInput() {
        HtmlPage page = HtmlElementBuilder.createPageFromString("<html><head></head><body></body></html>");
        HtmlElement input = (HtmlElement) page.createElement("input");
        page.getBody().appendChild(input);
        input.setAttribute("type", "hidden");
        input.setAttribute("name", "test");

        System.out.println(page.asXml());
    }

Results in:

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <head/>
  <body>
    <input type="hidden"/>
  </body>
</html>

There are workarounds for this but this feels very error prone.

@shapiroronny shapiroronny changed the title HtmlInput changeType new instance causes HtmlInput changeType new instance causes side effects Jan 29, 2024
@rbri
Copy link
Member

rbri commented Jan 29, 2024

related to #658

@rbri
Copy link
Member

rbri commented Jan 29, 2024

Any good suggestions?

@shapiroronny
Copy link
Contributor Author

shapiroronny commented Jan 30, 2024

As a user an UnsupportedOperationException when passing type to setAttribute to force us to use changeType would have been friendlier but I'm not sure how viable that is.

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