mirror of
https://github.com/servo/servo.git
synced 2025-06-24 09:04:33 +01:00
auto merge of #658 : june0cho/servo/docBindings, r=jdm
Add a getter for document.head and document.title. Like in the modified test (document.title="changed title"), setters for elements doesn't seem to be called.
This commit is contained in:
commit
ad41c3acfb
5 changed files with 95 additions and 11 deletions
|
@ -5,12 +5,12 @@
|
||||||
use dom::bindings::codegen::DocumentBinding;
|
use dom::bindings::codegen::DocumentBinding;
|
||||||
use dom::bindings::utils::{DOMString, WrapperCache, ErrorResult, null_string, str};
|
use dom::bindings::utils::{DOMString, WrapperCache, ErrorResult, null_string, str};
|
||||||
use dom::bindings::utils::{BindingObject, CacheableWrapper, rust_box, DerivedWrapper};
|
use dom::bindings::utils::{BindingObject, CacheableWrapper, rust_box, DerivedWrapper};
|
||||||
use dom::element::{HTMLHtmlElement, HTMLHtmlElementTypeId};
|
use dom::element::{HTMLHtmlElement, HTMLTitleElement, HTMLHtmlElementTypeId, HTMLHeadElementTypeId, HTMLTitleElementTypeId};
|
||||||
use dom::event::Event;
|
use dom::event::Event;
|
||||||
use dom::htmlcollection::HTMLCollection;
|
use dom::htmlcollection::HTMLCollection;
|
||||||
use dom::htmldocument::HTMLDocument;
|
use dom::htmldocument::HTMLDocument;
|
||||||
use dom::htmlelement::HTMLElement;
|
use dom::htmlelement::HTMLElement;
|
||||||
use dom::node::{AbstractNode, ScriptView, Node};
|
use dom::node::{AbstractNode, ScriptView, Node, ElementNodeTypeId, Text};
|
||||||
use dom::window::Window;
|
use dom::window::Window;
|
||||||
use dom::windowproxy::WindowProxy;
|
use dom::windowproxy::WindowProxy;
|
||||||
|
|
||||||
|
@ -274,14 +274,77 @@ impl Document {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Title(&self) -> DOMString {
|
pub fn Title(&self) -> DOMString {
|
||||||
str(self.title.clone())
|
let mut title = ~"";
|
||||||
|
match self.doctype {
|
||||||
|
SVG => {
|
||||||
|
fail!("no SVG document yet")
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
let _ = for self.root.traverse_preorder |node| {
|
||||||
|
if node.type_id() != ElementNodeTypeId(HTMLTitleElementTypeId) {
|
||||||
|
loop;
|
||||||
|
}
|
||||||
|
for node.children().advance |child| {
|
||||||
|
if child.is_text() {
|
||||||
|
do child.with_imm_text() |text| {
|
||||||
|
let s = text.parent.Data();
|
||||||
|
title = title + s.to_str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let v: ~[&str] = title.word_iter().collect();
|
||||||
|
title = v.connect(" ");
|
||||||
|
title = title.trim().to_owned();
|
||||||
|
str(title)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn SetTitle(&mut self, title: &DOMString, _rv: &mut ErrorResult) {
|
pub fn SetTitle(&self, title: &DOMString, _rv: &mut ErrorResult) {
|
||||||
self.title = match title {
|
match self.doctype {
|
||||||
&str(ref s) => s.clone(),
|
SVG => {
|
||||||
&null_string => ~""
|
fail!("no SVG document yet")
|
||||||
};
|
},
|
||||||
|
_ => {
|
||||||
|
let (_scope, cx) = self.get_scope_and_cx();
|
||||||
|
let _ = for self.root.traverse_preorder |node| {
|
||||||
|
if node.type_id() != ElementNodeTypeId(HTMLHeadElementTypeId) {
|
||||||
|
loop;
|
||||||
|
}
|
||||||
|
let mut has_title = false;
|
||||||
|
for node.children().advance |child| {
|
||||||
|
if child.type_id() != ElementNodeTypeId(HTMLTitleElementTypeId) {
|
||||||
|
loop;
|
||||||
|
}
|
||||||
|
has_title = true;
|
||||||
|
for child.children().advance |title_child| {
|
||||||
|
child.remove_child(title_child);
|
||||||
|
}
|
||||||
|
let new_text = unsafe {
|
||||||
|
Node::as_abstract_node(cx, @Text::new(title.to_str()))
|
||||||
|
};
|
||||||
|
child.add_child(new_text);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if !has_title {
|
||||||
|
let new_title = @HTMLTitleElement {
|
||||||
|
parent: HTMLElement::new(HTMLTitleElementTypeId, ~"title")
|
||||||
|
};
|
||||||
|
let new_title = unsafe {
|
||||||
|
Node::as_abstract_node(cx, new_title)
|
||||||
|
};
|
||||||
|
let new_text = unsafe {
|
||||||
|
Node::as_abstract_node(cx, @Text::new(title.to_str()))
|
||||||
|
};
|
||||||
|
new_title.add_child(new_text);
|
||||||
|
node.add_child(new_title);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Dir(&self) -> DOMString {
|
pub fn Dir(&self) -> DOMString {
|
||||||
|
|
|
@ -6,9 +6,9 @@ use dom::bindings::codegen::HTMLDocumentBinding;
|
||||||
use dom::bindings::utils::{DOMString, ErrorResult, null_string};
|
use dom::bindings::utils::{DOMString, ErrorResult, null_string};
|
||||||
use dom::bindings::utils::{CacheableWrapper, BindingObject, WrapperCache};
|
use dom::bindings::utils::{CacheableWrapper, BindingObject, WrapperCache};
|
||||||
use dom::document::{AbstractDocument, Document, WrappableDocument, HTML};
|
use dom::document::{AbstractDocument, Document, WrappableDocument, HTML};
|
||||||
use dom::element::Element;
|
use dom::element::{Element, HTMLHeadElementTypeId};
|
||||||
use dom::htmlcollection::HTMLCollection;
|
use dom::htmlcollection::HTMLCollection;
|
||||||
use dom::node::{AbstractNode, ScriptView};
|
use dom::node::{AbstractNode, ScriptView, ElementNodeTypeId};
|
||||||
use dom::window::Window;
|
use dom::window::Window;
|
||||||
|
|
||||||
use js::jsapi::{JSObject, JSContext};
|
use js::jsapi::{JSObject, JSContext};
|
||||||
|
@ -68,7 +68,14 @@ impl HTMLDocument {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn GetHead(&self) -> Option<AbstractNode<ScriptView>> {
|
pub fn GetHead(&self) -> Option<AbstractNode<ScriptView>> {
|
||||||
None
|
let mut headNode: Option<AbstractNode<ScriptView>> = None;
|
||||||
|
let _ = for self.parent.root.traverse_preorder |child| {
|
||||||
|
if child.type_id() == ElementNodeTypeId(HTMLHeadElementTypeId) {
|
||||||
|
headNode = Some(child);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
headNode
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Images(&self) -> @mut HTMLCollection {
|
pub fn Images(&self) -> @mut HTMLCollection {
|
||||||
|
|
|
@ -349,6 +349,13 @@ impl<'self, View> AbstractNode<View> {
|
||||||
self.transmute(f)
|
self.transmute(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_mut_text<R>(self, f: &fn(&mut Text) -> R) -> R {
|
||||||
|
if !self.is_text() {
|
||||||
|
fail!(~"node is not text");
|
||||||
|
}
|
||||||
|
self.transmute_mut(f)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_element(self) -> bool {
|
pub fn is_element(self) -> bool {
|
||||||
match self.type_id() {
|
match self.type_id() {
|
||||||
ElementNodeTypeId(*) => true,
|
ElementNodeTypeId(*) => true,
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
<html>
|
<html>
|
||||||
<!-- comment -->
|
<!-- comment -->
|
||||||
<head>
|
<head>
|
||||||
|
<title>test_binding
|
||||||
|
page </title>
|
||||||
<script src="test_bindings.js"></script>
|
<script src="test_bindings.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
|
@ -96,6 +96,11 @@ window.alert(tags.length);
|
||||||
window.alert(tags[0]);
|
window.alert(tags[0]);
|
||||||
window.alert(tags[0].tagName);
|
window.alert(tags[0].tagName);
|
||||||
|
|
||||||
|
window.alert("Document:");
|
||||||
|
let head = document.head;
|
||||||
|
window.alert(head);
|
||||||
|
window.alert(head.tagName);
|
||||||
|
|
||||||
window.alert("DOMParser:");
|
window.alert("DOMParser:");
|
||||||
window.alert(DOMParser);
|
window.alert(DOMParser);
|
||||||
let parser = new DOMParser();
|
let parser = new DOMParser();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue