mirror of
https://github.com/servo/servo.git
synced 2025-06-24 09:04:33 +01:00
Remove document::DocumentType (fixes #1730).
This commit is contained in:
parent
e2617a6396
commit
5ede84fa46
5 changed files with 53 additions and 83 deletions
|
@ -13,7 +13,7 @@ use dom::bindings::utils::{ErrorResult, Fallible, NotSupported, InvalidCharacter
|
|||
use dom::bindings::utils::{xml_name_type, InvalidXMLName};
|
||||
use dom::comment::Comment;
|
||||
use dom::documentfragment::DocumentFragment;
|
||||
use dom::documenttype;
|
||||
use dom::documenttype::DocumentType;
|
||||
use dom::domimplementation::DOMImplementation;
|
||||
use dom::element::{Element};
|
||||
use dom::element::{HTMLHtmlElementTypeId, HTMLHeadElementTypeId, HTMLTitleElementTypeId};
|
||||
|
@ -50,19 +50,11 @@ pub enum DocumentTypeId {
|
|||
HTMLDocumentTypeId
|
||||
}
|
||||
|
||||
#[deriving(Eq,Encodable)]
|
||||
pub enum DocumentType {
|
||||
HTML,
|
||||
SVG,
|
||||
XML
|
||||
}
|
||||
|
||||
#[deriving(Encodable)]
|
||||
pub struct Document {
|
||||
node: Node,
|
||||
reflector_: Reflector,
|
||||
window: JS<Window>,
|
||||
doctype: DocumentType,
|
||||
idmap: HashMap<DOMString, JS<Element>>,
|
||||
implementation: Option<JS<DOMImplementation>>,
|
||||
content_type: DOMString,
|
||||
|
@ -105,25 +97,20 @@ impl Document {
|
|||
raw_doc
|
||||
}
|
||||
|
||||
pub fn new_inherited(window: JS<Window>, url: Option<Url>, doctype: DocumentType, content_type: Option<DOMString>) -> Document {
|
||||
let node_type = match doctype {
|
||||
HTML => HTMLDocumentTypeId,
|
||||
SVG | XML => PlainDocumentTypeId
|
||||
};
|
||||
pub fn new_inherited(window: JS<Window>, url: Option<Url>, doctype: DocumentTypeId, content_type: Option<DOMString>) -> Document {
|
||||
Document {
|
||||
node: Node::new_without_doc(DocumentNodeTypeId(node_type)),
|
||||
node: Node::new_without_doc(DocumentNodeTypeId(doctype)),
|
||||
reflector_: Reflector::new(),
|
||||
window: window,
|
||||
doctype: doctype,
|
||||
idmap: HashMap::new(),
|
||||
implementation: None,
|
||||
content_type: match content_type {
|
||||
Some(string) => string.clone(),
|
||||
None => match doctype {
|
||||
// http://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
|
||||
HTML => ~"text/html",
|
||||
HTMLDocumentTypeId => ~"text/html",
|
||||
// http://dom.spec.whatwg.org/#concept-document-content-type
|
||||
SVG | XML => ~"application/xml"
|
||||
PlainDocumentTypeId => ~"application/xml"
|
||||
}
|
||||
},
|
||||
extra: Untraceable {
|
||||
|
@ -139,7 +126,7 @@ impl Document {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn new(window: &JS<Window>, url: Option<Url>, doctype: DocumentType, content_type: Option<DOMString>) -> JS<Document> {
|
||||
pub fn new(window: &JS<Window>, url: Option<Url>, doctype: DocumentTypeId, content_type: Option<DOMString>) -> JS<Document> {
|
||||
let document = Document::new_inherited(window.clone(), url, doctype, content_type);
|
||||
Document::reflect_document(~document, window, DocumentBinding::Wrap)
|
||||
}
|
||||
|
@ -148,7 +135,7 @@ impl Document {
|
|||
impl Document {
|
||||
// http://dom.spec.whatwg.org/#dom-document
|
||||
pub fn Constructor(owner: &JS<Window>) -> Fallible<JS<Document>> {
|
||||
Ok(Document::new(owner, None, XML, None))
|
||||
Ok(Document::new(owner, None, PlainDocumentTypeId, None))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +195,7 @@ impl Document {
|
|||
}
|
||||
|
||||
// http://dom.spec.whatwg.org/#dom-document-doctype
|
||||
pub fn GetDoctype(&self) -> Option<JS<documenttype::DocumentType>> {
|
||||
pub fn GetDoctype(&self) -> Option<JS<DocumentType>> {
|
||||
self.node.children().find(|child| child.is_doctype())
|
||||
.map(|node| DocumentTypeCast::to(&node))
|
||||
}
|
||||
|
@ -290,24 +277,17 @@ impl Document {
|
|||
// http://www.whatwg.org/specs/web-apps/current-work/#document.title
|
||||
pub fn Title(&self, _: &JS<Document>) -> DOMString {
|
||||
let mut title = ~"";
|
||||
match self.doctype {
|
||||
SVG => {
|
||||
fail!("no SVG document yet")
|
||||
},
|
||||
_ => {
|
||||
self.GetDocumentElement().map(|root| {
|
||||
let root: JS<Node> = NodeCast::from(&root);
|
||||
root.traverse_preorder()
|
||||
.find(|node| node.type_id() == ElementNodeTypeId(HTMLTitleElementTypeId))
|
||||
.map(|title_elem| {
|
||||
for child in title_elem.children() {
|
||||
let text: JS<Text> = TextCast::to(&child);
|
||||
title.push_str(text.get().characterdata.data.as_slice());
|
||||
}
|
||||
});
|
||||
self.GetDocumentElement().map(|root| {
|
||||
let root: JS<Node> = NodeCast::from(&root);
|
||||
root.traverse_preorder()
|
||||
.find(|node| node.type_id() == ElementNodeTypeId(HTMLTitleElementTypeId))
|
||||
.map(|title_elem| {
|
||||
for child in title_elem.children() {
|
||||
let text: JS<Text> = TextCast::to(&child);
|
||||
title.push_str(text.get().characterdata.data.as_slice());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
let v: ~[&str] = title.words().collect();
|
||||
title = v.connect(" ");
|
||||
title = title.trim().to_owned();
|
||||
|
@ -316,41 +296,34 @@ impl Document {
|
|||
|
||||
// http://www.whatwg.org/specs/web-apps/current-work/#document.title
|
||||
pub fn SetTitle(&self, abstract_self: &JS<Document>, title: DOMString) -> ErrorResult {
|
||||
match self.doctype {
|
||||
SVG => {
|
||||
fail!("no SVG document yet")
|
||||
},
|
||||
_ => {
|
||||
self.GetDocumentElement().map(|root| {
|
||||
let root: JS<Node> = NodeCast::from(&root);
|
||||
let mut head_node = root.traverse_preorder().find(|child| {
|
||||
child.get().type_id == ElementNodeTypeId(HTMLHeadElementTypeId)
|
||||
});
|
||||
head_node.as_mut().map(|head| {
|
||||
self.GetDocumentElement().map(|root| {
|
||||
let root: JS<Node> = NodeCast::from(&root);
|
||||
let mut head_node = root.traverse_preorder().find(|child| {
|
||||
child.get().type_id == ElementNodeTypeId(HTMLHeadElementTypeId)
|
||||
});
|
||||
head_node.as_mut().map(|head| {
|
||||
|
||||
let mut title_node = head.children().find(|child| {
|
||||
child.get().type_id == ElementNodeTypeId(HTMLTitleElementTypeId)
|
||||
});
|
||||
|
||||
title_node.as_mut().map(|title_node| {
|
||||
for mut title_child in title_node.children() {
|
||||
title_node.RemoveChild(&mut title_child);
|
||||
}
|
||||
let new_text = self.CreateTextNode(abstract_self, title.clone());
|
||||
title_node.AppendChild(&mut NodeCast::from(&new_text));
|
||||
});
|
||||
|
||||
if title_node.is_none() {
|
||||
let mut new_title: JS<Node> =
|
||||
NodeCast::from(&HTMLTitleElement::new(~"title", abstract_self));
|
||||
let new_text = self.CreateTextNode(abstract_self, title.clone());
|
||||
new_title.AppendChild(&mut NodeCast::from(&new_text));
|
||||
head.AppendChild(&mut new_title);
|
||||
}
|
||||
});
|
||||
let mut title_node = head.children().find(|child| {
|
||||
child.get().type_id == ElementNodeTypeId(HTMLTitleElementTypeId)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
title_node.as_mut().map(|title_node| {
|
||||
for mut title_child in title_node.children() {
|
||||
title_node.RemoveChild(&mut title_child);
|
||||
}
|
||||
let new_text = self.CreateTextNode(abstract_self, title.clone());
|
||||
title_node.AppendChild(&mut NodeCast::from(&new_text));
|
||||
});
|
||||
|
||||
if title_node.is_none() {
|
||||
let mut new_title: JS<Node> =
|
||||
NodeCast::from(&HTMLTitleElement::new(~"title", abstract_self));
|
||||
let new_text = self.CreateTextNode(abstract_self, title.clone());
|
||||
new_title.AppendChild(&mut NodeCast::from(&new_text));
|
||||
head.AppendChild(&mut new_title);
|
||||
}
|
||||
});
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue