Implement document.URL & document.documentURI

Specs:
http://dom.spec.whatwg.org/#dom-document-url
http://dom.spec.whatwg.org/#dom-document-documenturi

This is a sub-task for #1428.
This commit is contained in:
Bruno de Oliveira Abinader 2014-01-29 14:24:49 -04:00
parent 0777f211df
commit 43416ef91f
7 changed files with 64 additions and 14 deletions

View file

@ -25,6 +25,7 @@ use html::hubbub_html_parser::build_element_from_tag;
use layout_interface::{DocumentDamageLevel, ContentChangedDocumentDamage}; use layout_interface::{DocumentDamageLevel, ContentChangedDocumentDamage};
use servo_util::namespace::Null; use servo_util::namespace::Null;
use extra::url::{Url, from_str};
use js::jsapi::{JSObject, JSContext, JSTracer}; use js::jsapi::{JSObject, JSContext, JSTracer};
use std::ascii::StrAsciiExt; use std::ascii::StrAsciiExt;
use std::cast; use std::cast;
@ -89,7 +90,8 @@ pub struct Document {
doctype: DocumentType, doctype: DocumentType,
idmap: HashMap<DOMString, AbstractNode>, idmap: HashMap<DOMString, AbstractNode>,
implementation: Option<@mut DOMImplementation>, implementation: Option<@mut DOMImplementation>,
content_type: DOMString content_type: DOMString,
url: Url
} }
impl Document { impl Document {
@ -110,7 +112,7 @@ impl Document {
abstract abstract
} }
pub fn new_inherited(window: @mut Window, doctype: DocumentType, content_type: Option<DOMString>) -> Document { pub fn new_inherited(window: @mut Window, url: Option<Url>, doctype: DocumentType, content_type: Option<DOMString>) -> Document {
let node_type = match doctype { let node_type = match doctype {
HTML => HTMLDocumentTypeId, HTML => HTMLDocumentTypeId,
SVG | XML => PlainDocumentTypeId SVG | XML => PlainDocumentTypeId
@ -130,19 +132,23 @@ impl Document {
// http://dom.spec.whatwg.org/#concept-document-content-type // http://dom.spec.whatwg.org/#concept-document-content-type
SVG | XML => ~"application/xml" SVG | XML => ~"application/xml"
} }
},
url: match url {
None => from_str("about:blank").unwrap(),
Some(_url) => _url
} }
} }
} }
pub fn new(window: @mut Window, doctype: DocumentType, content_type: Option<DOMString>) -> AbstractDocument { pub fn new(window: @mut Window, url: Option<Url>, doctype: DocumentType, content_type: Option<DOMString>) -> AbstractDocument {
let document = Document::new_inherited(window, doctype, content_type); let document = Document::new_inherited(window, url, doctype, content_type);
Document::reflect_document(@mut document, window, DocumentBinding::Wrap) Document::reflect_document(@mut document, window, DocumentBinding::Wrap)
} }
} }
impl Document { impl Document {
pub fn Constructor(owner: @mut Window) -> Fallible<AbstractDocument> { pub fn Constructor(owner: @mut Window) -> Fallible<AbstractDocument> {
Ok(Document::new(owner, XML, None)) Ok(Document::new(owner, None, XML, None))
} }
} }
@ -167,6 +173,7 @@ impl Reflectable for Document {
} }
impl Document { impl Document {
// http://dom.spec.whatwg.org/#dom-document-implementation
pub fn Implementation(&mut self) -> @mut DOMImplementation { pub fn Implementation(&mut self) -> @mut DOMImplementation {
if self.implementation.is_none() { if self.implementation.is_none() {
self.implementation = Some(DOMImplementation::new(self.window)); self.implementation = Some(DOMImplementation::new(self.window));
@ -174,6 +181,16 @@ impl Document {
self.implementation.unwrap() self.implementation.unwrap()
} }
// http://dom.spec.whatwg.org/#dom-document-url
pub fn URL(&self) -> DOMString {
self.url.to_str()
}
// http://dom.spec.whatwg.org/#dom-document-documenturi
pub fn DocumentURI(&self) -> DOMString {
self.URL()
}
// http://dom.spec.whatwg.org/#dom-document-content_type // http://dom.spec.whatwg.org/#dom-document-content_type
pub fn ContentType(&self) -> DOMString { pub fn ContentType(&self) -> DOMString {
self.content_type.clone() self.content_type.clone()

View file

@ -63,7 +63,7 @@ impl DOMImplementation {
// http://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument // http://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
pub fn CreateHTMLDocument(&self, title: Option<DOMString>) -> AbstractDocument { pub fn CreateHTMLDocument(&self, title: Option<DOMString>) -> AbstractDocument {
// Step 1-2. // Step 1-2.
let abstract_doc = HTMLDocument::new(self.owner); let abstract_doc = HTMLDocument::new(self.owner, None);
assert!(abstract_doc.document().doctype == HTML); assert!(abstract_doc.document().doctype == HTML);
let abstract_node = AbstractNode::from_document(abstract_doc); let abstract_node = AbstractNode::from_document(abstract_doc);

View file

@ -38,7 +38,7 @@ impl DOMParser {
-> Fallible<AbstractDocument> { -> Fallible<AbstractDocument> {
match ty { match ty {
Text_html => { Text_html => {
Ok(HTMLDocument::new(self.owner)) Ok(HTMLDocument::new(self.owner, None))
} }
Text_xml => { Text_xml => {
Document::Constructor(self.owner) Document::Constructor(self.owner)

View file

@ -9,6 +9,7 @@ use dom::htmlcollection::HTMLCollection;
use dom::window::Window; use dom::window::Window;
use servo_util::namespace::Null; use servo_util::namespace::Null;
use extra::url::Url;
use js::jsapi::JSTracer; use js::jsapi::JSTracer;
use std::str::eq_slice; use std::str::eq_slice;
@ -17,14 +18,14 @@ pub struct HTMLDocument {
} }
impl HTMLDocument { impl HTMLDocument {
pub fn new_inherited(window: @mut Window) -> HTMLDocument { pub fn new_inherited(window: @mut Window, url: Option<Url>) -> HTMLDocument {
HTMLDocument { HTMLDocument {
parent: Document::new_inherited(window, HTML, None) parent: Document::new_inherited(window, url, HTML, None)
} }
} }
pub fn new(window: @mut Window) -> AbstractDocument { pub fn new(window: @mut Window, url: Option<Url>) -> AbstractDocument {
let document = HTMLDocument::new_inherited(window); let document = HTMLDocument::new_inherited(window, url);
Document::reflect_document(@mut document, window, HTMLDocumentBinding::Wrap) Document::reflect_document(@mut document, window, HTMLDocumentBinding::Wrap)
} }
} }

View file

@ -26,8 +26,8 @@ enum VisibilityState { "hidden", "visible" };
[Constructor] [Constructor]
interface Document : Node { interface Document : Node {
readonly attribute DOMImplementation implementation; readonly attribute DOMImplementation implementation;
// readonly attribute DOMString URL; readonly attribute DOMString URL;
// readonly attribute DOMString documentURI; readonly attribute DOMString documentURI;
// readonly attribute DOMString compatMode; // readonly attribute DOMString compatMode;
// readonly attribute DOMString characterSet; // readonly attribute DOMString characterSet;
readonly attribute DOMString contentType; readonly attribute DOMString contentType;

View file

@ -690,7 +690,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(window); let document = HTMLDocument::new(window, Some(url.clone()));
let html_parsing_result = hubbub_html_parser::parse_html(cx.ptr, let html_parsing_result = hubbub_html_parser::parse_html(cx.ptr,
document, document,
url.clone(), url.clone(),

View file

@ -0,0 +1,32 @@
<!doctype html>
<html>
<head>
<title></title>
<script src="harness.js"></script>
<script>
// test1: URL & documentURI
{
isnot(document.URL, null, "test1-0, URL & documentURI");
isnot(document.documentURI, null, "test1-1, URL & documentURI");
is(document.URL, document.documentURI, "test1-2, URL & documentURI");
}
// test2: new document
{
var doc = new Document();
is(doc.URL, "about:blank", "test2-0, new document");
}
// test3: current document
{
var url = document.URL.split("/");
is(url[0], "file:", "test3-0, current document");
is(url[url.length-1], "test_document_url.html", "test3-1, current document");
}
finish();
</script>
</head>
<body>
</body>
</html>