Implement selectedIndex IDL attribute on HTMLOptionsCollection.

This commit is contained in:
Corey Farwell 2017-02-12 19:27:08 -05:00
parent a656782075
commit d4ad51bfde
5 changed files with 65 additions and 24 deletions

View file

@ -6,6 +6,7 @@ use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
use dom::bindings::codegen::Bindings::HTMLCollectionBinding::HTMLCollectionMethods; use dom::bindings::codegen::Bindings::HTMLCollectionBinding::HTMLCollectionMethods;
use dom::bindings::codegen::Bindings::HTMLOptionsCollectionBinding; use dom::bindings::codegen::Bindings::HTMLOptionsCollectionBinding;
use dom::bindings::codegen::Bindings::HTMLOptionsCollectionBinding::HTMLOptionsCollectionMethods; use dom::bindings::codegen::Bindings::HTMLOptionsCollectionBinding::HTMLOptionsCollectionMethods;
use dom::bindings::codegen::Bindings::HTMLSelectElementBinding::HTMLSelectElementMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeBinding::NodeMethods; use dom::bindings::codegen::Bindings::NodeBinding::NodeBinding::NodeMethods;
use dom::bindings::codegen::UnionTypes::{HTMLOptionElementOrHTMLOptGroupElement, HTMLElementOrLong}; use dom::bindings::codegen::UnionTypes::{HTMLOptionElementOrHTMLOptGroupElement, HTMLElementOrLong};
use dom::bindings::error::{Error, ErrorResult}; use dom::bindings::error::{Error, ErrorResult};
@ -16,6 +17,7 @@ use dom::bindings::str::DOMString;
use dom::element::Element; use dom::element::Element;
use dom::htmlcollection::{CollectionFilter, HTMLCollection}; use dom::htmlcollection::{CollectionFilter, HTMLCollection};
use dom::htmloptionelement::HTMLOptionElement; use dom::htmloptionelement::HTMLOptionElement;
use dom::htmlselectelement::HTMLSelectElement;
use dom::node::{document_from_node, Node}; use dom::node::{document_from_node, Node};
use dom::window::Window; use dom::window::Window;
@ -182,4 +184,22 @@ impl HTMLOptionsCollectionMethods for HTMLOptionsCollection {
element.Remove(); element.Remove();
} }
} }
// https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-selectedindex
fn SelectedIndex(&self) -> i32 {
self.upcast()
.root_node()
.downcast::<HTMLSelectElement>()
.expect("HTMLOptionsCollection not rooted on a HTMLSelectElement")
.SelectedIndex()
}
// https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-selectedindex
fn SetSelectedIndex(&self, index: i32) {
self.upcast()
.root_node()
.downcast::<HTMLSelectElement>()
.expect("HTMLOptionsCollection not rooted on a HTMLSelectElement")
.SetSelectedIndex(index)
}
} }

View file

@ -14,5 +14,5 @@ interface HTMLOptionsCollection : HTMLCollection {
void add((HTMLOptionElement or HTMLOptGroupElement) element, optional (HTMLElement or long)? before = null); void add((HTMLOptionElement or HTMLOptGroupElement) element, optional (HTMLElement or long)? before = null);
//[CEReactions] //[CEReactions]
void remove(long index); void remove(long index);
//attribute long selectedIndex; attribute long selectedIndex;
}; };

View file

@ -175595,7 +175595,7 @@
"testharness" "testharness"
], ],
"html/semantics/forms/the-select-element/selected-index.html": [ "html/semantics/forms/the-select-element/selected-index.html": [
"3ab30abb683f3fb42b6fdc84555126379d472a3d", "0753a7487a10bde3b879d4c2ed10ba3d0260a48a",
"testharness" "testharness"
], ],
"html/semantics/forms/the-textarea-element/.gitkeep": [ "html/semantics/forms/the-textarea-element/.gitkeep": [

View file

@ -720,18 +720,12 @@
[HTMLCollection interface: calling namedItem(DOMString) on document.all with too few arguments must throw TypeError] [HTMLCollection interface: calling namedItem(DOMString) on document.all with too few arguments must throw TypeError]
expected: FAIL expected: FAIL
[HTMLOptionsCollection interface: attribute selectedIndex]
expected: FAIL
[HTMLOptionsCollection interface: document.createElement("select").options must inherit property "add" with the proper type (3)] [HTMLOptionsCollection interface: document.createElement("select").options must inherit property "add" with the proper type (3)]
expected: FAIL expected: FAIL
[HTMLOptionsCollection interface: document.createElement("select").options must inherit property "remove" with the proper type (4)] [HTMLOptionsCollection interface: document.createElement("select").options must inherit property "remove" with the proper type (4)]
expected: FAIL expected: FAIL
[HTMLOptionsCollection interface: document.createElement("select").options must inherit property "selectedIndex" with the proper type (5)]
expected: FAIL
[HTMLPropertiesCollection interface: existence and properties of interface object] [HTMLPropertiesCollection interface: existence and properties of interface object]
expected: FAIL expected: FAIL
@ -9723,9 +9717,6 @@
[Navigator interface: window.navigator must inherit property "hardwareConcurrency" with the proper type (22)] [Navigator interface: window.navigator must inherit property "hardwareConcurrency" with the proper type (22)]
expected: FAIL expected: FAIL
[HTMLOptionsCollection interface: document.createElement("select").options must inherit property "selectedIndex" with the proper type (4)]
expected: FAIL
[HTMLEmbedElement interface: document.createElement("embed") must inherit property "align" with the proper type (5)] [HTMLEmbedElement interface: document.createElement("embed") must inherit property "align" with the proper type (5)]
expected: FAIL expected: FAIL

View file

@ -28,46 +28,76 @@
</form> </form>
<script> <script>
function assertSelectedIndex(select, value) {
assert_equals(select.selectedIndex, value);
assert_equals(select.options.selectedIndex, value);
}
test(function () { test(function () {
var select = document.getElementById('empty'); var select = document.getElementById('empty');
assert_equals(select.selectedIndex, -1); assertSelectedIndex(select, -1);
}, "get empty"); }, "get empty");
test(function () { test(function () {
var select = document.getElementById('default'); var select = document.getElementById('default');
assert_equals(select.selectedIndex, 0); assertSelectedIndex(select, 0);
}, "get default"); }, "get default");
test(function () { test(function () {
var select = document.getElementById('disabled'); var select = document.getElementById('disabled');
assert_equals(select.selectedIndex, 1); assertSelectedIndex(select, 1);
}, "get disabled"); }, "get disabled");
test(function () { test(function () {
var select = document.getElementById('selected'); var select = document.getElementById('selected');
assert_equals(select.selectedIndex, 1); assertSelectedIndex(select, 1);
}, "get unselected"); }, "get unselected");
test(function () { test(function () {
var select = document.getElementById('empty'); var select = document.getElementById('empty');
select.selectedIndex = 1; select.selectedIndex = 1;
assert_equals(select.selectedIndex, -1); assertSelectedIndex(select, -1);
}, "set empty"); }, "set empty (HTMLSelectElement)");
test(function () {
var select = document.getElementById('empty');
select.options.selectedIndex = 1;
assertSelectedIndex(select, -1);
}, "set empty (HTMLOptionsCollection)");
test(function () { test(function () {
var select = document.getElementById('default'); var select = document.getElementById('default');
assert_equals(select.selectedIndex, 0); assertSelectedIndex(select, 0);
select.selectedIndex = 2; select.selectedIndex = 2;
assert_equals(select.selectedIndex, 2); assertSelectedIndex(select, 2);
}, "set"); this.add_cleanup(() => select.selectedIndex = 0);
}, "set (HTMLSelectElement)");
test(function () {
var select = document.getElementById('default');
assertSelectedIndex(select, 0);
select.options.selectedIndex = 2;
assertSelectedIndex(select, 2);
this.add_cleanup(() => select.selectedIndex = 0);
}, "set (HTMLOptionsCollection)");
test(function () { test(function () {
var select = document.getElementById('selected'); var select = document.getElementById('selected');
var form = document.getElementById('form'); var form = document.getElementById('form');
assert_equals(select.selectedIndex, 1); assertSelectedIndex(select, 1);
select.selectedIndex = 0; select.selectedIndex = 0;
assert_equals(select.selectedIndex, 0); assertSelectedIndex(select, 0);
form.reset(); form.reset();
assert_equals(select.selectedIndex, 1); assertSelectedIndex(select, 1);
}, "set and reset"); }, "set and reset (HTMLSelectElement)");
test(function () {
var select = document.getElementById('selected');
var form = document.getElementById('form');
assertSelectedIndex(select, 1);
select.options.selectedIndex = 0;
assertSelectedIndex(select, 0);
form.reset();
assertSelectedIndex(select, 1);
}, "set and reset (HTMLOptionsCollection)");
</script> </script>