Ensure that Reflectors are the first field

This commit is contained in:
Manish Goregaokar 2014-12-10 11:56:20 +05:30
parent d761877ef6
commit 21a888341d
14 changed files with 146 additions and 21 deletions

View file

@ -272,9 +272,18 @@ impl LintPass for InheritancePass {
// #[_dom_struct_marker] here without also checking for #[dom_struct] // #[_dom_struct_marker] here without also checking for #[dom_struct]
if ty::has_attr(cx.tcx, ast_util::local_def(id), "_dom_struct_marker") { if ty::has_attr(cx.tcx, ast_util::local_def(id), "_dom_struct_marker") {
// Find the reflector, if any // Find the reflector, if any
let reflector_span = def.fields.iter() let reflector_span = def.fields.iter().enumerate()
.find(|f| match_lang_ty(cx, &*f.node.ty, "reflector")) .find(|&(ctr, f)| {
.map(|f| f.span); if match_lang_ty(cx, &*f.node.ty, "reflector") {
if ctr > 0 {
cx.span_lint(INHERITANCE_INTEGRITY, f.span,
"The Reflector should be the first field of the DOM struct");
}
return true;
}
false
})
.map(|(_, f)| f.span);
// Find all #[dom_struct] fields // Find all #[dom_struct] fields
let dom_spans: Vec<_> = def.fields.iter().enumerate().filter_map(|(ctr, f)| { let dom_spans: Vec<_> = def.fields.iter().enumerate().filter_map(|(ctr, f)| {
if let ast::TyPath(_, _, ty_id) = f.node.ty.node { if let ast::TyPath(_, _, ty_id) = f.node.ty.node {

View file

@ -62,15 +62,15 @@ impl DOMErrorName {
#[dom_struct] #[dom_struct]
pub struct DOMException { pub struct DOMException {
reflector_: Reflector,
code: DOMErrorName, code: DOMErrorName,
reflector_: Reflector
} }
impl DOMException { impl DOMException {
fn new_inherited(code: DOMErrorName) -> DOMException { fn new_inherited(code: DOMErrorName) -> DOMException {
DOMException { DOMException {
reflector_: Reflector::new(),
code: code, code: code,
reflector_: Reflector::new()
} }
} }

View file

@ -27,15 +27,15 @@ use servo_util::str::DOMString;
#[dom_struct] #[dom_struct]
pub struct DOMImplementation { pub struct DOMImplementation {
document: JS<Document>,
reflector_: Reflector, reflector_: Reflector,
document: JS<Document>,
} }
impl DOMImplementation { impl DOMImplementation {
fn new_inherited(document: JSRef<Document>) -> DOMImplementation { fn new_inherited(document: JSRef<Document>) -> DOMImplementation {
DOMImplementation { DOMImplementation {
document: JS::from_rooted(document),
reflector_: Reflector::new(), reflector_: Reflector::new(),
document: JS::from_rooted(document),
} }
} }

View file

@ -19,15 +19,15 @@ use servo_util::str::DOMString;
#[dom_struct] #[dom_struct]
pub struct DOMParser { pub struct DOMParser {
reflector_: Reflector,
window: JS<Window>, //XXXjdm Document instead? window: JS<Window>, //XXXjdm Document instead?
reflector_: Reflector
} }
impl DOMParser { impl DOMParser {
fn new_inherited(window: JSRef<Window>) -> DOMParser { fn new_inherited(window: JSRef<Window>) -> DOMParser {
DOMParser { DOMParser {
reflector_: Reflector::new(),
window: JS::from_rooted(window), window: JS::from_rooted(window),
reflector_: Reflector::new()
} }
} }

View file

@ -51,8 +51,8 @@ pub enum EventCancelable {
#[dom_struct] #[dom_struct]
pub struct Event { pub struct Event {
type_id: EventTypeId,
reflector_: Reflector, reflector_: Reflector,
type_id: EventTypeId,
current_target: MutNullableJS<EventTarget>, current_target: MutNullableJS<EventTarget>,
target: MutNullableJS<EventTarget>, target: MutNullableJS<EventTarget>,
type_: DOMRefCell<DOMString>, type_: DOMRefCell<DOMString>,
@ -71,8 +71,8 @@ pub struct Event {
impl Event { impl Event {
pub fn new_inherited(type_id: EventTypeId) -> Event { pub fn new_inherited(type_id: EventTypeId) -> Event {
Event { Event {
type_id: type_id,
reflector_: Reflector::new(), reflector_: Reflector::new(),
type_id: type_id,
current_target: Default::default(), current_target: Default::default(),
target: Default::default(), target: Default::default(),
phase: Cell::new(EventPhase::None), phase: Cell::new(EventPhase::None),

View file

@ -72,16 +72,16 @@ pub struct EventListenerEntry {
#[dom_struct] #[dom_struct]
pub struct EventTarget { pub struct EventTarget {
type_id: EventTargetTypeId,
reflector_: Reflector, reflector_: Reflector,
type_id: EventTargetTypeId,
handlers: DOMRefCell<HashMap<DOMString, Vec<EventListenerEntry>, FnvHasher>>, handlers: DOMRefCell<HashMap<DOMString, Vec<EventListenerEntry>, FnvHasher>>,
} }
impl EventTarget { impl EventTarget {
pub fn new_inherited(type_id: EventTargetTypeId) -> EventTarget { pub fn new_inherited(type_id: EventTargetTypeId) -> EventTarget {
EventTarget { EventTarget {
type_id: type_id,
reflector_: Reflector::new(), reflector_: Reflector::new(),
type_id: type_id,
handlers: DOMRefCell::new(HashMap::with_hasher(FnvHasher)), handlers: DOMRefCell::new(HashMap::with_hasher(FnvHasher)),
} }
} }

View file

@ -29,8 +29,8 @@ pub enum FormDatum {
#[dom_struct] #[dom_struct]
pub struct FormData { pub struct FormData {
data: DOMRefCell<HashMap<DOMString, Vec<FormDatum>>>,
reflector_: Reflector, reflector_: Reflector,
data: DOMRefCell<HashMap<DOMString, Vec<FormDatum>>>,
global: GlobalField, global: GlobalField,
form: Option<JS<HTMLFormElement>> form: Option<JS<HTMLFormElement>>
} }
@ -38,8 +38,8 @@ pub struct FormData {
impl FormData { impl FormData {
fn new_inherited(form: Option<JSRef<HTMLFormElement>>, global: &GlobalRef) -> FormData { fn new_inherited(form: Option<JSRef<HTMLFormElement>>, global: &GlobalRef) -> FormData {
FormData { FormData {
data: DOMRefCell::new(HashMap::new()),
reflector_: Reflector::new(), reflector_: Reflector::new(),
data: DOMRefCell::new(HashMap::new()),
global: GlobalField::from_rooted(global), global: GlobalField::from_rooted(global),
form: form.map(|f| JS::from_rooted(f)), form: form.map(|f| JS::from_rooted(f)),
} }

View file

@ -32,15 +32,15 @@ pub enum CollectionTypeId {
#[dom_struct] #[dom_struct]
pub struct HTMLCollection { pub struct HTMLCollection {
collection: CollectionTypeId,
reflector_: Reflector, reflector_: Reflector,
collection: CollectionTypeId,
} }
impl HTMLCollection { impl HTMLCollection {
fn new_inherited(collection: CollectionTypeId) -> HTMLCollection { fn new_inherited(collection: CollectionTypeId) -> HTMLCollection {
HTMLCollection { HTMLCollection {
collection: collection,
reflector_: Reflector::new(), reflector_: Reflector::new(),
collection: collection,
} }
} }

View file

@ -50,3 +50,46 @@ impl HTMLTableRowElement {
} }
} }
pub trait HTMLTableRowElementHelpers {
fn get_background_color(&self) -> Option<RGBA>;
}
impl HTMLTableRowElementHelpers for HTMLTableRowElement {
fn get_background_color(&self) -> Option<RGBA> {
self.background_color.get()
}
}
impl<'a> VirtualMethods for JSRef<'a, HTMLTableRowElement> {
fn super_type<'a>(&'a self) -> Option<&'a VirtualMethods> {
let htmlelement: &JSRef<HTMLElement> = HTMLElementCast::from_borrowed_ref(self);
Some(htmlelement as &VirtualMethods)
}
fn after_set_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.after_set_attr(attr),
_ => ()
}
match attr.local_name() {
&atom!("bgcolor") => {
self.background_color.set(str::parse_legacy_color(attr.value().as_slice()).ok())
}
_ => {}
}
}
fn before_remove_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.before_remove_attr(attr),
_ => ()
}
match attr.local_name() {
&atom!("bgcolor") => self.background_color.set(None),
_ => {}
}
}
}

View file

@ -49,3 +49,45 @@ impl HTMLTableSectionElement {
} }
} }
pub trait HTMLTableSectionElementHelpers {
fn get_background_color(&self) -> Option<RGBA>;
}
impl HTMLTableSectionElementHelpers for HTMLTableSectionElement {
fn get_background_color(&self) -> Option<RGBA> {
self.background_color.get()
}
}
impl<'a> VirtualMethods for JSRef<'a, HTMLTableSectionElement> {
fn super_type<'a>(&'a self) -> Option<&'a VirtualMethods> {
let htmlelement: &JSRef<HTMLElement> = HTMLElementCast::from_borrowed_ref(self);
Some(htmlelement as &VirtualMethods)
}
fn after_set_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.after_set_attr(attr),
_ => ()
}
match attr.local_name() {
&atom!("bgcolor") => {
self.background_color.set(str::parse_legacy_color(attr.value().as_slice()).ok())
}
_ => {}
}
}
fn before_remove_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.before_remove_attr(attr),
_ => ()
}
match attr.local_name() {
&atom!("bgcolor") => self.background_color.set(None),
_ => {}
}
}
}

View file

@ -329,3 +329,20 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTextAreaElement> {
} }
} }
impl<'a> FormControl<'a> for JSRef<'a, HTMLTextAreaElement> {
fn to_element(self) -> JSRef<'a, Element> {
ElementCast::from_ref(self)
}
// https://html.spec.whatwg.org/multipage/forms.html#concept-fe-mutable
fn mutable(self) -> bool {
// https://html.spec.whatwg.org/multipage/forms.html#the-textarea-element:concept-fe-mutable
!(self.Disabled() || self.ReadOnly())
}
fn reset(self) {
// https://html.spec.whatwg.org/multipage/forms.html#the-textarea-element:concept-form-reset-control
self.SetValue(self.DefaultValue());
self.value_changed.set(false);
}
}

View file

@ -64,3 +64,17 @@ impl<'a> HTMLTitleElementMethods for JSRef<'a, HTMLTitleElement> {
} }
} }
impl<'a> VirtualMethods for JSRef<'a, HTMLTitleElement> {
fn super_type<'a>(&'a self) -> Option<&'a VirtualMethods> {
let htmlelement: &JSRef<HTMLElement> = HTMLElementCast::from_borrowed_ref(self);
Some(htmlelement as &VirtualMethods)
}
fn bind_to_tree(&self, is_in_doc: bool) {
let node: JSRef<Node> = NodeCast::from_ref(*self);
if is_in_doc {
let document = node.owner_doc().root();
document.send_title_to_compositor()
}
}
}

View file

@ -19,15 +19,15 @@ pub enum NodeListType {
#[dom_struct] #[dom_struct]
pub struct NodeList { pub struct NodeList {
list_type: NodeListType,
reflector_: Reflector, reflector_: Reflector,
list_type: NodeListType,
} }
impl NodeList { impl NodeList {
fn new_inherited(list_type: NodeListType) -> NodeList { fn new_inherited(list_type: NodeListType) -> NodeList {
NodeList { NodeList {
list_type: list_type,
reflector_: Reflector::new(), reflector_: Reflector::new(),
list_type: list_type,
} }
} }

View file

@ -24,15 +24,15 @@ use std::ascii::OwnedAsciiExt;
#[dom_struct] #[dom_struct]
pub struct URLSearchParams { pub struct URLSearchParams {
data: DOMRefCell<HashMap<DOMString, Vec<DOMString>>>,
reflector_: Reflector, reflector_: Reflector,
data: DOMRefCell<HashMap<DOMString, Vec<DOMString>>>,
} }
impl URLSearchParams { impl URLSearchParams {
fn new_inherited() -> URLSearchParams { fn new_inherited() -> URLSearchParams {
URLSearchParams { URLSearchParams {
data: DOMRefCell::new(HashMap::new()),
reflector_: Reflector::new(), reflector_: Reflector::new(),
data: DOMRefCell::new(HashMap::new()),
} }
} }