Add construction stack

This commit is contained in:
Connor Brewster 2017-07-19 12:58:15 -06:00
parent 82de4c49f3
commit 8c5005fc44
2 changed files with 42 additions and 14 deletions

View file

@ -80,6 +80,7 @@ use dom::bindings::guard::Guard;
use dom::bindings::js::Root; use dom::bindings::js::Root;
use dom::bindings::utils::{DOM_PROTOTYPE_SLOT, ProtoOrIfaceArray, get_proto_or_iface_array}; use dom::bindings::utils::{DOM_PROTOTYPE_SLOT, ProtoOrIfaceArray, get_proto_or_iface_array};
use dom::create::create_native_html_element; use dom::create::create_native_html_element;
use dom::customelementregistry::ConstructionStackEntry;
use dom::element::{Element, ElementCreator}; use dom::element::{Element, ElementCreator};
use dom::htmlelement::HTMLElement; use dom::htmlelement::HTMLElement;
use dom::window::Window; use dom::window::Window;
@ -281,24 +282,42 @@ pub unsafe fn html_constructor<T>(window: &Window, call_args: &CallArgs) -> Fall
} }
} }
// Step 8.1 let entry = definition.construction_stack.borrow().last().cloned();
let name = QualName::new(None, ns!(html), definition.local_name.clone()); match entry {
let element = if definition.is_autonomous() { // Step 8
Root::upcast(HTMLElement::new(name.local, None, &*document)) None => {
} else { // Step 8.1
create_native_html_element(name, None, &*document, ElementCreator::ScriptCreated) let name = QualName::new(None, ns!(html), definition.local_name.clone());
}; let element = if definition.is_autonomous() {
Root::upcast(HTMLElement::new(name.local, None, &*document))
} else {
create_native_html_element(name, None, &*document, ElementCreator::ScriptCreated)
};
// Step 8.2 is performed in the generated caller code. // Step 8.2 is performed in the generated caller code.
// TODO: Step 8.3 - 8.4 // TODO: Step 8.3 - 8.4
// Set the element's custom element state and definition. // Set the element's custom element state and definition.
element.set_custom_element_definition(definition.clone());
// Step 8.5 // Step 8.5
Root::downcast(element).ok_or(Error::InvalidState) Root::downcast(element).ok_or(Error::InvalidState)
},
// Step 9
Some(ConstructionStackEntry::Element(element)) => {
// Step 11 is performed in the generated caller code.
// TODO: Steps 9-13 // Step 12
// Custom element upgrades are not implemented yet, so these steps are unnecessary. let mut construction_stack = definition.construction_stack.borrow_mut();
construction_stack.pop();
construction_stack.push(ConstructionStackEntry::AlreadyConstructedMarker);
// Step 13
Root::downcast(element).ok_or(Error::InvalidState)
},
// Step 10
Some(ConstructionStackEntry::AlreadyConstructedMarker) => Err(Error::InvalidState),
}
} }
pub fn push_new_element_queue() { pub fn push_new_element_queue() {

View file

@ -366,6 +366,12 @@ pub struct LifecycleCallbacks {
attribute_changed_callback: Option<Rc<Function>>, attribute_changed_callback: Option<Rc<Function>>,
} }
#[derive(HeapSizeOf, JSTraceable, Clone)]
pub enum ConstructionStackEntry {
Element(Root<Element>),
AlreadyConstructedMarker,
}
/// https://html.spec.whatwg.org/multipage/#custom-element-definition /// https://html.spec.whatwg.org/multipage/#custom-element-definition
#[derive(HeapSizeOf, JSTraceable, Clone)] #[derive(HeapSizeOf, JSTraceable, Clone)]
pub struct CustomElementDefinition { pub struct CustomElementDefinition {
@ -379,6 +385,8 @@ pub struct CustomElementDefinition {
pub observed_attributes: Vec<DOMString>, pub observed_attributes: Vec<DOMString>,
pub callbacks: LifecycleCallbacks, pub callbacks: LifecycleCallbacks,
pub construction_stack: DOMRefCell<Vec<ConstructionStackEntry>>,
} }
impl CustomElementDefinition { impl CustomElementDefinition {
@ -394,6 +402,7 @@ impl CustomElementDefinition {
constructor: constructor, constructor: constructor,
observed_attributes: observed_attributes, observed_attributes: observed_attributes,
callbacks: callbacks, callbacks: callbacks,
construction_stack: Default::default(),
} }
} }