Issue #1071 - Ensure that Documents always have a non-null Window.

This commit is contained in:
Ms2ger 2013-10-16 10:00:38 +02:00
parent c9c9eec3d8
commit 60b6d1bb57
7 changed files with 29 additions and 53 deletions

View file

@ -112,7 +112,7 @@ pub enum DocumentType {
pub struct Document { pub struct Document {
priv root: Option<AbstractNode<ScriptView>>, priv root: Option<AbstractNode<ScriptView>>,
reflector_: Reflector, reflector_: Reflector,
window: Option<@mut Window>, window: @mut Window,
doctype: DocumentType, doctype: DocumentType,
title: ~str, title: ~str,
idmap: HashMap<~str, AbstractNode<ScriptView>> idmap: HashMap<~str, AbstractNode<ScriptView>>
@ -120,7 +120,7 @@ pub struct Document {
impl Document { impl Document {
#[fixed_stack_segment] #[fixed_stack_segment]
pub fn new(window: Option<@mut Window>, doctype: DocumentType) -> Document { pub fn new(window: @mut Window, doctype: DocumentType) -> Document {
Document { Document {
root: None, root: None,
reflector_: Reflector::new(), reflector_: Reflector::new(),
@ -134,7 +134,7 @@ impl Document {
pub fn Constructor(owner: @mut Window) -> Fallible<AbstractDocument> { pub fn Constructor(owner: @mut Window) -> Fallible<AbstractDocument> {
let cx = owner.get_cx(); let cx = owner.get_cx();
let document = AbstractDocument::as_abstract(cx, @mut Document::new(None, XML)); let document = AbstractDocument::as_abstract(cx, @mut Document::new(owner, XML));
let root = @HTMLHtmlElement { let root = @HTMLHtmlElement {
htmlelement: HTMLElement::new(HTMLHtmlElementTypeId, ~"html", document) htmlelement: HTMLElement::new(HTMLHtmlElementTypeId, ~"html", document)
@ -216,10 +216,7 @@ impl Reflectable for Document {
impl BindingObject for Document { impl BindingObject for Document {
fn GetParentObject(&self, _cx: *JSContext) -> Option<@mut Reflectable> { fn GetParentObject(&self, _cx: *JSContext) -> Option<@mut Reflectable> {
match self.window { Some(self.window as @mut Reflectable)
Some(win) => Some(win as @mut Reflectable),
None => None
}
} }
} }
@ -249,11 +246,11 @@ impl Document {
} }
fn get_cx(&self) -> *JSContext { fn get_cx(&self) -> *JSContext {
self.window.get_ref().get_cx() self.window.get_cx()
} }
fn get_scope_and_cx(&self) -> (*JSObject, *JSContext) { fn get_scope_and_cx(&self) -> (*JSObject, *JSContext) {
let win = self.window.get_ref(); let win = self.window;
(win.reflector().get_jsobject(), win.get_cx()) (win.reflector().get_jsobject(), win.get_cx())
} }
@ -518,15 +515,11 @@ impl Document {
} }
pub fn content_changed(&self) { pub fn content_changed(&self) {
for window in self.window.iter() { self.window.content_changed();
window.content_changed()
}
} }
pub fn wait_until_safe_to_modify_dom(&self) { pub fn wait_until_safe_to_modify_dom(&self) {
for window in self.window.iter() { self.window.wait_until_safe_to_modify_dom();
window.wait_until_safe_to_modify_dom();
}
} }
pub fn register_nodes_with_id(&mut self, root: &AbstractNode<ScriptView>) { pub fn register_nodes_with_id(&mut self, root: &AbstractNode<ScriptView>) {

View file

@ -43,10 +43,10 @@ impl DOMParser {
let cx = self.owner.get_cx(); let cx = self.owner.get_cx();
let document = match ty { let document = match ty {
Text_html => { Text_html => {
HTMLDocument::new(None) HTMLDocument::new(self.owner)
} }
Text_xml => { Text_xml => {
AbstractDocument::as_abstract(cx, @mut Document::new(None, XML)) AbstractDocument::as_abstract(cx, @mut Document::new(self.owner, XML))
} }
_ => { _ => {
fail!("unsupported document type") fail!("unsupported document type")

View file

@ -274,7 +274,7 @@ impl Element {
pub fn GetClientRects(&self, abstract_self: AbstractNode<ScriptView>) -> @mut ClientRectList { pub fn GetClientRects(&self, abstract_self: AbstractNode<ScriptView>) -> @mut ClientRectList {
let document = self.node.owner_doc; let document = self.node.owner_doc;
let win = document.with_base(|doc| doc.window).expect("no window"); let win = document.with_base(|doc| doc.window);
let node = abstract_self; let node = abstract_self;
assert!(node.is_element()); assert!(node.is_element());
let (port, chan) = comm::stream(); let (port, chan) = comm::stream();
@ -301,7 +301,7 @@ impl Element {
pub fn GetBoundingClientRect(&self, abstract_self: AbstractNode<ScriptView>) -> @mut ClientRect { pub fn GetBoundingClientRect(&self, abstract_self: AbstractNode<ScriptView>) -> @mut ClientRect {
let document = self.node.owner_doc; let document = self.node.owner_doc;
let win = document.with_base(|doc| doc.window).expect("no window"); let win = document.with_base(|doc| doc.window);
let node = abstract_self; let node = abstract_self;
assert!(node.is_element()); assert!(node.is_element());
let (port, chan) = comm::stream(); let (port, chan) = comm::stream();

View file

@ -24,12 +24,12 @@ pub struct HTMLDocument {
} }
impl HTMLDocument { impl HTMLDocument {
pub fn new(window: Option<@mut Window>) -> AbstractDocument { pub fn new(window: @mut Window) -> AbstractDocument {
let doc = @mut HTMLDocument { let doc = @mut HTMLDocument {
parent: Document::new(window, HTML) parent: Document::new(window, HTML)
}; };
AbstractDocument::as_abstract(window.get_ref().get_cx(), doc) AbstractDocument::as_abstract(window.get_cx(), doc)
} }
} }

View file

@ -44,10 +44,9 @@ impl HTMLImageElement {
if "src" == name { if "src" == name {
let doc = self.htmlelement.element.node.owner_doc; let doc = self.htmlelement.element.node.owner_doc;
do doc.with_base |doc| { do doc.with_base |doc| {
for window in doc.window.iter() { let window = doc.window;
let url = window.page.url.map(|&(ref url, _)| url.clone()); let url = window.page.url.map(|&(ref url, _)| url.clone());
self.update_image(window.image_cache_task.clone(), url); self.update_image(window.image_cache_task.clone(), url);
}
} }
} }
} }
@ -100,19 +99,11 @@ impl HTMLImageElement {
pub fn Width(&self, abstract_self: AbstractNode<ScriptView>) -> u32 { pub fn Width(&self, abstract_self: AbstractNode<ScriptView>) -> u32 {
let node = &self.htmlelement.element.node; let node = &self.htmlelement.element.node;
match node.owner_doc.with_base(|doc| doc.window) { let page = node.owner_doc.with_base(|doc| doc.window).page;
Some(win) => { let (port, chan) = stream();
let page = win.page; match page.query_layout(ContentBoxQuery(abstract_self, chan), port) {
let (port, chan) = stream(); ContentBoxResponse(rect) => {
match page.query_layout(ContentBoxQuery(abstract_self, chan), port) { to_px(rect.size.width) as u32
ContentBoxResponse(rect) => {
to_px(rect.size.width) as u32
}
}
}
None => {
debug!("no window");
0
} }
} }
} }
@ -129,19 +120,11 @@ impl HTMLImageElement {
pub fn Height(&self, abstract_self: AbstractNode<ScriptView>) -> u32 { pub fn Height(&self, abstract_self: AbstractNode<ScriptView>) -> u32 {
let node = &self.htmlelement.element.node; let node = &self.htmlelement.element.node;
match node.owner_doc.with_base(|doc| doc.window) { let page = node.owner_doc.with_base(|doc| doc.window).page;
Some(win) => { let (port, chan) = stream();
let page = win.page; match page.query_layout(ContentBoxQuery(abstract_self, chan), port) {
let (port, chan) = stream(); ContentBoxResponse(rect) => {
match page.query_layout(ContentBoxQuery(abstract_self, chan), port) { to_px(rect.size.height) as u32
ContentBoxResponse(rect) => {
to_px(rect.size.height) as u32
}
}
}
None => {
debug!("no window");
0
} }
} }
} }

View file

@ -664,7 +664,7 @@ impl Node<ScriptView> {
} }
pub fn get_scope_and_cx(&self) -> (*JSObject, *JSContext) { pub fn get_scope_and_cx(&self) -> (*JSObject, *JSContext) {
let win = self.owner_doc.with_base(|doc| doc.window.unwrap()); let win = self.owner_doc.with_base(|doc| doc.window);
(win.reflector().get_jsobject(), win.get_cx()) (win.reflector().get_jsobject(), win.get_cx())
} }

View file

@ -717,7 +717,7 @@ impl ScriptTask {
// Parse HTML. // Parse HTML.
// //
// Note: We can parse the next document in parallel with any previous documents. // Note: We can parse the next document in parallel with any previous documents.
let document = HTMLDocument::new(Some(window)); let document = HTMLDocument::new(window);
let html_parsing_result = hubbub_html_parser::parse_html(cx.ptr, let html_parsing_result = hubbub_html_parser::parse_html(cx.ptr,
document, document,