Implement selectedIndex property on <select>.

This commit is contained in:
Corey Farwell 2016-10-16 08:46:23 -04:00
parent f90b256472
commit bec5bf49bd
5 changed files with 110 additions and 7 deletions

View file

@ -299,6 +299,36 @@ impl HTMLSelectElementMethods for HTMLSelectElement {
opt.set_selectedness(false);
}
}
// https://html.spec.whatwg.org/multipage/#dom-select-selectedindex
fn SelectedIndex(&self) -> i32 {
self.upcast::<Node>()
.traverse_preorder()
.filter_map(Root::downcast::<HTMLOptionElement>)
.enumerate()
.filter(|&(_, ref opt_elem)| opt_elem.Selected())
.map(|(i, _)| i as i32)
.next()
.unwrap_or(-1)
}
// https://html.spec.whatwg.org/multipage/#dom-select-selectedindex
fn SetSelectedIndex(&self, index: i32) {
let mut opt_iter = self.upcast::<Node>()
.traverse_preorder()
.filter_map(Root::downcast::<HTMLOptionElement>);
for opt in opt_iter.by_ref().take(index as usize) {
opt.set_selectedness(false);
}
if let Some(opt) = opt_iter.next() {
opt.set_selectedness(true);
opt.set_dirtiness(true);
// Reset remaining <option> elements
for opt in opt_iter {
opt.set_selectedness(false);
}
}
}
}
impl VirtualMethods for HTMLSelectElement {