Skip to content

Commit

Permalink
Implement HTMLOptionElement named constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
teapotd committed Jan 3, 2020
1 parent 8002c6b commit 155f69c
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 93 deletions.
38 changes: 36 additions & 2 deletions components/script/dom/htmloptionelement.rs
Expand Up @@ -8,12 +8,14 @@ use crate::dom::bindings::codegen::Bindings::HTMLOptionElementBinding;
use crate::dom::bindings::codegen::Bindings::HTMLOptionElementBinding::HTMLOptionElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLSelectElementBinding::HTMLSelectElementBinding::HTMLSelectElementMethods;
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::characterdata::CharacterData;
use crate::dom::document::Document;
use crate::dom::element::{AttributeMutation, Element};
use crate::dom::element::{AttributeMutation, CustomElementCreationMode, Element, ElementCreator};
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlformelement::HTMLFormElement;
use crate::dom::htmloptgroupelement::HTMLOptGroupElement;
Expand All @@ -22,8 +24,9 @@ use crate::dom::htmlselectelement::HTMLSelectElement;
use crate::dom::node::{BindContext, Node, ShadowIncluding, UnbindContext};
use crate::dom::text::Text;
use crate::dom::virtualmethods::VirtualMethods;
use crate::dom::window::Window;
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix};
use html5ever::{LocalName, Prefix, QualName};
use std::cell::Cell;
use style::element_state::ElementState;
use style::str::{split_html_space_chars, str_join};
Expand Down Expand Up @@ -72,6 +75,37 @@ impl HTMLOptionElement {
)
}

// https://html.spec.whatwg.org/multipage/#dom-option
pub fn Option(
window: &Window,
text: DOMString,
value: Option<DOMString>,
default_selected: bool,
selected: bool,
) -> Fallible<DomRoot<HTMLOptionElement>> {
let element = Element::create(
QualName::new(None, ns!(html), local_name!("option")),
None,
&window.Document(),
ElementCreator::ScriptCreated,
CustomElementCreationMode::Synchronous,
);

let option = DomRoot::downcast::<HTMLOptionElement>(element).unwrap();

if !text.is_empty() {
option.upcast::<Node>().SetTextContent(Some(text))
}

if let Some(val) = value {
option.SetValue(val)
}

option.SetDefaultSelected(default_selected);
option.set_selectedness(selected);
Ok(option)
}

pub fn set_selectedness(&self, selected: bool) {
self.selectedness.set(selected);
}
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/webidls/HTMLOptionElement.webidl
Expand Up @@ -3,9 +3,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

// https://html.spec.whatwg.org/multipage/#htmloptionelement
[Exposed=Window/*, NamedConstructor=Option(optional DOMString text = "", optional DOMString value,
[Exposed=Window, NamedConstructor=Option(optional DOMString text = "", optional DOMString value,
optional boolean defaultSelected = false,
optional boolean selected = false)*/]
optional boolean selected = false)]
interface HTMLOptionElement : HTMLElement {
[HTMLConstructor] constructor();

Expand Down
45 changes: 3 additions & 42 deletions tests/wpt/metadata/html/dom/idlharness.https.html.ini
Expand Up @@ -2106,9 +2106,6 @@
[HTMLSelectElement interface: attribute willValidate]
expected: FAIL
[HTMLOptionElement interface: named constructor]
expected: FAIL
[HTMLMarqueeElement interface: attribute hspace]
expected: FAIL
Expand Down Expand Up @@ -2253,9 +2250,6 @@
[HTMLInputElement interface: createInput("url") must inherit property "checkValidity()" with the proper type]
expected: FAIL

[HTMLOptionElement interface: named constructor prototype property]
expected: FAIL

[HTMLInputElement interface: createInput("week") must inherit property "validationMessage" with the proper type]
expected: FAIL

Expand Down Expand Up @@ -3147,9 +3141,6 @@
[HTMLInputElement interface: calling stepDown(long) on createInput("password") with too few arguments must throw TypeError]
expected: FAIL

[HTMLOptionElement interface: named constructor object]
expected: FAIL

[HTMLFrameElement interface: document.createElement("frame") must inherit property "frameBorder" with the proper type]
expected: FAIL

Expand Down Expand Up @@ -3261,9 +3252,6 @@
[HTMLMarqueeElement interface: attribute bgColor]
expected: FAIL

[Stringification of new Option()]
expected: FAIL

[HTMLInputElement interface: createInput("file") must inherit property "width" with the proper type]
expected: FAIL

Expand Down Expand Up @@ -3309,9 +3297,6 @@
[HTMLAreaElement interface: document.createElement("area") must inherit property "coords" with the proper type]
expected: FAIL

[HTMLOptionElement interface: new Option() must inherit property "label" with the proper type]
expected: FAIL

[HTMLTableCellElement interface: document.createElement("th") must inherit property "chOff" with the proper type]
expected: FAIL

Expand Down Expand Up @@ -3348,9 +3333,6 @@
[HTMLMarqueeElement interface: document.createElement("marquee") must inherit property "scrollAmount" with the proper type]
expected: FAIL

[HTMLOptionElement interface: new Option() must inherit property "form" with the proper type]
expected: FAIL

[HTMLInputElement interface: createInput("datetime-local") must inherit property "reportValidity()" with the proper type]
expected: FAIL

Expand Down Expand Up @@ -3441,9 +3423,6 @@
[HTMLSelectElement interface: operation setCustomValidity(DOMString)]
expected: FAIL

[HTMLOptionElement interface: named constructor name]
expected: FAIL

[HTMLVideoElement interface: document.createElement("video") must inherit property "height" with the proper type]
expected: FAIL

Expand All @@ -3456,9 +3435,6 @@
[HTMLTableElement interface: attribute rules]
expected: FAIL

[HTMLOptionElement interface: named constructor length]
expected: FAIL

[HTMLInputElement interface: createInput("email") must inherit property "stepUp(long)" with the proper type]
expected: FAIL

Expand Down Expand Up @@ -3744,9 +3720,6 @@
[HTMLInputElement interface: document.createElement("input") must inherit property "valueAsNumber" with the proper type]
expected: FAIL
[HTMLOptionElement interface: new Option() must inherit property "disabled" with the proper type]
expected: FAIL
[HTMLFieldSetElement interface: attribute name]
expected: FAIL
Expand Down Expand Up @@ -3963,9 +3936,6 @@
[HTMLAreaElement interface: attribute noHref]
expected: FAIL

[HTMLOptionElement interface: new Option() must inherit property "value" with the proper type]
expected: FAIL

[HTMLInputElement interface: createInput("number") must inherit property "valueAsNumber" with the proper type]
expected: FAIL

Expand Down Expand Up @@ -4182,9 +4152,6 @@
[HTMLMarqueeElement interface: document.createElement("marquee") must inherit property "trueSpeed" with the proper type]
expected: FAIL

[HTMLOptionElement interface: new Option() must inherit property "selected" with the proper type]
expected: FAIL

[HTMLElement interface: attribute contentEditable]
expected: FAIL

Expand Down Expand Up @@ -4290,9 +4257,6 @@
[HTMLFormElement interface: document.createElement("form") must inherit property "rel" with the proper type]
expected: FAIL
[HTMLOptionElement must be primary interface of new Option()]
expected: FAIL
[HTMLIFrameElement interface: attribute allowPaymentRequest]
expected: FAIL
Expand Down Expand Up @@ -4350,9 +4314,6 @@
[HTMLBodyElement interface: document.createElement("body") must inherit property "vLink" with the proper type]
expected: FAIL
[HTMLOptionElement interface: new Option() must inherit property "text" with the proper type]
expected: FAIL
[HTMLMediaElement interface: new Audio() must inherit property "seekable" with the proper type]
expected: FAIL
Expand Down Expand Up @@ -4521,9 +4482,6 @@
[HTMLMediaElement interface: operation getStartDate()]
expected: FAIL
[HTMLOptionElement interface: new Option() must inherit property "defaultSelected" with the proper type]
expected: FAIL
[HTMLInputElement interface: createInput("email") must inherit property "list" with the proper type]
expected: FAIL
Expand Down Expand Up @@ -5028,3 +4986,6 @@
[SVGAElement includes HTMLHyperlinkElementUtils: member names are unique]
expected: FAIL

[HTMLOptionElement interface: named constructor without 'new']
expected: FAIL

This file was deleted.

@@ -1,6 +1,5 @@
[form-validation-checkValidity.html]
type: testharness
expected: ERROR
[[INPUT in TEXT status\] no constraint]
expected: FAIL

Expand Down Expand Up @@ -427,3 +426,27 @@
[[INPUT in EMAIL status\] not suffering from being too long (in a form)]
expected: FAIL

[[select\] no constraint (in a form)]
expected: FAIL

[[select\] suffering from being missing (in a form)]
expected: FAIL

[[textarea\] suffering from being missing (in a form)]
expected: FAIL

[[select\] no constraint]
expected: FAIL

[[select\] suffering from being missing]
expected: FAIL

[[textarea\] no constraint]
expected: FAIL

[[textarea\] suffering from being missing]
expected: FAIL

[[textarea\] no constraint (in a form)]
expected: FAIL

@@ -1,6 +1,5 @@
[form-validation-reportValidity.html]
type: testharness
expected: ERROR
[[INPUT in TEXT status\] no constraint]
expected: FAIL

Expand Down Expand Up @@ -433,3 +432,27 @@
[[INPUT in EMAIL status\] not suffering from being too long (in a form)]
expected: FAIL

[[select\] no constraint (in a form)]
expected: FAIL

[[select\] suffering from being missing (in a form)]
expected: FAIL

[[textarea\] suffering from being missing (in a form)]
expected: FAIL

[[select\] no constraint]
expected: FAIL

[[select\] suffering from being missing]
expected: FAIL

[[textarea\] no constraint]
expected: FAIL

[[textarea\] suffering from being missing]
expected: FAIL

[[textarea\] no constraint (in a form)]
expected: FAIL

@@ -1,6 +1,5 @@
[form-validation-validity-customError.html]
type: testharness
expected: ERROR
[[input\] The validity.customError must be true if the custom validity error message is not empty]
expected: FAIL

Expand All @@ -13,3 +12,15 @@
[[button\] The validity.customError must be false if the custom validity error message is empty]
expected: FAIL

[[select\] The validity.customError must be false i the custom validity error message is empty]
expected: FAIL

[[select\] The validity.customError must be true if the custom validity error message is not empty]
expected: FAIL

[[textarea\] The validity.customError must be false if the custom validity error message is empty]
expected: FAIL

[[textarea\] The validity.customError must be true if the custom validity error message is not empty]
expected: FAIL

@@ -1,6 +1,5 @@
[form-validation-validity-valid.html]
type: testharness
expected: ERROR
[[INPUT in TEXT status\] validity.valid must be false if validity.tooLong is true]
expected: FAIL

Expand Down Expand Up @@ -154,3 +153,6 @@
[[INPUT in DATETIME-LOCAL status\] validity.valid must be false if validity.valueMissing is true]
expected: FAIL

[[textarea\] validity.valid must be false if validity.valueMissing is true]
expected: FAIL

@@ -1,6 +1,5 @@
[form-validation-validity-valueMissing.html]
type: testharness
expected: ERROR
[[INPUT in TEXT status\] The required attribute is not set]
expected: FAIL

Expand Down Expand Up @@ -304,3 +303,15 @@
[[INPUT in DATETIME-LOCAL status\] The value attribute is empty string]
expected: FAIL

[[textarea\] The value is not empty]
expected: FAIL

[[textarea\] The value is empty]
expected: FAIL

[[textarea\] The required attribute is not set]
expected: FAIL

[[select\] Selected the option with value equals to empty]
expected: FAIL

@@ -1,6 +1,5 @@
[form-validation-willValidate.html]
type: testharness
expected: ERROR
[[INPUT in HIDDEN status\] Must be barred from the constraint validation]
expected: FAIL

Expand Down Expand Up @@ -268,3 +267,21 @@
[[output\] The willValidate attribute must be false since OUTPUT is not a submittable element]
expected: FAIL

[[textarea\] Must be barred from the constraint validation]
expected: FAIL

[[textarea\] The willValidate attribute must be false if it has a datalist ancestor]
expected: FAIL

[[textarea\] The willValidate attribute must be true if an element is mutable]
expected: FAIL

[[select\] The willValidate attribute must be false if it has a datalist ancestor]
expected: FAIL

[[select\] The willValidate attribute must be true if an element is mutable]
expected: FAIL

[[select\] Must be barred from the constraint validation]
expected: FAIL

0 comments on commit 155f69c

Please sign in to comment.