Make Document a Node.

This commit is contained in:
Josh Matthews 2013-10-17 16:42:51 +01:00
parent b1c068b203
commit 4e47d59165
10 changed files with 94 additions and 96 deletions

View file

@ -13,14 +13,12 @@ use dom::element::{HTMLHtmlElementTypeId, HTMLHeadElementTypeId, HTMLTitleElemen
use dom::htmlcollection::HTMLCollection;
use dom::htmldocument::HTMLDocument;
use dom::htmlelement::HTMLElement;
use dom::htmlhtmlelement::HTMLHtmlElement;
use dom::node::{AbstractNode, ScriptView, Node, ElementNodeTypeId};
use dom::node::{AbstractNode, ScriptView, Node, ElementNodeTypeId, DocumentNodeTypeId};
use dom::text::Text;
use dom::window::Window;
use dom::htmltitleelement::HTMLTitleElement;
use html::hubbub_html_parser::build_element_from_tag;
use js::jsapi::{JSObject, JSContext, JSVal};
use js::jsapi::{JSTRACE_OBJECT, JSTracer, JS_CallTracer};
use js::jsapi::{JSObject, JSContext, JSVal, JSTracer};
use js::glue::RUST_OBJECT_TO_JSVAL;
use servo_util::tree::{TreeNodeRef, ElementLike};
@ -29,12 +27,18 @@ use std::hashmap::HashMap;
use std::cast;
use std::ptr;
use std::str::eq_slice;
use std::libc;
use std::ascii::StrAsciiExt;
use std::unstable::raw::Box;
#[deriving(Eq)]
pub enum DocumentTypeId {
PlainDocumentTypeId,
HTMLDocumentTypeId
}
pub trait ReflectableDocument {
fn init_reflector(@mut self, cx: *JSContext);
fn init_node(@mut self, doc: AbstractDocument);
}
#[deriving(Eq)]
@ -45,9 +49,11 @@ pub struct AbstractDocument {
impl AbstractDocument {
pub fn as_abstract<T: ReflectableDocument>(cx: *JSContext, doc: @mut T) -> AbstractDocument {
doc.init_reflector(cx);
AbstractDocument {
let abstract = AbstractDocument {
document: unsafe { cast::transmute(doc) }
}
};
doc.init_node(abstract);
abstract
}
pub fn document<'a>(&'a self) -> &'a Document {
@ -91,7 +97,7 @@ impl AbstractDocument {
});
let document = self.mut_document();
document.root = Some(root);
document.node.AppendChild(AbstractNode::from_document(*self), root);
// Register elements having "id" attribute to the owner doc.
document.register_nodes_with_id(&root);
document.content_changed();
@ -105,7 +111,7 @@ pub enum DocumentType {
}
pub struct Document {
priv root: Option<AbstractNode<ScriptView>>,
node: Node<ScriptView>,
reflector_: Reflector,
window: @mut Window,
doctype: DocumentType,
@ -116,8 +122,12 @@ pub struct Document {
impl Document {
#[fixed_stack_segment]
pub fn new(window: @mut Window, doctype: DocumentType) -> Document {
let node_type = match doctype {
HTML => HTMLDocumentTypeId,
SVG | XML => PlainDocumentTypeId
};
Document {
root: None,
node: Node::new_without_doc(DocumentNodeTypeId(node_type)),
reflector_: Reflector::new(),
window: window,
doctype: doctype,
@ -128,16 +138,7 @@ impl Document {
pub fn Constructor(owner: @mut Window) -> Fallible<AbstractDocument> {
let cx = owner.get_cx();
let document = AbstractDocument::as_abstract(cx, @mut Document::new(owner, XML));
let root = @HTMLHtmlElement {
htmlelement: HTMLElement::new(HTMLHtmlElementTypeId, ~"html", document)
};
let root = unsafe { Node::as_abstract_node(cx, root) };
document.set_root(root);
Ok(document)
Ok(AbstractDocument::as_abstract(cx, @mut Document::new(owner, XML)))
}
}
@ -145,6 +146,11 @@ impl ReflectableDocument for Document {
fn init_reflector(@mut self, cx: *JSContext) {
self.wrap_object_shared(cx, ptr::null()); //XXXjdm a proper scope would be nice
}
fn init_node(@mut self, doc: AbstractDocument) {
self.node.set_owner_doc(doc);
self.node.add_to_doc(AbstractNode::from_document(doc), doc);
}
}
impl Reflectable for AbstractDocument {
@ -184,11 +190,11 @@ impl DerivedWrapper for AbstractDocument {
impl Reflectable for Document {
fn reflector<'a>(&'a self) -> &'a Reflector {
&self.reflector_
self.node.reflector()
}
fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
&mut self.reflector_
self.node.mut_reflector()
}
fn wrap_object_shared(@mut self, cx: *JSContext, scope: *JSObject) -> *JSObject {
@ -202,7 +208,7 @@ impl Reflectable for Document {
impl Document {
pub fn GetDocumentElement(&self) -> Option<AbstractNode<ScriptView>> {
self.root
self.node.first_child
}
fn get_cx(&self) -> *JSContext {
@ -414,21 +420,6 @@ fn foreach_ided_elements(root: &AbstractNode<ScriptView>,
impl Traceable for Document {
#[fixed_stack_segment]
fn trace(&self, tracer: *mut JSTracer) {
match self.root {
None => {},
Some(root) => {
unsafe {
(*tracer).debugPrinter = ptr::null();
(*tracer).debugPrintIndex = -1;
do "root".to_c_str().with_ref |name| {
(*tracer).debugPrintArg = name as *libc::c_void;
debug!("tracing root node");
JS_CallTracer(tracer as *JSTracer,
root.reflector().get_jsobject(),
JSTRACE_OBJECT as u32);
}
}
}
}
self.node.trace(tracer);
}
}