mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Implement JSManaged for DOM objects.
This commit is contained in:
parent
061269f963
commit
625325434b
137 changed files with 3644 additions and 2778 deletions
|
@ -3,36 +3,39 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::bindings::codegen::DOMImplementationBinding;
|
||||
use dom::bindings::codegen::InheritTypes::{NodeCast, DocumentCast};
|
||||
use dom::bindings::js::JS;
|
||||
use dom::bindings::utils::{Reflector, Reflectable, reflect_dom_object};
|
||||
use dom::bindings::utils::{Fallible, InvalidCharacter, NamespaceError};
|
||||
use dom::bindings::utils::{QName, Name, InvalidXMLName, xml_name_type};
|
||||
use dom::document::{AbstractDocument, HTML, HTMLDocumentTypeId};
|
||||
use dom::document::{Document, HTML, HTMLDocumentTypeId};
|
||||
use dom::documenttype::DocumentType;
|
||||
use dom::htmldocument::HTMLDocument;
|
||||
use dom::htmlbodyelement::HTMLBodyElement;
|
||||
use dom::htmlheadelement::HTMLHeadElement;
|
||||
use dom::htmlhtmlelement::HTMLHtmlElement;
|
||||
use dom::htmltitleelement::HTMLTitleElement;
|
||||
use dom::node::{AbstractNode, DocumentNodeTypeId};
|
||||
use dom::node::{Node, DocumentNodeTypeId, NodeHelpers, INode};
|
||||
use dom::text::Text;
|
||||
use dom::window::Window;
|
||||
use servo_util::str::DOMString;
|
||||
|
||||
#[deriving(Encodable)]
|
||||
pub struct DOMImplementation {
|
||||
owner: @mut Window,
|
||||
reflector_: Reflector
|
||||
owner: JS<Window>,
|
||||
reflector_: Reflector,
|
||||
}
|
||||
|
||||
impl DOMImplementation {
|
||||
pub fn new_inherited(owner: @mut Window) -> DOMImplementation {
|
||||
pub fn new_inherited(owner: JS<Window>) -> DOMImplementation {
|
||||
DOMImplementation {
|
||||
owner: owner,
|
||||
reflector_: Reflector::new()
|
||||
reflector_: Reflector::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(owner: @mut Window) -> @mut DOMImplementation {
|
||||
reflect_dom_object(@mut DOMImplementation::new_inherited(owner), owner,
|
||||
pub fn new(owner: &JS<Window>) -> JS<DOMImplementation> {
|
||||
reflect_dom_object(~DOMImplementation::new_inherited(owner.clone()), owner.get(),
|
||||
DOMImplementationBinding::Wrap)
|
||||
}
|
||||
}
|
||||
|
@ -50,66 +53,66 @@ impl Reflectable for DOMImplementation {
|
|||
// http://dom.spec.whatwg.org/#domimplementation
|
||||
impl DOMImplementation {
|
||||
// http://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
|
||||
pub fn CreateDocumentType(&self, qname: DOMString, pubid: DOMString, sysid: DOMString) -> Fallible<AbstractNode> {
|
||||
pub fn CreateDocumentType(&self, qname: DOMString, pubid: DOMString, sysid: DOMString) -> Fallible<JS<DocumentType>> {
|
||||
match xml_name_type(qname) {
|
||||
// Step 1.
|
||||
InvalidXMLName => Err(InvalidCharacter),
|
||||
// Step 2.
|
||||
Name => Err(NamespaceError),
|
||||
// Step 3.
|
||||
QName => Ok(DocumentType::new(qname, Some(pubid), Some(sysid), self.owner.Document()))
|
||||
QName => Ok(DocumentType::new(qname, Some(pubid), Some(sysid), &self.owner.get().Document()))
|
||||
}
|
||||
}
|
||||
|
||||
// http://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
|
||||
pub fn CreateHTMLDocument(&self, title: Option<DOMString>) -> AbstractDocument {
|
||||
pub fn CreateHTMLDocument(&self, title: Option<DOMString>) -> JS<Document> {
|
||||
// Step 1-2.
|
||||
let abstract_doc = HTMLDocument::new(self.owner, None);
|
||||
assert!(abstract_doc.document().doctype == HTML);
|
||||
let doc: JS<Document> = DocumentCast::from(&HTMLDocument::new(&self.owner, None));
|
||||
assert!(doc.get().doctype == HTML);
|
||||
|
||||
let abstract_node = AbstractNode::from_document(abstract_doc);
|
||||
assert!(abstract_node.type_id() == DocumentNodeTypeId(HTMLDocumentTypeId));
|
||||
let mut doc_node: JS<Node> = NodeCast::from(&doc);
|
||||
assert!(doc_node.type_id() == DocumentNodeTypeId(HTMLDocumentTypeId));
|
||||
|
||||
{
|
||||
// Step 3.
|
||||
let doc_type = DocumentType::new(~"html", None, None, abstract_doc);
|
||||
abstract_node.AppendChild(doc_type);
|
||||
let doc_type = DocumentType::new(~"html", None, None, &doc);
|
||||
doc_node.AppendChild(&mut NodeCast::from(&doc_type));
|
||||
}
|
||||
|
||||
{
|
||||
// Step 4.
|
||||
let doc_html = HTMLHtmlElement::new(~"html", abstract_doc);
|
||||
abstract_node.AppendChild(doc_html);
|
||||
let mut doc_html = NodeCast::from(&HTMLHtmlElement::new(~"html", &doc));
|
||||
doc_node.AppendChild(&mut doc_html);
|
||||
|
||||
{
|
||||
// Step 5.
|
||||
let doc_head = HTMLHeadElement::new(~"head", abstract_doc);
|
||||
doc_html.AppendChild(doc_head);
|
||||
let mut doc_head = NodeCast::from(&HTMLHeadElement::new(~"head", &doc));
|
||||
doc_html.AppendChild(&mut doc_head);
|
||||
|
||||
// Step 6.
|
||||
match title {
|
||||
None => (),
|
||||
Some(title_str) => {
|
||||
// Step 6.1.
|
||||
let doc_title = HTMLTitleElement::new(~"title", abstract_doc);
|
||||
doc_head.AppendChild(doc_title);
|
||||
let mut doc_title = NodeCast::from(&HTMLTitleElement::new(~"title", &doc));
|
||||
doc_head.AppendChild(&mut doc_title);
|
||||
|
||||
// Step 6.2.
|
||||
let title_text = Text::new(title_str, abstract_doc);
|
||||
doc_title.AppendChild(title_text);
|
||||
let title_text = Text::new(title_str, &doc);
|
||||
doc_title.AppendChild(&mut NodeCast::from(&title_text));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 7.
|
||||
let doc_body = HTMLBodyElement::new(~"body", abstract_doc);
|
||||
doc_html.AppendChild(doc_body);
|
||||
let doc_body = HTMLBodyElement::new(~"body", &doc);
|
||||
doc_html.AppendChild(&mut NodeCast::from(&doc_body));
|
||||
}
|
||||
|
||||
// Step 8.
|
||||
// FIXME: https://github.com/mozilla/servo/issues/1522
|
||||
|
||||
// Step 9.
|
||||
abstract_doc
|
||||
doc
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue