Skip to content

Commit

Permalink
HTMLFormControlsCollection and RadioNodeList are now iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Aug 1, 2024
1 parent 9ec24d6 commit 3dbbb3a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

<body>
<release version="4.5.0" date="xxxx, 2024" description="WebWorker, Bugfixes">
<action type="add" dev="rbri">
HTMLFormControlsCollection is now iterable.
</action>
<action type="add" dev="rbri">
RadioNodeList is now iterable.
</action>
<action type="add" dev="Lai Quang Duong">
HTMLFormControlsCollection namedItem() implemented.
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
*/
package org.htmlunit.javascript.host.dom;

import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.html.DomNode;
import org.htmlunit.html.HtmlRadioButtonInput;
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxConstructor;
import org.htmlunit.javascript.configuration.JsxGetter;
import org.htmlunit.javascript.configuration.JsxSetter;
import org.htmlunit.javascript.configuration.JsxSymbol;

import java.util.List;

Expand Down Expand Up @@ -118,4 +121,9 @@ public void setValue(final String newValue) {
}
}
}

@JsxSymbol
public Scriptable iterator() {
return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
*/
package org.htmlunit.javascript.host.html;

import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.html.DomElement;
import org.htmlunit.html.DomNode;
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxConstructor;
import org.htmlunit.javascript.configuration.JsxFunction;
import org.htmlunit.javascript.configuration.JsxSymbol;
import org.htmlunit.javascript.host.dom.RadioNodeList;

import java.util.ArrayList;
Expand Down Expand Up @@ -111,4 +114,10 @@ public Object namedItem(final String name) {
nodeList.setElementsSupplier(getElementSupplier());
return nodeList;
}

@JsxSymbol
@Override
public Scriptable iterator() {
return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* Tests for {@link RadioNodeList}.
*
* @author Lai Quang Duong
* @author Ronald Brill
*/
@RunWith(BrowserRunner.class)
public class RadioNodeListTest extends WebDriverTestCase {
Expand Down Expand Up @@ -113,4 +114,26 @@ public void setValue() throws Exception {

loadPageVerifyTitle2(html);
}

/**
* @throws Exception on test failure
*/
@Test
@Alerts({"first", "first", "first", "first"})
public void iterable() throws Exception {
final String html = "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " for (let e of form.first) {\n"
+ " log(e.name)\n"
+ " }\n"
+ " }\n"
+ "</script>\n"
+ "</head><body onload='test()'>\n"
+ FORM_HTML
+ "</body></html>\n";

loadPageVerifyTitle2(html);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* Tests for {@link HTMLFormControlsCollection}.
*
* @author Lai Quang Duong
* @author Ronald Brill
*/
@RunWith(BrowserRunner.class)
public class HTMLFormControlsCollectionTest extends WebDriverTestCase {
Expand Down Expand Up @@ -63,4 +64,33 @@ public void namedItem() throws Exception {

loadPageVerifyTitle2(html);
}

/**
* @throws Exception on test failure
*/
@Test
@Alerts({"first", "first", "first", "first", "second"})
public void iterable() throws Exception {
final String html = "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " for (let e of document.form.elements) {\n"
+ " log(e.name)\n"
+ " }\n"
+ " }\n"
+ "</script>\n"
+ "</head><body onload='test()'>\n"
+ "<form name='form'>\n"
+ "<input type='text' name='first' value='0'/>\n"
+ "<input type='radio' name='first'/>\n"
+ "<input type='radio' name='first' value='2' checked/>\n"
+ "<input type='radio' name='first' value='3'/>\n"
+ "\n"
+ "<input type='radio' name='second' value='1'/>\n"
+ "</form>"
+ "</body></html>\n";

loadPageVerifyTitle2(html);
}
}

0 comments on commit 3dbbb3a

Please sign in to comment.