Introduce trait Castable

This trait is used to hold onto the downcast and upcast functions of all
castable IDL interfaces. A castable IDL interface is one which either derives
from or is derived by other interfaces.

The deriving relation is represented by implementations of marker trait
DerivedFrom<T: Castable> generated in InheritTypes.

/^[ ]*use dom::bindings::codegen::InheritTypes::.*(Base|Cast|Derived)/ {
    /::[a-zA-Z]+(Base|Cast|Derived);/d
    s/([{ ])[a-zA-Z]+(Base|Cast|Derived), /\1/g
    s/([{ ])[a-zA-Z]+(Base|Cast|Derived), /\1/g
    s/, [a-zA-Z]+(Base|Cast|Derived)([},])/\2/g
    s/, [a-zA-Z]+(Base|Cast|Derived)([},])/\2/g
    /\{([a-zA-Z]+(Base|Cast|Derived))?\};$/d
    s/\{([a-zA-Z_]+)\};$/\1;/
}

s/([a-zA-Z]+)Cast::from_ref\(\&?\**([a-zA-Z_]+)(\.r\(\))?\)/\2.upcast::<\1>()/g
s/([a-zA-Z]+)Cast::from_ref\(\&?\**([a-zA-Z_]+)(\.[a-zA-Z_]+\(\))?\)/\2\3.upcast::<\1>()/g
s/\(([a-zA-Z]+)Cast::from_ref\)/\(Castable::upcast::<\1>\)/g

s/([a-zA-Z]+)Cast::from_root/Root::upcast::<\1>/g

s/([a-zA-Z]+)Cast::from_layout_js\(\&([a-zA-Z_.]+)\)/\2.upcast::<\1>()/g

s/([a-zA-Z]+)Cast::to_ref\(\&?\**([a-zA-Z_]+)(\.r\(\))?\)/\2.downcast::<\1>()/g
s/([a-zA-Z]+)Cast::to_ref\(\&?\**([a-zA-Z_]+)(\.[a-zA-Z_]+\(\))?\)/\2\3.downcast::<\1>()/g
s/\(([a-zA-Z]+)Cast::to_ref\)/\(Castable::downcast::<\1>\)/g

s/([a-zA-Z]+)Cast::to_root/Root::downcast::<\1>/g

s/([a-zA-Z]+)Cast::to_layout_js\(&?([a-zA-Z_.]+(\(\))?)\)/\2.downcast::<\1>()/g

s/\.is_document\(\)/.is::<Document>()/g
s/\.is_htmlanchorelement\(\)/.is::<HTMLAnchorElement>()/g
s/\.is_htmlappletelement\(\)/.is::<HTMLAppletElement>()/g
s/\.is_htmlareaelement\(\)/.is::<HTMLAreaElement>()/g
s/\.is_htmlbodyelement\(\)/.is::<HTMLBodyElement>()/g
s/\.is_htmlembedelement\(\)/.is::<HTMLEmbedElement>()/g
s/\.is_htmlfieldsetelement\(\)/.is::<HTMLFieldSetElement>()/g
s/\.is_htmlformelement\(\)/.is::<HTMLFormElement>()/g
s/\.is_htmlframesetelement\(\)/.is::<HTMLFrameSetElement>()/g
s/\.is_htmlhtmlelement\(\)/.is::<HTMLHtmlElement>()/g
s/\.is_htmlimageelement\(\)/.is::<HTMLImageElement>()/g
s/\.is_htmllegendelement\(\)/.is::<HTMLLegendElement>()/g
s/\.is_htmloptgroupelement\(\)/.is::<HTMLOptGroupElement>()/g
s/\.is_htmloptionelement\(\)/.is::<HTMLOptionElement>()/g
s/\.is_htmlscriptelement\(\)/.is::<HTMLScriptElement>()/g
s/\.is_htmltabledatacellelement\(\)/.is::<HTMLTableDataCellElement>()/g
s/\.is_htmltableheadercellelement\(\)/.is::<HTMLTableHeaderCellElement>()/g
s/\.is_htmltablerowelement\(\)/.is::<HTMLTableRowElement>()/g
s/\.is_htmltablesectionelement\(\)/.is::<HTMLTableSectionElement>()/g
s/\.is_htmltitleelement\(\)/.is::<HTMLTitleElement>()/g
This commit is contained in:
Anthony Ramine 2015-10-06 17:21:44 +02:00
parent bd363b009d
commit 13ea3ac413
82 changed files with 1124 additions and 1148 deletions

View file

@ -9,11 +9,8 @@ use dom::bindings::codegen::Bindings::HTMLElementBinding;
use dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, ElementTypeId};
use dom::bindings::codegen::InheritTypes::{EventTargetCast, HTMLBodyElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLElementTypeId, HTMLFrameSetElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLHtmlElementDerived, HTMLInputElementCast};
use dom::bindings::codegen::InheritTypes::{NodeCast, NodeTypeId};
use dom::bindings::codegen::InheritTypes::{ElementTypeId, HTMLElementTypeId, NodeTypeId};
use dom::bindings::conversions::Castable;
use dom::bindings::error::{Error, ErrorResult};
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::utils::Reflectable;
@ -21,6 +18,10 @@ use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration};
use dom::document::Document;
use dom::domstringmap::DOMStringMap;
use dom::element::{AttributeMutation, Element, EventState};
use dom::eventtarget::EventTarget;
use dom::htmlbodyelement::HTMLBodyElement;
use dom::htmlframesetelement::HTMLFrameSetElement;
use dom::htmlhtmlelement::HTMLHtmlElement;
use dom::htmlinputelement::HTMLInputElement;
use dom::node::{Node, SEQUENTIALLY_FOCUSABLE};
use dom::node::{document_from_node, window_from_node};
@ -70,13 +71,13 @@ impl HTMLElement {
}
fn is_body_or_frameset(&self) -> bool {
let eventtarget = EventTargetCast::from_ref(self);
eventtarget.is_htmlbodyelement() || eventtarget.is_htmlframesetelement()
let eventtarget = self.upcast::<EventTarget>();
eventtarget.is::<HTMLBodyElement>() || eventtarget.is::<HTMLFrameSetElement>()
}
fn update_sequentially_focusable_status(&self) {
let element = ElementCast::from_ref(self);
let node = NodeCast::from_ref(self);
let element = self.upcast::<Element>();
let node = self.upcast::<Node>();
if element.has_attribute(&atom!("tabindex")) {
node.set_flag(SEQUENTIALLY_FOCUSABLE, true);
} else {
@ -117,7 +118,7 @@ impl HTMLElementMethods for HTMLElement {
fn Style(&self) -> Root<CSSStyleDeclaration> {
self.style_decl.or_init(|| {
let global = window_from_node(self);
CSSStyleDeclaration::new(global.r(), ElementCast::from_ref(self), None, CSSModificationAccess::ReadWrite)
CSSStyleDeclaration::new(global.r(), self.upcast::<Element>(), None, CSSModificationAccess::ReadWrite)
})
}
@ -150,7 +151,7 @@ impl HTMLElementMethods for HTMLElement {
let win = window_from_node(self);
win.r().GetOnload()
} else {
let target = EventTargetCast::from_ref(self);
let target = self.upcast::<EventTarget>();
target.get_event_handler_common("load")
}
}
@ -161,20 +162,20 @@ impl HTMLElementMethods for HTMLElement {
let win = window_from_node(self);
win.r().SetOnload(listener)
} else {
let target = EventTargetCast::from_ref(self);
let target = self.upcast::<EventTarget>();
target.set_event_handler_common("load", listener)
}
}
// https://html.spec.whatwg.org/multipage/#dom-click
fn Click(&self) {
let maybe_input: Option<&HTMLInputElement> = HTMLInputElementCast::to_ref(self);
let maybe_input: Option<&HTMLInputElement> = self.downcast::<HTMLInputElement>();
if let Some(i) = maybe_input {
if i.Disabled() {
return;
}
}
let element = ElementCast::from_ref(self);
let element = self.upcast::<Element>();
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=27430 ?
element.as_maybe_activatable().map(|a| a.synthetic_click_activation(false, false, false, false));
}
@ -183,7 +184,7 @@ impl HTMLElementMethods for HTMLElement {
fn Focus(&self) {
// TODO: Mark the element as locked for focus and run the focusing steps.
// https://html.spec.whatwg.org/multipage/#focusing-steps
let element = ElementCast::from_ref(self);
let element = self.upcast::<Element>();
let document = document_from_node(self);
let document = document.r();
document.begin_focus_transaction();
@ -194,7 +195,7 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#dom-blur
fn Blur(&self) {
// TODO: Run the unfocusing steps.
let el = ElementCast::from_ref(self);
let el = self.upcast::<Element>();
if !el.get_focus_state() {
return;
}
@ -207,11 +208,11 @@ impl HTMLElementMethods for HTMLElement {
// https://drafts.csswg.org/cssom-view/#extensions-to-the-htmlelement-interface
fn GetOffsetParent(&self) -> Option<Root<Element>> {
if self.is_htmlbodyelement() || self.is_htmlhtmlelement() {
if self.is::<HTMLBodyElement>() || self.is::<HTMLHtmlElement>() {
return None;
}
let node = NodeCast::from_ref(self);
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (element, _) = window.offset_parent_query(node.to_trusted_node_address());
@ -220,11 +221,11 @@ impl HTMLElementMethods for HTMLElement {
// https://drafts.csswg.org/cssom-view/#extensions-to-the-htmlelement-interface
fn OffsetTop(&self) -> i32 {
if self.is_htmlbodyelement() {
if self.is::<HTMLBodyElement>() {
return 0;
}
let node = NodeCast::from_ref(self);
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (_, rect) = window.offset_parent_query(node.to_trusted_node_address());
@ -233,11 +234,11 @@ impl HTMLElementMethods for HTMLElement {
// https://drafts.csswg.org/cssom-view/#extensions-to-the-htmlelement-interface
fn OffsetLeft(&self) -> i32 {
if self.is_htmlbodyelement() {
if self.is::<HTMLBodyElement>() {
return 0;
}
let node = NodeCast::from_ref(self);
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (_, rect) = window.offset_parent_query(node.to_trusted_node_address());
@ -246,7 +247,7 @@ impl HTMLElementMethods for HTMLElement {
// https://drafts.csswg.org/cssom-view/#extensions-to-the-htmlelement-interface
fn OffsetWidth(&self) -> i32 {
let node = NodeCast::from_ref(self);
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (_, rect) = window.offset_parent_query(node.to_trusted_node_address());
@ -255,7 +256,7 @@ impl HTMLElementMethods for HTMLElement {
// https://drafts.csswg.org/cssom-view/#extensions-to-the-htmlelement-interface
fn OffsetHeight(&self) -> i32 {
let node = NodeCast::from_ref(self);
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (_, rect) = window.offset_parent_query(node.to_trusted_node_address());
@ -285,12 +286,12 @@ impl HTMLElement {
.nth(1).map_or(false, |ch| ch >= 'a' && ch <= 'z') {
return Err(Error::Syntax);
}
let element = ElementCast::from_ref(self);
let element = self.upcast::<Element>();
element.set_custom_attribute(to_snake_case(name), value)
}
pub fn get_custom_attr(&self, local_name: DOMString) -> Option<DOMString> {
let element = ElementCast::from_ref(self);
let element = self.upcast::<Element>();
let local_name = Atom::from_slice(&to_snake_case(local_name));
element.get_attribute(&ns!(""), &local_name).map(|attr| {
(**attr.r().value()).to_owned()
@ -298,7 +299,7 @@ impl HTMLElement {
}
pub fn delete_custom_attr(&self, local_name: DOMString) {
let element = ElementCast::from_ref(self);
let element = self.upcast::<Element>();
let local_name = Atom::from_slice(&to_snake_case(local_name));
element.remove_attribute(&ns!(""), &local_name);
}
@ -306,7 +307,7 @@ impl HTMLElement {
impl VirtualMethods for HTMLElement {
fn super_type(&self) -> Option<&VirtualMethods> {
let element: &Element = ElementCast::from_ref(self);
let element: &Element = self.upcast::<Element>();
Some(element as &VirtualMethods)
}
@ -318,7 +319,7 @@ impl VirtualMethods for HTMLElement {
let (cx, url, reflector) = (window.r().get_cx(),
window.r().get_url(),
window.r().reflector().get_jsobject());
let evtarget = EventTargetCast::from_ref(self);
let evtarget = self.upcast::<EventTarget>();
evtarget.set_event_handler_uncompiled(cx, url, reflector,
&name[2..],
(**attr.value()).to_owned());