diff options
Diffstat (limited to 'src')
5 files changed, 55 insertions, 13 deletions
diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs index 81fc5c62fbd..503f618384d 100644 --- a/src/components/script/dom/document.rs +++ b/src/components/script/dom/document.rs @@ -59,6 +59,7 @@ use std::collections::hashmap::HashMap; use std::ascii::StrAsciiExt; use std::cell::{Cell, RefCell}; use url::Url; +use time; #[deriving(PartialEq,Encodable)] pub enum IsHTMLDocument { @@ -74,6 +75,7 @@ pub struct Document { idmap: Traceable<RefCell<HashMap<DOMString, Vec<JS<Element>>>>>, implementation: Cell<Option<JS<DOMImplementation>>>, content_type: DOMString, + last_modified: Traceable<RefCell<Option<DOMString>>>, pub encoding_name: Traceable<RefCell<DOMString>>, pub is_html_document: bool, url: Untraceable<Url>, @@ -146,6 +148,7 @@ pub trait DocumentHelpers { fn url<'a>(&'a self) -> &'a Url; fn quirks_mode(&self) -> QuirksMode; fn set_quirks_mode(&self, mode: QuirksMode); + fn set_last_modified(&self, value: DOMString); fn set_encoding_name(&self, name: DOMString); fn content_changed(&self); fn damage_and_reflow(&self, damage: DocumentDamageLevel); @@ -168,6 +171,10 @@ impl<'a> DocumentHelpers for JSRef<'a, Document> { self.quirks_mode.deref().set(mode); } + fn set_last_modified(&self, value: DOMString) { + *self.last_modified.deref().borrow_mut() = Some(value); + } + fn set_encoding_name(&self, name: DOMString) { *self.encoding_name.deref().borrow_mut() = name; } @@ -278,6 +285,7 @@ impl Document { NonHTMLDocument => "application/xml".to_string() } }, + last_modified: Traceable::new(RefCell::new(None)), url: Untraceable::new(url), // http://dom.spec.whatwg.org/#concept-document-quirks quirks_mode: Untraceable::new(Cell::new(NoQuirks)), @@ -569,6 +577,14 @@ impl<'a> DocumentMethods for JSRef<'a, Document> { } } + // http://www.whatwg.org/html/#dom-document-lastmodified + fn LastModified(&self) -> DOMString { + match *self.last_modified.borrow() { + Some(ref t) => t.clone(), + None => time::now().strftime("%m/%d/%Y %H:%M:%S"), + } + } + // http://dom.spec.whatwg.org/#dom-document-createrange fn CreateRange(&self) -> Temporary<Range> { Range::new(self) diff --git a/src/components/script/dom/webidls/Document.webidl b/src/components/script/dom/webidls/Document.webidl index 0a58227cdbe..0599ba71f95 100644 --- a/src/components/script/dom/webidls/Document.webidl +++ b/src/components/script/dom/webidls/Document.webidl @@ -49,6 +49,7 @@ interface Document : Node { /* http://www.whatwg.org/specs/web-apps/current-work/#the-document-object */ partial interface Document { + readonly attribute DOMString lastModified; [SetterThrows] attribute DOMString title; [SetterThrows] diff --git a/src/components/script/html/hubbub_html_parser.rs b/src/components/script/html/hubbub_html_parser.rs index c71a8845826..8b49a2bae03 100644 --- a/src/components/script/html/hubbub_html_parser.rs +++ b/src/components/script/html/hubbub_html_parser.rs @@ -33,6 +33,8 @@ use std::cell::RefCell; use std::comm::{channel, Sender, Receiver}; use style::Stylesheet; use url::{Url, UrlParser}; +use http::headers::HeaderEnum; +use time; macro_rules! handle_element( ($document: expr, @@ -158,6 +160,30 @@ fn js_script_listener(to_parent: Sender<HtmlDiscoveryMessage>, assert!(to_parent.send_opt(HtmlDiscoveredScript(result_vec)).is_ok()); } +// Parses an RFC 2616 compliant date/time string, and returns a localized +// date/time string in a format suitable for document.lastModified. +fn parse_last_modified(timestamp: &str) -> String { + let format = "%m/%d/%Y %H:%M:%S"; + + // RFC 822, updated by RFC 1123 + match time::strptime(timestamp, "%a, %d %b %Y %T %Z") { + Ok(t) => return t.to_local().strftime(format), + Err(_) => () + } + + // RFC 850, obsoleted by RFC 1036 + match time::strptime(timestamp, "%A, %d-%b-%y %T %Z") { + Ok(t) => return t.to_local().strftime(format), + Err(_) => () + } + + // ANSI C's asctime() format + match time::strptime(timestamp, "%c") { + Ok(t) => t.to_local().strftime(format), + Err(_) => String::from_str("") + } +} + // Silly macros to handle constructing DOM nodes. This produces bad code and should be optimized // via atomization (issue #85). @@ -323,6 +349,18 @@ pub fn parse_html(page: &Page, debug!("Fetched page; metadata is {:?}", load_response.metadata); + load_response.metadata.headers.map(|headers| { + let header = headers.iter().find(|h| + h.header_name().as_slice().to_ascii_lower() == "last-modified".to_string() + ); + + match header { + Some(h) => document.set_last_modified( + parse_last_modified(h.header_value().as_slice())), + None => {}, + }; + }); + let base_url = &load_response.metadata.final_url; { diff --git a/src/test/wpt/metadata/html/dom/documents/resource-metadata-management/document-lastModified-01.html.ini b/src/test/wpt/metadata/html/dom/documents/resource-metadata-management/document-lastModified-01.html.ini deleted file mode 100644 index 484720908d5..00000000000 --- a/src/test/wpt/metadata/html/dom/documents/resource-metadata-management/document-lastModified-01.html.ini +++ /dev/null @@ -1,8 +0,0 @@ -[document-lastModified-01.html] - type: testharness - [Date returned by lastModified is in the user\'s local time zone.] - expected: FAIL - - [Date returned by lastModified is in the form "MM/DD/YYYY hh:mm:ss".] - expected: FAIL - diff --git a/src/test/wpt/metadata/html/dom/documents/resource-metadata-management/document-lastModified.html.ini b/src/test/wpt/metadata/html/dom/documents/resource-metadata-management/document-lastModified.html.ini deleted file mode 100644 index 1593471370f..00000000000 --- a/src/test/wpt/metadata/html/dom/documents/resource-metadata-management/document-lastModified.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[document-lastModified.html] - type: testharness - [lastModified should return the last modified date and time] - expected: FAIL - |