Skip to content

Commit

Permalink
Implement document.lastModified (fixes #2972, #3127).
Browse files Browse the repository at this point in the history
  • Loading branch information
mechaxl authored and Ms2ger committed Sep 4, 2014
1 parent f787700 commit e2bfdb3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/components/script/dom/document.rs
Expand Up @@ -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 {
Expand All @@ -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>,
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/components/script/dom/webidls/Document.webidl
Expand Up @@ -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]
Expand Down
38 changes: 38 additions & 0 deletions src/components/script/html/hubbub_html_parser.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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).

Expand Down Expand Up @@ -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;

{
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit e2bfdb3

Please sign in to comment.