aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/htmlselectelement.rs
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2016-09-22 13:14:27 -0500
committerGitHub <noreply@github.com>2016-09-22 13:14:27 -0500
commitd74f6adf54b22b4fe8429e0c7ba5327e58818286 (patch)
treea1224c58ec0cac596b98362b1d62a177b275d433 /components/script/dom/htmlselectelement.rs
parentb86420d693d411a0d42a07574dbf4b2f867d0e28 (diff)
parent670693ba7ed62081db5dc7accec1020cf5d71a1a (diff)
downloadservo-d74f6adf54b22b4fe8429e0c7ba5327e58818286.tar.gz
servo-d74f6adf54b22b4fe8429e0c7ba5327e58818286.zip
Auto merge of #13066 - mskrzypkows:htmlselect_index, r=KiChjang
Implement indexed access on select elements <!-- Please describe your changes on the following line: --> Added methods for indexed access on select: SetLength, Length, Item, IndexedGetter --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #11763 (github issue number if applicable). <!-- Either: --> - [x] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/13066) <!-- Reviewable:end -->
Diffstat (limited to 'components/script/dom/htmlselectelement.rs')
-rw-r--r--components/script/dom/htmlselectelement.rs47
1 files changed, 46 insertions, 1 deletions
diff --git a/components/script/dom/htmlselectelement.rs b/components/script/dom/htmlselectelement.rs
index 222dff3e52c..6cf63c7e630 100644
--- a/components/script/dom/htmlselectelement.rs
+++ b/components/script/dom/htmlselectelement.rs
@@ -6,6 +6,7 @@ use dom::attr::Attr;
use dom::bindings::codegen::Bindings::HTMLOptionElementBinding::HTMLOptionElementMethods;
use dom::bindings::codegen::Bindings::HTMLSelectElementBinding;
use dom::bindings::codegen::Bindings::HTMLSelectElementBinding::HTMLSelectElementMethods;
+use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::UnionTypes::HTMLElementOrLong;
use dom::bindings::codegen::UnionTypes::HTMLOptionElementOrHTMLOptGroupElement;
use dom::bindings::inheritance::Castable;
@@ -17,7 +18,7 @@ use dom::htmlelement::HTMLElement;
use dom::htmlfieldsetelement::HTMLFieldSetElement;
use dom::htmlformelement::{FormDatumValue, FormControl, FormDatum, HTMLFormElement};
use dom::htmloptionelement::HTMLOptionElement;
-use dom::node::{Node, UnbindContext, window_from_node};
+use dom::node::{document_from_node, Node, UnbindContext, window_from_node};
use dom::nodelist::NodeList;
use dom::validation::Validatable;
use dom::validitystate::ValidityState;
@@ -183,6 +184,50 @@ impl HTMLSelectElementMethods for HTMLSelectElement {
fn Labels(&self) -> Root<NodeList> {
self.upcast::<HTMLElement>().labels()
}
+
+ // https://html.spec.whatwg.org/multipage/#dom-select-length
+ fn SetLength(&self, value: u32) {
+ let length = self.Length();
+ let node = self.upcast::<Node>();
+ if value < length { // truncate the number of option elements
+ let mut iter = node.rev_children().take((length - value) as usize);
+ while let Some(child) = iter.next() {
+ if let Err(e) = node.RemoveChild(&child) {
+ warn!("Error removing child of HTMLSelectElement: {:?}", e);
+ }
+ }
+ } else if value > length { // add new blank option elements
+ let document = document_from_node(self);
+ for _ in 0..(value - length) {
+ let element = HTMLOptionElement::new(atom!("option"), None, &document.upcast());
+ if let Err(e) = node.AppendChild(element.upcast()) {
+ warn!("error appending child of HTMLSelectElement: {:?}", e);
+ }
+ }
+ }
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-select-length
+ fn Length(&self) -> u32 {
+ self.upcast::<Node>()
+ .traverse_preorder()
+ .filter_map(Root::downcast::<HTMLOptionElement>)
+ .count() as u32
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-select-item
+ fn Item(&self, index: u32) -> Option<Root<Element>> {
+ self.upcast::<Node>()
+ .traverse_preorder()
+ .filter_map(Root::downcast::<HTMLOptionElement>)
+ .nth(index as usize)
+ .map(|item| Root::from_ref(item.upcast()))
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-select-item
+ fn IndexedGetter(&self, index: u32) -> Option<Root<Element>> {
+ self.Item(index)
+ }
}
impl VirtualMethods for HTMLSelectElement {