aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/htmloptionelement.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/dom/htmloptionelement.rs')
-rw-r--r--components/script/dom/htmloptionelement.rs38
1 files changed, 36 insertions, 2 deletions
diff --git a/components/script/dom/htmloptionelement.rs b/components/script/dom/htmloptionelement.rs
index 3893aea95ab..4ebb2860a79 100644
--- a/components/script/dom/htmloptionelement.rs
+++ b/components/script/dom/htmloptionelement.rs
@@ -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;
@@ -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};
@@ -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);
}