Move EventTargetTypeId/NodeTypeId to DOMClass

This commit is contained in:
Michael Wu 2015-07-10 21:27:41 -04:00 committed by Anthony Ramine
parent 8d7ba12f28
commit 941f7dc04b
24 changed files with 149 additions and 74 deletions

View file

@ -81,9 +81,6 @@ pub struct Node {
/// The JavaScript reflector for this node.
eventtarget: EventTarget,
/// The type of node that this is.
type_id: NodeTypeId,
/// The parent of this node.
parent_node: MutNullableHeap<JS<Node>>,
@ -284,9 +281,10 @@ impl LayoutDataRef {
}
/// The different types of nodes.
#[derive(JSTraceable, Copy, Clone, PartialEq, Debug)]
#[derive(HeapSizeOf)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum NodeTypeId {
Node,
CharacterData(CharacterDataTypeId),
DocumentType,
DocumentFragment,
@ -452,7 +450,7 @@ impl Node {
/// Returns a string that describes this node.
pub fn debug_str(&self) -> String {
format!("{:?}", self.type_id)
format!("{:?}", self.type_id())
}
pub fn is_in_doc(&self) -> bool {
@ -461,12 +459,15 @@ impl Node {
/// Returns the type ID of this node. Fails if this node is borrowed mutably.
pub fn type_id(&self) -> NodeTypeId {
self.type_id
match *self.eventtarget.type_id() {
EventTargetTypeId::Node(type_id) => type_id,
_ => unreachable!(),
}
}
// https://dom.spec.whatwg.org/#concept-node-length
pub fn len(&self) -> u32 {
match self.type_id {
match self.type_id() {
NodeTypeId::DocumentType => 0,
NodeTypeId::CharacterData(_) => {
CharacterDataCast::to_ref(self).unwrap().Length()
@ -486,12 +487,12 @@ impl Node {
#[inline]
pub fn is_anchor_element(&self) -> bool {
self.type_id == NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLAnchorElement))
self.type_id() == NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLAnchorElement))
}
#[inline]
pub fn is_doctype(&self) -> bool {
self.type_id == NodeTypeId::DocumentType
self.type_id() == NodeTypeId::DocumentType
}
pub fn get_flag(&self, flag: NodeFlags) -> bool {
@ -1026,7 +1027,7 @@ impl LayoutNodeHelpers for LayoutJS<Node> {
#[inline]
#[allow(unsafe_code)]
unsafe fn type_id_for_layout(&self) -> NodeTypeId {
(*self.unsafe_get()).type_id
(*self.unsafe_get()).type_id()
}
#[inline]
@ -1393,8 +1394,7 @@ impl Node {
fn new_(type_id: NodeTypeId, doc: Option<&Document>) -> Node {
Node {
eventtarget: EventTarget::new_inherited(EventTargetTypeId::Node(type_id)),
type_id: type_id,
eventtarget: EventTarget::new_inherited(),
parent_node: Default::default(),
first_child: Default::default(),
@ -1456,6 +1456,8 @@ impl Node {
// Step 4-5.
match node.type_id() {
NodeTypeId::Node => unreachable!(),
NodeTypeId::CharacterData(CharacterDataTypeId::CharacterData) => unreachable!(),
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => {
if parent.is_document() {
return Err(HierarchyRequest);
@ -1538,7 +1540,7 @@ impl Node {
}
},
NodeTypeId::CharacterData(_) => (),
NodeTypeId::Document => unreachable!(),
NodeTypeId::Document | NodeTypeId::Node => unreachable!(),
}
}
Ok(())
@ -1698,6 +1700,8 @@ impl Node {
// Step 2.
// XXXabinader: clone() for each node as trait?
let copy: Root<Node> = match node.type_id() {
NodeTypeId::Node => unreachable!(),
NodeTypeId::CharacterData(CharacterDataTypeId::CharacterData) => unreachable!(),
NodeTypeId::DocumentType => {
let doctype: &DocumentType = DocumentTypeCast::to_ref(node).unwrap();
let doctype = DocumentType::new(doctype.name().clone(),
@ -1833,7 +1837,7 @@ impl Node {
}
}
match node.type_id {
match node.type_id() {
NodeTypeId::Element(_) => {
let element = ElementCast::to_ref(node).unwrap();
// Step 1.
@ -1889,7 +1893,9 @@ impl Node {
impl NodeMethods for Node {
// https://dom.spec.whatwg.org/#dom-node-nodetype
fn NodeType(&self) -> u16 {
match self.type_id {
match self.type_id() {
NodeTypeId::Node => unreachable!(),
NodeTypeId::CharacterData(CharacterDataTypeId::CharacterData) => unreachable!(),
NodeTypeId::CharacterData(CharacterDataTypeId::Text) =>
NodeConstants::TEXT_NODE,
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) =>
@ -1909,11 +1915,13 @@ impl NodeMethods for Node {
// https://dom.spec.whatwg.org/#dom-node-nodename
fn NodeName(&self) -> DOMString {
match self.type_id {
match self.type_id() {
NodeTypeId::Node => unreachable!(),
NodeTypeId::Element(..) => {
let elem: &Element = ElementCast::to_ref(self).unwrap();
elem.TagName()
}
NodeTypeId::CharacterData(CharacterDataTypeId::CharacterData) => unreachable!(),
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => "#text".to_owned(),
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
let processing_instruction: &ProcessingInstruction =
@ -1937,7 +1945,8 @@ impl NodeMethods for Node {
// https://dom.spec.whatwg.org/#dom-node-ownerdocument
fn GetOwnerDocument(&self) -> Option<Root<Document>> {
match self.type_id {
match self.type_id() {
NodeTypeId::Node => unreachable!(),
NodeTypeId::CharacterData(..) |
NodeTypeId::Element(..) |
NodeTypeId::DocumentType |
@ -1992,19 +2001,31 @@ impl NodeMethods for Node {
// https://dom.spec.whatwg.org/#dom-node-nodevalue
fn GetNodeValue(&self) -> Option<DOMString> {
CharacterDataCast::to_ref(self).map(|c| c.Data())
match self.type_id() {
NodeTypeId::CharacterData(..) => {
let chardata: &CharacterData = CharacterDataCast::to_ref(self).unwrap();
Some(chardata.Data())
}
_ => {
None
}
}
}
// https://dom.spec.whatwg.org/#dom-node-nodevalue
fn SetNodeValue(&self, val: Option<DOMString>) {
if let NodeTypeId::CharacterData(..) = self.type_id {
self.SetTextContent(val)
match self.type_id() {
NodeTypeId::CharacterData(..) => {
self.SetTextContent(val)
}
_ => {}
}
}
// https://dom.spec.whatwg.org/#dom-node-textcontent
fn GetTextContent(&self) -> Option<DOMString> {
match self.type_id {
match self.type_id() {
NodeTypeId::Node => unreachable!(),
NodeTypeId::DocumentFragment |
NodeTypeId::Element(..) => {
let content = Node::collect_text_contents(self.traverse_preorder());
@ -2024,7 +2045,8 @@ impl NodeMethods for Node {
// https://dom.spec.whatwg.org/#dom-node-textcontent
fn SetTextContent(&self, value: Option<DOMString>) {
let value = value.unwrap_or(String::new());
match self.type_id {
match self.type_id() {
NodeTypeId::Node => unreachable!(),
NodeTypeId::DocumentFragment |
NodeTypeId::Element(..) => {
// Step 1-2.
@ -2065,7 +2087,7 @@ impl NodeMethods for Node {
fn ReplaceChild(&self, node: &Node, child: &Node) -> Fallible<Root<Node>> {
// Step 1.
match self.type_id {
match self.type_id() {
NodeTypeId::Document |
NodeTypeId::DocumentFragment |
NodeTypeId::Element(..) => (),
@ -2084,6 +2106,7 @@ impl NodeMethods for Node {
// Step 4-5.
match node.type_id() {
NodeTypeId::Node => unreachable!(),
NodeTypeId::CharacterData(CharacterDataTypeId::Text) if self.is_document() =>
return Err(HierarchyRequest),
NodeTypeId::DocumentType if !self.is_document() => return Err(HierarchyRequest),
@ -2147,7 +2170,7 @@ impl NodeMethods for Node {
}
},
NodeTypeId::CharacterData(..) => (),
NodeTypeId::Document => unreachable!()
NodeTypeId::Document | NodeTypeId::Node => unreachable!()
}
}