Skip to content

Commit 499b236

Browse files
committed
Document.getSelection() is defined for Firefox and IE also
Document.getElementsByName() is defined for Firefox also Document.getElementsByName() returns a NodeList instead of a HTMLCollection HTMLCollection are iterable now - you can use them in for..of loops - cleanup
1 parent 5f20542 commit 499b236

7 files changed

Lines changed: 101 additions & 77 deletions

File tree

src/changes/changes.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
INCOMPATIBLE CHANGE: CookieManager.getPort(URL) removed. Looks like
1313
<a href="http://code.google.com/p/googleappengine/issues/detail?id=4784"> is solved.
1414
</action>
15+
<action type="fix" dev="rbri">
16+
Document.getSelection() is defined for Firefox and IE also.
17+
</action>
18+
<action type="fix" dev="rbri">
19+
Document.getElementsByName() is defined for Firefox also.
20+
</action>
21+
<action type="fix" dev="rbri">
22+
Document.getElementsByName() returns a NodeList instead of a HTMLCollection.
23+
</action>
1524
<action type="add" dev="rbri" issue="576">
1625
HTMLCollection are iterable now - you can use them in for..of loops.
1726
</action>

src/main/java/org/htmlunit/javascript/host/dom/Document.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,8 +1955,8 @@ public HTMLCollection getElementsByClassName(final String className) {
19551955
* @param elementName - value of the {@code name} attribute to look for
19561956
* @return all HTML elements that have a {@code name} attribute with the specified value
19571957
*/
1958-
@JsxFunction({CHROME, EDGE, IE})
1959-
public HTMLCollection getElementsByName(final String elementName) {
1958+
@JsxFunction({CHROME, EDGE, FF, FF_ESR, IE})
1959+
public NodeList getElementsByName(final String elementName) {
19601960
return null;
19611961
}
19621962

@@ -2009,7 +2009,7 @@ public String getContentType() {
20092009
* Returns the current selection.
20102010
* @return the current selection
20112011
*/
2012-
@JsxFunction({CHROME, EDGE})
2012+
@JsxFunction
20132013
public Selection getSelection() {
20142014
return null;
20152015
}

src/main/java/org/htmlunit/javascript/host/html/HTMLCollection.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,12 @@
3232
import java.util.function.Supplier;
3333

3434
import org.htmlunit.BrowserVersion;
35-
import org.htmlunit.corejs.javascript.BaseFunction;
3635
import org.htmlunit.corejs.javascript.Callable;
3736
import org.htmlunit.corejs.javascript.Context;
37+
import org.htmlunit.corejs.javascript.ES6Iterator;
3838
import org.htmlunit.corejs.javascript.NativeArrayIterator;
39-
import org.htmlunit.corejs.javascript.NativeArrayIterator.ARRAY_ITERATOR_TYPE;
4039
import org.htmlunit.corejs.javascript.ScriptRuntime;
4140
import org.htmlunit.corejs.javascript.Scriptable;
42-
import org.htmlunit.corejs.javascript.ScriptableObject;
43-
import org.htmlunit.corejs.javascript.SymbolKey;
4441
import org.htmlunit.corejs.javascript.Undefined;
4542
import org.htmlunit.html.DomElement;
4643
import org.htmlunit.html.DomNode;
@@ -50,6 +47,7 @@
5047
import org.htmlunit.javascript.configuration.JsxConstructor;
5148
import org.htmlunit.javascript.configuration.JsxFunction;
5249
import org.htmlunit.javascript.configuration.JsxGetter;
50+
import org.htmlunit.javascript.configuration.JsxSymbol;
5351
import org.htmlunit.javascript.host.dom.AbstractList;
5452

5553
/**
@@ -76,18 +74,8 @@ public class HTMLCollection extends AbstractList implements Callable {
7674
*/
7775
@JsxConstructor({CHROME, EDGE, FF, FF_ESR})
7876
public HTMLCollection() {
79-
defineProperty(SymbolKey.ITERATOR, IteratorMethod_, ScriptableObject.DONTENUM);
8077
}
8178

82-
private static org.htmlunit.corejs.javascript.BaseFunction IteratorMethod_ =
83-
new BaseFunction() {
84-
@Override
85-
public Object call(
86-
final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
87-
return new NativeArrayIterator(scope, thisObj, ARRAY_ITERATOR_TYPE.VALUES);
88-
}
89-
};
90-
9179
/**
9280
* Creates an instance.
9381
* @param domNode parent scope
@@ -129,6 +117,11 @@ protected HTMLCollection create(final DomNode parentScope, final List<DomNode> i
129117
return new HTMLCollection(parentScope, initialElements);
130118
}
131119

120+
@JsxSymbol({CHROME, EDGE, FF, FF_ESR})
121+
public ES6Iterator iterator() {
122+
return new NativeArrayIterator(getParentScope(), this, NativeArrayIterator.ARRAY_ITERATOR_TYPE.VALUES);
123+
}
124+
132125
/**
133126
* Returns the length.
134127
* @return the length

src/main/java/org/htmlunit/javascript/host/html/HTMLDocument.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.htmlunit.javascript.host.dom.AbstractList.EffectOnCache;
7474
import org.htmlunit.javascript.host.dom.Attr;
7575
import org.htmlunit.javascript.host.dom.Document;
76+
import org.htmlunit.javascript.host.dom.NodeList;
7677
import org.htmlunit.javascript.host.dom.Selection;
7778
import org.htmlunit.javascript.host.event.Event;
7879
import org.htmlunit.util.Cookie;
@@ -613,17 +614,17 @@ public HTMLCollection getElementsByClassName(final String className) {
613614
* {@inheritDoc}
614615
*/
615616
@Override
616-
@JsxFunction({FF, FF_ESR})
617-
public HTMLCollection getElementsByName(final String elementName) {
617+
public NodeList getElementsByName(final String elementName) {
618618
implicitCloseIfNecessary();
619+
619620
if ("null".equals(elementName)
620621
|| (elementName.isEmpty()
621622
&& getBrowserVersion().hasFeature(HTMLDOCUMENT_ELEMENTS_BY_NAME_EMPTY))) {
622-
return HTMLCollection.emptyCollection(getWindow().getDomNodeOrDie());
623+
return NodeList.staticNodeList(getWindow(), new ArrayList<DomNode>());
623624
}
624625

625626
final HtmlPage page = getPage();
626-
final HTMLCollection elements = new HTMLCollection(page, true);
627+
final NodeList elements = new NodeList(page, true);
627628
elements.setElementsSupplier(
628629
(Supplier<List<DomNode>> & Serializable)
629630
() -> new ArrayList<>(page.getElementsByName(elementName)));
@@ -837,7 +838,6 @@ public void setHead(final ScriptableObject head) {
837838
* {@inheritDoc}
838839
*/
839840
@Override
840-
@JsxFunction
841841
public Selection getSelection() {
842842
return getWindow().getSelectionImpl();
843843
}

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11263,14 +11263,14 @@ public void slot() throws Exception {
1126311263
FF_ESR = "constructor()",
1126411264
IE = "constructor")
1126511265
@HtmlUnitNYI(CHROME = "constructor(),cookie,dispatchEvent(),documentElement,getElementById(),"
11266-
+ "getSelection(),head,open(),write(),writeln()",
11266+
+ "head,open(),write(),writeln()",
1126711267
EDGE = "constructor(),cookie,dispatchEvent(),documentElement,getElementById(),"
11268-
+ "getSelection(),head,open(),write(),writeln()",
11268+
+ "head,open(),write(),writeln()",
1126911269
FF_ESR = "close(),constructor(),cookie,dispatchEvent(),documentElement,getElementById(),"
11270-
+ "getElementsByName(),getSelection(),head,open(),write(),writeln()",
11270+
+ "head,open(),write(),writeln()",
1127111271
FF = "close(),constructor(),cookie,dispatchEvent(),documentElement,getElementById(),"
11272-
+ "getElementsByName(),getSelection(),head,open(),write(),writeln()",
11273-
IE = "constructor,cookie,dispatchEvent(),documentElement,getElementById(),getSelection(),"
11272+
+ "head,open(),write(),writeln()",
11273+
IE = "constructor,cookie,dispatchEvent(),documentElement,getElementById(),"
1127411274
+ "head,open(),write(),writeln()")
1127511275
public void htmlDocument() throws Exception {
1127611276
testString("", "document");
@@ -11806,18 +11806,14 @@ public void sourceBufferList() throws Exception {
1180611806
* @throws Exception if the test fails
1180711807
*/
1180811808
@Test
11809-
@Alerts(CHROME = "constructor(),entries(),forEach(),item(),keys(),length,values()",
11810-
EDGE = "constructor(),entries(),forEach(),item(),keys(),length,values()",
11811-
FF = "constructor(),entries(),forEach(),item(),keys(),length,values()",
11812-
FF_ESR = "constructor(),entries(),forEach(),item(),keys(),length,values()",
11813-
IE = "constructor,item(),length,namedItem(),tags(),urns()")
11814-
@HtmlUnitNYI(CHROME = "constructor(),item(),length,namedItem()",
11809+
@Alerts(CHROME = "constructor(),item(),length,namedItem()",
1181511810
EDGE = "constructor(),item(),length,namedItem()",
1181611811
FF = "constructor(),item(),length,namedItem()",
1181711812
FF_ESR = "constructor(),item(),length,namedItem()",
11818-
IE = "constructor,item(),length,namedItem(),tags()")
11813+
IE = "constructor,item(),length,namedItem(),tags(),urns()")
11814+
@HtmlUnitNYI(IE = "constructor,item(),length,namedItem(),tags()")
1181911815
public void htmlCollection() throws Exception {
11820-
testString("", "document.getElementsByName('myLog')");
11816+
testString("", "document.getElementsByTagName('div')");
1182111817
}
1182211818

1182311819
/**
@@ -11943,10 +11939,25 @@ public void htmlCollectionDocumentScripts() throws Exception {
1194311939
FF = "constructor(),entries(),forEach(),item(),keys(),length,values()",
1194411940
FF_ESR = "constructor(),entries(),forEach(),item(),keys(),length,values()",
1194511941
IE = "constructor,item(),length")
11946-
public void nodeList() throws Exception {
11942+
public void nodeListElementById() throws Exception {
1194711943
testString("", "document.getElementById('myLog').childNodes");
1194811944
}
1194911945

11946+
/**
11947+
* Test {@link NodeList}.
11948+
*
11949+
* @throws Exception if the test fails
11950+
*/
11951+
@Test
11952+
@Alerts(CHROME = "constructor(),entries(),forEach(),item(),keys(),length,values()",
11953+
EDGE = "constructor(),entries(),forEach(),item(),keys(),length,values()",
11954+
FF = "constructor(),entries(),forEach(),item(),keys(),length,values()",
11955+
FF_ESR = "constructor(),entries(),forEach(),item(),keys(),length,values()",
11956+
IE = "constructor,item(),length")
11957+
public void nodeListElementsByName() throws Exception {
11958+
testString("", "document.getElementsByName('myLog')");
11959+
}
11960+
1195011961
/**
1195111962
* Test {@link NodeList}.
1195211963
*

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4553,11 +4553,11 @@ public void slot() throws Exception {
45534553
@Alerts("-")
45544554
@HtmlUnitNYI(CHROME = "open(),write(),writeln()",
45554555
EDGE = "open(),write(),writeln()",
4556-
FF_ESR = "close(),cookie,getElementsByName(),getSelection(),head,"
4556+
FF_ESR = "close(),cookie,head,"
45574557
+ "open(),write(),writeln()",
4558-
FF = "close(),cookie,getElementsByName(),getSelection(),head,"
4558+
FF = "close(),cookie,head,"
45594559
+ "open(),write(),writeln()",
4560-
IE = "getSelection(),open(),write(),writeln()")
4560+
IE = "open(),write(),writeln()")
45614561
public void htmlDocument() throws Exception {
45624562
testString("", "document, xmlDocument");
45634563
}
@@ -4802,8 +4802,9 @@ public void htmlDocument() throws Exception {
48024802
+ "createProcessingInstruction(),createRange(),createTextNode(),createTreeWalker(),"
48034803
+ "currentScript,defaultView,designMode,doctype,documentElement,documentURI,domain,elementFromPoint(),"
48044804
+ "embeds,evaluate(),execCommand(),fgColor,firstElementChild,fonts,forms,"
4805-
+ "getElementById(),getElementsByClassName(),"
4806-
+ "getElementsByTagName(),getElementsByTagNameNS(),hasFocus(),hidden,images,implementation,"
4805+
+ "getElementById(),getElementsByClassName(),getElementsByName(),"
4806+
+ "getElementsByTagName(),getElementsByTagNameNS(),getSelection(),"
4807+
+ "hasFocus(),hidden,images,implementation,"
48074808
+ "importNode(),inputEncoding,lastElementChild,lastModified,linkColor,links,location,onabort,"
48084809
+ "onafterscriptexecute,onbeforescriptexecute,onblur,oncanplay,oncanplaythrough,onchange,"
48094810
+ "onclick,oncontextmenu,oncopy,oncut,ondblclick,ondrag,ondragend,ondragenter,ondragleave,"
@@ -4824,8 +4825,9 @@ public void htmlDocument() throws Exception {
48244825
+ "createProcessingInstruction(),createRange(),createTextNode(),createTreeWalker(),"
48254826
+ "currentScript,defaultView,designMode,doctype,documentElement,documentURI,domain,elementFromPoint(),"
48264827
+ "embeds,evaluate(),execCommand(),fgColor,firstElementChild,fonts,forms,"
4827-
+ "getElementById(),getElementsByClassName(),"
4828-
+ "getElementsByTagName(),getElementsByTagNameNS(),hasFocus(),hidden,images,implementation,"
4828+
+ "getElementById(),getElementsByClassName(),getElementsByName(),"
4829+
+ "getElementsByTagName(),getElementsByTagNameNS(),getSelection(),"
4830+
+ "hasFocus(),hidden,images,implementation,"
48294831
+ "importNode(),inputEncoding,lastElementChild,lastModified,linkColor,links,location,onabort,"
48304832
+ "onafterscriptexecute,onbeforescriptexecute,onblur,oncanplay,oncanplaythrough,onchange,"
48314833
+ "onclick,oncontextmenu,oncopy,oncut,ondblclick,ondrag,ondragend,ondragenter,ondragleave,"
@@ -4845,7 +4847,8 @@ public void htmlDocument() throws Exception {
48454847
+ "createProcessingInstruction(),createRange(),createTextNode(),createTreeWalker(),defaultCharset,"
48464848
+ "defaultView,designMode,doctype,documentElement,documentMode,domain,elementFromPoint(),embeds,"
48474849
+ "execCommand(),fgColor,forms,frames,getElementById(),getElementsByClassName(),getElementsByName(),"
4848-
+ "getElementsByTagName(),getElementsByTagNameNS(),hasFocus(),head,hidden,images,implementation,"
4850+
+ "getElementsByTagName(),getElementsByTagNameNS(),getSelection(),"
4851+
+ "hasFocus(),head,hidden,images,implementation,"
48494852
+ "importNode(),inputEncoding,lastModified,linkColor,links,location,onabort,onactivate,"
48504853
+ "onbeforeactivate,onbeforedeactivate,onblur,oncanplay,oncanplaythrough,onchange,onclick,"
48514854
+ "oncontextmenu,ondblclick,ondeactivate,ondrag,ondragend,ondragenter,ondragleave,ondragover,"

src/test/java/org/htmlunit/javascript/host/html/HTMLCollectionTest.java

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,18 @@ public void has() throws Exception {
363363
* @throws Exception if the test fails
364364
*/
365365
@Test
366-
@Alerts({"myForm", "mySecondForm"})
366+
@Alerts(DEFAULT = {"myForm", "mySecondForm"},
367+
IE = "exception")
367368
public void forOf() throws Exception {
368369
final String html = "<html><head>\n"
369370
+ "<script>\n"
370371
+ LOG_TITLE_FUNCTION
371372
+ " function test() {\n"
372-
+ " for (f of document.forms) {\n"
373-
+ " log(f.name);\n"
374-
+ " }\n"
373+
+ " try {"
374+
+ " for (f of document.forms) {\n"
375+
+ " log(f.name);\n"
376+
+ " }\n"
377+
+ " } catch(e) { log('exception') }\n"
375378
+ " }\n"
376379
+ "</script>\n"
377380
+ "</head>\n"
@@ -387,28 +390,30 @@ public void forOf() throws Exception {
387390
* @throws Exception if the test fails
388391
*/
389392
@Test
390-
@Alerts({"myForm", "mySecondForm", "dynamicForm", "-", "myForm", "mySecondForm", "dynamicForm"})
393+
@Alerts(DEFAULT = {"myForm", "mySecondForm", "dynamicForm", "-", "myForm", "mySecondForm", "dynamicForm"},
394+
IE = "exception")
391395
public void forOfDynamicAtEnd() throws Exception {
392396
final String html = "<html><head>\n"
393397
+ "<script>\n"
394398
+ LOG_TITLE_FUNCTION
395399
+ " function test() {\n"
396-
+ " var i = 0;"
397-
+ " for (f of document.forms) {\n"
398-
+ " i++;\n"
399-
+ " if (i == 1) {\n"
400-
+ " var frm = document.createElement('FORM');\n"
401-
+ " frm.name = 'dynamicForm';\n"
402-
+ " document.body.appendChild(frm);\n"
400+
+ " try {"
401+
+ " var i = 0;\n"
402+
+ " for (f of document.forms) {\n"
403+
+ " i++;\n"
404+
+ " if (i == 1) {\n"
405+
+ " var frm = document.createElement('FORM');\n"
406+
+ " frm.name = 'dynamicForm';\n"
407+
+ " document.body.appendChild(frm);\n"
408+
+ " }\n"
409+
+ " log(f.name);\n"
403410
+ " }\n"
404-
+ " log(f.name);\n"
405-
+ " }\n"
406411

407-
+ " log('-');\n"
408-
+ " for (f of document.forms) {\n"
409-
+ " log(f.name);\n"
410-
+ " }\n"
411-
+ " }\n"
412+
+ " log('-');\n"
413+
+ " for (f of document.forms) {\n"
414+
+ " log(f.name);\n"
415+
+ " }\n"
416+
+ " } catch(e) { log('exception') }\n"
412417
+ "</script>\n"
413418
+ "</head>\n"
414419
+ "<body onload='test()'>\n"
@@ -423,27 +428,30 @@ public void forOfDynamicAtEnd() throws Exception {
423428
* @throws Exception if the test fails
424429
*/
425430
@Test
426-
@Alerts({"myForm", "myForm", "mySecondForm", "-", "dynamicForm", "myForm", "mySecondForm"})
431+
@Alerts(DEFAULT = {"myForm", "myForm", "mySecondForm", "-", "dynamicForm", "myForm", "mySecondForm"},
432+
IE = "exception")
427433
public void forOfDynamicAtStart() throws Exception {
428434
final String html = "<html><head>\n"
429435
+ "<script>\n"
430436
+ LOG_TITLE_FUNCTION
431437
+ " function test() {\n"
432-
+ " var i = 0;"
433-
+ " for (f of document.forms) {\n"
434-
+ " i++;\n"
435-
+ " if (i == 1) {\n"
436-
+ " var frm = document.createElement('FORM');\n"
437-
+ " frm.name = 'dynamicForm';\n"
438-
+ " document.body.insertBefore(frm, document.getElementsByName('myForm')[0]);\n"
438+
+ " try{\n"
439+
+ " var i = 0;\n"
440+
+ " for (f of document.forms) {\n"
441+
+ " i++;\n"
442+
+ " if (i == 1) {\n"
443+
+ " var frm = document.createElement('FORM');\n"
444+
+ " frm.name = 'dynamicForm';\n"
445+
+ " document.body.insertBefore(frm, document.getElementsByName('myForm')[0]);\n"
446+
+ " }\n"
447+
+ " log(f.name);\n"
439448
+ " }\n"
440-
+ " log(f.name);\n"
441-
+ " }\n"
442449

443-
+ " log('-');\n"
444-
+ " for (f of document.forms) {\n"
445-
+ " log(f.name);\n"
446-
+ " }\n"
450+
+ " log('-');\n"
451+
+ " for (f of document.forms) {\n"
452+
+ " log(f.name);\n"
453+
+ " }\n"
454+
+ " } catch(e) { log('exception') }\n"
447455
+ " }\n"
448456
+ "</script>\n"
449457
+ "</head>\n"

0 commit comments

Comments
 (0)