Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom element upgrades #17935

Merged
merged 10 commits into from Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/script/dom/attr.rs
Expand Up @@ -188,7 +188,7 @@ impl Attr {

if owner.get_custom_element_definition().is_some() {
let reaction = CallbackReaction::AttributeChanged(name, Some(old_value), Some(new_value), namespace);
ScriptThread::enqueue_callback_reaction(owner, reaction);
ScriptThread::enqueue_callback_reaction(owner, reaction, None);
}

assert!(Some(owner) == self.owner().r());
Expand Down
7 changes: 6 additions & 1 deletion components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -5377,7 +5377,12 @@ def definition_body(self):
},
};

JS_SetPrototype(cx, result.reflector().get_jsobject(), prototype.handle());
rooted!(in(cx) let mut element = result.reflector().get_jsobject().get());
if !JS_WrapObject(cx, element.handle_mut()) {
return false;
}

JS_SetPrototype(cx, element.handle(), prototype.handle());

(result).to_jsval(cx, args.rval());
return true;
Expand Down
59 changes: 40 additions & 19 deletions components/script/dom/bindings/interface.rs
Expand Up @@ -80,7 +80,8 @@ use dom::bindings::guard::Guard;
use dom::bindings::js::Root;
use dom::bindings::utils::{DOM_PROTOTYPE_SLOT, ProtoOrIfaceArray, get_proto_or_iface_array};
use dom::create::create_native_html_element;
use dom::element::{Element, ElementCreator};
use dom::customelementregistry::ConstructionStackEntry;
use dom::element::{CustomElementState, Element, ElementCreator};
use dom::htmlelement::HTMLElement;
use dom::window::Window;
use html5ever::LocalName;
Expand Down Expand Up @@ -281,24 +282,44 @@ pub unsafe fn html_constructor<T>(window: &Window, call_args: &CallArgs) -> Fall
}
}

// Step 8.1
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.

// TODO: Step 8.3 - 8.4
// Set the element's custom element state and definition.

// Step 8.5
Root::downcast(element).ok_or(Error::InvalidState)

// TODO: Steps 9-13
// Custom element upgrades are not implemented yet, so these steps are unnecessary.
let entry = definition.construction_stack.borrow().last().cloned();
match entry {
// Step 8
None => {
// Step 8.1
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.3
element.set_custom_element_state(CustomElementState::Custom);

// Step 8.4
element.set_custom_element_definition(definition.clone());

// Step 8.5
Root::downcast(element).ok_or(Error::InvalidState)
},
// Step 9
Some(ConstructionStackEntry::Element(element)) => {
// Step 11 is performed in the generated caller code.

// Step 12
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() {
Expand Down
45 changes: 37 additions & 8 deletions components/script/dom/create.rs
Expand Up @@ -5,8 +5,9 @@
use dom::bindings::error::{report_pending_exception, throw_dom_exception};
use dom::bindings::js::Root;
use dom::bindings::reflector::DomObject;
use dom::customelementregistry::{is_valid_custom_element_name, upgrade_element};
use dom::document::Document;
use dom::element::{CustomElementCreationMode, Element, ElementCreator};
use dom::element::{CustomElementCreationMode, CustomElementState, Element, ElementCreator};
use dom::globalscope::GlobalScope;
use dom::htmlanchorelement::HTMLAnchorElement;
use dom::htmlappletelement::HTMLAppletElement;
Expand Down Expand Up @@ -80,6 +81,7 @@ use dom::htmlvideoelement::HTMLVideoElement;
use dom::svgsvgelement::SVGSVGElement;
use html5ever::{LocalName, Prefix, QualName};
use js::jsapi::JSAutoCompartment;
use script_thread::ScriptThread;
use servo_config::prefs::PREFS;

fn create_svg_element(name: QualName,
Expand Down Expand Up @@ -121,13 +123,18 @@ fn create_html_element(name: QualName,
assert!(name.ns == ns!(html));

// Step 4
let definition = document.lookup_custom_element_definition(name.local.clone(), is);
let definition = document.lookup_custom_element_definition(&name.ns, &name.local, is.as_ref());

if let Some(definition) = definition {
if definition.is_autonomous() {
match mode {
// TODO: Handle asynchronous CE creation. Relies on CE upgrades.
CustomElementCreationMode::Asynchronous => {},
CustomElementCreationMode::Asynchronous => {
let result = Root::upcast::<Element>(
HTMLElement::new(name.local.clone(), prefix.clone(), document));
result.set_custom_element_state(CustomElementState::Undefined);
ScriptThread::enqueue_upgrade_reaction(&*result, definition);
return result;
},
CustomElementCreationMode::Synchronous => {
let local_name = name.local.clone();
return match definition.create_element(document, prefix.clone()) {
Expand All @@ -148,20 +155,40 @@ fn create_html_element(name: QualName,
}

// Step 6.1.2
Root::upcast(HTMLUnknownElement::new(local_name, prefix, document))
let element = Root::upcast::<Element>(
HTMLUnknownElement::new(local_name, prefix, document));
element.set_custom_element_state(CustomElementState::Failed);
element
},
};
},
}
} else {
// Steps 5.1-5.2
let element = create_native_html_element(name, prefix, document, creator);
element.set_is(definition.name.clone());
// TODO: Enqueue custom element upgrade
element.set_custom_element_state(CustomElementState::Undefined);
match mode {
// Step 5.3
CustomElementCreationMode::Synchronous =>
upgrade_element(definition, &*element),
// Step 5.4
CustomElementCreationMode::Asynchronous =>
ScriptThread::enqueue_upgrade_reaction(&*element, definition),
}
return element;
}
}

create_native_html_element(name, prefix, document, creator)
// Steps 7.1-7.2
let result = create_native_html_element(name.clone(), prefix, document, creator);

// Step 7.3
if is_valid_custom_element_name(&*name.local) || is.is_some() {
result.set_custom_element_state(CustomElementState::Undefined);
}

result
}

pub fn create_native_html_element(name: QualName,
Expand All @@ -184,6 +211,7 @@ pub fn create_native_html_element(name: QualName,

// This is a big match, and the IDs for inline-interned atoms are not very structured.
// Perhaps we should build a perfect hash from those IDs instead.
// https://html.spec.whatwg.org/multipage/#elements-in-the-dom
match name.local {
local_name!("a") => make!(HTMLAnchorElement),
local_name!("abbr") => make!(HTMLElement),
Expand Down Expand Up @@ -326,7 +354,8 @@ pub fn create_native_html_element(name: QualName,
local_name!("video") => make!(HTMLVideoElement),
local_name!("wbr") => make!(HTMLElement),
local_name!("xmp") => make!(HTMLPreElement),
_ => make!(HTMLUnknownElement),
_ if is_valid_custom_element_name(&*name.local) => make!(HTMLElement),
_ => make!(HTMLUnknownElement),
}
}

Expand Down