Auto merge of #15132 - nox:load-fixes, r=jdm

Improve the end

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/15132)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-01-20 15:25:38 -08:00 committed by GitHub
commit 1b68f79468

View file

@ -1539,34 +1539,71 @@ impl Document {
loader.fetch_async(load, request, fetch_target); loader.fetch_async(load, request, fetch_target);
} }
// https://html.spec.whatwg.org/multipage/#the-end
// https://html.spec.whatwg.org/multipage/#delay-the-load-event
pub fn finish_load(&self, load: LoadType) { pub fn finish_load(&self, load: LoadType) {
// This does not delay the load event anymore.
debug!("Document got finish_load: {:?}", load); debug!("Document got finish_load: {:?}", load);
// The parser might need the loader, so restrict the lifetime of the borrow. self.loader.borrow_mut().finish_load(&load);
{
let mut loader = self.loader.borrow_mut();
loader.finish_load(&load);
}
match load { match load {
LoadType::Stylesheet(_) => { LoadType::Stylesheet(_) => {
self.process_deferred_scripts(); // A stylesheet finishing to load may unblock any pending
// parsing-blocking script or deferred script.
self.process_pending_parsing_blocking_script(); self.process_pending_parsing_blocking_script();
// Step 3.
self.process_deferred_scripts();
}, },
LoadType::PageSource(_) => { LoadType::PageSource(_) => {
// Deferred scripts have to wait for page to finish loading,
// this is the first opportunity to process them.
// Step 3.
self.process_deferred_scripts(); self.process_deferred_scripts();
}, },
_ => {}, _ => {},
} }
if !self.loader.borrow().is_blocked() && !self.loader.borrow().events_inhibited() { // Step 4 is in another castle, namely at the end of
self.loader.borrow_mut().inhibit_events(); // process_deferred_scripts.
// Schedule a task to fire a "load" event.
debug!("Document loads are complete."); // Step 5 can be found in asap_script_loaded and
let handler = box DocumentProgressHandler::new(Trusted::new(self)); // asap_in_order_script_loaded.
self.window.dom_manipulation_task_source().queue(handler, self.window.upcast()).unwrap();
if self.loader.borrow().is_blocked() {
// Step 6.
return;
} }
// The rest will ever run only once per document.
if self.loader.borrow().events_inhibited() {
return;
}
self.loader.borrow_mut().inhibit_events();
// Step 7.
debug!("Document loads are complete.");
let handler = box DocumentProgressHandler::new(Trusted::new(self));
self.window.dom_manipulation_task_source().queue(handler, self.window.upcast()).unwrap();
// Step 8.
// TODO: pageshow event.
// Step 9.
// TODO: pending application cache download process tasks.
// Step 10.
// TODO: printing steps.
// Step 11.
// TODO: ready for post-load tasks.
// Step 12.
// TODO: completely loaded.
} }
// https://html.spec.whatwg.org/multipage/#pending-parsing-blocking-script
pub fn set_pending_parsing_blocking_script(&self, pub fn set_pending_parsing_blocking_script(&self,
script: &HTMLScriptElement, script: &HTMLScriptElement,
load: Option<ScriptResult>) { load: Option<ScriptResult>) {
@ -1574,6 +1611,7 @@ impl Document {
*self.pending_parsing_blocking_script.borrow_mut() = Some(PendingScript::new_with_load(script, load)); *self.pending_parsing_blocking_script.borrow_mut() = Some(PendingScript::new_with_load(script, load));
} }
// https://html.spec.whatwg.org/multipage/#pending-parsing-blocking-script
pub fn has_pending_parsing_blocking_script(&self) -> bool { pub fn has_pending_parsing_blocking_script(&self) -> bool {
self.pending_parsing_blocking_script.borrow().is_some() self.pending_parsing_blocking_script.borrow().is_some()
} }
@ -1603,11 +1641,12 @@ impl Document {
} }
} }
// https://html.spec.whatwg.org/multipage/#set-of-scripts-that-will-execute-as-soon-as-possible
pub fn add_asap_script(&self, script: &HTMLScriptElement) { pub fn add_asap_script(&self, script: &HTMLScriptElement) {
self.asap_scripts_set.borrow_mut().push(JS::from_ref(script)); self.asap_scripts_set.borrow_mut().push(JS::from_ref(script));
} }
/// https://html.spec.whatwg.org/multipage/#the-end step 3. /// https://html.spec.whatwg.org/multipage/#the-end step 5.
/// https://html.spec.whatwg.org/multipage/#prepare-a-script step 22.d. /// https://html.spec.whatwg.org/multipage/#prepare-a-script step 22.d.
pub fn asap_script_loaded(&self, element: &HTMLScriptElement, result: ScriptResult) { pub fn asap_script_loaded(&self, element: &HTMLScriptElement, result: ScriptResult) {
{ {
@ -1618,11 +1657,12 @@ impl Document {
element.execute(result); element.execute(result);
} }
// https://html.spec.whatwg.org/multipage/#list-of-scripts-that-will-execute-in-order-as-soon-as-possible
pub fn push_asap_in_order_script(&self, script: &HTMLScriptElement) { pub fn push_asap_in_order_script(&self, script: &HTMLScriptElement) {
self.asap_in_order_scripts_list.push(script); self.asap_in_order_scripts_list.push(script);
} }
/// https://html.spec.whatwg.org/multipage/#the-end step 3. /// https://html.spec.whatwg.org/multipage/#the-end step 5.
/// https://html.spec.whatwg.org/multipage/#prepare-a-script step 22.c. /// https://html.spec.whatwg.org/multipage/#prepare-a-script step 22.c.
pub fn asap_in_order_script_loaded(&self, pub fn asap_in_order_script_loaded(&self,
element: &HTMLScriptElement, element: &HTMLScriptElement,
@ -1633,10 +1673,12 @@ impl Document {
} }
} }
// https://html.spec.whatwg.org/multipage/#list-of-scripts-that-will-execute-when-the-document-has-finished-parsing
pub fn add_deferred_script(&self, script: &HTMLScriptElement) { pub fn add_deferred_script(&self, script: &HTMLScriptElement) {
self.deferred_scripts.push(script); self.deferred_scripts.push(script);
} }
/// https://html.spec.whatwg.org/multipage/#the-end step 3.
/// https://html.spec.whatwg.org/multipage/#prepare-a-script step 22.d. /// https://html.spec.whatwg.org/multipage/#prepare-a-script step 22.d.
pub fn deferred_script_loaded(&self, element: &HTMLScriptElement, result: ScriptResult) { pub fn deferred_script_loaded(&self, element: &HTMLScriptElement, result: ScriptResult) {
self.deferred_scripts.loaded(element, result); self.deferred_scripts.loaded(element, result);
@ -1665,6 +1707,7 @@ impl Document {
} }
} }
// https://html.spec.whatwg.org/multipage/#the-end step 4.
pub fn maybe_dispatch_dom_content_loaded(&self) { pub fn maybe_dispatch_dom_content_loaded(&self) {
if self.domcontentloaded_dispatched.get() { if self.domcontentloaded_dispatched.get() {
return; return;
@ -1675,6 +1718,7 @@ impl Document {
update_with_current_time_ms(&self.dom_content_loaded_event_start); update_with_current_time_ms(&self.dom_content_loaded_event_start);
// Step 4.1.
let window = self.window(); let window = self.window();
window.dom_manipulation_task_source().queue_event(self.upcast(), atom!("DOMContentLoaded"), window.dom_manipulation_task_source().queue_event(self.upcast(), atom!("DOMContentLoaded"),
EventBubbles::Bubbles, EventCancelable::NotCancelable, window); EventBubbles::Bubbles, EventCancelable::NotCancelable, window);
@ -1683,6 +1727,9 @@ impl Document {
ReflowQueryType::NoQuery, ReflowQueryType::NoQuery,
ReflowReason::DOMContentLoaded); ReflowReason::DOMContentLoaded);
update_with_current_time_ms(&self.dom_content_loaded_event_end); update_with_current_time_ms(&self.dom_content_loaded_event_end);
// Step 4.2.
// TODO: client message queue.
} }
pub fn notify_constellation_load(&self) { pub fn notify_constellation_load(&self) {