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 fecd2b2 commit 267873e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,40 @@
*/
package org.htmlunit.javascript.host.html;

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

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;

/**
* 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}
}

public HTMLFormControlsCollection(final DomNode domNode, final boolean attributeChangeSensitive) {
super(domNode, attributeChangeSensitive);
}

HTMLFormControlsCollection(final DomNode domNode, final List<DomNode> initialElements) {
super(domNode, initialElements);
}

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

/**
* @see <a href="https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#the-htmlformcontrolscollection-interface">HTML Standard</a>
*/
@Override
@JsxFunction
public Object namedItem(final String name) {
if (name.isEmpty()) {
return null;
}

List<DomNode> elements = new ArrayList<>();

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

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Local variable 'elements' could be declared final Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/javascript/host/html/HTMLFormControlsCollection.java"},"region":{"endColumn":31,"endLine":70,"startColumn":23,"startLine":70}}}],"message":{"text":"Local variable 'elements' could be declared final"},"ruleId":"LocalVariableCouldBeFinal","ruleIndex":246}
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));
}

RadioNodeList nodeList = new RadioNodeList(getDomNodeOrDie(), elements);

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

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Local variable 'nodeList' could be declared final Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/javascript/host/html/HTMLFormControlsCollection.java"},"region":{"endColumn":31,"endLine":94,"startColumn":23,"startLine":94}}}],"message":{"text":"Local variable 'nodeList' could be declared final"},"ruleId":"LocalVariableCouldBeFinal","ruleIndex":247}
nodeList.setElementsSupplier(getElementSupplier());
return nodeList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ 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, 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 @@ -18,7 +18,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 267873e

Please sign in to comment.