Skip to content

Commit

Permalink
Add fixes based on review.
Browse files Browse the repository at this point in the history
- Use if let instead of match for Option

- Refactor common code into pick_if_selected_and_reset
  • Loading branch information
dagnir committed Oct 28, 2015
1 parent 92e0083 commit 4849033
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
28 changes: 13 additions & 15 deletions components/script/dom/htmloptionelement.rs
Expand Up @@ -56,6 +56,17 @@ impl HTMLOptionElement {
pub fn set_selectedness(&self, selected: bool) {
self.selectedness.set(selected);
}

fn pick_if_selected_and_reset(&self) {
if let Some(select) = self.upcast::<Node>().ancestors()
.filter_map(Root::downcast::<HTMLSelectElement>)
.next() {
if self.Selected() {
select.pick_option(self);
}
select.ask_for_reset();
}
}
}

fn collect_text(element: &Element, value: &mut DOMString) {
Expand Down Expand Up @@ -139,13 +150,7 @@ impl HTMLOptionElementMethods for HTMLOptionElement {
fn SetSelected(&self, selected: bool) {
self.dirtiness.set(true);
self.selectedness.set(selected);
if let Some(select) = self.upcast::<Node>().ancestors()
.filter_map(Root::downcast::<HTMLSelectElement>).next() {
if selected {
select.pick_option(self);
}
select.ask_for_reset();
}
self.pick_if_selected_and_reset();
}
}

Expand Down Expand Up @@ -198,14 +203,7 @@ impl VirtualMethods for HTMLOptionElement {

self.upcast::<Element>().check_parent_disabled_state_for_option();

if let Some(select) = self.upcast::<Node>().ancestors()
.filter_map(Root::downcast::<HTMLSelectElement>)
.next() {
if self.Selected() {
select.pick_option(self);
}
select.ask_for_reset();
}
self.pick_if_selected_and_reset();
}

fn unbind_from_tree(&self, tree_in_doc: bool) {
Expand Down
13 changes: 6 additions & 7 deletions components/script/dom/htmlselectelement.rs
Expand Up @@ -70,13 +70,12 @@ impl HTMLSelectElement {
}
}

match last_selected {
Some(last_selected) => last_selected.set_selectedness(true),
None => {
if self.display_size() == 1 {
if let Some(first_enabled) = first_enabled {
first_enabled.set_selectedness(true);
}
if let Some(last_selected) = last_selected {
last_selected.set_selectedness(true);
} else {
if self.display_size() == 1 {
if let Some(first_enabled) = first_enabled {
first_enabled.set_selectedness(true);
}
}
}
Expand Down

0 comments on commit 4849033

Please sign in to comment.