Skip to content

Commit 064699c

Browse files
committed
HTMLElement#getAttributeNames() implemented
1 parent 9ce43b6 commit 064699c

File tree

5 files changed

+427
-17
lines changed

5 files changed

+427
-17
lines changed

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
<body>
1010
<release version="4.20.0" date="December xx, 2025" description="Bugfixes">
11+
<action type="add" dev="rbri">
12+
HTMLElement#getAttributeNames() implemented.
13+
</action>
1114
<action type="add" dev="rbri" issue="#1053">
12-
HTMLElement#moveBefore() implemented.
15+
HTMLElement#moveBefore() implemented (also for Document and DocumentFragment).
1316
</action>
1417
<action type="update" dev="rbri">
1518
neko: code cleanup and warning fixes.

src/main/java/org/htmlunit/javascript/host/Element.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.IOException;
2626
import java.io.Serializable;
27+
import java.util.ArrayList;
2728
import java.util.HashMap;
2829
import java.util.Map;
2930
import java.util.Objects;
@@ -673,6 +674,26 @@ public DOMRectList getClientRects() {
673674
return rectList;
674675
}
675676

677+
/**
678+
* @return the attribute names of the element as an Array of strings.
679+
* If the element has no attributes it returns an empty array.
680+
*/
681+
@JsxFunction
682+
public Scriptable getAttributeNames() {
683+
final org.w3c.dom.NamedNodeMap attributes = getDomNodeOrDie().getAttributes();
684+
685+
if (attributes.getLength() == 0) {
686+
return JavaScriptEngine.newArray(this, 0);
687+
}
688+
689+
final ArrayList<String> res = new ArrayList<>();
690+
for (int i = 0; i < attributes.getLength(); i++) {
691+
res.add(attributes.item(i).getNodeName());
692+
}
693+
694+
return JavaScriptEngine.newArray(this, res.toArray());
695+
}
696+
676697
/**
677698
* Returns whether the {@code display} is {@code none} or not.
678699
* @return whether the {@code display} is {@code none} or not

src/test/java/org/htmlunit/general/ElementOwnPropertiesTest.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ public void htmlElement() throws Exception {
625625
+ "webkitMatchesSelector()")
626626
@HtmlUnitNYI(CHROME = "after(),append(),attributes[GCE],before(),childElementCount[GCE],children[GCE],"
627627
+ "classList[GCE],className[GSCE],clientHeight[GCE],clientLeft[GCE],clientTop[GCE],"
628-
+ "clientWidth[GCE],closest(),constructor(),firstElementChild[GCE],getAttribute(),getAttributeNode(),"
628+
+ "clientWidth[GCE],closest(),constructor(),firstElementChild[GCE],getAttribute(),"
629+
+ "getAttributeNames(),getAttributeNode(),"
629630
+ "getAttributeNodeNS(),getAttributeNS(),getBoundingClientRect(),getClientRects(),"
630631
+ "getElementsByClassName(),getElementsByTagName(),getElementsByTagNameNS(),getHTML(),"
631632
+ "hasAttribute(),hasAttributeNS(),hasAttributes(),id[GSCE],innerHTML[GSCE],insertAdjacentElement(),"
@@ -641,7 +642,8 @@ public void htmlElement() throws Exception {
641642
+ "toggleAttribute(),webkitMatchesSelector()",
642643
EDGE = "after(),append(),attributes[GCE],before(),childElementCount[GCE],children[GCE],"
643644
+ "classList[GCE],className[GSCE],clientHeight[GCE],clientLeft[GCE],clientTop[GCE],"
644-
+ "clientWidth[GCE],closest(),constructor(),firstElementChild[GCE],getAttribute(),getAttributeNode(),"
645+
+ "clientWidth[GCE],closest(),constructor(),firstElementChild[GCE],getAttribute(),"
646+
+ "getAttributeNames(),getAttributeNode(),"
645647
+ "getAttributeNodeNS(),getAttributeNS(),getBoundingClientRect(),getClientRects(),"
646648
+ "getElementsByClassName(),getElementsByTagName(),getElementsByTagNameNS(),getHTML(),"
647649
+ "hasAttribute(),hasAttributeNS(),hasAttributes(),id[GSCE],innerHTML[GSCE],insertAdjacentElement(),"
@@ -657,7 +659,8 @@ public void htmlElement() throws Exception {
657659
+ "toggleAttribute(),webkitMatchesSelector()",
658660
FF = "after(),append(),attributes[GCE],before(),childElementCount[GCE],children[GCE],classList[GCE],"
659661
+ "className[GSCE],clientHeight[GCE],clientLeft[GCE],clientTop[GCE],clientWidth[GCE],closest(),"
660-
+ "constructor(),firstElementChild[GCE],getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
662+
+ "constructor(),firstElementChild[GCE],getAttribute(),"
663+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
661664
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
662665
+ "getElementsByTagName(),getElementsByTagNameNS(),"
663666
+ "getHTML(),hasAttribute(),hasAttributeNS(),hasAttributes(),"
@@ -673,7 +676,8 @@ public void htmlElement() throws Exception {
673676
+ "webkitMatchesSelector()",
674677
FF_ESR = "after(),append(),attributes[GCE],before(),childElementCount[GCE],children[GCE],classList[GCE],"
675678
+ "className[GSCE],clientHeight[GCE],clientLeft[GCE],clientTop[GCE],clientWidth[GCE],closest(),"
676-
+ "constructor(),firstElementChild[GCE],getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
679+
+ "constructor(),firstElementChild[GCE],getAttribute(),"
680+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
677681
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
678682
+ "getElementsByTagName(),getElementsByTagNameNS(),"
679683
+ "getHTML(),hasAttribute(),hasAttributeNS(),hasAttributes(),"
@@ -825,7 +829,8 @@ public void element() throws Exception {
825829
@HtmlUnitNYI(CHROME = "after(),append(),attributes[GCE],before(),childElementCount[GCE],"
826830
+ "children[GCE],classList[GCE],"
827831
+ "className[GSCE],clientHeight[GCE],clientLeft[GCE],clientTop[GCE],clientWidth[GCE],closest(),"
828-
+ "constructor(),firstElementChild[GCE],getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
832+
+ "constructor(),firstElementChild[GCE],getAttribute(),"
833+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
829834
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
830835
+ "getElementsByTagName(),getElementsByTagNameNS(),getHTML(),"
831836
+ "hasAttribute(),hasAttributeNS(),"
@@ -843,7 +848,8 @@ public void element() throws Exception {
843848
EDGE = "after(),append(),attributes[GCE],before(),childElementCount[GCE],"
844849
+ "children[GCE],classList[GCE],"
845850
+ "className[GSCE],clientHeight[GCE],clientLeft[GCE],clientTop[GCE],clientWidth[GCE],closest(),"
846-
+ "constructor(),firstElementChild[GCE],getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
851+
+ "constructor(),firstElementChild[GCE],getAttribute(),"
852+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
847853
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
848854
+ "getElementsByTagName(),getElementsByTagNameNS(),getHTML(),"
849855
+ "hasAttribute(),hasAttributeNS(),"
@@ -861,7 +867,8 @@ public void element() throws Exception {
861867
FF = "after(),append(),attributes[GCE],before(),childElementCount[GCE],"
862868
+ "children[GCE],classList[GCE],"
863869
+ "className[GSCE],clientHeight[GCE],clientLeft[GCE],clientTop[GCE],clientWidth[GCE],closest(),"
864-
+ "constructor(),firstElementChild[GCE],getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
870+
+ "constructor(),firstElementChild[GCE],getAttribute(),"
871+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
865872
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
866873
+ "getElementsByTagName(),getElementsByTagNameNS(),getHTML(),hasAttribute(),hasAttributeNS(),"
867874
+ "hasAttributes(),id[GSCE],innerHTML[GSCE],insertAdjacentElement(),insertAdjacentHTML(),"
@@ -879,7 +886,8 @@ public void element() throws Exception {
879886
FF_ESR = "after(),append(),attributes[GCE],before(),childElementCount[GCE],"
880887
+ "children[GCE],classList[GCE],"
881888
+ "className[GSCE],clientHeight[GCE],clientLeft[GCE],clientTop[GCE],clientWidth[GCE],closest(),"
882-
+ "constructor(),firstElementChild[GCE],getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
889+
+ "constructor(),firstElementChild[GCE],getAttribute(),"
890+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
883891
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
884892
+ "getElementsByTagName(),getElementsByTagNameNS(),getHTML(),hasAttribute(),hasAttributeNS(),"
885893
+ "hasAttributes(),id[GSCE],innerHTML[GSCE],insertAdjacentElement(),insertAdjacentHTML(),"

src/test/java/org/htmlunit/general/ElementPropertiesTest.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,8 @@ public void htmlElement() throws Exception {
724724
+ "webkitMatchesSelector()")
725725
@HtmlUnitNYI(CHROME = "append(),attributes,"
726726
+ "childElementCount,children,classList,className,clientHeight,clientLeft,clientTop,"
727-
+ "clientWidth,closest(),firstElementChild,getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
727+
+ "clientWidth,closest(),firstElementChild,getAttribute(),"
728+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
728729
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
729730
+ "getElementsByTagName(),getElementsByTagNameNS(),"
730731
+ "getHTML(),hasAttribute(),hasAttributeNS(),hasAttributes(),"
@@ -740,7 +741,8 @@ public void htmlElement() throws Exception {
740741
+ "tagName,toggleAttribute(),webkitMatchesSelector()",
741742
EDGE = "append(),attributes,"
742743
+ "childElementCount,children,classList,className,clientHeight,clientLeft,clientTop,"
743-
+ "clientWidth,closest(),firstElementChild,getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
744+
+ "clientWidth,closest(),firstElementChild,getAttribute(),"
745+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
744746
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
745747
+ "getElementsByTagName(),getElementsByTagNameNS(),"
746748
+ "getHTML(),hasAttribute(),hasAttributeNS(),hasAttributes(),"
@@ -756,7 +758,8 @@ public void htmlElement() throws Exception {
756758
+ "tagName,toggleAttribute(),webkitMatchesSelector()",
757759
FF = "append(),attributes,"
758760
+ "childElementCount,children,classList,className,clientHeight,clientLeft,clientTop,"
759-
+ "clientWidth,closest(),firstElementChild,getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
761+
+ "clientWidth,closest(),firstElementChild,getAttribute(),"
762+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
760763
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
761764
+ "getElementsByTagName(),getElementsByTagNameNS(),getHTML(),hasAttribute(),hasAttributeNS(),"
762765
+ "hasAttributes(),id,innerHTML,insertAdjacentElement(),insertAdjacentHTML(),insertAdjacentText(),"
@@ -771,7 +774,8 @@ public void htmlElement() throws Exception {
771774
+ "tagName,toggleAttribute(),webkitMatchesSelector()",
772775
FF_ESR = "append(),attributes,"
773776
+ "childElementCount,children,classList,className,clientHeight,clientLeft,clientTop,"
774-
+ "clientWidth,closest(),firstElementChild,getAttribute(),getAttributeNode(),getAttributeNodeNS(),"
777+
+ "clientWidth,closest(),firstElementChild,getAttribute(),"
778+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),"
775779
+ "getAttributeNS(),getBoundingClientRect(),getClientRects(),getElementsByClassName(),"
776780
+ "getElementsByTagName(),getElementsByTagNameNS(),getHTML(),hasAttribute(),hasAttributeNS(),"
777781
+ "hasAttributes(),id,innerHTML,insertAdjacentElement(),insertAdjacentHTML(),insertAdjacentText(),"
@@ -890,7 +894,8 @@ public void element() throws Exception {
890894
+ "toggleAttribute(),"
891895
+ "webkitMatchesSelector()")
892896
@HtmlUnitNYI(CHROME = "after(),attributes,before(),classList,className,clientHeight,clientLeft,clientTop,"
893-
+ "clientWidth,closest(),getAttribute(),getAttributeNode(),getAttributeNodeNS(),getAttributeNS(),"
897+
+ "clientWidth,closest(),getAttribute(),"
898+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),getAttributeNS(),"
894899
+ "getBoundingClientRect(),getClientRects(),getElementsByClassName(),getElementsByTagName(),"
895900
+ "getElementsByTagNameNS(),getHTML(),"
896901
+ "hasAttribute(),hasAttributeNS(),hasAttributes(),id,innerHTML,"
@@ -903,7 +908,8 @@ public void element() throws Exception {
903908
+ "scrollWidth,setAttribute(),setAttributeNode(),setAttributeNS(),"
904909
+ "tagName,toggleAttribute(),webkitMatchesSelector()",
905910
EDGE = "after(),attributes,before(),classList,className,clientHeight,clientLeft,clientTop,"
906-
+ "clientWidth,closest(),getAttribute(),getAttributeNode(),getAttributeNodeNS(),getAttributeNS(),"
911+
+ "clientWidth,closest(),getAttribute(),"
912+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),getAttributeNS(),"
907913
+ "getBoundingClientRect(),getClientRects(),getElementsByClassName(),getElementsByTagName(),"
908914
+ "getElementsByTagNameNS(),getHTML(),"
909915
+ "hasAttribute(),hasAttributeNS(),hasAttributes(),id,innerHTML,"
@@ -917,7 +923,8 @@ public void element() throws Exception {
917923
+ "tagName,toggleAttribute(),webkitMatchesSelector()",
918924
FF = "after(),attributes,before(),"
919925
+ "classList,className,clientHeight,clientLeft,clientTop,clientWidth,"
920-
+ "closest(),getAttribute(),getAttributeNode(),getAttributeNodeNS(),getAttributeNS(),"
926+
+ "closest(),getAttribute(),"
927+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),getAttributeNS(),"
921928
+ "getBoundingClientRect(),"
922929
+ "getClientRects(),getElementsByClassName(),getElementsByTagName(),getElementsByTagNameNS(),getHTML(),"
923930
+ "hasAttribute(),hasAttributeNS(),hasAttributes(),id,innerHTML,insertAdjacentElement(),"
@@ -930,7 +937,8 @@ public void element() throws Exception {
930937
+ "tagName,toggleAttribute(),webkitMatchesSelector()",
931938
FF_ESR = "after(),attributes,before(),"
932939
+ "classList,className,clientHeight,clientLeft,clientTop,clientWidth,"
933-
+ "closest(),getAttribute(),getAttributeNode(),getAttributeNodeNS(),getAttributeNS(),"
940+
+ "closest(),getAttribute(),"
941+
+ "getAttributeNames(),getAttributeNode(),getAttributeNodeNS(),getAttributeNS(),"
934942
+ "getBoundingClientRect(),"
935943
+ "getClientRects(),getElementsByClassName(),getElementsByTagName(),getElementsByTagNameNS(),getHTML(),"
936944
+ "hasAttribute(),hasAttributeNS(),hasAttributes(),id,innerHTML,insertAdjacentElement(),"

0 commit comments

Comments
 (0)