auto merge of #4268 : Ms2ger/servo/script_task, r=jdm

This commit is contained in:
bors-servo 2014-12-08 09:43:11 -07:00
commit 3f60bcf2c2
2 changed files with 96 additions and 109 deletions

View file

@ -16,72 +16,26 @@ use dom::node::{Node, NodeHelpers, TrustedNodeAddress};
use dom::servohtmlparser;
use dom::servohtmlparser::ServoHTMLParser;
use dom::text::Text;
use page::Page;
use parse::Parser;
use encoding::all::UTF_8;
use encoding::types::{Encoding, DecodeReplace};
use servo_net::resource_task::{Load, LoadData, Payload, Done, ResourceTask};
use servo_msg::constellation_msg::LoadData as MsgLoadData;
use servo_net::resource_task::{Payload, Done, LoadResponse};
use servo_util::task_state;
use servo_util::task_state::IN_HTML_PARSER;
use std::ascii::AsciiExt;
use std::comm::channel;
use std::fmt::{mod, Show};
use std::str::MaybeOwned;
use url::Url;
use time::{Tm, strptime};
use html5ever::Attribute;
use html5ever::tree_builder::{TreeSink, QuirksMode, NodeOrText, AppendNode, AppendText};
use string_cache::QualName;
use hyper::header::{Header, HeaderFormat};
use hyper::header::common::util as header_util;
pub enum HTMLInput {
InputString(String),
InputUrl(Url),
}
//FIXME(seanmonstar): uplift to Hyper
#[deriving(Clone)]
struct LastModified(pub Tm);
impl Header for LastModified {
#[inline]
fn header_name(_: Option<LastModified>) -> &'static str {
"Last-Modified"
}
// Parses an RFC 2616 compliant date/time string,
fn parse_header(raw: &[Vec<u8>]) -> Option<LastModified> {
header_util::from_one_raw_str(raw).and_then(|s: String| {
let s = s.as_slice();
strptime(s, "%a, %d %b %Y %T %Z").or_else(|_| {
strptime(s, "%A, %d-%b-%y %T %Z")
}).or_else(|_| {
strptime(s, "%c")
}).ok().map(|tm| LastModified(tm))
})
}
}
impl HeaderFormat for LastModified {
// a localized date/time string in a format suitable
// for document.lastModified.
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
let LastModified(ref tm) = *self;
match tm.tm_gmtoff {
0 => tm.rfc822().fmt(f),
_ => tm.to_utc().rfc822().fmt(f)
}
}
}
fn dom_last_modified(tm: &Tm) -> String {
tm.to_local().strftime("%m/%d/%Y %H:%M:%S").unwrap()
}
trait SinkHelpers {
fn get_or_create(&self, child: NodeOrText<TrustedNodeAddress>) -> Temporary<Node>;
}
@ -207,52 +161,11 @@ impl<'a> TreeSink<TrustedNodeAddress> for servohtmlparser::Sink {
}
}
// The url from msg_load_data is ignored here
pub fn parse_html(page: &Page,
document: JSRef<Document>,
pub fn parse_html(document: JSRef<Document>,
input: HTMLInput,
resource_task: ResourceTask,
msg_load_data: Option<MsgLoadData>) {
let (base_url, load_response) = match input {
InputUrl(ref url) => {
// Wait for the LoadResponse so that the parser knows the final URL.
let (input_chan, input_port) = channel();
let mut load_data = LoadData::new(url.clone(), input_chan);
msg_load_data.map(|m| {
load_data.headers = m.headers;
load_data.method = m.method;
load_data.data = m.data;
});
resource_task.send(Load(load_data));
let load_response = input_port.recv();
load_response.metadata.headers.as_ref().map(|headers| {
headers.get().map(|&LastModified(ref tm)| {
document.set_last_modified(dom_last_modified(tm));
});
});
let base_url = load_response.metadata.final_url.clone();
{
// Store the final URL before we start parsing, so that DOM routines
// (e.g. HTMLImageElement::update_image) can resolve relative URLs
// correctly.
*page.mut_url() = Some((base_url.clone(), true));
}
(Some(base_url), Some(load_response))
},
InputString(_) => {
match *page.url() {
Some((ref page_url, _)) => (Some(page_url.clone()), None),
None => (None, None),
}
},
};
let parser = ServoHTMLParser::new(base_url.clone(), document).root();
base_url: Url,
load_response: Option<LoadResponse>) {
let parser = ServoHTMLParser::new(Some(base_url.clone()), document).root();
let parser: JSRef<ServoHTMLParser> = *parser;
task_state::enter(IN_HTML_PARSER);
@ -265,7 +178,7 @@ pub fn parse_html(page: &Page,
let load_response = load_response.unwrap();
match load_response.metadata.content_type {
Some((ref t, _)) if t.as_slice().eq_ignore_ascii_case("image") => {
let page = format!("<html><body><img src='{:s}' /></body></html>", base_url.as_ref().unwrap().serialize());
let page = format!("<html><body><img src='{:s}' /></body></html>", base_url.serialize());
parser.parse_chunk(page);
},
_ => {

View file

@ -51,7 +51,8 @@ use servo_msg::constellation_msg::{KeyModifiers, SUPER, SHIFT, CONTROL, ALT, Rep
use servo_msg::constellation_msg::{Released};
use servo_msg::constellation_msg;
use servo_net::image_cache_task::ImageCacheTask;
use servo_net::resource_task::ResourceTask;
use servo_net::resource_task::{ResourceTask, Load};
use servo_net::resource_task::LoadData as NetLoadData;
use servo_net::storage_task::StorageTask;
use servo_util::geometry::to_frac_px;
use servo_util::smallvec::{SmallVec1, SmallVec};
@ -59,6 +60,8 @@ use servo_util::task::spawn_named_with_send_on_failure;
use servo_util::task_state;
use geom::point::Point2D;
use hyper::header::{Header, HeaderFormat};
use hyper::header::common::util as header_util;
use js::jsapi::{JS_SetWrapObjectCallbacks, JS_SetGCZeal, JS_DEFAULT_ZEAL_FREQ, JS_GC};
use js::jsapi::{JSContext, JSRuntime, JSTracer};
use js::jsapi::{JS_SetGCParameter, JSGC_MAX_BYTES};
@ -71,9 +74,11 @@ use libc::size_t;
use std::any::{Any, AnyRefExt};
use std::collections::HashSet;
use std::comm::{channel, Sender, Receiver, Select};
use std::fmt::{mod, Show};
use std::mem::replace;
use std::rc::Rc;
use std::u32;
use time::{Tm, strptime};
local_data_key!(pub StackRoots: *const RootCollection)
@ -693,20 +698,17 @@ impl ScriptTask {
message for a layout channel that is not associated with this script task. This
is a bug.");
let last_loaded_url = replace(&mut *page.mut_url(), None);
match last_loaded_url {
Some((ref loaded, needs_reflow)) if *loaded == url => {
*page.mut_url() = Some((loaded.clone(), false));
if needs_reflow {
let last_url = match &mut *page.mut_url() {
&Some((ref mut loaded, ref mut needs_reflow)) if *loaded == url => {
if replace(needs_reflow, false) {
self.force_reflow(&*page);
}
return;
},
_ => (),
}
url => replace(url, None).map(|(loaded, _)| loaded),
};
let is_javascript = url.scheme.as_slice() == "javascript";
let last_url = last_loaded_url.map(|(ref loaded, _)| loaded.clone());
let cx = self.js_context.borrow();
let cx = cx.as_ref().unwrap();
@ -726,7 +728,7 @@ impl ScriptTask {
} else {
url.clone()
};
let document = Document::new(*window, Some(doc_url), HTMLDocument,
let document = Document::new(*window, Some(doc_url.clone()), HTMLDocument,
None, FromParser).root();
window.init_browser_context(*document);
@ -751,18 +753,51 @@ impl ScriptTask {
InputString(strval.unwrap_or("".to_string()))
};
parse_html(&*page, *document, parser_input, self.resource_task.clone(), Some(load_data));
let (base_url, load_response) = match parser_input {
InputUrl(ref url) => {
// Wait for the LoadResponse so that the parser knows the final URL.
let (input_chan, input_port) = channel();
self.resource_task.send(Load(NetLoadData {
url: url.clone(),
method: load_data.method,
headers: load_data.headers,
data: load_data.data,
cors: None,
consumer: input_chan,
}));
let load_response = input_port.recv();
load_response.metadata.headers.as_ref().map(|headers| {
headers.get().map(|&LastModified(ref tm)| {
document.set_last_modified(dom_last_modified(tm));
});
});
let base_url = load_response.metadata.final_url.clone();
{
// Store the final URL before we start parsing, so that DOM routines
// (e.g. HTMLImageElement::update_image) can resolve relative URLs
// correctly.
*page.mut_url() = Some((base_url.clone(), true));
}
(base_url, Some(load_response))
},
InputString(_) => {
(doc_url, None)
},
};
parse_html(*document, parser_input, base_url, load_response);
url = page.get_url().clone();
document.set_ready_state(DocumentReadyStateValues::Interactive);
// Kick off the initial reflow of the page.
debug!("kicking off initial reflow of {}", url);
{
let document_js_ref = (&*document).clone();
let document_as_node = NodeCast::from_ref(document_js_ref);
document.content_changed(document_as_node);
}
document.content_changed(NodeCast::from_ref(*document));
window.flush_layout();
{
@ -1166,3 +1201,42 @@ pub fn get_page(page: &Rc<Page>, pipeline_id: PipelineId) -> Rc<Page> {
message for a layout channel that is not associated with this script task.\
This is a bug.")
}
//FIXME(seanmonstar): uplift to Hyper
#[deriving(Clone)]
struct LastModified(pub Tm);
impl Header for LastModified {
#[inline]
fn header_name(_: Option<LastModified>) -> &'static str {
"Last-Modified"
}
// Parses an RFC 2616 compliant date/time string,
fn parse_header(raw: &[Vec<u8>]) -> Option<LastModified> {
header_util::from_one_raw_str(raw).and_then(|s: String| {
let s = s.as_slice();
strptime(s, "%a, %d %b %Y %T %Z").or_else(|_| {
strptime(s, "%A, %d-%b-%y %T %Z")
}).or_else(|_| {
strptime(s, "%c")
}).ok().map(|tm| LastModified(tm))
})
}
}
impl HeaderFormat for LastModified {
// a localized date/time string in a format suitable
// for document.lastModified.
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
let LastModified(ref tm) = *self;
match tm.tm_gmtoff {
0 => tm.rfc822().fmt(f),
_ => tm.to_utc().rfc822().fmt(f)
}
}
}
fn dom_last_modified(tm: &Tm) -> String {
tm.to_local().strftime("%m/%d/%Y %H:%M:%S").unwrap()
}