Skip to content

Commit

Permalink
StaticDomNodeList has to be serializable (issue #513)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Oct 22, 2022
1 parent 73ce413 commit 906ac9e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="2.66.0" date="xxxx, 2022" description="Chrome/Edge 106, Firefox 106, Bugfixes, NumberFormat, HTMLMediaElement">
<action type="fix" dev="rbri" issue="513">
StaticDomNodeList has to be serializable.
</action>
<action type="fix" dev="rbri" issue="513">
Lambda functions used by Element have to be serializable.
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package com.gargoylesoftware.htmlunit.html;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.List;

Expand All @@ -23,8 +24,9 @@
* An implementation of DomNodeList that is static.
*
* @author Ahmed Ashour
* @author Ronald Brill
*/
class StaticDomNodeList extends AbstractList<DomNode> implements DomNodeList<DomNode> {
class StaticDomNodeList extends AbstractList<DomNode> implements DomNodeList<DomNode>, Serializable {

private final List<DomNode> elements_;

Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,35 @@ public void serializationLambda() throws Exception {
assertEquals("Hello there!", page2.getHtmlElementById("myId").getFirstChild().getNodeValue());
}

/**
* @throws Exception if the test fails
*/
@Test
public void serializationStaticDomNodeList() throws Exception {
final String content =
"<html><body>\n"
+ "<div id='myId'>Hello there!</div>\n"
+ "<form name='f' id='f'></form>\n"
+ "<script>var y = document.querySelectorAll('*');</script>\n"
+ "</body></html>";

final HtmlPage page1 = loadPage(content);
final byte[] bytes = SerializationUtils.serialize(page1);

final HtmlPage page2 = (HtmlPage) SerializationUtils.deserialize(bytes);

final Iterator<HtmlElement> iterator1 = page1.getHtmlElementDescendants().iterator();
final Iterator<HtmlElement> iterator2 = page2.getHtmlElementDescendants().iterator();
while (iterator1.hasNext()) {
assertTrue(iterator2.hasNext());
final HtmlElement element1 = iterator1.next();
final HtmlElement element2 = iterator2.next();
assertEquals(element1.getNodeName(), element2.getNodeName());
}
assertFalse(iterator2.hasNext());
assertEquals("Hello there!", page2.getHtmlElementById("myId").getFirstChild().getNodeValue());
}

/**
* Verifies that a cloned HtmlPage has its own "idMap_".
* @throws Exception if the test fails
Expand Down

0 comments on commit 906ac9e

Please sign in to comment.