mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
Auto merge of #25413 - pshaughn:optionindex, r=jdm
Implement HTMLOptionElement.index <!-- Please describe your changes on the following line: --> Made list_of_options public in HTMLSelectElement so HTMLOptionElement can see it, added index-getting to HTMLOptionElement. --- <!-- 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 #25392 <!-- Either: --> - [X] There are tests for these changes OR <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
commit
6b79a8f042
5 changed files with 43 additions and 25 deletions
|
@ -28,6 +28,7 @@ use crate::dom::window::Window;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use html5ever::{LocalName, Prefix, QualName};
|
use html5ever::{LocalName, Prefix, QualName};
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
use std::convert::TryInto;
|
||||||
use style::element_state::ElementState;
|
use style::element_state::ElementState;
|
||||||
use style::str::{split_html_space_chars, str_join};
|
use style::str::{split_html_space_chars, str_join};
|
||||||
|
|
||||||
|
@ -127,6 +128,41 @@ impl HTMLOptionElement {
|
||||||
select.ask_for_reset();
|
select.ask_for_reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/#concept-option-index
|
||||||
|
fn index(&self) -> i32 {
|
||||||
|
if let Some(parent) = self.upcast::<Node>().GetParentNode() {
|
||||||
|
if let Some(select_parent) = parent.downcast::<HTMLSelectElement>() {
|
||||||
|
// return index in parent select's list of options
|
||||||
|
return self.index_in_select(select_parent);
|
||||||
|
} else if parent.is::<HTMLOptGroupElement>() {
|
||||||
|
if let Some(grandparent) = parent.GetParentNode() {
|
||||||
|
if let Some(select_grandparent) = grandparent.downcast::<HTMLSelectElement>() {
|
||||||
|
// return index in grandparent select's list of options
|
||||||
|
return self.index_in_select(select_grandparent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// "If the option element is not in a list of options,
|
||||||
|
// then the option element's index is zero."
|
||||||
|
// self is neither a child of a select, nor a grandchild of a select
|
||||||
|
// via an optgroup, so it is not in a list of options
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn index_in_select(&self, select: &HTMLSelectElement) -> i32 {
|
||||||
|
match select.list_of_options().position(|n| &*n == self) {
|
||||||
|
Some(index) => index.try_into().unwrap_or(0),
|
||||||
|
None => {
|
||||||
|
// shouldn't happen but not worth a browser panic
|
||||||
|
warn!(
|
||||||
|
"HTMLOptionElement called index_in_select at a select that did not contain it"
|
||||||
|
);
|
||||||
|
0
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME(ajeffrey): Provide a way of buffering DOMStrings other than using Strings
|
// FIXME(ajeffrey): Provide a way of buffering DOMStrings other than using Strings
|
||||||
|
@ -225,6 +261,11 @@ impl HTMLOptionElementMethods for HTMLOptionElement {
|
||||||
self.selectedness.set(selected);
|
self.selectedness.set(selected);
|
||||||
self.pick_if_selected_and_reset();
|
self.pick_if_selected_and_reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/#dom-option-index
|
||||||
|
fn Index(&self) -> i32 {
|
||||||
|
self.index()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VirtualMethods for HTMLOptionElement {
|
impl VirtualMethods for HTMLOptionElement {
|
||||||
|
|
|
@ -102,7 +102,7 @@ impl HTMLSelectElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#concept-select-option-list
|
// https://html.spec.whatwg.org/multipage/#concept-select-option-list
|
||||||
fn list_of_options(&self) -> impl Iterator<Item = DomRoot<HTMLOptionElement>> {
|
pub fn list_of_options(&self) -> impl Iterator<Item = DomRoot<HTMLOptionElement>> {
|
||||||
self.upcast::<Node>().children().flat_map(|node| {
|
self.upcast::<Node>().children().flat_map(|node| {
|
||||||
if node.is::<HTMLOptionElement>() {
|
if node.is::<HTMLOptionElement>() {
|
||||||
let node = DomRoot::downcast::<HTMLOptionElement>(node).unwrap();
|
let node = DomRoot::downcast::<HTMLOptionElement>(node).unwrap();
|
||||||
|
|
|
@ -22,5 +22,5 @@ interface HTMLOptionElement : HTMLElement {
|
||||||
|
|
||||||
[CEReactions]
|
[CEReactions]
|
||||||
attribute DOMString text;
|
attribute DOMString text;
|
||||||
// readonly attribute long index;
|
readonly attribute long index;
|
||||||
};
|
};
|
||||||
|
|
|
@ -2337,9 +2337,6 @@
|
||||||
[HTMLInputElement interface: createInput("url") must inherit property "stepDown(long)" with the proper type]
|
[HTMLInputElement interface: createInput("url") must inherit property "stepDown(long)" with the proper type]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[HTMLOptionElement interface: new Option() must inherit property "index" with the proper type]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[HTMLInputElement interface: createInput("password") must inherit property "height" with the proper type]
|
[HTMLInputElement interface: createInput("password") must inherit property "height" with the proper type]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
@ -3702,9 +3699,6 @@
|
||||||
[HTMLInputElement interface: calling setCustomValidity(DOMString) on createInput("range") with too few arguments must throw TypeError]
|
[HTMLInputElement interface: calling setCustomValidity(DOMString) on createInput("range") with too few arguments must throw TypeError]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[HTMLOptionElement interface: attribute index]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[HTMLInputElement interface: document.createElement("input") must inherit property "valueAsNumber" with the proper type]
|
[HTMLInputElement interface: document.createElement("input") must inherit property "valueAsNumber" with the proper type]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
@ -4923,9 +4917,6 @@
|
||||||
[HTMLInputElement interface: createInput("number") must inherit property "valueAsDate" with the proper type]
|
[HTMLInputElement interface: createInput("number") must inherit property "valueAsDate" with the proper type]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[HTMLOptionElement interface: document.createElement("option") must inherit property "index" with the proper type]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[HTMLInputElement interface: createInput("email") must inherit property "setCustomValidity(DOMString)" with the proper type]
|
[HTMLInputElement interface: createInput("email") must inherit property "setCustomValidity(DOMString)" with the proper type]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
[option-index.html]
|
|
||||||
type: testharness
|
|
||||||
[option index should work inside the document]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[option index should always be 0 for options in datalists]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[option index should always be 0 for options with no container]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[option index should always be 0 for options not even in the document]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue