Skip to content

Commit

Permalink
HTMLFormControlsCollection: implement namedItem()
Browse files Browse the repository at this point in the history
  • Loading branch information
duonglaiquang committed Aug 1, 2024
1 parent cdb1f85 commit cdcf65f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,51 @@
*/
package org.htmlunit.javascript.host.html;

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

import java.util.ArrayList;
import java.util.List;

/**
* A JavaScript object for {@code HTMLFormControlsCollection}.
*
* @author Ahmed Ashour
* @author Ronald Brill
* @author Lai Quang Duong
*/
@JsxClass
public class HTMLFormControlsCollection extends HTMLCollection {

/**
* Creates an instance.
*/
public HTMLFormControlsCollection() {

Check failure on line 40 in src/main/java/org/htmlunit/javascript/host/html/HTMLFormControlsCollection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Document empty constructor Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/javascript/host/html/HTMLFormControlsCollection.java"},"region":{"endColumn":38,"endLine":40,"startColumn":12,"startLine":40}}}],"message":{"text":"Document empty constructor"},"ruleId":"UncommentedEmptyConstructor","ruleIndex":221}

Check failure on line 40 in src/main/java/org/htmlunit/javascript/host/html/HTMLFormControlsCollection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 It is a good practice to call super() in a constructor Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/javascript/host/html/HTMLFormControlsCollection.java"},"region":{"endColumn":38,"endLine":40,"startColumn":12,"startLine":40}}}],"message":{"text":"It is a good practice to call super() in a constructor"},"ruleId":"CallSuperInConstructor","ruleIndex":183}
}

/**
* Creates an instance.
* @param domNode parent scope
* @param attributeChangeSensitive indicates if the content of the collection may change when an attribute
* of a descendant node of parentScope changes (attribute added, modified or removed)
*/
public HTMLFormControlsCollection(final DomNode domNode, final boolean attributeChangeSensitive) {
super(domNode, attributeChangeSensitive);
}

/**
* Constructs an instance with an initial cache value.
* @param domNode the parent scope, on which we listen for changes
* @param initialElements the initial content for the cache
*/
HTMLFormControlsCollection(final DomNode domNode, final List<DomNode> initialElements) {
super(domNode, initialElements);
}

/**
* JavaScript constructor.
*/
Expand All @@ -34,4 +67,47 @@ public class HTMLFormControlsCollection extends HTMLCollection {
public void jsConstructor() {
super.jsConstructor();
}

/**
* Returns the element with ID or name match the specified value from the collection.
* If there are multiple matching elements, then a RadioNodeList object containing all those elements is returned.
* @param name the name or id the element or elements to return
* @return the element or elements corresponding to the specified name or id
* @see <a href="https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#the-htmlformcontrolscollection-interface">HTML Standard</a>

Check failure on line 76 in src/main/java/org/htmlunit/javascript/host/html/HTMLFormControlsCollection.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 146). Raw Output: /home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/javascript/host/html/HTMLFormControlsCollection.java:76:0: error: Line is longer than 120 characters (found 146). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
*/
@Override
@JsxFunction
public Object namedItem(final String name) {
if (name.isEmpty()) {
return null;
}

final List<DomNode> elements = new ArrayList<>();
for (final Object next : getElements()) {
if (next instanceof DomElement) {
final DomElement elem = (DomElement) next;
final String nodeName = elem.getAttributeDirect(DomElement.NAME_ATTRIBUTE);
if (name.equals(nodeName)) {
elements.add(elem);
continue;
}

final String id = elem.getId();
if (name.equals(id)) {
elements.add(elem);
}
}
}

if (elements.isEmpty()) {
return null;
}
if (elements.size() == 1) {
return getScriptableForElement(elements.get(0));
}

final RadioNodeList nodeList = new RadioNodeList(getDomNodeOrDie(), elements);
nodeList.setElementsSupplier(getElementSupplier());
return nodeList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ public void setName(final String name) {
* @return the value of this property
*/
@JsxGetter
public HTMLCollection getElements() {
public HTMLFormControlsCollection getElements() {
final HtmlForm htmlForm = getHtmlForm();

final HTMLCollection elements = new HTMLCollection(htmlForm, false) {
final HTMLFormControlsCollection elements =new HTMLFormControlsCollection(htmlForm,

Check failure on line 96 in src/main/java/org/htmlunit/javascript/host/html/HTMLFormElement.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 '=' is not followed by whitespace. Raw Output: /home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/javascript/host/html/HTMLFormElement.java:96:51: error: '=' is not followed by whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck)
false) {
@Override
protected Object getWithPreemption(final String name) {
return HTMLFormElement.this.getWithPreemption(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class HTMLFormControlsCollectionTest extends WebDriverTestCase {
* @throws Exception on test failure
*/
@Test
@Alerts({"false", "false", "0", "true", "1", "null"}) //FIXME: should be "true", "true", "2", "true", "1", "null"
@Alerts({"true", "true", "2", "true", "1", "null"})
public void namedItem() throws Exception {
final String html = "<html><head>\n"
+ "<script>\n"
Expand Down

0 comments on commit cdcf65f

Please sign in to comment.