/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use crate::cookie_rs; use crate::document_loader::{DocumentLoader, LoadType}; use crate::dom::activation::{synthetic_click_activation, ActivationSource}; use crate::dom::attr::Attr; use crate::dom::beforeunloadevent::BeforeUnloadEvent; use crate::dom::bindings::callback::ExceptionHandling; use crate::dom::bindings::cell::DomRefCell; use crate::dom::bindings::codegen::Bindings::BeforeUnloadEventBinding::BeforeUnloadEventBinding::BeforeUnloadEventMethods; use crate::dom::bindings::codegen::Bindings::DocumentBinding; use crate::dom::bindings::codegen::Bindings::DocumentBinding::ElementCreationOptions; use crate::dom::bindings::codegen::Bindings::DocumentBinding::{ DocumentMethods, DocumentReadyState, }; use crate::dom::bindings::codegen::Bindings::EventBinding::EventBinding::EventMethods; use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementBinding::HTMLIFrameElementMethods; use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; use crate::dom::bindings::codegen::Bindings::NodeFilterBinding::NodeFilter; use crate::dom::bindings::codegen::Bindings::PerformanceBinding::PerformanceMethods; use crate::dom::bindings::codegen::Bindings::TouchBinding::TouchMethods; use crate::dom::bindings::codegen::Bindings::WindowBinding::{ FrameRequestCallback, ScrollBehavior, WindowMethods, }; use crate::dom::bindings::codegen::UnionTypes::NodeOrString; use crate::dom::bindings::error::{Error, ErrorResult, Fallible}; use crate::dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId}; use crate::dom::bindings::num::Finite; use crate::dom::bindings::refcounted::{Trusted, TrustedPromise}; use crate::dom::bindings::reflector::{reflect_dom_object, DomObject}; use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, MutNullableDom, RootedReference}; use crate::dom::bindings::str::{DOMString, USVString}; use crate::dom::bindings::xmlname::XMLName::InvalidXMLName; use crate::dom::bindings::xmlname::{ namespace_from_domstring, validate_and_extract, xml_name_type, }; use crate::dom::closeevent::CloseEvent; use crate::dom::comment::Comment; use crate::dom::cssstylesheet::CSSStyleSheet; use crate::dom::customelementregistry::CustomElementDefinition; use crate::dom::customevent::CustomEvent; use crate::dom::documentfragment::DocumentFragment; use crate::dom::documenttype::DocumentType; use crate::dom::domimplementation::DOMImplementation; use crate::dom::element::CustomElementCreationMode; use crate::dom::element::{ Element, ElementCreator, ElementPerformFullscreenEnter, ElementPerformFullscreenExit, }; use crate::dom::errorevent::ErrorEvent; use crate::dom::event::{Event, EventBubbles, EventCancelable, EventDefault, EventStatus}; use crate::dom::eventtarget::EventTarget; use crate::dom::focusevent::FocusEvent; use crate::dom::globalscope::GlobalScope; use crate::dom::hashchangeevent::HashChangeEvent; use crate::dom::htmlanchorelement::HTMLAnchorElement; use crate::dom::htmlareaelement::HTMLAreaElement; use crate::dom::htmlbaseelement::HTMLBaseElement; use crate::dom::htmlbodyelement::HTMLBodyElement; use crate::dom::htmlcollection::{CollectionFilter, HTMLCollection}; use crate::dom::htmlelement::HTMLElement; use crate::dom::htmlembedelement::HTMLEmbedElement; use crate::dom::htmlformelement::{FormControl, FormControlElementHelpers, HTMLFormElement}; use crate::dom::htmlheadelement::HTMLHeadElement; use crate::dom::htmlhtmlelement::HTMLHtmlElement; use crate::dom::htmliframeelement::HTMLIFrameElement; use crate::dom::htmlimageelement::HTMLImageElement; use crate::dom::htmlmetaelement::HTMLMetaElement; use crate::dom::htmlscriptelement::{HTMLScriptElement, ScriptResult}; use crate::dom::htmltitleelement::HTMLTitleElement; use crate::dom::keyboardevent::KeyboardEvent; use crate::dom::location::Location; use crate::dom::messageevent::MessageEvent; use crate::dom::mouseevent::MouseEvent; use crate::dom::node::VecPreOrderInsertionHelper; use crate::dom::node::{self, document_from_node, window_from_node, CloneChildrenFlag}; use crate::dom::node::{LayoutNodeHelpers, Node, NodeDamage, NodeFlags}; use crate::dom::nodeiterator::NodeIterator; use crate::dom::nodelist::NodeList; use crate::dom::pagetransitionevent::PageTransitionEvent; use crate::dom::popstateevent::PopStateEvent; use crate::dom::processinginstruction::ProcessingInstruction; use crate::dom::progressevent::ProgressEvent; use crate::dom::promise::Promise; use crate::dom::range::Range; use crate::dom::servoparser::ServoParser; use crate::dom::storageevent::StorageEvent; use crate::dom::stylesheetlist::StyleSheetList; use crate::dom::text::Text; use crate::dom::touch::Touch; use crate::dom::touchevent::TouchEvent; use crate::dom::touchlist::TouchList; use crate::dom::treewalker::TreeWalker; use crate::dom::uievent::UIEvent; use crate::dom::virtualmethods::vtable_for; use crate::dom::webglcontextevent::WebGLContextEvent; use crate::dom::window::{ReflowReason, Window}; use crate::dom::windowproxy::WindowProxy; use crate::fetch::FetchCanceller; use crate::script_runtime::{CommonScriptMsg, ScriptThreadEventCategory}; use crate::script_thread::{MainThreadScriptMsg, ScriptThread}; use crate::task_source::{TaskSource, TaskSourceName}; use crate::timers::OneshotTimerCallback; use devtools_traits::ScriptToDevtoolsControlMsg; use dom_struct::dom_struct; use embedder_traits::EmbedderMsg; use encoding_rs::{Encoding, UTF_8}; use euclid::Point2D; use html5ever::{LocalName, Namespace, QualName}; use hyper_serde::Serde; use ipc_channel::ipc::{self, IpcSender}; use js::jsapi::JS_GetRuntime; use js::jsapi::{JSContext, JSObject, JSRuntime}; use keyboard_types::{Key, KeyState, Modifiers}; use metrics::{ InteractiveFlag, InteractiveMetrics, InteractiveWindow, ProfilerMetadataFactory, ProgressiveWebMetric, }; use mime::{self, Mime}; use msg::constellation_msg::BrowsingContextId; use net_traits::pub_domains::is_pub_domain; use net_traits::request::RequestInit; use net_traits::response::HttpsState; use net_traits::CookieSource::NonHTTP; use net_traits::CoreResourceMsg::{GetCookiesForUrl, SetCookiesForUrl}; use net_traits::{FetchResponseMsg, IpcSend, ReferrerPolicy}; use num_traits::ToPrimitive; use profile_traits::ipc as profile_ipc; use profile_traits::time::{TimerMetadata, TimerMetadataFrameType, TimerMetadataReflowType}; use ref_slice::ref_slice; use script_layout_interface::message::{Msg, NodesFromPointQueryType, QueryMsg, ReflowGoal}; use script_traits::{AnimationState, DocumentActivity, MouseButton, MouseEventType}; use script_traits::{MsDuration, ScriptMsg, TouchEventType, TouchId, UntrustedNodeAddress}; use servo_arc::Arc; use servo_atoms::Atom; use servo_config::prefs::PREFS; use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl}; use std::borrow::ToOwned; use std::cell::{Cell, Ref, RefMut}; use std::collections::hash_map::Entry::{Occupied, Vacant}; use std::collections::{HashMap, HashSet, VecDeque}; use std::default::Default; use std::fmt; use std::mem; use std::ptr::NonNull; use std::rc::Rc; use std::time::{Duration, Instant}; use style::attr::AttrValue; use style::context::QuirksMode; use style::invalidation::element::restyle_hints::RestyleHint; use style::media_queries::{Device, MediaList, MediaType}; use style::selector_parser::{RestyleDamage, Snapshot}; use style::shared_lock::{SharedRwLock as StyleSharedRwLock, SharedRwLockReadGuard}; use style::str::{split_html_space_chars, str_join}; use style::stylesheet_set::DocumentStylesheetSet; use style::stylesheets::{CssRule, Origin, OriginSet, Stylesheet}; use time; use url::percent_encoding::percent_decode; use url::Host; /// The number of times we are allowed to see spurious `requestAnimationFrame()` calls before /// falling back to fake ones. /// /// A spurious `requestAnimationFrame()` call is defined as one that does not change the DOM. const SPURIOUS_ANIMATION_FRAME_THRESHOLD: u8 = 5; /// The amount of time between fake `requestAnimationFrame()`s. const FAKE_REQUEST_ANIMATION_FRAME_DELAY: u64 = 16; pub enum TouchEventResult { Processed(bool), Forwarded, } pub enum FireMouseEventType { Move, Over, Out, } impl FireMouseEventType { pub fn as_str(&self) -> &str { match self { &FireMouseEventType::Move => "mousemove", &FireMouseEventType::Over => "mouseover", &FireMouseEventType::Out => "mouseout", } } } #[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)] pub enum IsHTMLDocument { HTMLDocument, NonHTMLDocument, } #[derive(Debug, MallocSizeOf)] pub struct PendingRestyle { /// If this element had a state or attribute change since the last restyle, track /// the original condition of the element. pub snapshot: Option, /// Any explicit restyles hints that have been accumulated for this element. pub hint: RestyleHint, /// Any explicit restyles damage that have been accumulated for this element. pub damage: RestyleDamage, } impl PendingRestyle { pub fn new() -> Self { PendingRestyle { snapshot: None, hint: RestyleHint::empty(), damage: RestyleDamage::empty(), } } } #[derive(Clone, JSTraceable, MallocSizeOf)] #[must_root] struct StyleSheetInDocument { #[ignore_malloc_size_of = "Arc"] sheet: Arc, owner: Dom, } impl fmt::Debug for StyleSheetInDocument { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { self.sheet.fmt(formatter) } } impl PartialEq for StyleSheetInDocument { fn eq(&self, other: &Self) -> bool { Arc::ptr_eq(&self.sheet, &other.sheet) } } impl ::style::stylesheets::StylesheetInDocument for StyleSheetInDocument { fn origin(&self, guard: &SharedRwLockReadGuard) -> Origin { self.sheet.origin(guard) } fn quirks_mode(&self, guard: &SharedRwLockReadGuard) -> QuirksMode { self.sheet.quirks_mode(guard) } fn enabled(&self) -> bool { self.sheet.enabled() } fn media<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> Option<&'a MediaList> { self.sheet.media(guard) } fn rules<'a, 'b: 'a>(&'a self, guard: &'b SharedRwLockReadGuard) -> &'a [CssRule] { self.sheet.rules(guard) } } /// #[dom_struct] pub struct Document { node: Node, window: Dom, implementation: MutNullableDom, #[ignore_malloc_size_of = "type from external crate"] content_type: Mime, last_modified: Option, encoding: Cell<&'static Encoding>, has_browsing_context: bool, is_html_document: bool, activity: Cell, url: DomRefCell, #[ignore_malloc_size_of = "defined in selectors"] quirks_mode: Cell, /// Caches for the getElement methods id_map: DomRefCell>>>, tag_map: DomRefCell>>, tagns_map: DomRefCell>>, classes_map: DomRefCell, Dom>>, images: MutNullableDom, embeds: MutNullableDom, links: MutNullableDom, forms: MutNullableDom, scripts: MutNullableDom, anchors: MutNullableDom, applets: MutNullableDom, /// Lock use for style attributes and author-origin stylesheet objects in this document. /// Can be acquired once for accessing many objects. style_shared_lock: StyleSharedRwLock, /// List of stylesheets associated with nodes in this document. |None| if the list needs to be refreshed. stylesheets: DomRefCell>, stylesheet_list: MutNullableDom, ready_state: Cell, /// Whether the DOMContentLoaded event has already been dispatched. domcontentloaded_dispatched: Cell, /// The element that has most recently requested focus for itself. possibly_focused: MutNullableDom, /// The element that currently has the document focus context. focused: MutNullableDom, /// The script element that is currently executing. current_script: MutNullableDom, /// pending_parsing_blocking_script: DomRefCell>, /// Number of stylesheets that block executing the next parser-inserted script script_blocking_stylesheets_count: Cell, /// https://html.spec.whatwg.org/multipage/#list-of-scripts-that-will-execute-when-the-document-has-finished-parsing deferred_scripts: PendingInOrderScriptVec, /// asap_in_order_scripts_list: PendingInOrderScriptVec, /// asap_scripts_set: DomRefCell>>, /// /// True if scripting is enabled for all scripts in this document scripting_enabled: bool, /// /// Current identifier of animation frame callback animation_frame_ident: Cell, /// /// List of animation frame callbacks animation_frame_list: DomRefCell)>>, /// Whether we're in the process of running animation callbacks. /// /// Tracking this is not necessary for correctness. Instead, it is an optimization to avoid /// sending needless `ChangeRunningAnimationsState` messages to the compositor. running_animation_callbacks: Cell, /// Tracks all outstanding loads related to this document. loader: DomRefCell, /// The current active HTML parser, to allow resuming after interruptions. current_parser: MutNullableDom, /// When we should kick off a reflow. This happens during parsing. reflow_timeout: Cell>, /// The cached first `base` element with an `href` attribute. base_element: MutNullableDom, /// This field is set to the document itself for inert documents. /// appropriate_template_contents_owner_document: MutNullableDom, /// Information on elements needing restyle to ship over to the layout thread when the /// time comes. pending_restyles: DomRefCell, PendingRestyle>>, /// This flag will be true if layout suppressed a reflow attempt that was /// needed in order for the page to be painted. needs_paint: Cell, /// active_touch_points: DomRefCell>>, /// Navigation Timing properties: /// dom_loading: Cell, dom_interactive: Cell, dom_content_loaded_event_start: Cell, dom_content_loaded_event_end: Cell, dom_complete: Cell, top_level_dom_complete: Cell, load_event_start: Cell, load_event_end: Cell, /// https_state: Cell, /// The document's origin. origin: MutableOrigin, /// https://w3c.github.io/webappsec-referrer-policy/#referrer-policy-states referrer_policy: Cell>, /// referrer: Option, /// target_element: MutNullableDom, /// #[ignore_malloc_size_of = "Defined in std"] last_click_info: DomRefCell)>>, /// ignore_destructive_writes_counter: Cell, /// ignore_opens_during_unload_counter: Cell, /// The number of spurious `requestAnimationFrame()` requests we've received. /// /// A rAF request is considered spurious if nothing was actually reflowed. spurious_animation_frames: Cell, /// Track the total number of elements in this DOM's tree. /// This is sent to the layout thread every time a reflow is done; /// layout uses this to determine if the gains from parallel layout will be worth the overhead. /// /// See also: https://github.com/servo/servo/issues/10110 dom_count: Cell, /// Entry node for fullscreen. fullscreen_element: MutNullableDom, /// Map from ID to set of form control elements that have that ID as /// their 'form' content attribute. Used to reset form controls /// whenever any element with the same ID as the form attribute /// is inserted or removed from the document. /// See https://html.spec.whatwg.org/multipage/#form-owner form_id_listener_map: DomRefCell>>>, interactive_time: DomRefCell, tti_window: DomRefCell, /// RAII canceller for Fetch canceller: FetchCanceller, /// https://html.spec.whatwg.org/multipage/#throw-on-dynamic-markup-insertion-counter throw_on_dynamic_markup_insertion_counter: Cell, /// https://html.spec.whatwg.org/multipage/#page-showing page_showing: Cell, /// Whether the document is salvageable. salvageable: Cell, /// Whether the unload event has already been fired. fired_unload: Cell, /// List of responsive images responsive_images: DomRefCell>>, } #[derive(JSTraceable, MallocSizeOf)] struct ImagesFilter; impl CollectionFilter for ImagesFilter { fn filter(&self, elem: &Element, _root: &Node) -> bool { elem.is::() } } #[derive(JSTraceable, MallocSizeOf)] struct EmbedsFilter; impl CollectionFilter for EmbedsFilter { fn filter(&self, elem: &Element, _root: &Node) -> bool { elem.is::() } } #[derive(JSTraceable, MallocSizeOf)] struct LinksFilter; impl CollectionFilter for LinksFilter { fn filter(&self, elem: &Element, _root: &Node) -> bool { (elem.is::() || elem.is::()) && elem.has_attribute(&local_name!("href")) } } #[derive(JSTraceable, MallocSizeOf)] struct FormsFilter; impl CollectionFilter for FormsFilter { fn filter(&self, elem: &Element, _root: &Node) -> bool { elem.is::() } } #[derive(JSTraceable, MallocSizeOf)] struct ScriptsFilter; impl CollectionFilter for ScriptsFilter { fn filter(&self, elem: &Element, _root: &Node) -> bool { elem.is::() } } #[derive(JSTraceable, MallocSizeOf)] struct AnchorsFilter; impl CollectionFilter for AnchorsFilter { fn filter(&self, elem: &Element, _root: &Node) -> bool { elem.is::() && elem.has_attribute(&local_name!("href")) } } impl Document { #[inline] pub fn loader(&self) -> Ref { self.loader.borrow() } #[inline] pub fn loader_mut(&self) -> RefMut { self.loader.borrow_mut() } #[inline] pub fn has_browsing_context(&self) -> bool { self.has_browsing_context } /// #[inline] pub fn browsing_context(&self) -> Option> { if self.has_browsing_context { self.window.undiscarded_window_proxy() } else { None } } #[inline] pub fn window(&self) -> &Window { &*self.window } #[inline] pub fn is_html_document(&self) -> bool { self.is_html_document } pub fn set_https_state(&self, https_state: HttpsState) { self.https_state.set(https_state); } pub fn is_fully_active(&self) -> bool { self.activity.get() == DocumentActivity::FullyActive } pub fn is_active(&self) -> bool { self.activity.get() != DocumentActivity::Inactive } pub fn set_activity(&self, activity: DocumentActivity) { // This function should only be called on documents with a browsing context assert!(self.has_browsing_context); // Set the document's activity level, reflow if necessary, and suspend or resume timers. if activity != self.activity.get() { self.activity.set(activity); if activity == DocumentActivity::FullyActive { self.title_changed(); self.dirty_all_nodes(); self.window() .reflow(ReflowGoal::Full, ReflowReason::CachedPageNeededReflow); self.window().resume(); // html.spec.whatwg.org/multipage/#history-traversal // Step 4.6 if self.ready_state.get() == DocumentReadyState::Complete { let document = Trusted::new(self); self.window .dom_manipulation_task_source() .queue( task!(fire_pageshow_event: move || { let document = document.root(); let window = document.window(); // Step 4.6.1 if document.page_showing.get() { return; } // Step 4.6.2 document.page_showing.set(true); // Step 4.6.4 let event = PageTransitionEvent::new( window, atom!("pageshow"), false, // bubbles false, // cancelable true, // persisted ); let event = event.upcast::(); event.set_trusted(true); // FIXME(nox): Why are errors silenced here? let _ = window.upcast::().dispatch_event_with_target( document.upcast(), &event, ); }), self.window.upcast(), ) .unwrap(); } } else { self.window().suspend(); } } } pub fn origin(&self) -> &MutableOrigin { &self.origin } // https://dom.spec.whatwg.org/#concept-document-url pub fn url(&self) -> ServoUrl { self.url.borrow().clone() } pub fn set_url(&self, url: ServoUrl) { *self.url.borrow_mut() = url; } // https://html.spec.whatwg.org/multipage/#fallback-base-url pub fn fallback_base_url(&self) -> ServoUrl { // Step 1: iframe srcdoc (#4767). // Step 2: about:blank with a creator browsing context. // Step 3. self.url() } // https://html.spec.whatwg.org/multipage/#document-base-url pub fn base_url(&self) -> ServoUrl { match self.base_element() { // Step 1. None => self.fallback_base_url(), // Step 2. Some(base) => base.frozen_base_url(), } } pub fn needs_paint(&self) -> bool { self.needs_paint.get() } pub fn needs_reflow(&self) -> bool { // FIXME: This should check the dirty bit on the document, // not the document element. Needs some layout changes to make // that workable. self.stylesheets.borrow().has_changed() || self.GetDocumentElement().map_or(false, |root| { root.upcast::().has_dirty_descendants() || !self.pending_restyles.borrow().is_empty() || self.needs_paint() }) } /// Returns the first `base` element in the DOM that has an `href` attribute. pub fn base_element(&self) -> Option> { self.base_element.get() } /// Refresh the cached first base element in the DOM. /// pub fn refresh_base_element(&self) { let base = self .upcast::() .traverse_preorder() .filter_map(DomRoot::downcast::) .find(|element| { element .upcast::() .has_attribute(&local_name!("href")) }); self.base_element.set(base.r()); } pub fn dom_count(&self) -> u32 { self.dom_count.get() } /// This is called by `bind_to_tree` when a node is added to the DOM. /// The internal count is used by layout to determine whether to be sequential or parallel. /// (it's sequential for small DOMs) pub fn increment_dom_count(&self) { self.dom_count.set(self.dom_count.get() + 1); } /// This is called by `unbind_from_tree` when a node is removed from the DOM. pub fn decrement_dom_count(&self) { self.dom_count.set(self.dom_count.get() - 1); } pub fn quirks_mode(&self) -> QuirksMode { self.quirks_mode.get() } pub fn set_quirks_mode(&self, mode: QuirksMode) { self.quirks_mode.set(mode); if mode == QuirksMode::Quirks { self.window .layout_chan() .send(Msg::SetQuirksMode(mode)) .unwrap(); } } pub fn encoding(&self) -> &'static Encoding { self.encoding.get() } pub fn set_encoding(&self, encoding: &'static Encoding) { self.encoding.set(encoding); } pub fn content_and_heritage_changed(&self, node: &Node) { if node.is_in_doc() { node.note_dirty_descendants(); } // FIXME(emilio): This is very inefficient, ideally the flag above would // be enough and incremental layout could figure out from there. node.dirty(NodeDamage::OtherNodeDamage); } /// Reflows and disarms the timer if the reflow timer has expired. pub fn reflow_if_reflow_timer_expired(&self) { if let Some(reflow_timeout) = self.reflow_timeout.get() { if time::precise_time_ns() < reflow_timeout { return; } self.reflow_timeout.set(None); self.window .reflow(ReflowGoal::Full, ReflowReason::RefreshTick); } } /// Schedules a reflow to be kicked off at the given `timeout` (in `time::precise_time_ns()` /// units). This reflow happens even if the event loop is busy. This is used to display initial /// page content during parsing. pub fn set_reflow_timeout(&self, timeout: u64) { if let Some(existing_timeout) = self.reflow_timeout.get() { if existing_timeout < timeout { return; } } self.reflow_timeout.set(Some(timeout)) } /// Remove any existing association between the provided id and any elements in this document. pub fn unregister_named_element(&self, to_unregister: &Element, id: Atom) { debug!( "Removing named element from document {:p}: {:p} id={}", self, to_unregister, id ); // Limit the scope of the borrow because id_map might be borrowed again by // GetElementById through the following sequence of calls // reset_form_owner_for_listeners -> reset_form_owner -> GetElementById { let mut id_map = self.id_map.borrow_mut(); let is_empty = match id_map.get_mut(&id) { None => false, Some(elements) => { let position = elements .iter() .position(|element| &**element == to_unregister) .expect("This element should be in registered."); elements.remove(position); elements.is_empty() }, }; if is_empty { id_map.remove(&id); } } self.reset_form_owner_for_listeners(&id); } /// Associate an element present in this document with the provided id. pub fn register_named_element(&self, element: &Element, id: Atom) { debug!( "Adding named element to document {:p}: {:p} id={}", self, element, id ); assert!(element.upcast::().is_in_doc()); assert!(!id.is_empty()); let root = self.GetDocumentElement().expect( "The element is in the document, so there must be a document \ element.", ); // Limit the scope of the borrow because id_map might be borrowed again by // GetElementById through the following sequence of calls // reset_form_owner_for_listeners -> reset_form_owner -> GetElementById { let mut id_map = self.id_map.borrow_mut(); let elements = id_map.entry(id.clone()).or_insert(Vec::new()); elements.insert_pre_order(element, root.r().upcast::()); } self.reset_form_owner_for_listeners(&id); } pub fn register_form_id_listener(&self, id: DOMString, listener: &T) { let mut map = self.form_id_listener_map.borrow_mut(); let listener = listener.to_element(); let set = map.entry(Atom::from(id)).or_insert(HashSet::new()); set.insert(Dom::from_ref(listener)); } pub fn unregister_form_id_listener( &self, id: DOMString, listener: &T, ) { let mut map = self.form_id_listener_map.borrow_mut(); if let Occupied(mut entry) = map.entry(Atom::from(id)) { entry .get_mut() .remove(&Dom::from_ref(listener.to_element())); if entry.get().is_empty() { entry.remove(); } } } /// Attempt to find a named element in this page's document. /// pub fn find_fragment_node(&self, fragid: &str) -> Option> { // Step 1 is not handled here; the fragid is already obtained by the calling function // Step 2: Simply use None to indicate the top of the document. // Step 3 & 4 percent_decode(fragid.as_bytes()) .decode_utf8() .ok() // Step 5 .and_then(|decoded_fragid| self.get_element_by_id(&Atom::from(decoded_fragid))) // Step 6 .or_else(|| self.get_anchor_by_name(fragid)) // Step 7 & 8 } /// Scroll to the target element, and when we do not find a target /// and the fragment is empty or "top", scroll to the top. /// pub fn check_and_scroll_fragment(&self, fragment: &str) { let target = self.find_fragment_node(fragment); // Step 1 self.set_target_element(target.r()); let point = target .r() .map(|element| { // FIXME(#8275, pcwalton): This is pretty bogus when multiple layers are involved. // Really what needs to happen is that this needs to go through layout to ask which // layer the element belongs to, and have it send the scroll message to the // compositor. let rect = element.upcast::().bounding_content_box_or_zero(); // In order to align with element edges, we snap to unscaled pixel boundaries, since // the paint thread currently does the same for drawing elements. This is important // for pages that require pixel perfect scroll positioning for proper display // (like Acid2). Since we don't have the device pixel ratio here, this might not be // accurate, but should work as long as the ratio is a whole number. Once #8275 is // fixed this should actually take into account the real device pixel ratio. ( rect.origin.x.to_nearest_px() as f32, rect.origin.y.to_nearest_px() as f32, ) }) .or_else(|| { if fragment.is_empty() || fragment.eq_ignore_ascii_case("top") { // FIXME(stshine): this should be the origin of the stacking context space, // which may differ under the influence of writing mode. Some((0.0, 0.0)) } else { None } }); if let Some((x, y)) = point { // Step 3 let global_scope = self.window.upcast::(); self.window.update_viewport_for_scroll(x, y); self.window.perform_a_scroll( x, y, global_scope.pipeline_id().root_scroll_id(), ScrollBehavior::Instant, target.r(), ); } } fn get_anchor_by_name(&self, name: &str) -> Option> { let check_anchor = |node: &HTMLAnchorElement| { let elem = node.upcast::(); elem.get_attribute(&ns!(), &local_name!("name")) .map_or(false, |attr| &**attr.value() == name) }; let doc_node = self.upcast::(); doc_node .traverse_preorder() .filter_map(DomRoot::downcast) .find(|node| check_anchor(&node)) .map(DomRoot::upcast) } // https://html.spec.whatwg.org/multipage/#current-document-readiness pub fn set_ready_state(&self, state: DocumentReadyState) { match state { DocumentReadyState::Loading => { update_with_current_time_ms(&self.dom_loading); }, DocumentReadyState::Complete => { update_with_current_time_ms(&self.dom_complete); }, DocumentReadyState::Interactive => update_with_current_time_ms(&self.dom_interactive), }; self.ready_state.set(state); self.upcast::() .fire_event(atom!("readystatechange")); } /// Return whether scripting is enabled or not pub fn is_scripting_enabled(&self) -> bool { self.scripting_enabled } /// Return the element that currently has focus. // https://w3c.github.io/uievents/#events-focusevent-doc-focus pub fn get_focused_element(&self) -> Option> { self.focused.get() } /// Initiate a new round of checking for elements requesting focus. The last element to call /// `request_focus` before `commit_focus_transaction` is called will receive focus. pub fn begin_focus_transaction(&self) { self.possibly_focused.set(None); } /// Request that the given element receive focus once the current transaction is complete. pub fn request_focus(&self, elem: &Element) { if elem.is_focusable_area() { self.possibly_focused.set(Some(elem)) } } /// Reassign the focus context to the element that last requested focus during this /// transaction, or none if no elements requested it. pub fn commit_focus_transaction(&self, focus_type: FocusType) { if self.focused == self.possibly_focused.get().r() { return; } if let Some(ref elem) = self.focused.get() { let node = elem.upcast::(); elem.set_focus_state(false); // FIXME: pass appropriate relatedTarget self.fire_focus_event(FocusEventType::Blur, node, None); // Notify the embedder to hide the input method. if elem.input_method_type().is_some() { self.send_to_embedder(EmbedderMsg::HideIME); } } self.focused.set(self.possibly_focused.get().r()); if let Some(ref elem) = self.focused.get() { elem.set_focus_state(true); let node = elem.upcast::(); // FIXME: pass appropriate relatedTarget self.fire_focus_event(FocusEventType::Focus, node, None); // Update the focus state for all elements in the focus chain. // https://html.spec.whatwg.org/multipage/#focus-chain if focus_type == FocusType::Element { self.window().send_to_constellation(ScriptMsg::Focus); } // Notify the embedder to display an input method. if let Some(kind) = elem.input_method_type() { self.send_to_embedder(EmbedderMsg::ShowIME(kind)); } } } /// Handles any updates when the document's title has changed. pub fn title_changed(&self) { if self.browsing_context().is_some() { self.send_title_to_embedder(); } } /// Sends this document's title to the constellation. pub fn send_title_to_embedder(&self) { let window = self.window(); if window.is_top_level() { let title = Some(String::from(self.Title())); self.send_to_embedder(EmbedderMsg::ChangePageTitle(title)); } } fn send_to_embedder(&self, msg: EmbedderMsg) { let window = self.window(); window.send_to_embedder(msg); } pub fn dirty_all_nodes(&self) { let root = self.upcast::(); for node in root.traverse_preorder() { node.dirty(NodeDamage::OtherNodeDamage) } } #[allow(unsafe_code)] pub fn handle_mouse_event( &self, js_runtime: *mut JSRuntime, _button: MouseButton, client_point: Point2D, mouse_event_type: MouseEventType, node_address: Option, point_in_node: Option>, ) { let mouse_event_type_string = match mouse_event_type { MouseEventType::Click => "click".to_owned(), MouseEventType::MouseUp => "mouseup".to_owned(), MouseEventType::MouseDown => "mousedown".to_owned(), }; debug!("{}: at {:?}", mouse_event_type_string, client_point); let el = node_address.and_then(|address| { let node = unsafe { node::from_untrusted_node_address(js_runtime, address) }; node.inclusive_ancestors() .filter_map(DomRoot::downcast::) .next() }); let el = match el { Some(el) => el, None => return, }; let node = el.upcast::(); debug!("{} on {:?}", mouse_event_type_string, node.debug_str()); // Prevent click event if form control element is disabled. if let MouseEventType::Click = mouse_event_type { if el.click_event_filter_by_disabled_state() { return; } self.begin_focus_transaction(); } // https://w3c.github.io/uievents/#event-type-click let client_x = client_point.x as i32; let client_y = client_point.y as i32; let click_count = 1; let event = MouseEvent::new( &self.window, DOMString::from(mouse_event_type_string), EventBubbles::Bubbles, EventCancelable::Cancelable, Some(&self.window), click_count, client_x, client_y, client_x, client_y, // TODO: Get real screen coordinates? false, false, false, false, 0i16, None, point_in_node, ); let event = event.upcast::(); // https://w3c.github.io/uievents/#trusted-events event.set_trusted(true); // https://html.spec.whatwg.org/multipage/#run-authentic-click-activation-steps let activatable = el.as_maybe_activatable(); match mouse_event_type { MouseEventType::Click => el.authentic_click_activation(event), MouseEventType::MouseDown => { if let Some(a) = activatable { a.enter_formal_activation_state(); } let target = node.upcast(); event.fire(target); }, MouseEventType::MouseUp => { if let Some(a) = activatable { a.exit_formal_activation_state(); } let target = node.upcast(); event.fire(target); }, } if let MouseEventType::Click = mouse_event_type { self.commit_focus_transaction(FocusType::Element); self.maybe_fire_dblclick(client_point, node); } self.window .reflow(ReflowGoal::Full, ReflowReason::MouseEvent); } fn maybe_fire_dblclick(&self, click_pos: Point2D, target: &Node) { // https://w3c.github.io/uievents/#event-type-dblclick let now = Instant::now(); let opt = self.last_click_info.borrow_mut().take(); if let Some((last_time, last_pos)) = opt { let DBL_CLICK_TIMEOUT = Duration::from_millis( PREFS .get("dom.document.dblclick_timeout") .as_u64() .unwrap_or(300), ); let DBL_CLICK_DIST_THRESHOLD = PREFS .get("dom.document.dblclick_dist") .as_u64() .unwrap_or(1); // Calculate distance between this click and the previous click. let line = click_pos - last_pos; let dist = (line.dot(line) as f64).sqrt(); if now.duration_since(last_time) < DBL_CLICK_TIMEOUT && dist < DBL_CLICK_DIST_THRESHOLD as f64 { // A double click has occurred if this click is within a certain time and dist. of previous click. let click_count = 2; let client_x = click_pos.x as i32; let client_y = click_pos.y as i32; let event = MouseEvent::new( &self.window, DOMString::from("dblclick"), EventBubbles::Bubbles, EventCancelable::Cancelable, Some(&self.window), click_count, client_x, client_y, client_x, client_y, false, false, false, false, 0i16, None, None, ); event.upcast::().fire(target.upcast()); // When a double click occurs, self.last_click_info is left as None so that a // third sequential click will not cause another double click. return; } } // Update last_click_info with the time and position of the click. *self.last_click_info.borrow_mut() = Some((now, click_pos)); } pub fn fire_mouse_event( &self, client_point: Point2D, target: &EventTarget, event_name: FireMouseEventType, ) { let client_x = client_point.x.to_i32().unwrap_or(0); let client_y = client_point.y.to_i32().unwrap_or(0); let mouse_event = MouseEvent::new( &self.window, DOMString::from(event_name.as_str()), EventBubbles::Bubbles, EventCancelable::Cancelable, Some(&self.window), 0i32, client_x, client_y, client_x, client_y, false, false, false, false, 0i16, None, None, ); let event = mouse_event.upcast::(); event.fire(target); } #[allow(unsafe_code)] pub fn handle_mouse_move_event( &self, js_runtime: *mut JSRuntime, client_point: Option>, prev_mouse_over_target: &MutNullableDom, node_address: Option, ) { let client_point = match client_point { None => { // If there's no point, there's no target under the mouse // FIXME: dispatch mouseout here. We have no point. prev_mouse_over_target.set(None); return; }, Some(client_point) => client_point, }; let maybe_new_target = node_address.and_then(|address| { let node = unsafe { node::from_untrusted_node_address(js_runtime, address) }; node.inclusive_ancestors() .filter_map(DomRoot::downcast::) .next() }); // Send mousemove event to topmost target, unless it's an iframe, in which case the // compositor should have also sent an event to the inner document. let new_target = match maybe_new_target { Some(ref target) => target, None => return, }; self.fire_mouse_event(client_point, new_target.upcast(), FireMouseEventType::Move); // Nothing more to do here, mousemove is sent, // and the element under the mouse hasn't changed. if maybe_new_target == prev_mouse_over_target.get() { return; } let old_target_is_ancestor_of_new_target = match (prev_mouse_over_target.get(), maybe_new_target.as_ref()) { (Some(old_target), Some(new_target)) => old_target .upcast::() .is_ancestor_of(new_target.upcast::()), _ => false, }; // Here we know the target has changed, so we must update the state, // dispatch mouseout to the previous one, mouseover to the new one, if let Some(old_target) = prev_mouse_over_target.get() { // If the old target is an ancestor of the new target, this can be skipped // completely, since the node's hover state will be reseted below. if !old_target_is_ancestor_of_new_target { for element in old_target .upcast::() .inclusive_ancestors() .filter_map(DomRoot::downcast::) { element.set_hover_state(false); element.set_active_state(false); } } // Remove hover state to old target and its parents self.fire_mouse_event(client_point, old_target.upcast(), FireMouseEventType::Out); // TODO: Fire mouseleave here only if the old target is // not an ancestor of the new target. } if let Some(ref new_target) = maybe_new_target { for element in new_target .upcast::() .inclusive_ancestors() .filter_map(DomRoot::downcast::) { if element.hover_state() { break; } element.set_hover_state(true); } self.fire_mouse_event(client_point, &new_target.upcast(), FireMouseEventType::Over); // TODO: Fire mouseenter here. } // Store the current mouse over target for next frame. prev_mouse_over_target.set(maybe_new_target.r()); self.window .reflow(ReflowGoal::Full, ReflowReason::MouseEvent); } #[allow(unsafe_code)] pub fn handle_touch_event( &self, js_runtime: *mut JSRuntime, event_type: TouchEventType, touch_id: TouchId, point: Point2D, node_address: Option, ) -> TouchEventResult { let TouchId(identifier) = touch_id; let event_name = match event_type { TouchEventType::Down => "touchstart", TouchEventType::Move => "touchmove", TouchEventType::Up => "touchend", TouchEventType::Cancel => "touchcancel", }; let el = node_address.and_then(|address| { let node = unsafe { node::from_untrusted_node_address(js_runtime, address) }; node.inclusive_ancestors() .filter_map(DomRoot::downcast::) .next() }); let el = match el { Some(el) => el, None => return TouchEventResult::Forwarded, }; let target = DomRoot::upcast::(el); let window = &*self.window; let client_x = Finite::wrap(point.x as f64); let client_y = Finite::wrap(point.y as f64); let page_x = Finite::wrap(point.x as f64 + window.PageXOffset() as f64); let page_y = Finite::wrap(point.y as f64 + window.PageYOffset() as f64); let touch = Touch::new( window, identifier, &target, client_x, client_y, // TODO: Get real screen coordinates? client_x, client_y, page_x, page_y, ); match event_type { TouchEventType::Down => { // Add a new touch point self.active_touch_points .borrow_mut() .push(Dom::from_ref(&*touch)); }, TouchEventType::Move => { // Replace an existing touch point let mut active_touch_points = self.active_touch_points.borrow_mut(); match active_touch_points .iter_mut() .find(|t| t.Identifier() == identifier) { Some(t) => *t = Dom::from_ref(&*touch), None => warn!("Got a touchmove event for a non-active touch point"), } }, TouchEventType::Up | TouchEventType::Cancel => { // Remove an existing touch point let mut active_touch_points = self.active_touch_points.borrow_mut(); match active_touch_points .iter() .position(|t| t.Identifier() == identifier) { Some(i) => { active_touch_points.swap_remove(i); }, None => warn!("Got a touchend event for a non-active touch point"), } }, } rooted_vec!(let mut target_touches); let touches = { let touches = self.active_touch_points.borrow(); target_touches.extend(touches.iter().filter(|t| t.Target() == target).cloned()); TouchList::new(window, touches.r()) }; let event = TouchEvent::new( window, DOMString::from(event_name), EventBubbles::Bubbles, EventCancelable::Cancelable, Some(window), 0i32, &touches, &TouchList::new(window, ref_slice(&&*touch)), &TouchList::new(window, target_touches.r()), // FIXME: modifier keys false, false, false, false, ); let event = event.upcast::(); let result = event.fire(&target); window.reflow(ReflowGoal::Full, ReflowReason::MouseEvent); match result { EventStatus::Canceled => TouchEventResult::Processed(false), EventStatus::NotCanceled => TouchEventResult::Processed(true), } } /// The entry point for all key processing for web content pub fn dispatch_key_event(&self, keyboard_event: ::keyboard_types::KeyboardEvent) { let focused = self.get_focused_element(); let body = self.GetBody(); let target = match (&focused, &body) { (&Some(ref focused), _) => focused.upcast(), (&None, &Some(ref body)) => body.upcast(), (&None, &None) => self.window.upcast(), }; let keyevent = KeyboardEvent::new( &self.window, DOMString::from(keyboard_event.state.to_string()), true, true, Some(&self.window), 0, keyboard_event.key.clone(), DOMString::from(keyboard_event.code.to_string()), keyboard_event.location as u32, keyboard_event.repeat, keyboard_event.is_composing, keyboard_event.modifiers, 0, keyboard_event.key.legacy_keycode(), ); let event = keyevent.upcast::(); event.fire(target); let mut cancel_state = event.get_cancel_state(); // https://w3c.github.io/uievents/#keys-cancelable-keys if keyboard_event.state == KeyState::Down && keyboard_event.key.legacy_charcode() != 0 && cancel_state != EventDefault::Prevented { // https://w3c.github.io/uievents/#keypress-event-order let event = KeyboardEvent::new( &self.window, DOMString::from("keypress"), true, true, Some(&self.window), 0, keyboard_event.key.clone(), DOMString::from(keyboard_event.code.to_string()), keyboard_event.location as u32, keyboard_event.repeat, keyboard_event.is_composing, keyboard_event.modifiers, keyboard_event.key.legacy_charcode(), 0, ); let ev = event.upcast::(); ev.fire(target); cancel_state = ev.get_cancel_state(); } if cancel_state == EventDefault::Allowed { let msg = EmbedderMsg::Keyboard(keyboard_event.clone()); self.send_to_embedder(msg); // This behavior is unspecced // We are supposed to dispatch synthetic click activation for Space and/or Return, // however *when* we do it is up to us. // Here, we're dispatching it after the key event so the script has a chance to cancel it // https://www.w3.org/Bugs/Public/show_bug.cgi?id=27337 match keyboard_event.key { Key::Character(ref letter) if letter == " " && keyboard_event.state == KeyState::Up => { let maybe_elem = target.downcast::(); if let Some(el) = maybe_elem { synthetic_click_activation( el, false, false, false, false, ActivationSource::NotFromClick, ) } } Key::Enter if keyboard_event.state == KeyState::Up => { let maybe_elem = target.downcast::(); if let Some(el) = maybe_elem { if let Some(a) = el.as_maybe_activatable() { let ctrl = keyboard_event.modifiers.contains(Modifiers::CONTROL); let alt = keyboard_event.modifiers.contains(Modifiers::ALT); let shift = keyboard_event.modifiers.contains(Modifiers::SHIFT); let meta = keyboard_event.modifiers.contains(Modifiers::META); a.implicit_submission(ctrl, alt, shift, meta); } } }, _ => (), } } self.window.reflow(ReflowGoal::Full, ReflowReason::KeyEvent); } // https://dom.spec.whatwg.org/#converting-nodes-into-a-node pub fn node_from_nodes_and_strings( &self, mut nodes: Vec, ) -> Fallible> { if nodes.len() == 1 { Ok(match nodes.pop().unwrap() { NodeOrString::Node(node) => node, NodeOrString::String(string) => DomRoot::upcast(self.CreateTextNode(string)), }) } else { let fragment = DomRoot::upcast::(self.CreateDocumentFragment()); for node in nodes { match node { NodeOrString::Node(node) => { fragment.AppendChild(&node)?; }, NodeOrString::String(string) => { let node = DomRoot::upcast::(self.CreateTextNode(string)); // No try!() here because appending a text node // should not fail. fragment.AppendChild(&node).unwrap(); }, } } Ok(fragment) } } pub fn get_body_attribute(&self, local_name: &LocalName) -> DOMString { match self .GetBody() .and_then(DomRoot::downcast::) { Some(ref body) => body.upcast::().get_string_attribute(local_name), None => DOMString::new(), } } pub fn set_body_attribute(&self, local_name: &LocalName, value: DOMString) { if let Some(ref body) = self .GetBody() .and_then(DomRoot::downcast::) { let body = body.upcast::(); let value = body.parse_attribute(&ns!(), &local_name, value); body.set_attribute(local_name, value); } } pub fn set_current_script(&self, script: Option<&HTMLScriptElement>) { self.current_script.set(script); } pub fn get_script_blocking_stylesheets_count(&self) -> u32 { self.script_blocking_stylesheets_count.get() } pub fn increment_script_blocking_stylesheet_count(&self) { let count_cell = &self.script_blocking_stylesheets_count; count_cell.set(count_cell.get() + 1); } pub fn decrement_script_blocking_stylesheet_count(&self) { let count_cell = &self.script_blocking_stylesheets_count; assert!(count_cell.get() > 0); count_cell.set(count_cell.get() - 1); } pub fn invalidate_stylesheets(&self) { self.stylesheets.borrow_mut().force_dirty(OriginSet::all()); // Mark the document element dirty so a reflow will be performed. // // FIXME(emilio): Use the DocumentStylesheetSet invalidation stuff. if let Some(element) = self.GetDocumentElement() { element.upcast::().dirty(NodeDamage::NodeStyleDamaged); } } /// pub fn request_animation_frame(&self, callback: AnimationFrameCallback) -> u32 { let ident = self.animation_frame_ident.get() + 1; self.animation_frame_ident.set(ident); self.animation_frame_list .borrow_mut() .push((ident, Some(callback))); // TODO: Should tick animation only when document is visible // If we are running 'fake' animation frames, we unconditionally // set up a one-shot timer for script to execute the rAF callbacks. if self.is_faking_animation_frames() { let callback = FakeRequestAnimationFrameCallback { document: Trusted::new(self), }; self.global().schedule_callback( OneshotTimerCallback::FakeRequestAnimationFrame(callback), MsDuration::new(FAKE_REQUEST_ANIMATION_FRAME_DELAY), ); } else if !self.running_animation_callbacks.get() { // No need to send a `ChangeRunningAnimationsState` if we're running animation callbacks: // we're guaranteed to already be in the "animation callbacks present" state. // // This reduces CPU usage by avoiding needless thread wakeups in the common case of // repeated rAF. let event = ScriptMsg::ChangeRunningAnimationsState(AnimationState::AnimationCallbacksPresent); self.window().send_to_constellation(event); } ident } /// pub fn cancel_animation_frame(&self, ident: u32) { let mut list = self.animation_frame_list.borrow_mut(); if let Some(pair) = list.iter_mut().find(|pair| pair.0 == ident) { pair.1 = None; } } /// pub fn run_the_animation_frame_callbacks(&self) { rooted_vec!(let mut animation_frame_list); mem::swap( &mut *animation_frame_list, &mut *self.animation_frame_list.borrow_mut(), ); self.running_animation_callbacks.set(true); let was_faking_animation_frames = self.is_faking_animation_frames(); let timing = self.global().performance().Now(); for (_, callback) in animation_frame_list.drain(..) { if let Some(callback) = callback { callback.call(self, *timing); } } self.running_animation_callbacks.set(false); let spurious = !self .window .reflow(ReflowGoal::Full, ReflowReason::RequestAnimationFrame); if spurious && !was_faking_animation_frames { // If the rAF callbacks did not mutate the DOM, then the // reflow call above means that layout will not be invoked, // and therefore no new frame will be sent to the compositor. // If this happens, the compositor will not tick the animation // and the next rAF will never be called! When this happens // for several frames, then the spurious rAF detection below // will kick in and use a timer to tick the callbacks. However, // for the interim frames where we are deciding whether this rAF // is considered spurious, we need to ensure that the layout // and compositor *do* tick the animation. self.window .force_reflow(ReflowGoal::Full, ReflowReason::RequestAnimationFrame); } // Only send the animation change state message after running any callbacks. // This means that if the animation callback adds a new callback for // the next frame (which is the common case), we won't send a NoAnimationCallbacksPresent // message quickly followed by an AnimationCallbacksPresent message. // // If this frame was spurious and we've seen too many spurious frames in a row, tell the // constellation to stop giving us video refresh callbacks, to save energy. (A spurious // animation frame is one in which the callback did not mutate the DOM—that is, an // animation frame that wasn't actually used for animation.) let is_empty = self.animation_frame_list.borrow().is_empty(); if is_empty || (!was_faking_animation_frames && self.is_faking_animation_frames()) { if is_empty { // If the current animation frame list in the DOM instance is empty, // we can reuse the original `Vec` that we put on the stack to // avoid allocating a new one next time an animation callback // is queued. mem::swap( &mut *self.animation_frame_list.borrow_mut(), &mut *animation_frame_list, ); } let event = ScriptMsg::ChangeRunningAnimationsState( AnimationState::NoAnimationCallbacksPresent, ); self.window().send_to_constellation(event); } // Update the counter of spurious animation frames. if spurious { if self.spurious_animation_frames.get() < SPURIOUS_ANIMATION_FRAME_THRESHOLD { self.spurious_animation_frames .set(self.spurious_animation_frames.get() + 1) } } else { self.spurious_animation_frames.set(0) } } pub fn fetch_async( &self, load: LoadType, request: RequestInit, fetch_target: IpcSender, ) { let mut loader = self.loader.borrow_mut(); 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) { // This does not delay the load event anymore. debug!("Document got finish_load: {:?}", load); self.loader.borrow_mut().finish_load(&load); match load { LoadType::Stylesheet(_) => { // A stylesheet finishing to load may unblock any pending // parsing-blocking script or deferred script. self.process_pending_parsing_blocking_script(); // Step 3. self.process_deferred_scripts(); }, LoadType::PageSource(_) => { if self.has_browsing_context { // Disarm the reflow timer and trigger the initial reflow. self.reflow_timeout.set(None); self.upcast::().dirty(NodeDamage::OtherNodeDamage); self.window .reflow(ReflowGoal::Full, ReflowReason::FirstLoad); } // Deferred scripts have to wait for page to finish loading, // this is the first opportunity to process them. // Step 3. self.process_deferred_scripts(); }, _ => {}, } // Step 4 is in another castle, namely at the end of // process_deferred_scripts. // Step 5 can be found in asap_script_loaded and // asap_in_order_script_loaded. let loader = self.loader.borrow(); // Servo measures when the top-level content (not iframes) is loaded. if (self.top_level_dom_complete.get() == 0) && loader.is_only_blocked_by_iframes() { update_with_current_time_ms(&self.top_level_dom_complete); } if loader.is_blocked() || loader.events_inhibited() { // Step 6. return; } ScriptThread::mark_document_with_no_blocked_loads(self); } // https://html.spec.whatwg.org/multipage/#prompt-to-unload-a-document pub fn prompt_to_unload(&self, recursive_flag: bool) -> bool { // TODO: Step 1, increase the event loop's termination nesting level by 1. // Step 2 self.incr_ignore_opens_during_unload_counter(); //Step 3-5. let document = Trusted::new(self); let beforeunload_event = BeforeUnloadEvent::new( &self.window, atom!("beforeunload"), EventBubbles::Bubbles, EventCancelable::Cancelable, ); let event = beforeunload_event.upcast::(); event.set_trusted(true); let event_target = self.window.upcast::(); let has_listeners = event.has_listeners_for(&event_target, &atom!("beforeunload")); event_target.dispatch_event_with_target(document.root().upcast(), &event); // TODO: Step 6, decrease the event loop's termination nesting level by 1. // Step 7 if has_listeners { self.salvageable.set(false); } let mut can_unload = true; // TODO: Step 8, also check sandboxing modals flag. let default_prevented = event.DefaultPrevented(); let return_value_not_empty = !event .downcast::() .unwrap() .ReturnValue() .is_empty(); if default_prevented || return_value_not_empty { let (chan, port) = ipc::channel().expect("Failed to create IPC channel!"); let msg = EmbedderMsg::AllowUnload(chan); self.send_to_embedder(msg); can_unload = port.recv().unwrap(); } // Step 9 if !recursive_flag { for iframe in self.iter_iframes() { // TODO: handle the case of cross origin iframes. let document = document_from_node(&*iframe); can_unload = document.prompt_to_unload(true); if !document.salvageable() { self.salvageable.set(false); } if !can_unload { break; } } } // Step 10 self.decr_ignore_opens_during_unload_counter(); can_unload } // https://html.spec.whatwg.org/multipage/#unload-a-document pub fn unload(&self, recursive_flag: bool, recycle: bool) { // TODO: Step 1, increase the event loop's termination nesting level by 1. // Step 2 self.incr_ignore_opens_during_unload_counter(); let document = Trusted::new(self); // Step 3-6 if self.page_showing.get() { self.page_showing.set(false); let event = PageTransitionEvent::new( &self.window, atom!("pagehide"), false, // bubbles false, // cancelable self.salvageable.get(), // persisted ); let event = event.upcast::(); event.set_trusted(true); let _ = self .window .upcast::() .dispatch_event_with_target(document.root().upcast(), &event); // TODO Step 6, document visibility steps. } // Step 7 if !self.fired_unload.get() { let event = Event::new( &self.window.upcast(), atom!("unload"), EventBubbles::Bubbles, EventCancelable::Cancelable, ); event.set_trusted(true); let event_target = self.window.upcast::(); let has_listeners = event.has_listeners_for(&event_target, &atom!("unload")); let _ = event_target.dispatch_event_with_target(document.root().upcast(), &event); self.fired_unload.set(true); // Step 9 if has_listeners { self.salvageable.set(false); } } // TODO: Step 8, decrease the event loop's termination nesting level by 1. // Step 13 if !recursive_flag { for iframe in self.iter_iframes() { // TODO: handle the case of cross origin iframes. let document = document_from_node(&*iframe); document.unload(true, recycle); if !document.salvageable() { self.salvageable.set(false); } } } // Step 10, 14 if !self.salvageable.get() { // https://html.spec.whatwg.org/multipage/#unloading-document-cleanup-steps let global_scope = self.window.upcast::(); // Step 1 of clean-up steps. global_scope.close_event_sources(); let msg = ScriptMsg::DiscardDocument; let _ = global_scope.script_to_constellation_chan().send(msg); } // Step 15, End self.decr_ignore_opens_during_unload_counter(); } // https://html.spec.whatwg.org/multipage/#the-end pub fn maybe_queue_document_completion(&self) { if self.loader.borrow().is_blocked() { // Step 6. return; } assert!(!self.loader.borrow().events_inhibited()); self.loader.borrow_mut().inhibit_events(); // The rest will ever run only once per document. // Step 7. debug!("Document loads are complete."); let document = Trusted::new(self); self.window .dom_manipulation_task_source() .queue( task!(fire_load_event: move || { let document = document.root(); let window = document.window(); if !window.is_alive() { return; } // Step 7.1. document.set_ready_state(DocumentReadyState::Complete); // Step 7.2. if document.browsing_context().is_none() { return; } let event = Event::new( window.upcast(), atom!("load"), EventBubbles::DoesNotBubble, EventCancelable::NotCancelable, ); event.set_trusted(true); // http://w3c.github.io/navigation-timing/#widl-PerformanceNavigationTiming-loadEventStart update_with_current_time_ms(&document.load_event_start); debug!("About to dispatch load for {:?}", document.url()); // FIXME(nox): Why are errors silenced here? let _ = window.upcast::().dispatch_event_with_target( document.upcast(), &event, ); // http://w3c.github.io/navigation-timing/#widl-PerformanceNavigationTiming-loadEventEnd update_with_current_time_ms(&document.load_event_end); window.reflow(ReflowGoal::Full, ReflowReason::DocumentLoaded); document.notify_constellation_load(); if let Some(fragment) = document.url().fragment() { document.check_and_scroll_fragment(fragment); } }), self.window.upcast(), ) .unwrap(); // Step 8. let document = Trusted::new(self); if document.root().browsing_context().is_some() { self.window .dom_manipulation_task_source() .queue( task!(fire_pageshow_event: move || { let document = document.root(); let window = document.window(); if document.page_showing.get() || !window.is_alive() { return; } document.page_showing.set(true); let event = PageTransitionEvent::new( window, atom!("pageshow"), false, // bubbles false, // cancelable false, // persisted ); let event = event.upcast::(); event.set_trusted(true); // FIXME(nox): Why are errors silenced here? let _ = window.upcast::().dispatch_event_with_target( document.upcast(), &event, ); }), self.window.upcast(), ) .unwrap(); } // 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, script: &HTMLScriptElement, load: Option, ) { assert!(!self.has_pending_parsing_blocking_script()); *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 { self.pending_parsing_blocking_script.borrow().is_some() } /// https://html.spec.whatwg.org/multipage/#prepare-a-script step 22.d. pub fn pending_parsing_blocking_script_loaded( &self, element: &HTMLScriptElement, result: ScriptResult, ) { { let mut blocking_script = self.pending_parsing_blocking_script.borrow_mut(); let entry = blocking_script.as_mut().unwrap(); assert!(&*entry.element == element); entry.loaded(result); } self.process_pending_parsing_blocking_script(); } fn process_pending_parsing_blocking_script(&self) { if self.script_blocking_stylesheets_count.get() > 0 { return; } let pair = self .pending_parsing_blocking_script .borrow_mut() .as_mut() .and_then(PendingScript::take_result); if let Some((element, result)) = pair { *self.pending_parsing_blocking_script.borrow_mut() = None; self.get_current_parser() .unwrap() .resume_with_pending_parsing_blocking_script(&element, result); } } // https://html.spec.whatwg.org/multipage/#set-of-scripts-that-will-execute-as-soon-as-possible pub fn add_asap_script(&self, script: &HTMLScriptElement) { self.asap_scripts_set .borrow_mut() .push(Dom::from_ref(script)); } /// https://html.spec.whatwg.org/multipage/#the-end step 5. /// https://html.spec.whatwg.org/multipage/#prepare-a-script step 22.d. pub fn asap_script_loaded(&self, element: &HTMLScriptElement, result: ScriptResult) { { let mut scripts = self.asap_scripts_set.borrow_mut(); let idx = scripts .iter() .position(|entry| &**entry == element) .unwrap(); scripts.swap_remove(idx); } 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) { self.asap_in_order_scripts_list.push(script); } /// https://html.spec.whatwg.org/multipage/#the-end step 5. /// https://html.spec.whatwg.org/multipage/#prepare-a-script step 22.c. pub fn asap_in_order_script_loaded(&self, element: &HTMLScriptElement, result: ScriptResult) { self.asap_in_order_scripts_list.loaded(element, result); while let Some((element, result)) = self .asap_in_order_scripts_list .take_next_ready_to_be_executed() { element.execute(result); } } // 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) { 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. pub fn deferred_script_loaded(&self, element: &HTMLScriptElement, result: ScriptResult) { self.deferred_scripts.loaded(element, result); self.process_deferred_scripts(); } /// https://html.spec.whatwg.org/multipage/#the-end step 3. fn process_deferred_scripts(&self) { if self.ready_state.get() != DocumentReadyState::Interactive { return; } // Part of substep 1. loop { if self.script_blocking_stylesheets_count.get() > 0 { return; } if let Some((element, result)) = self.deferred_scripts.take_next_ready_to_be_executed() { element.execute(result); } else { break; } } if self.deferred_scripts.is_empty() { // https://html.spec.whatwg.org/multipage/#the-end step 4. self.maybe_dispatch_dom_content_loaded(); } } // https://html.spec.whatwg.org/multipage/#the-end step 4. pub fn maybe_dispatch_dom_content_loaded(&self) { if self.domcontentloaded_dispatched.get() { return; } self.domcontentloaded_dispatched.set(true); assert_ne!( self.ReadyState(), DocumentReadyState::Complete, "Complete before DOMContentLoaded?" ); update_with_current_time_ms(&self.dom_content_loaded_event_start); // Step 4.1. let window = self.window(); window.dom_manipulation_task_source().queue_event( self.upcast(), atom!("DOMContentLoaded"), EventBubbles::Bubbles, EventCancelable::NotCancelable, window, ); window.reflow(ReflowGoal::Full, ReflowReason::DOMContentLoaded); update_with_current_time_ms(&self.dom_content_loaded_event_end); // html parsing has finished - set dom content loaded self.interactive_time .borrow() .maybe_set_tti(self, InteractiveFlag::DOMContentLoaded); // Step 4.2. // TODO: client message queue. } // https://html.spec.whatwg.org/multipage/#abort-a-document pub 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(); let global_scope = self.window.upcast::(); let loads_cancelled = self.loader.borrow_mut().cancel_all_loads(); let event_sources_canceled = global_scope.close_event_sources(); if loads_cancelled || event_sources_canceled { // If any loads were canceled. self.salvageable.set(false); }; // Also Step 2. // Note: the spec says to discard any tasks queued for fetch. // This cancels all tasks on the networking task source, which might be too broad. // See https://github.com/whatwg/html/issues/3837 self.window .cancel_all_tasks_from_source(TaskSourceName::Networking); // Step 3. if let Some(parser) = self.get_current_parser() { parser.abort(); self.salvageable.set(false); } } pub fn notify_constellation_load(&self) { self.window().send_to_constellation(ScriptMsg::LoadComplete); } pub fn set_current_parser(&self, script: Option<&ServoParser>) { self.current_parser.set(script); } pub fn get_current_parser(&self) -> Option> { self.current_parser.get() } pub fn can_invoke_script(&self) -> bool { match self.get_current_parser() { Some(parser) => { // It is safe to run script if the parser is not actively parsing, // or if it is impossible to interact with the token stream. parser.parser_is_not_active() || self.throw_on_dynamic_markup_insertion_counter.get() > 0 }, None => true, } } /// Iterate over all iframes in the document. pub fn iter_iframes(&self) -> impl Iterator> { self.upcast::() .traverse_preorder() .filter_map(DomRoot::downcast::) } /// Find an iframe element in the document. pub fn find_iframe( &self, browsing_context_id: BrowsingContextId, ) -> Option> { self.iter_iframes() .find(|node| node.browsing_context_id() == Some(browsing_context_id)) } pub fn get_dom_loading(&self) -> u64 { self.dom_loading.get() } pub fn get_dom_interactive(&self) -> u64 { self.dom_interactive.get() } pub fn set_navigation_start(&self, navigation_start: u64) { self.interactive_time .borrow_mut() .set_navigation_start(navigation_start); } pub fn get_interactive_metrics(&self) -> Ref { self.interactive_time.borrow() } pub fn has_recorded_tti_metric(&self) -> bool { self.get_interactive_metrics().get_tti().is_some() } pub fn get_dom_content_loaded_event_start(&self) -> u64 { self.dom_content_loaded_event_start.get() } pub fn get_dom_content_loaded_event_end(&self) -> u64 { self.dom_content_loaded_event_end.get() } pub fn get_dom_complete(&self) -> u64 { self.dom_complete.get() } pub fn get_top_level_dom_complete(&self) -> u64 { self.top_level_dom_complete.get() } pub fn get_load_event_start(&self) -> u64 { self.load_event_start.get() } pub fn get_load_event_end(&self) -> u64 { self.load_event_end.get() } pub fn start_tti(&self) { if self.get_interactive_metrics().needs_tti() { self.tti_window.borrow_mut().start_window(); } } /// check tti for this document /// if it's been 10s since this doc encountered a task over 50ms, then we consider the /// main thread available and try to set tti pub fn record_tti_if_necessary(&self) { if self.has_recorded_tti_metric() { return; } if self.tti_window.borrow().needs_check() { self.get_interactive_metrics().maybe_set_tti( self, InteractiveFlag::TimeToInteractive(self.tti_window.borrow().get_start()), ); } } // https://html.spec.whatwg.org/multipage/#fire-a-focus-event fn fire_focus_event( &self, focus_event_type: FocusEventType, node: &Node, related_target: Option<&EventTarget>, ) { let (event_name, does_bubble) = match focus_event_type { FocusEventType::Focus => (DOMString::from("focus"), EventBubbles::DoesNotBubble), FocusEventType::Blur => (DOMString::from("blur"), EventBubbles::DoesNotBubble), }; let event = FocusEvent::new( &self.window, event_name, does_bubble, EventCancelable::NotCancelable, Some(&self.window), 0i32, related_target, ); let event = event.upcast::(); event.set_trusted(true); let target = node.upcast(); event.fire(target); } /// pub fn is_cookie_averse(&self) -> bool { !self.has_browsing_context || !url_has_network_scheme(&self.url()) } pub fn nodes_from_point( &self, client_point: &Point2D, reflow_goal: NodesFromPointQueryType, ) -> Vec { if !self .window .layout_reflow(QueryMsg::NodesFromPointQuery(*client_point, reflow_goal)) { return vec![]; }; self.window.layout().nodes_from_point_response() } /// pub fn lookup_custom_element_definition( &self, namespace: &Namespace, local_name: &LocalName, is: Option<&LocalName>, ) -> Option> { if !PREFS .get("dom.customelements.enabled") .as_boolean() .unwrap_or(false) { return None; } // Step 1 if *namespace != ns!(html) { return None; } // Step 2 if !self.has_browsing_context { return None; } // Step 3 let registry = self.window.CustomElements(); registry.lookup_definition(local_name, is) } pub fn increment_throw_on_dynamic_markup_insertion_counter(&self) { let counter = self.throw_on_dynamic_markup_insertion_counter.get(); self.throw_on_dynamic_markup_insertion_counter .set(counter + 1); } pub fn decrement_throw_on_dynamic_markup_insertion_counter(&self) { let counter = self.throw_on_dynamic_markup_insertion_counter.get(); self.throw_on_dynamic_markup_insertion_counter .set(counter - 1); } pub fn react_to_environment_changes(&self) { for image in self.responsive_images.borrow().iter() { image.react_to_environment_changes(); } } pub fn register_responsive_image(&self, img: &HTMLImageElement) { self.responsive_images.borrow_mut().push(Dom::from_ref(img)); } pub fn unregister_responsive_image(&self, img: &HTMLImageElement) { let index = self .responsive_images .borrow() .iter() .position(|x| **x == *img); if let Some(i) = index { self.responsive_images.borrow_mut().remove(i); } } } #[derive(MallocSizeOf, PartialEq)] pub enum DocumentSource { FromParser, NotFromParser, } #[allow(unsafe_code)] pub trait LayoutDocumentHelpers { unsafe fn is_html_document_for_layout(&self) -> bool; unsafe fn drain_pending_restyles(&self) -> Vec<(LayoutDom, PendingRestyle)>; unsafe fn needs_paint_from_layout(&self); unsafe fn will_paint(&self); unsafe fn quirks_mode(&self) -> QuirksMode; unsafe fn style_shared_lock(&self) -> &StyleSharedRwLock; } #[allow(unsafe_code)] impl LayoutDocumentHelpers for LayoutDom { #[inline] unsafe fn is_html_document_for_layout(&self) -> bool { (*self.unsafe_get()).is_html_document } #[inline] #[allow(unrooted_must_root)] unsafe fn drain_pending_restyles(&self) -> Vec<(LayoutDom, PendingRestyle)> { let mut elements = (*self.unsafe_get()) .pending_restyles .borrow_mut_for_layout(); // Elements were in a document when they were adding to this list, but that // may no longer be true when the next layout occurs. let result = elements .drain() .map(|(k, v)| (k.to_layout(), v)) .filter(|&(ref k, _)| k.upcast::().get_flag(NodeFlags::IS_IN_DOC)) .collect(); result } #[inline] unsafe fn needs_paint_from_layout(&self) { (*self.unsafe_get()).needs_paint.set(true) } #[inline] unsafe fn will_paint(&self) { (*self.unsafe_get()).needs_paint.set(false) } #[inline] unsafe fn quirks_mode(&self) -> QuirksMode { (*self.unsafe_get()).quirks_mode() } #[inline] unsafe fn style_shared_lock(&self) -> &StyleSharedRwLock { (*self.unsafe_get()).style_shared_lock() } } // https://html.spec.whatwg.org/multipage/#is-a-registrable-domain-suffix-of-or-is-equal-to // The spec says to return a bool, we actually return an Option containing // the parsed host in the successful case, to avoid having to re-parse the host. fn get_registrable_domain_suffix_of_or_is_equal_to( host_suffix_string: &str, original_host: Host, ) -> Option { // Step 1 if host_suffix_string.is_empty() { return None; } // Step 2-3. let host = match Host::parse(host_suffix_string) { Ok(host) => host, Err(_) => return None, }; // Step 4. if host != original_host { // Step 4.1 let host = match host { Host::Domain(ref host) => host, _ => return None, }; let original_host = match original_host { Host::Domain(ref original_host) => original_host, _ => return None, }; // Step 4.2 let index = original_host.len().checked_sub(host.len())?; let (prefix, suffix) = original_host.split_at(index); if !prefix.ends_with(".") { return None; } if suffix != host { return None; } // Step 4.3 if is_pub_domain(host) { return None; } } // Step 5 Some(host) } /// fn url_has_network_scheme(url: &ServoUrl) -> bool { match url.scheme() { "ftp" | "http" | "https" => true, _ => false, } } #[derive(Clone, Copy, Eq, JSTraceable, MallocSizeOf, PartialEq)] pub enum HasBrowsingContext { No, Yes, } impl Document { pub fn new_inherited( window: &Window, has_browsing_context: HasBrowsingContext, url: Option, origin: MutableOrigin, is_html_document: IsHTMLDocument, content_type: Option, last_modified: Option, activity: DocumentActivity, source: DocumentSource, doc_loader: DocumentLoader, referrer: Option, referrer_policy: Option, canceller: FetchCanceller, ) -> Document { let url = url.unwrap_or_else(|| ServoUrl::parse("about:blank").unwrap()); let (ready_state, domcontentloaded_dispatched) = if source == DocumentSource::FromParser { (DocumentReadyState::Loading, false) } else { (DocumentReadyState::Complete, true) }; let interactive_time = InteractiveMetrics::new(window.time_profiler_chan().clone(), url.clone()); Document { node: Node::new_document_node(), window: Dom::from_ref(window), has_browsing_context: has_browsing_context == HasBrowsingContext::Yes, implementation: Default::default(), content_type: match content_type { Some(mime_data) => mime_data, None => match is_html_document { // https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument IsHTMLDocument::HTMLDocument => mime::TEXT_HTML, // https://dom.spec.whatwg.org/#concept-document-content-type IsHTMLDocument::NonHTMLDocument => "application/xml".parse().unwrap(), }, }, last_modified: last_modified, url: DomRefCell::new(url), // https://dom.spec.whatwg.org/#concept-document-quirks quirks_mode: Cell::new(QuirksMode::NoQuirks), // https://dom.spec.whatwg.org/#concept-document-encoding encoding: Cell::new(UTF_8), is_html_document: is_html_document == IsHTMLDocument::HTMLDocument, activity: Cell::new(activity), id_map: DomRefCell::new(HashMap::new()), tag_map: DomRefCell::new(HashMap::new()), tagns_map: DomRefCell::new(HashMap::new()), classes_map: DomRefCell::new(HashMap::new()), images: Default::default(), embeds: Default::default(), links: Default::default(), forms: Default::default(), scripts: Default::default(), anchors: Default::default(), applets: Default::default(), style_shared_lock: { lazy_static! { /// Per-process shared lock for author-origin stylesheets /// /// FIXME: make it per-document or per-pipeline instead: /// /// (Need to figure out what to do with the style attribute /// of elements adopted into another document.) static ref PER_PROCESS_AUTHOR_SHARED_LOCK: StyleSharedRwLock = { StyleSharedRwLock::new() }; } PER_PROCESS_AUTHOR_SHARED_LOCK.clone() //StyleSharedRwLock::new() }, stylesheets: DomRefCell::new(DocumentStylesheetSet::new()), stylesheet_list: MutNullableDom::new(None), ready_state: Cell::new(ready_state), domcontentloaded_dispatched: Cell::new(domcontentloaded_dispatched), possibly_focused: Default::default(), focused: Default::default(), current_script: Default::default(), pending_parsing_blocking_script: Default::default(), script_blocking_stylesheets_count: Cell::new(0u32), deferred_scripts: Default::default(), asap_in_order_scripts_list: Default::default(), asap_scripts_set: Default::default(), scripting_enabled: has_browsing_context == HasBrowsingContext::Yes, animation_frame_ident: Cell::new(0), animation_frame_list: DomRefCell::new(vec![]), running_animation_callbacks: Cell::new(false), loader: DomRefCell::new(doc_loader), current_parser: Default::default(), reflow_timeout: Cell::new(None), base_element: Default::default(), appropriate_template_contents_owner_document: Default::default(), pending_restyles: DomRefCell::new(HashMap::new()), needs_paint: Cell::new(false), active_touch_points: DomRefCell::new(Vec::new()), dom_loading: Cell::new(Default::default()), dom_interactive: Cell::new(Default::default()), dom_content_loaded_event_start: Cell::new(Default::default()), dom_content_loaded_event_end: Cell::new(Default::default()), dom_complete: Cell::new(Default::default()), top_level_dom_complete: Cell::new(Default::default()), load_event_start: Cell::new(Default::default()), load_event_end: Cell::new(Default::default()), https_state: Cell::new(HttpsState::None), origin: origin, referrer: referrer, referrer_policy: Cell::new(referrer_policy), target_element: MutNullableDom::new(None), last_click_info: DomRefCell::new(None), ignore_destructive_writes_counter: Default::default(), ignore_opens_during_unload_counter: Default::default(), spurious_animation_frames: Cell::new(0), dom_count: Cell::new(1), fullscreen_element: MutNullableDom::new(None), form_id_listener_map: Default::default(), interactive_time: DomRefCell::new(interactive_time), tti_window: DomRefCell::new(InteractiveWindow::new()), canceller: canceller, throw_on_dynamic_markup_insertion_counter: Cell::new(0), page_showing: Cell::new(false), salvageable: Cell::new(true), fired_unload: Cell::new(false), responsive_images: Default::default(), } } // https://dom.spec.whatwg.org/#dom-document-document pub fn Constructor(window: &Window) -> Fallible> { let doc = window.Document(); let docloader = DocumentLoader::new(&*doc.loader()); Ok(Document::new( window, HasBrowsingContext::No, None, doc.origin().clone(), IsHTMLDocument::NonHTMLDocument, None, None, DocumentActivity::Inactive, DocumentSource::NotFromParser, docloader, None, None, Default::default(), )) } pub fn new( window: &Window, has_browsing_context: HasBrowsingContext, url: Option, origin: MutableOrigin, doctype: IsHTMLDocument, content_type: Option, last_modified: Option, activity: DocumentActivity, source: DocumentSource, doc_loader: DocumentLoader, referrer: Option, referrer_policy: Option, canceller: FetchCanceller, ) -> DomRoot { let document = reflect_dom_object( Box::new(Document::new_inherited( window, has_browsing_context, url, origin, doctype, content_type, last_modified, activity, source, doc_loader, referrer, referrer_policy, canceller, )), window, DocumentBinding::Wrap, ); { let node = document.upcast::(); node.set_owner_doc(&document); } document } fn create_node_list bool>(&self, callback: F) -> DomRoot { let doc = self.GetDocumentElement(); let maybe_node = doc.r().map(Castable::upcast::); let iter = maybe_node .iter() .flat_map(|node| node.traverse_preorder()) .filter(|node| callback(&node)); NodeList::new_simple_list(&self.window, iter) } fn get_html_element(&self) -> Option> { self.GetDocumentElement().and_then(DomRoot::downcast) } /// Return a reference to the per-document shared lock used in stylesheets. pub fn style_shared_lock(&self) -> &StyleSharedRwLock { &self.style_shared_lock } /// Flushes the stylesheet list, and returns whether any stylesheet changed. pub fn flush_stylesheets_for_reflow(&self) -> bool { // NOTE(emilio): The invalidation machinery is used on the replicated // list on the layout thread. // // FIXME(emilio): This really should differentiate between CSSOM changes // and normal stylesheets additions / removals, because in the last case // the layout thread already has that information and we could avoid // dirtying the whole thing. let mut stylesheets = self.stylesheets.borrow_mut(); let have_changed = stylesheets.has_changed(); stylesheets.flush_without_invalidation(); have_changed } /// Returns a `Device` suitable for media query evaluation. /// /// FIXME(emilio): This really needs to be somehow more in sync with layout. /// Feels like a hack. /// /// Also, shouldn't return an option, I'm quite sure. pub fn device(&self) -> Option { let window_size = self.window().window_size()?; let viewport_size = window_size.initial_viewport; let device_pixel_ratio = window_size.device_pixel_ratio; Some(Device::new( MediaType::screen(), viewport_size, device_pixel_ratio, )) } /// Remove a stylesheet owned by `owner` from the list of document sheets. #[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily. pub fn remove_stylesheet(&self, owner: &Element, s: &Arc) { self.window() .layout_chan() .send(Msg::RemoveStylesheet(s.clone())) .unwrap(); let guard = s.shared_lock.read(); // FIXME(emilio): Would be nice to remove the clone, etc. self.stylesheets.borrow_mut().remove_stylesheet( None, StyleSheetInDocument { sheet: s.clone(), owner: Dom::from_ref(owner), }, &guard, ); } /// Add a stylesheet owned by `owner` to the list of document sheets, in the /// correct tree position. #[allow(unrooted_must_root)] // Owner needs to be rooted already necessarily. pub fn add_stylesheet(&self, owner: &Element, sheet: Arc) { // FIXME(emilio): It'd be nice to unify more code between the elements // that own stylesheets, but StylesheetOwner is more about loading // them... debug_assert!( owner.as_stylesheet_owner().is_some() || owner.is::(), "Wat" ); let mut stylesheets = self.stylesheets.borrow_mut(); let insertion_point = stylesheets .iter() .map(|(sheet, _origin)| sheet) .find(|sheet_in_doc| { owner .upcast::() .is_before(sheet_in_doc.owner.upcast()) }) .cloned(); self.window() .layout_chan() .send(Msg::AddStylesheet( sheet.clone(), insertion_point.as_ref().map(|s| s.sheet.clone()), )) .unwrap(); let sheet = StyleSheetInDocument { sheet, owner: Dom::from_ref(owner), }; let lock = self.style_shared_lock(); let guard = lock.read(); match insertion_point { Some(ip) => { stylesheets.insert_stylesheet_before(None, sheet, ip, &guard); }, None => { stylesheets.append_stylesheet(None, sheet, &guard); }, } } /// Returns the number of document stylesheets. pub fn stylesheet_count(&self) -> usize { self.stylesheets.borrow().len() } pub fn salvageable(&self) -> bool { self.salvageable.get() } pub fn stylesheet_at(&self, index: usize) -> Option> { let stylesheets = self.stylesheets.borrow(); stylesheets .get(Origin::Author, index) .and_then(|s| s.owner.upcast::().get_cssom_stylesheet()) } /// pub fn appropriate_template_contents_owner_document(&self) -> DomRoot { self.appropriate_template_contents_owner_document .or_init(|| { let doctype = if self.is_html_document { IsHTMLDocument::HTMLDocument } else { IsHTMLDocument::NonHTMLDocument }; let new_doc = Document::new( self.window(), HasBrowsingContext::No, None, // https://github.com/whatwg/html/issues/2109 MutableOrigin::new(ImmutableOrigin::new_opaque()), doctype, None, None, DocumentActivity::Inactive, DocumentSource::NotFromParser, DocumentLoader::new(&self.loader()), None, None, Default::default(), ); new_doc .appropriate_template_contents_owner_document .set(Some(&new_doc)); new_doc }) } pub fn get_element_by_id(&self, id: &Atom) -> Option> { self.id_map .borrow() .get(&id) .map(|ref elements| DomRoot::from_ref(&*(*elements)[0])) } pub fn ensure_pending_restyle(&self, el: &Element) -> RefMut { let map = self.pending_restyles.borrow_mut(); RefMut::map(map, |m| { m.entry(Dom::from_ref(el)) .or_insert_with(PendingRestyle::new) }) } pub fn element_state_will_change(&self, el: &Element) { let mut entry = self.ensure_pending_restyle(el); if entry.snapshot.is_none() { entry.snapshot = Some(Snapshot::new(el.html_element_in_html_document())); } let snapshot = entry.snapshot.as_mut().unwrap(); if snapshot.state.is_none() { snapshot.state = Some(el.state()); } } pub fn element_attr_will_change(&self, el: &Element, attr: &Attr) { // FIXME(emilio): Kind of a shame we have to duplicate this. // // I'm getting rid of the whole hashtable soon anyway, since all it does // right now is populate the element restyle data in layout, and we // could in theory do it in the DOM I think. let mut entry = self.ensure_pending_restyle(el); if entry.snapshot.is_none() { entry.snapshot = Some(Snapshot::new(el.html_element_in_html_document())); } if attr.local_name() == &local_name!("style") { entry.hint.insert(RestyleHint::RESTYLE_STYLE_ATTRIBUTE); } if vtable_for(el.upcast()).attribute_affects_presentational_hints(attr) { entry.hint.insert(RestyleHint::RESTYLE_SELF); } let snapshot = entry.snapshot.as_mut().unwrap(); if attr.local_name() == &local_name!("id") { snapshot.id_changed = true; } else if attr.local_name() == &local_name!("class") { snapshot.class_changed = true; } else { snapshot.other_attributes_changed = true; } if snapshot.attrs.is_none() { let attrs = el .attrs() .iter() .map(|attr| (attr.identifier().clone(), attr.value().clone())) .collect(); snapshot.attrs = Some(attrs); } } pub fn set_referrer_policy(&self, policy: Option) { self.referrer_policy.set(policy); } //TODO - default still at no-referrer pub fn get_referrer_policy(&self) -> Option { return self.referrer_policy.get(); } pub fn set_target_element(&self, node: Option<&Element>) { if let Some(ref element) = self.target_element.get() { element.set_target_state(false); } self.target_element.set(node); if let Some(ref element) = self.target_element.get() { element.set_target_state(true); } self.window .reflow(ReflowGoal::Full, ReflowReason::ElementStateChanged); } pub fn incr_ignore_destructive_writes_counter(&self) { self.ignore_destructive_writes_counter .set(self.ignore_destructive_writes_counter.get() + 1); } pub fn decr_ignore_destructive_writes_counter(&self) { self.ignore_destructive_writes_counter .set(self.ignore_destructive_writes_counter.get() - 1); } pub fn is_prompting_or_unloading(&self) -> bool { self.ignore_opens_during_unload_counter.get() > 0 } fn incr_ignore_opens_during_unload_counter(&self) { self.ignore_opens_during_unload_counter .set(self.ignore_opens_during_unload_counter.get() + 1); } fn decr_ignore_opens_during_unload_counter(&self) { self.ignore_opens_during_unload_counter .set(self.ignore_opens_during_unload_counter.get() - 1); } /// Whether we've seen so many spurious animation frames (i.e. animation frames that didn't /// mutate the DOM) that we've decided to fall back to fake ones. fn is_faking_animation_frames(&self) -> bool { self.spurious_animation_frames.get() >= SPURIOUS_ANIMATION_FRAME_THRESHOLD } // https://fullscreen.spec.whatwg.org/#dom-element-requestfullscreen #[allow(unrooted_must_root)] pub fn enter_fullscreen(&self, pending: &Element) -> Rc { // Step 1 let promise = Promise::new(self.global().r()); let mut error = false; // Step 4 // check namespace match *pending.namespace() { ns!(mathml) => { if pending.local_name().as_ref() != "math" { error = true; } }, ns!(svg) => { if pending.local_name().as_ref() != "svg" { error = true; } }, ns!(html) => (), _ => error = true, } // fullscreen element ready check if !pending.fullscreen_element_ready_check() { error = true; } // TODO fullscreen is supported // TODO This algorithm is allowed to request fullscreen. // Step 5 Parallel start let window = self.window(); // Step 6 if !error { let event = EmbedderMsg::SetFullscreenState(true); self.send_to_embedder(event); } let pipeline_id = self.window().pipeline_id(); // Step 7 let trusted_pending = Trusted::new(pending); let trusted_promise = TrustedPromise::new(promise.clone()); let handler = ElementPerformFullscreenEnter::new(trusted_pending, trusted_promise, error); // NOTE: This steps should be running in parallel // https://fullscreen.spec.whatwg.org/#dom-element-requestfullscreen let script_msg = CommonScriptMsg::Task( ScriptThreadEventCategory::EnterFullscreen, handler, pipeline_id, TaskSourceName::DOMManipulation, ); let msg = MainThreadScriptMsg::Common(script_msg); window.main_thread_script_chan().send(msg).unwrap(); promise } // https://fullscreen.spec.whatwg.org/#exit-fullscreen #[allow(unrooted_must_root)] pub fn exit_fullscreen(&self) -> Rc { let global = self.global(); // Step 1 let promise = Promise::new(global.r()); // Step 2 if self.fullscreen_element.get().is_none() { promise.reject_error(Error::Type(String::from("fullscreen is null"))); return promise; } // TODO Step 3-6 let element = self.fullscreen_element.get().unwrap(); // Step 7 Parallel start let window = self.window(); // Step 8 let event = EmbedderMsg::SetFullscreenState(true); self.send_to_embedder(event); // Step 9 let trusted_element = Trusted::new(element.r()); let trusted_promise = TrustedPromise::new(promise.clone()); let handler = ElementPerformFullscreenExit::new(trusted_element, trusted_promise); let pipeline_id = Some(global.pipeline_id()); // NOTE: This steps should be running in parallel // https://fullscreen.spec.whatwg.org/#exit-fullscreen let script_msg = CommonScriptMsg::Task( ScriptThreadEventCategory::ExitFullscreen, handler, pipeline_id, TaskSourceName::DOMManipulation, ); let msg = MainThreadScriptMsg::Common(script_msg); window.main_thread_script_chan().send(msg).unwrap(); promise } pub fn set_fullscreen_element(&self, element: Option<&Element>) { self.fullscreen_element.set(element); } pub fn get_allow_fullscreen(&self) -> bool { // https://html.spec.whatwg.org/multipage/#allowed-to-use match self.browsing_context() { // Step 1 None => false, Some(_) => { // Step 2 let window = self.window(); if window.is_top_level() { true } else { // Step 3 window.GetFrameElement().map_or(false, |el| { el.has_attribute(&local_name!("allowfullscreen")) }) } }, } } fn reset_form_owner_for_listeners(&self, id: &Atom) { let map = self.form_id_listener_map.borrow(); if let Some(listeners) = map.get(id) { for listener in listeners { listener .r() .as_maybe_form_control() .expect("Element must be a form control") .reset_form_owner(); } } } } impl Element { fn click_event_filter_by_disabled_state(&self) -> bool { let node = self.upcast::(); match node.type_id() { NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLButtonElement)) | NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLInputElement)) | // NodeTypeId::Element(ElementTypeId::HTMLKeygenElement) | NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLOptionElement)) | NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLSelectElement)) | NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTextAreaElement)) if self.disabled_state() => true, _ => false, } } } impl ProfilerMetadataFactory for Document { fn new_metadata(&self) -> Option { Some(TimerMetadata { url: String::from(self.url().as_str()), iframe: TimerMetadataFrameType::RootWindow, incremental: TimerMetadataReflowType::Incremental, }) } } impl DocumentMethods for Document { // https://drafts.csswg.org/cssom/#dom-document-stylesheets fn StyleSheets(&self) -> DomRoot { self.stylesheet_list .or_init(|| StyleSheetList::new(&self.window, Dom::from_ref(&self))) } // https://dom.spec.whatwg.org/#dom-document-implementation fn Implementation(&self) -> DomRoot { self.implementation.or_init(|| DOMImplementation::new(self)) } // https://dom.spec.whatwg.org/#dom-document-url fn URL(&self) -> USVString { USVString(String::from(self.url().as_str())) } // https://html.spec.whatwg.org/multipage/#dom-document-activeelement fn GetActiveElement(&self) -> Option> { // TODO: Step 2. match self.get_focused_element() { Some(element) => Some(element), // Step 3. and 4. None => match self.GetBody() { // Step 5. Some(body) => Some(DomRoot::upcast(body)), None => self.GetDocumentElement(), }, } } // https://html.spec.whatwg.org/multipage/#dom-document-hasfocus fn HasFocus(&self) -> bool { // Step 1-2. if self.window().parent_info().is_none() && self.is_fully_active() { return true; } // TODO Step 3. false } // https://html.spec.whatwg.org/multipage/#dom-document-domain fn Domain(&self) -> DOMString { // Step 1. if !self.has_browsing_context { return DOMString::new(); } // Step 2. match self.origin.effective_domain() { // Step 3. None => DOMString::new(), // Step 4. Some(Host::Domain(domain)) => DOMString::from(domain), Some(host) => DOMString::from(host.to_string()), } } // https://html.spec.whatwg.org/multipage/#dom-document-domain fn SetDomain(&self, value: DOMString) -> ErrorResult { // Step 1. if !self.has_browsing_context { return Err(Error::Security); } // TODO: Step 2. "If this Document object's active sandboxing // flag set has its sandboxed document.domain browsing context // flag set, then throw a "SecurityError" DOMException." // Steps 3-4. let effective_domain = match self.origin.effective_domain() { Some(effective_domain) => effective_domain, None => return Err(Error::Security), }; // Step 5 let host = match get_registrable_domain_suffix_of_or_is_equal_to(&*value, effective_domain) { None => return Err(Error::Security), Some(host) => host, }; // Step 6 self.origin.set_domain(host); Ok(()) } // https://html.spec.whatwg.org/multipage/#dom-document-referrer fn Referrer(&self) -> DOMString { match self.referrer { Some(ref referrer) => DOMString::from(referrer.to_string()), None => DOMString::new(), } } // https://dom.spec.whatwg.org/#dom-document-documenturi fn DocumentURI(&self) -> USVString { self.URL() } // https://dom.spec.whatwg.org/#dom-document-compatmode fn CompatMode(&self) -> DOMString { DOMString::from(match self.quirks_mode.get() { QuirksMode::LimitedQuirks | QuirksMode::NoQuirks => "CSS1Compat", QuirksMode::Quirks => "BackCompat", }) } // https://dom.spec.whatwg.org/#dom-document-characterset fn CharacterSet(&self) -> DOMString { DOMString::from(self.encoding.get().name()) } // https://dom.spec.whatwg.org/#dom-document-charset fn Charset(&self) -> DOMString { self.CharacterSet() } // https://dom.spec.whatwg.org/#dom-document-inputencoding fn InputEncoding(&self) -> DOMString { self.CharacterSet() } // https://dom.spec.whatwg.org/#dom-document-content_type fn ContentType(&self) -> DOMString { DOMString::from(self.content_type.to_string()) } // https://dom.spec.whatwg.org/#dom-document-doctype fn GetDoctype(&self) -> Option> { self.upcast::() .children() .filter_map(DomRoot::downcast) .next() } // https://dom.spec.whatwg.org/#dom-document-documentelement fn GetDocumentElement(&self) -> Option> { self.upcast::().child_elements().next() } // https://dom.spec.whatwg.org/#dom-document-getelementsbytagname fn GetElementsByTagName(&self, qualified_name: DOMString) -> DomRoot { let qualified_name = LocalName::from(&*qualified_name); match self.tag_map.borrow_mut().entry(qualified_name.clone()) { Occupied(entry) => DomRoot::from_ref(entry.get()), Vacant(entry) => { let result = HTMLCollection::by_qualified_name(&self.window, self.upcast(), qualified_name); entry.insert(Dom::from_ref(&*result)); result }, } } // https://dom.spec.whatwg.org/#dom-document-getelementsbytagnamens fn GetElementsByTagNameNS( &self, maybe_ns: Option, tag_name: DOMString, ) -> DomRoot { let ns = namespace_from_domstring(maybe_ns); let local = LocalName::from(tag_name); let qname = QualName::new(None, ns, local); match self.tagns_map.borrow_mut().entry(qname.clone()) { Occupied(entry) => DomRoot::from_ref(entry.get()), Vacant(entry) => { let result = HTMLCollection::by_qual_tag_name(&self.window, self.upcast(), qname); entry.insert(Dom::from_ref(&*result)); result }, } } // https://dom.spec.whatwg.org/#dom-document-getelementsbyclassname fn GetElementsByClassName(&self, classes: DOMString) -> DomRoot { let class_atoms: Vec = split_html_space_chars(&classes).map(Atom::from).collect(); match self.classes_map.borrow_mut().entry(class_atoms.clone()) { Occupied(entry) => DomRoot::from_ref(entry.get()), Vacant(entry) => { let result = HTMLCollection::by_atomic_class_name(&self.window, self.upcast(), class_atoms); entry.insert(Dom::from_ref(&*result)); result }, } } // https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid fn GetElementById(&self, id: DOMString) -> Option> { self.get_element_by_id(&Atom::from(id)) } // https://dom.spec.whatwg.org/#dom-document-createelement fn CreateElement( &self, mut local_name: DOMString, options: &ElementCreationOptions, ) -> Fallible> { if xml_name_type(&local_name) == InvalidXMLName { debug!("Not a valid element name"); return Err(Error::InvalidCharacter); } if self.is_html_document { local_name.make_ascii_lowercase(); } let is_xhtml = self.content_type.type_() == mime::APPLICATION && self.content_type.subtype().as_str() == "xhtml" && self.content_type.suffix() == Some(mime::XML); let ns = if self.is_html_document || is_xhtml { ns!(html) } else { ns!() }; let name = QualName::new(None, ns, LocalName::from(local_name)); let is = options.is.as_ref().map(|is| LocalName::from(&**is)); Ok(Element::create( name, is, self, ElementCreator::ScriptCreated, CustomElementCreationMode::Synchronous, )) } // https://dom.spec.whatwg.org/#dom-document-createelementns fn CreateElementNS( &self, namespace: Option, qualified_name: DOMString, options: &ElementCreationOptions, ) -> Fallible> { let (namespace, prefix, local_name) = validate_and_extract(namespace, &qualified_name)?; let name = QualName::new(prefix, namespace, local_name); let is = options.is.as_ref().map(|is| LocalName::from(&**is)); Ok(Element::create( name, is, self, ElementCreator::ScriptCreated, CustomElementCreationMode::Synchronous, )) } // https://dom.spec.whatwg.org/#dom-document-createattribute fn CreateAttribute(&self, mut local_name: DOMString) -> Fallible> { if xml_name_type(&local_name) == InvalidXMLName { debug!("Not a valid element name"); return Err(Error::InvalidCharacter); } if self.is_html_document { local_name.make_ascii_lowercase(); } let name = LocalName::from(local_name); let value = AttrValue::String("".to_owned()); Ok(Attr::new( &self.window, name.clone(), value, name, ns!(), None, None, )) } // https://dom.spec.whatwg.org/#dom-document-createattributens fn CreateAttributeNS( &self, namespace: Option, qualified_name: DOMString, ) -> Fallible> { let (namespace, prefix, local_name) = validate_and_extract(namespace, &qualified_name)?; let value = AttrValue::String("".to_owned()); let qualified_name = LocalName::from(qualified_name); Ok(Attr::new( &self.window, local_name, value, qualified_name, namespace, prefix, None, )) } // https://dom.spec.whatwg.org/#dom-document-createdocumentfragment fn CreateDocumentFragment(&self) -> DomRoot { DocumentFragment::new(self) } // https://dom.spec.whatwg.org/#dom-document-createtextnode fn CreateTextNode(&self, data: DOMString) -> DomRoot { Text::new(data, self) } // https://dom.spec.whatwg.org/#dom-document-createcomment fn CreateComment(&self, data: DOMString) -> DomRoot { Comment::new(data, self) } // https://dom.spec.whatwg.org/#dom-document-createprocessinginstruction fn CreateProcessingInstruction( &self, target: DOMString, data: DOMString, ) -> Fallible> { // Step 1. if xml_name_type(&target) == InvalidXMLName { return Err(Error::InvalidCharacter); } // Step 2. if data.contains("?>") { return Err(Error::InvalidCharacter); } // Step 3. Ok(ProcessingInstruction::new(target, data, self)) } // https://dom.spec.whatwg.org/#dom-document-importnode fn ImportNode(&self, node: &Node, deep: bool) -> Fallible> { // Step 1. if node.is::() { return Err(Error::NotSupported); } // Step 2. let clone_children = if deep { CloneChildrenFlag::CloneChildren } else { CloneChildrenFlag::DoNotCloneChildren }; Ok(Node::clone(node, Some(self), clone_children)) } // https://dom.spec.whatwg.org/#dom-document-adoptnode fn AdoptNode(&self, node: &Node) -> Fallible> { // Step 1. if node.is::() { return Err(Error::NotSupported); } // Step 2. Node::adopt(node, self); // Step 3. Ok(DomRoot::from_ref(node)) } // https://dom.spec.whatwg.org/#dom-document-createevent fn CreateEvent(&self, mut interface: DOMString) -> Fallible> { interface.make_ascii_lowercase(); match &*interface { "beforeunloadevent" => Ok(DomRoot::upcast(BeforeUnloadEvent::new_uninitialized( &self.window, ))), "closeevent" => Ok(DomRoot::upcast(CloseEvent::new_uninitialized( self.window.upcast(), ))), "customevent" => Ok(DomRoot::upcast(CustomEvent::new_uninitialized( self.window.upcast(), ))), "errorevent" => Ok(DomRoot::upcast(ErrorEvent::new_uninitialized( self.window.upcast(), ))), "events" | "event" | "htmlevents" | "svgevents" => { Ok(Event::new_uninitialized(&self.window.upcast())) }, "focusevent" => Ok(DomRoot::upcast(FocusEvent::new_uninitialized(&self.window))), "hashchangeevent" => Ok(DomRoot::upcast(HashChangeEvent::new_uninitialized( &self.window, ))), "keyboardevent" => Ok(DomRoot::upcast(KeyboardEvent::new_uninitialized( &self.window, ))), "messageevent" => Ok(DomRoot::upcast(MessageEvent::new_uninitialized( self.window.upcast(), ))), "mouseevent" | "mouseevents" => { Ok(DomRoot::upcast(MouseEvent::new_uninitialized(&self.window))) }, "pagetransitionevent" => Ok(DomRoot::upcast(PageTransitionEvent::new_uninitialized( &self.window, ))), "popstateevent" => Ok(DomRoot::upcast(PopStateEvent::new_uninitialized( &self.window, ))), "progressevent" => Ok(DomRoot::upcast(ProgressEvent::new_uninitialized( self.window.upcast(), ))), "storageevent" => Ok(DomRoot::upcast(StorageEvent::new_uninitialized( &self.window, "".into(), ))), "touchevent" => Ok(DomRoot::upcast(TouchEvent::new_uninitialized( &self.window, &TouchList::new(&self.window, &[]), &TouchList::new(&self.window, &[]), &TouchList::new(&self.window, &[]), ))), "uievent" | "uievents" => Ok(DomRoot::upcast(UIEvent::new_uninitialized(&self.window))), "webglcontextevent" => Ok(DomRoot::upcast(WebGLContextEvent::new_uninitialized( &self.window, ))), _ => Err(Error::NotSupported), } } // https://html.spec.whatwg.org/multipage/#dom-document-lastmodified fn LastModified(&self) -> DOMString { match self.last_modified { Some(ref t) => DOMString::from(t.clone()), None => DOMString::from( time::now() .strftime("%m/%d/%Y %H:%M:%S") .unwrap() .to_string(), ), } } // https://dom.spec.whatwg.org/#dom-document-createrange fn CreateRange(&self) -> DomRoot { Range::new_with_doc(self) } // https://dom.spec.whatwg.org/#dom-document-createnodeiteratorroot-whattoshow-filter fn CreateNodeIterator( &self, root: &Node, what_to_show: u32, filter: Option>, ) -> DomRoot { NodeIterator::new(self, root, what_to_show, filter) } // https://w3c.github.io/touch-events/#idl-def-Document fn CreateTouch( &self, window: &Window, target: &EventTarget, identifier: i32, page_x: Finite, page_y: Finite, screen_x: Finite, screen_y: Finite, ) -> DomRoot { let client_x = Finite::wrap(*page_x - window.PageXOffset() as f64); let client_y = Finite::wrap(*page_y - window.PageYOffset() as f64); Touch::new( window, identifier, target, screen_x, screen_y, client_x, client_y, page_x, page_y, ) } // https://w3c.github.io/touch-events/#idl-def-document-createtouchlist(touch...) fn CreateTouchList(&self, touches: &[&Touch]) -> DomRoot { TouchList::new(&self.window, &touches) } // https://dom.spec.whatwg.org/#dom-document-createtreewalker fn CreateTreeWalker( &self, root: &Node, what_to_show: u32, filter: Option>, ) -> DomRoot { TreeWalker::new(self, root, what_to_show, filter) } // https://html.spec.whatwg.org/multipage/#document.title fn Title(&self) -> DOMString { let title = self.GetDocumentElement().and_then(|root| { if root.namespace() == &ns!(svg) && root.local_name() == &local_name!("svg") { // Step 1. root.upcast::() .child_elements() .find(|node| { node.namespace() == &ns!(svg) && node.local_name() == &local_name!("title") }) .map(DomRoot::upcast::) } else { // Step 2. root.upcast::() .traverse_preorder() .find(|node| node.is::()) } }); match title { None => DOMString::new(), Some(ref title) => { // Steps 3-4. let value = title.child_text_content(); DOMString::from(str_join(split_html_space_chars(&value), " ")) }, } } // https://html.spec.whatwg.org/multipage/#document.title fn SetTitle(&self, title: DOMString) { let root = match self.GetDocumentElement() { Some(root) => root, None => return, }; let elem = if root.namespace() == &ns!(svg) && root.local_name() == &local_name!("svg") { let elem = root.upcast::().child_elements().find(|node| { node.namespace() == &ns!(svg) && node.local_name() == &local_name!("title") }); match elem { Some(elem) => DomRoot::upcast::(elem), None => { let name = QualName::new(None, ns!(svg), local_name!("title")); let elem = Element::create( name, None, self, ElementCreator::ScriptCreated, CustomElementCreationMode::Synchronous, ); let parent = root.upcast::(); let child = elem.upcast::(); parent .InsertBefore(child, parent.GetFirstChild().r()) .unwrap() }, } } else if root.namespace() == &ns!(html) { let elem = root .upcast::() .traverse_preorder() .find(|node| node.is::()); match elem { Some(elem) => elem, None => match self.GetHead() { Some(head) => { let name = QualName::new(None, ns!(html), local_name!("title")); let elem = Element::create( name, None, self, ElementCreator::ScriptCreated, CustomElementCreationMode::Synchronous, ); head.upcast::().AppendChild(elem.upcast()).unwrap() }, None => return, }, } } else { return; }; elem.SetTextContent(Some(title)); } // https://html.spec.whatwg.org/multipage/#dom-document-head fn GetHead(&self) -> Option> { self.get_html_element().and_then(|root| { root.upcast::() .children() .filter_map(DomRoot::downcast) .next() }) } // https://html.spec.whatwg.org/multipage/#dom-document-currentscript fn GetCurrentScript(&self) -> Option> { self.current_script.get() } // https://html.spec.whatwg.org/multipage/#dom-document-body fn GetBody(&self) -> Option> { self.get_html_element().and_then(|root| { let node = root.upcast::(); node.children() .find(|child| match child.type_id() { NodeTypeId::Element(ElementTypeId::HTMLElement( HTMLElementTypeId::HTMLBodyElement, )) | NodeTypeId::Element(ElementTypeId::HTMLElement( HTMLElementTypeId::HTMLFrameSetElement, )) => true, _ => false, }) .map(|node| DomRoot::downcast(node).unwrap()) }) } // https://html.spec.whatwg.org/multipage/#dom-document-body fn SetBody(&self, new_body: Option<&HTMLElement>) -> ErrorResult { // Step 1. let new_body = match new_body { Some(new_body) => new_body, None => return Err(Error::HierarchyRequest), }; let node = new_body.upcast::(); match node.type_id() { NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLBodyElement)) | NodeTypeId::Element(ElementTypeId::HTMLElement( HTMLElementTypeId::HTMLFrameSetElement, )) => {}, _ => return Err(Error::HierarchyRequest), } // Step 2. let old_body = self.GetBody(); if old_body.r() == Some(new_body) { return Ok(()); } match (self.get_html_element(), &old_body) { // Step 3. (Some(ref root), &Some(ref child)) => { let root = root.upcast::(); root.ReplaceChild(new_body.upcast(), child.upcast()) .unwrap(); }, // Step 4. (None, _) => return Err(Error::HierarchyRequest), // Step 5. (Some(ref root), &None) => { let root = root.upcast::(); root.AppendChild(new_body.upcast()).unwrap(); }, } Ok(()) } // https://html.spec.whatwg.org/multipage/#dom-document-getelementsbyname fn GetElementsByName(&self, name: DOMString) -> DomRoot { self.create_node_list(|node| { let element = match node.downcast::() { Some(element) => element, None => return false, }; if element.namespace() != &ns!(html) { return false; } element .get_attribute(&ns!(), &local_name!("name")) .map_or(false, |attr| &**attr.value() == &*name) }) } // https://html.spec.whatwg.org/multipage/#dom-document-images fn Images(&self) -> DomRoot { self.images.or_init(|| { let filter = Box::new(ImagesFilter); HTMLCollection::create(&self.window, self.upcast(), filter) }) } // https://html.spec.whatwg.org/multipage/#dom-document-embeds fn Embeds(&self) -> DomRoot { self.embeds.or_init(|| { let filter = Box::new(EmbedsFilter); HTMLCollection::create(&self.window, self.upcast(), filter) }) } // https://html.spec.whatwg.org/multipage/#dom-document-plugins fn Plugins(&self) -> DomRoot { self.Embeds() } // https://html.spec.whatwg.org/multipage/#dom-document-links fn Links(&self) -> DomRoot { self.links.or_init(|| { let filter = Box::new(LinksFilter); HTMLCollection::create(&self.window, self.upcast(), filter) }) } // https://html.spec.whatwg.org/multipage/#dom-document-forms fn Forms(&self) -> DomRoot { self.forms.or_init(|| { let filter = Box::new(FormsFilter); HTMLCollection::create(&self.window, self.upcast(), filter) }) } // https://html.spec.whatwg.org/multipage/#dom-document-scripts fn Scripts(&self) -> DomRoot { self.scripts.or_init(|| { let filter = Box::new(ScriptsFilter); HTMLCollection::create(&self.window, self.upcast(), filter) }) } // https://html.spec.whatwg.org/multipage/#dom-document-anchors fn Anchors(&self) -> DomRoot { self.anchors.or_init(|| { let filter = Box::new(AnchorsFilter); HTMLCollection::create(&self.window, self.upcast(), filter) }) } // https://html.spec.whatwg.org/multipage/#dom-document-applets fn Applets(&self) -> DomRoot { self.applets .or_init(|| HTMLCollection::always_empty(&self.window, self.upcast())) } // https://html.spec.whatwg.org/multipage/#dom-document-location fn GetLocation(&self) -> Option> { if self.is_fully_active() { Some(self.window.Location()) } else { None } } // https://dom.spec.whatwg.org/#dom-parentnode-children fn Children(&self) -> DomRoot { HTMLCollection::children(&self.window, self.upcast()) } // https://dom.spec.whatwg.org/#dom-parentnode-firstelementchild fn GetFirstElementChild(&self) -> Option> { self.upcast::().child_elements().next() } // https://dom.spec.whatwg.org/#dom-parentnode-lastelementchild fn GetLastElementChild(&self) -> Option> { self.upcast::() .rev_children() .filter_map(DomRoot::downcast) .next() } // https://dom.spec.whatwg.org/#dom-parentnode-childelementcount fn ChildElementCount(&self) -> u32 { self.upcast::().child_elements().count() as u32 } // https://dom.spec.whatwg.org/#dom-parentnode-prepend fn Prepend(&self, nodes: Vec) -> ErrorResult { self.upcast::().prepend(nodes) } // https://dom.spec.whatwg.org/#dom-parentnode-append fn Append(&self, nodes: Vec) -> ErrorResult { self.upcast::().append(nodes) } // https://dom.spec.whatwg.org/#dom-parentnode-queryselector fn QuerySelector(&self, selectors: DOMString) -> Fallible>> { let root = self.upcast::(); root.query_selector(selectors) } // https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall fn QuerySelectorAll(&self, selectors: DOMString) -> Fallible> { let root = self.upcast::(); root.query_selector_all(selectors) } // https://html.spec.whatwg.org/multipage/#dom-document-readystate fn ReadyState(&self) -> DocumentReadyState { self.ready_state.get() } // https://html.spec.whatwg.org/multipage/#dom-document-defaultview fn GetDefaultView(&self) -> Option> { if self.has_browsing_context { Some(DomRoot::from_ref(&*self.window)) } else { None } } // https://html.spec.whatwg.org/multipage/#dom-document-cookie fn GetCookie(&self) -> Fallible { if self.is_cookie_averse() { return Ok(DOMString::new()); } if !self.origin.is_tuple() { return Err(Error::Security); } let url = self.url(); let (tx, rx) = profile_ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); let _ = self .window .upcast::() .resource_threads() .send(GetCookiesForUrl(url, tx, NonHTTP)); let cookies = rx.recv().unwrap(); Ok(cookies.map_or(DOMString::new(), DOMString::from)) } // https://html.spec.whatwg.org/multipage/#dom-document-cookie fn SetCookie(&self, cookie: DOMString) -> ErrorResult { if self.is_cookie_averse() { return Ok(()); } if !self.origin.is_tuple() { return Err(Error::Security); } let cookies = if let Some(cookie) = cookie_rs::Cookie::parse(cookie.to_string()).ok().map(Serde) { vec![cookie] } else { vec![] }; let _ = self .window .upcast::() .resource_threads() .send(SetCookiesForUrl(self.url(), cookies, NonHTTP)); Ok(()) } // https://html.spec.whatwg.org/multipage/#dom-document-bgcolor fn BgColor(&self) -> DOMString { self.get_body_attribute(&local_name!("bgcolor")) } // https://html.spec.whatwg.org/multipage/#dom-document-bgcolor fn SetBgColor(&self, value: DOMString) { self.set_body_attribute(&local_name!("bgcolor"), value) } // https://html.spec.whatwg.org/multipage/#dom-document-fgcolor fn FgColor(&self) -> DOMString { self.get_body_attribute(&local_name!("text")) } // https://html.spec.whatwg.org/multipage/#dom-document-fgcolor fn SetFgColor(&self, value: DOMString) { self.set_body_attribute(&local_name!("text"), value) } #[allow(unsafe_code)] // https://html.spec.whatwg.org/multipage/#dom-tree-accessors:dom-document-nameditem-filter unsafe fn NamedGetter( &self, _cx: *mut JSContext, name: DOMString, ) -> Option> { #[derive(JSTraceable, MallocSizeOf)] struct NamedElementFilter { name: Atom, } impl CollectionFilter for NamedElementFilter { fn filter(&self, elem: &Element, _root: &Node) -> bool { filter_by_name(&self.name, elem.upcast()) } } // https://html.spec.whatwg.org/multipage/#dom-document-nameditem-filter fn filter_by_name(name: &Atom, node: &Node) -> bool { let html_elem_type = match node.type_id() { NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_, _ => return false, }; let elem = match node.downcast::() { Some(elem) => elem, None => return false, }; match html_elem_type { HTMLElementTypeId::HTMLFormElement => { match elem.get_attribute(&ns!(), &local_name!("name")) { Some(ref attr) => attr.value().as_atom() == name, None => false, } }, HTMLElementTypeId::HTMLImageElement => { match elem.get_attribute(&ns!(), &local_name!("name")) { Some(ref attr) => { if attr.value().as_atom() == name { true } else { match elem.get_attribute(&ns!(), &local_name!("id")) { Some(ref attr) => attr.value().as_atom() == name, None => false, } } }, None => false, } }, // TODO: Handle ,