diff options
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/bindings/str.rs | 7 | ||||
-rw-r--r-- | components/script/dom/document.rs | 215 | ||||
-rw-r--r-- | components/script/dom/eventtarget.rs | 4 | ||||
-rw-r--r-- | components/script/dom/servoparser/mod.rs | 115 | ||||
-rw-r--r-- | components/script/dom/webidls/Document.webidl | 6 | ||||
-rw-r--r-- | components/script/dom/window.rs | 28 |
6 files changed, 342 insertions, 33 deletions
diff --git a/components/script/dom/bindings/str.rs b/components/script/dom/bindings/str.rs index 896c2af5b24..d5b10fcb01e 100644 --- a/components/script/dom/bindings/str.rs +++ b/components/script/dom/bindings/str.rs @@ -170,11 +170,16 @@ impl DOMString { self.0.push_str(string) } - /// Truncates this `DOMString`, removing all contents. + /// Clears this `DOMString`, removing all contents. pub fn clear(&mut self) { self.0.clear() } + /// Shortens this String to the specified length. + pub fn truncate(&mut self, new_len: usize) { + self.0.truncate(new_len); + } + /// An iterator over the bytes of this `DOMString`. pub fn bytes(&self) -> Bytes { self.0.bytes() diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index b2e93325236..722e0283d1e 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -16,6 +16,7 @@ use dom::bindings::codegen::Bindings::DocumentBinding::{DocumentMethods, Documen use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods; use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull; use dom::bindings::codegen::Bindings::EventHandlerBinding::OnErrorEventHandlerNonNull; +use dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementBinding::HTMLIFrameElementMethods; use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; use dom::bindings::codegen::Bindings::NodeFilterBinding::NodeFilter; use dom::bindings::codegen::Bindings::PerformanceBinding::PerformanceMethods; @@ -131,7 +132,7 @@ use style::attr::AttrValue; use style::context::{QuirksMode, ReflowGoal}; use style::restyle_hints::{RestyleHint, RESTYLE_STYLE_ATTRIBUTE}; use style::selector_parser::{RestyleDamage, Snapshot}; -use style::str::{split_html_space_chars, str_join}; +use style::str::{HTML_SPACE_CHARACTERS, split_html_space_chars, str_join}; use style::stylesheets::Stylesheet; use task_source::TaskSource; use time; @@ -1756,6 +1757,37 @@ impl Document { // TODO: client message queue. } + // https://html.spec.whatwg.org/multipage/#abort-a-document + fn abort(&self) { + // We need to inhibit the loader before anything else. + self.loader.borrow_mut().inhibit_events(); + + // Step 1. + for iframe in self.iter_iframes() { + if let Some(document) = iframe.GetContentDocument() { + // TODO: abort the active documents of every child browsing context. + document.abort(); + // TODO: salvageable flag. + } + } + + // Step 2. + self.script_blocking_stylesheets_count.set(0); + *self.pending_parsing_blocking_script.borrow_mut() = None; + *self.asap_scripts_set.borrow_mut() = vec![]; + self.asap_in_order_scripts_list.clear(); + self.deferred_scripts.clear(); + + // TODO: https://github.com/servo/servo/issues/15236 + self.window.cancel_all_tasks(); + + // Step 3. + if let Some(parser) = self.get_current_parser() { + parser.abort(); + // TODO: salvageable flag. + } + } + pub fn notify_constellation_load(&self) { let global_scope = self.window.upcast::<GlobalScope>(); let pipeline_id = global_scope.pipeline_id(); @@ -3280,6 +3312,149 @@ impl DocumentMethods for Document { elements } + // https://html.spec.whatwg.org/multipage/#dom-document-open + fn Open(&self, type_: DOMString, replace: DOMString) -> Fallible<Root<Document>> { + if !self.is_html_document() { + // Step 1. + return Err(Error::InvalidState); + } + + // Step 2. + // TODO: handle throw-on-dynamic-markup-insertion counter. + + if !self.is_active() { + // Step 3. + return Ok(Root::from_ref(self)); + } + + let entry_responsible_document = GlobalScope::entry().as_window().Document(); + + if !self.origin.same_origin(&entry_responsible_document.origin) { + // Step 4. + return Err(Error::Security); + } + + if self.get_current_parser().map_or(false, |parser| parser.script_nesting_level() > 0) { + // Step 5. + return Ok(Root::from_ref(self)); + } + + // Step 6. + // TODO: ignore-opens-during-unload counter check. + + // Step 7: first argument already bound to `type_`. + + // Step 8. + // TODO: check session history's state. + let replace = replace.eq_ignore_ascii_case("replace"); + + // Step 9. + // TODO: salvageable flag. + + // Step 10. + // TODO: prompt to unload. + + // Step 11. + // TODO: unload. + + // Step 12. + self.abort(); + + // Step 13. + for node in self.upcast::<Node>().traverse_preorder() { + node.upcast::<EventTarget>().remove_all_listeners(); + } + + // Step 14. + // TODO: remove any tasks associated with the Document in any task source. + + // Step 15. + Node::replace_all(None, self.upcast::<Node>()); + + // Steps 16-18. + // Let's not? + // TODO: https://github.com/whatwg/html/issues/1698 + + // Step 19. + self.implementation.set(None); + self.location.set(None); + self.images.set(None); + self.embeds.set(None); + self.links.set(None); + self.forms.set(None); + self.scripts.set(None); + self.anchors.set(None); + self.applets.set(None); + *self.stylesheets.borrow_mut() = None; + self.stylesheets_changed_since_reflow.set(true); + self.animation_frame_ident.set(0); + self.animation_frame_list.borrow_mut().clear(); + self.pending_restyles.borrow_mut().clear(); + self.target_element.set(None); + *self.last_click_info.borrow_mut() = None; + + // Step 20. + self.set_encoding(UTF_8); + + // Step 21. + // TODO: reload override buffer. + + // Step 22. + // TODO: salvageable flag. + + let url = entry_responsible_document.url(); + + // Step 23. + self.set_url(url.clone()); + + // Step 24. + // TODO: mute iframe load. + + // Step 27. + let type_ = if type_.eq_ignore_ascii_case("replace") { + "text/html" + } else if let Some(position) = type_.find(';') { + &type_[0..position] + } else { + &*type_ + }; + let type_ = type_.trim_matches(HTML_SPACE_CHARACTERS); + + // Step 25. + let resource_threads = + self.window.upcast::<GlobalScope>().resource_threads().clone(); + *self.loader.borrow_mut() = + DocumentLoader::new_with_threads(resource_threads, Some(url.clone())); + ServoParser::parse_html_script_input(self, url, type_); + + // Step 26. + self.ready_state.set(DocumentReadyState::Interactive); + + // Step 28 is handled when creating the parser in step 25. + + // Step 29. + // TODO: truncate session history. + + // Step 30. + // TODO: remove history traversal tasks. + + // Step 31. + // TODO: remove earlier entries. + + if !replace { + // Step 32. + // TODO: add history entry. + } + + // Step 33. + // TODO: clear fired unload flag. + + // Step 34 is handled when creating the parser in step 25. + + // Step 35. + Ok(Root::from_ref(self)) + } + // https://html.spec.whatwg.org/multipage/#dom-document-write fn Write(&self, text: Vec<DOMString>) -> ErrorResult { if !self.is_html_document() { @@ -3294,9 +3469,8 @@ impl DocumentMethods for Document { return Ok(()); } - let parser = self.get_current_parser(); - let parser = match parser.as_ref() { - Some(parser) if parser.script_nesting_level() > 0 => parser, + let parser = match self.get_current_parser() { + Some(ref parser) if parser.can_write() => Root::from_ref(&**parser), _ => { // Either there is no parser, which means the parsing ended; // or script nesting level is 0, which means the method was @@ -3307,8 +3481,8 @@ impl DocumentMethods for Document { return Ok(()); } // Step 5. - // TODO: call document.open(). - return Err(Error::InvalidState); + self.Open("text/html".into(), "".into())?; + self.get_current_parser().unwrap() } }; @@ -3328,6 +3502,30 @@ impl DocumentMethods for Document { self.Write(text) } + // https://html.spec.whatwg.org/multipage/#dom-document-close + fn Close(&self) -> ErrorResult { + if !self.is_html_document() { + // Step 1. + return Err(Error::InvalidState); + } + + // Step 2. + // TODO: handle throw-on-dynamic-markup-insertion counter. + + let parser = match self.get_current_parser() { + Some(ref parser) if parser.is_script_created() => Root::from_ref(&**parser), + _ => { + // Step 3. + return Ok(()); + } + }; + + // Step 4-6. + parser.close(); + + Ok(()) + } + // https://html.spec.whatwg.org/multipage/#documentandelementeventhandlers document_and_element_event_handlers!(); @@ -3496,6 +3694,7 @@ impl PendingInOrderScriptVec { fn is_empty(&self) -> bool { self.scripts.borrow().is_empty() } + fn push(&self, element: &HTMLScriptElement) { self.scripts.borrow_mut().push_back(PendingScript::new(element)); } @@ -3515,6 +3714,10 @@ impl PendingInOrderScriptVec { scripts.pop_front(); pair } + + fn clear(&self) { + *self.scripts.borrow_mut() = Default::default(); + } } #[derive(HeapSizeOf, JSTraceable)] diff --git a/components/script/dom/eventtarget.rs b/components/script/dom/eventtarget.rs index 15790d7ce53..1e1a52830a6 100644 --- a/components/script/dom/eventtarget.rs +++ b/components/script/dom/eventtarget.rs @@ -306,6 +306,10 @@ impl EventTarget { event.dispatch(self, None) } + pub fn remove_all_listeners(&self) { + *self.handlers.borrow_mut() = Default::default(); + } + /// https://html.spec.whatwg.org/multipage/#event-handler-attributes:event-handlers-11 fn set_inline_event_listener(&self, ty: Atom, diff --git a/components/script/dom/servoparser/mod.rs b/components/script/dom/servoparser/mod.rs index 0d8e02d273e..67f1b23e583 100644 --- a/components/script/dom/servoparser/mod.rs +++ b/components/script/dom/servoparser/mod.rs @@ -38,6 +38,7 @@ use script_thread::ScriptThread; use script_traits::DocumentActivity; use servo_config::resource_files::read_resource_file; use servo_url::ServoUrl; +use std::ascii::AsciiExt; use std::cell::Cell; use std::mem; @@ -75,6 +76,10 @@ pub struct ServoParser { suspended: Cell<bool>, /// https://html.spec.whatwg.org/multipage/#script-nesting-level script_nesting_level: Cell<usize>, + /// https://html.spec.whatwg.org/multipage/#abort-a-parser + aborted: Cell<bool>, + /// https://html.spec.whatwg.org/multipage/#script-created-parser + script_created_parser: bool, } #[derive(PartialEq)] @@ -87,7 +92,8 @@ impl ServoParser { pub fn parse_html_document(document: &Document, input: DOMString, url: ServoUrl) { let parser = ServoParser::new(document, Tokenizer::Html(self::html::Tokenizer::new(document, url, None)), - LastChunkState::NotReceived); + LastChunkState::NotReceived, + ParserKind::Normal); parser.parse_chunk(String::from(input)); } @@ -129,7 +135,8 @@ impl ServoParser { Tokenizer::Html(self::html::Tokenizer::new(&document, url.clone(), Some(fragment_context))), - LastChunkState::Received); + LastChunkState::Received, + ParserKind::Normal); parser.parse_chunk(String::from(input)); // Step 14. @@ -139,10 +146,23 @@ impl ServoParser { } } + pub fn parse_html_script_input(document: &Document, url: ServoUrl, type_: &str) { + let parser = ServoParser::new(document, + Tokenizer::Html(self::html::Tokenizer::new(document, url, None)), + LastChunkState::NotReceived, + ParserKind::ScriptCreated); + document.set_current_parser(Some(&parser)); + if !type_.eq_ignore_ascii_case("text/html") { + parser.parse_chunk("<pre>\n".to_owned()); + parser.tokenizer.borrow_mut().set_plaintext_state(); + } + } + pub fn parse_xml_document(document: &Document, input: DOMString, url: ServoUrl) { let parser = ServoParser::new(document, Tokenizer::Xml(self::xml::Tokenizer::new(document, url)), - LastChunkState::NotReceived); + LastChunkState::NotReceived, + ParserKind::Normal); parser.parse_chunk(String::from(input)); } @@ -150,6 +170,10 @@ impl ServoParser { self.script_nesting_level.get() } + pub fn is_script_created(&self) -> bool { + self.script_created_parser + } + /// Corresponds to the latter part of the "Otherwise" branch of the 'An end /// tag whose tag name is "script"' of /// https://html.spec.whatwg.org/multipage/#parsing-main-incdata @@ -186,9 +210,13 @@ impl ServoParser { } } + pub fn can_write(&self) -> bool { + self.script_created_parser || self.script_nesting_level.get() > 0 + } + /// Steps 6-8 of https://html.spec.whatwg.org/multipage/#document.write() pub fn write(&self, text: Vec<DOMString>) { - assert!(self.script_nesting_level.get() > 0); + assert!(self.can_write()); if self.document.has_pending_parsing_blocking_script() { // There is already a pending parsing blocking script so the @@ -225,10 +253,47 @@ impl ServoParser { assert!(input.is_empty()); } + // Steps 4-6 of https://html.spec.whatwg.org/multipage/#dom-document-close + pub fn close(&self) { + assert!(self.script_created_parser); + + // Step 4. + self.last_chunk_received.set(true); + + if self.suspended.get() { + // Step 5. + return; + } + + // Step 6. + self.parse_sync(); + } + + // https://html.spec.whatwg.org/multipage/#abort-a-parser + pub fn abort(&self) { + assert!(!self.aborted.get()); + self.aborted.set(true); + + // Step 1. + *self.script_input.borrow_mut() = BufferQueue::new(); + *self.network_input.borrow_mut() = BufferQueue::new(); + + // Step 2. + self.document.set_ready_state(DocumentReadyState::Interactive); + + // Step 3. + self.tokenizer.borrow_mut().end(); + self.document.set_current_parser(None); + + // Step 4. + self.document.set_ready_state(DocumentReadyState::Interactive); + } + #[allow(unrooted_must_root)] fn new_inherited(document: &Document, tokenizer: Tokenizer, - last_chunk_state: LastChunkState) + last_chunk_state: LastChunkState, + kind: ParserKind) -> Self { ServoParser { reflector: Reflector::new(), @@ -239,15 +304,18 @@ impl ServoParser { last_chunk_received: Cell::new(last_chunk_state == LastChunkState::Received), suspended: Default::default(), script_nesting_level: Default::default(), + aborted: Default::default(), + script_created_parser: kind == ParserKind::ScriptCreated, } } #[allow(unrooted_must_root)] fn new(document: &Document, tokenizer: Tokenizer, - last_chunk_state: LastChunkState) + last_chunk_state: LastChunkState, + kind: ParserKind) -> Root<Self> { - reflect_dom_object(box ServoParser::new_inherited(document, tokenizer, last_chunk_state), + reflect_dom_object(box ServoParser::new_inherited(document, tokenizer, last_chunk_state, kind), document.window(), ServoParserBinding::Wrap) } @@ -301,6 +369,7 @@ impl ServoParser { { loop { assert!(!self.suspended.get()); + assert!(!self.aborted.get()); self.document.reflow_if_reflow_timer_expired(); let script = match feed(&mut *self.tokenizer.borrow_mut()) { @@ -358,6 +427,12 @@ impl Iterator for FragmentParsingResult { } } +#[derive(HeapSizeOf, JSTraceable, PartialEq)] +enum ParserKind { + Normal, + ScriptCreated, +} + #[derive(HeapSizeOf, JSTraceable)] #[must_root] enum Tokenizer { @@ -454,6 +529,9 @@ impl FetchResponseListener for ParserContext { Some(parser) => parser, None => return, }; + if parser.aborted.get() { + return; + } self.parser = Some(Trusted::new(&*parser)); @@ -512,15 +590,19 @@ impl FetchResponseListener for ParserContext { } fn process_response_chunk(&mut self, payload: Vec<u8>) { - if !self.is_synthesized_document { - // FIXME: use Vec<u8> (html5ever #34) - let data = UTF_8.decode(&payload, DecoderTrap::Replace).unwrap(); - let parser = match self.parser.as_ref() { - Some(parser) => parser.root(), - None => return, - }; - parser.parse_chunk(data); + if self.is_synthesized_document { + return; + } + // FIXME: use Vec<u8> (html5ever #34) + let data = UTF_8.decode(&payload, DecoderTrap::Replace).unwrap(); + let parser = match self.parser.as_ref() { + Some(parser) => parser.root(), + None => return, + }; + if parser.aborted.get() { + return; } + parser.parse_chunk(data); } fn process_response_eof(&mut self, status: Result<(), NetworkError>) { @@ -528,6 +610,9 @@ impl FetchResponseListener for ParserContext { Some(parser) => parser.root(), None => return, }; + if parser.aborted.get() { + return; + } if let Err(NetworkError::Internal(ref reason)) = status { // Show an error page for network errors, diff --git a/components/script/dom/webidls/Document.webidl b/components/script/dom/webidls/Document.webidl index 4955bf6e956..46e5ce05abf 100644 --- a/components/script/dom/webidls/Document.webidl +++ b/components/script/dom/webidls/Document.webidl @@ -111,9 +111,11 @@ partial /*sealed*/ interface Document { readonly attribute HTMLScriptElement? currentScript; // dynamic markup insertion - // Document open(optional DOMString type = "text/html", optional DOMString replace = ""); + [Throws] + Document open(optional DOMString type = "text/html", optional DOMString replace = ""); // WindowProxy open(DOMString url, DOMString name, DOMString features, optional boolean replace = false); - // void close(); + [Throws] + void close(); [Throws] void write(DOMString... text); [Throws] diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index 86957d943d9..f1834de260d 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -60,8 +60,8 @@ use net_traits::storage_thread::StorageType; use num_traits::ToPrimitive; use open; use origin::Origin; -use profile_traits::mem; -use profile_traits::time::ProfilerChan; +use profile_traits::mem::ProfilerChan as MemProfilerChan; +use profile_traits::time::ProfilerChan as TimeProfilerChan; use rustc_serialize::base64::{FromBase64, STANDARD, ToBase64}; use script_layout_interface::TrustedNodeAddress; use script_layout_interface::message::{Msg, Reflow, ReflowQueryType, ScriptReflow}; @@ -87,6 +87,7 @@ use std::cell::Cell; use std::collections::{HashMap, HashSet}; use std::default::Default; use std::io::{Write, stderr, stdout}; +use std::mem; use std::rc::Rc; use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -231,7 +232,7 @@ pub struct Window { /// A flag to prevent async events from attempting to interact with this window. #[ignore_heap_size_of = "defined in std"] - ignore_further_async_events: Arc<AtomicBool>, + ignore_further_async_events: DOMRefCell<Arc<AtomicBool>>, error_reporter: CSSErrorReporter, @@ -255,7 +256,7 @@ impl Window { *self.js_runtime.borrow_for_script_deallocation() = None; self.browsing_context.set(None); self.current_state.set(WindowState::Zombie); - self.ignore_further_async_events.store(true, Ordering::Relaxed); + self.ignore_further_async_events.borrow().store(true, Ordering::Relaxed); } } @@ -917,10 +918,19 @@ impl WindowMethods for Window { impl Window { pub fn get_runnable_wrapper(&self) -> RunnableWrapper { RunnableWrapper { - cancelled: Some(self.ignore_further_async_events.clone()), + cancelled: Some(self.ignore_further_async_events.borrow().clone()), } } + /// Cancels all the tasks associated with that window. + /// + /// This sets the current `ignore_further_async_events` sentinel value to + /// `true` and replaces it with a brand new one for future tasks. + pub fn cancel_all_tasks(&self) { + let cancelled = mem::replace(&mut *self.ignore_further_async_events.borrow_mut(), Default::default()); + cancelled.store(true, Ordering::Relaxed); + } + pub fn clear_js_runtime(&self) { // We tear down the active document, which causes all the attached // nodes to dispose of their layout data. This messages the layout @@ -944,7 +954,7 @@ impl Window { self.current_state.set(WindowState::Zombie); *self.js_runtime.borrow_mut() = None; self.browsing_context.set(None); - self.ignore_further_async_events.store(true, Ordering::SeqCst); + self.ignore_further_async_events.borrow().store(true, Ordering::SeqCst); } /// https://drafts.csswg.org/cssom-view/#dom-window-scroll @@ -1611,8 +1621,8 @@ impl Window { image_cache_thread: ImageCacheThread, resource_threads: ResourceThreads, bluetooth_thread: IpcSender<BluetoothRequest>, - mem_profiler_chan: mem::ProfilerChan, - time_profiler_chan: ProfilerChan, + mem_profiler_chan: MemProfilerChan, + time_profiler_chan: TimeProfilerChan, devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>, constellation_chan: IpcSender<ConstellationMsg>, control_chan: IpcSender<ConstellationControlMsg>, @@ -1681,7 +1691,7 @@ impl Window { devtools_marker_sender: DOMRefCell::new(None), devtools_markers: DOMRefCell::new(HashSet::new()), webdriver_script_chan: DOMRefCell::new(None), - ignore_further_async_events: Arc::new(AtomicBool::new(false)), + ignore_further_async_events: Default::default(), error_reporter: error_reporter, scroll_offsets: DOMRefCell::new(HashMap::new()), media_query_lists: WeakMediaQueryListVec::new(), |