Always get browsing context from document

This commit is contained in:
Fernando Jiménez Moreno 2019-01-30 17:15:20 +01:00
parent 2e5c058463
commit be06f1e9b3
3 changed files with 39 additions and 31 deletions

View file

@ -488,9 +488,14 @@ impl Document {
self.has_browsing_context self.has_browsing_context
} }
/// <https://html.spec.whatwg.org/multipage/#concept-document-bc>
#[inline] #[inline]
pub fn browsing_context(&self) -> Option<DomRoot<WindowProxy>> { pub fn browsing_context(&self) -> Option<DomRoot<WindowProxy>> {
self.document_or_shadow_root.browsing_context() if self.has_browsing_context {
self.window.undiscarded_window_proxy()
} else {
None
}
} }
#[inline] #[inline]
@ -2622,7 +2627,7 @@ impl Document {
let has_browsing_context = has_browsing_context == HasBrowsingContext::Yes; let has_browsing_context = has_browsing_context == HasBrowsingContext::Yes;
Document { Document {
node: Node::new_document_node(), node: Node::new_document_node(),
document_or_shadow_root: DocumentOrShadowRootImpl::new(window, has_browsing_context), document_or_shadow_root: DocumentOrShadowRootImpl::new(window),
window: Dom::from_ref(window), window: Dom::from_ref(window),
has_browsing_context, has_browsing_context,
implementation: Default::default(), implementation: Default::default(),
@ -4263,14 +4268,22 @@ impl DocumentMethods for Document {
// https://drafts.csswg.org/cssom-view/#dom-document-elementfrompoint // https://drafts.csswg.org/cssom-view/#dom-document-elementfrompoint
fn ElementFromPoint(&self, x: Finite<f64>, y: Finite<f64>) -> Option<DomRoot<Element>> { fn ElementFromPoint(&self, x: Finite<f64>, y: Finite<f64>) -> Option<DomRoot<Element>> {
self.document_or_shadow_root self.document_or_shadow_root.element_from_point(
.element_from_point(x, y, self.GetDocumentElement()) x,
y,
self.GetDocumentElement(),
self.has_browsing_context,
)
} }
// https://drafts.csswg.org/cssom-view/#dom-document-elementsfrompoint // https://drafts.csswg.org/cssom-view/#dom-document-elementsfrompoint
fn ElementsFromPoint(&self, x: Finite<f64>, y: Finite<f64>) -> Vec<DomRoot<Element>> { fn ElementsFromPoint(&self, x: Finite<f64>, y: Finite<f64>) -> Vec<DomRoot<Element>> {
self.document_or_shadow_root self.document_or_shadow_root.elements_from_point(
.elements_from_point(x, y, self.GetDocumentElement()) x,
y,
self.GetDocumentElement(),
self.has_browsing_context,
)
} }
// https://html.spec.whatwg.org/multipage/#dom-document-open // https://html.spec.whatwg.org/multipage/#dom-document-open

View file

@ -13,7 +13,6 @@ use crate::dom::htmlelement::HTMLElement;
use crate::dom::node; use crate::dom::node;
use crate::dom::shadowroot::ShadowRoot; use crate::dom::shadowroot::ShadowRoot;
use crate::dom::window::Window; use crate::dom::window::Window;
use crate::dom::windowproxy::WindowProxy;
use euclid::Point2D; use euclid::Point2D;
use js::jsapi::JS_GetRuntime; use js::jsapi::JS_GetRuntime;
use script_layout_interface::message::{NodesFromPointQueryType, QueryMsg}; use script_layout_interface::message::{NodesFromPointQueryType, QueryMsg};
@ -55,28 +54,16 @@ impl DocumentOrShadowRoot {
#[must_root] #[must_root]
#[derive(JSTraceable, MallocSizeOf)] #[derive(JSTraceable, MallocSizeOf)]
pub struct DocumentOrShadowRootImpl { pub struct DocumentOrShadowRootImpl {
has_browsing_context: bool,
window: Dom<Window>, window: Dom<Window>,
} }
impl DocumentOrShadowRootImpl { impl DocumentOrShadowRootImpl {
pub fn new(window: &Window, has_browsing_context: bool) -> Self { pub fn new(window: &Window) -> Self {
Self { Self {
has_browsing_context,
window: Dom::from_ref(window), window: Dom::from_ref(window),
} }
} }
/// <https://html.spec.whatwg.org/multipage/#concept-document-bc>
#[inline]
pub fn browsing_context(&self) -> Option<DomRoot<WindowProxy>> {
if self.has_browsing_context {
self.window.undiscarded_window_proxy()
} else {
None
}
}
pub fn nodes_from_point( pub fn nodes_from_point(
&self, &self,
client_point: &Point2D<f32>, client_point: &Point2D<f32>,
@ -99,13 +86,14 @@ impl DocumentOrShadowRootImpl {
x: Finite<f64>, x: Finite<f64>,
y: Finite<f64>, y: Finite<f64>,
document_element: Option<DomRoot<Element>>, document_element: Option<DomRoot<Element>>,
has_browsing_context: bool,
) -> Option<DomRoot<Element>> { ) -> Option<DomRoot<Element>> {
let x = *x as f32; let x = *x as f32;
let y = *y as f32; let y = *y as f32;
let point = &Point2D::new(x, y); let point = &Point2D::new(x, y);
let viewport = self.window.window_size().initial_viewport; let viewport = self.window.window_size().initial_viewport;
if self.browsing_context().is_none() { if has_browsing_context {
return None; return None;
} }
@ -138,13 +126,14 @@ impl DocumentOrShadowRootImpl {
x: Finite<f64>, x: Finite<f64>,
y: Finite<f64>, y: Finite<f64>,
document_element: Option<DomRoot<Element>>, document_element: Option<DomRoot<Element>>,
has_browsing_context: bool,
) -> Vec<DomRoot<Element>> { ) -> Vec<DomRoot<Element>> {
let x = *x as f32; let x = *x as f32;
let y = *y as f32; let y = *y as f32;
let point = &Point2D::new(x, y); let point = &Point2D::new(x, y);
let viewport = self.window.window_size().initial_viewport; let viewport = self.window.window_size().initial_viewport;
if self.browsing_context().is_none() { if has_browsing_context {
return vec![]; return vec![];
} }

View file

@ -23,7 +23,7 @@ use dom_struct::dom_struct;
pub struct ShadowRoot { pub struct ShadowRoot {
document_fragment: DocumentFragment, document_fragment: DocumentFragment,
document_or_shadow_root: DocumentOrShadowRootImpl, document_or_shadow_root: DocumentOrShadowRootImpl,
has_browsing_context: bool, document: Dom<Document>,
host: Dom<Element>, host: Dom<Element>,
stylesheet_list: MutNullableDom<StyleSheetList>, stylesheet_list: MutNullableDom<StyleSheetList>,
window: Dom<Window>, window: Dom<Window>,
@ -32,18 +32,14 @@ pub struct ShadowRoot {
impl ShadowRoot { impl ShadowRoot {
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
fn new_inherited(host: &Element, document: &Document) -> ShadowRoot { fn new_inherited(host: &Element, document: &Document) -> ShadowRoot {
let has_browsing_context = true;
let document_fragment = DocumentFragment::new_inherited(document); let document_fragment = DocumentFragment::new_inherited(document);
document_fragment document_fragment
.upcast::<Node>() .upcast::<Node>()
.set_flag(NodeFlags::IS_IN_SHADOW_TREE, true); .set_flag(NodeFlags::IS_IN_SHADOW_TREE, true);
ShadowRoot { ShadowRoot {
document_fragment, document_fragment,
document_or_shadow_root: DocumentOrShadowRootImpl::new( document_or_shadow_root: DocumentOrShadowRootImpl::new(document.window()),
document.window(), document: Dom::from_ref(document),
has_browsing_context,
),
has_browsing_context,
host: Dom::from_ref(host), host: Dom::from_ref(host),
stylesheet_list: MutNullableDom::new(None), stylesheet_list: MutNullableDom::new(None),
window: Dom::from_ref(document.window()), window: Dom::from_ref(document.window()),
@ -85,14 +81,24 @@ impl ShadowRootMethods for ShadowRoot {
fn ElementFromPoint(&self, x: Finite<f64>, y: Finite<f64>) -> Option<DomRoot<Element>> { fn ElementFromPoint(&self, x: Finite<f64>, y: Finite<f64>) -> Option<DomRoot<Element>> {
// XXX return the result of running the retargeting algorithm with context object // XXX return the result of running the retargeting algorithm with context object
// and the original result as input // and the original result as input
self.document_or_shadow_root.element_from_point(x, y, None) self.document_or_shadow_root.element_from_point(
x,
y,
None,
self.document.has_browsing_context(),
)
} }
// https://drafts.csswg.org/cssom-view/#dom-document-elementsfrompoint // https://drafts.csswg.org/cssom-view/#dom-document-elementsfrompoint
fn ElementsFromPoint(&self, x: Finite<f64>, y: Finite<f64>) -> Vec<DomRoot<Element>> { fn ElementsFromPoint(&self, x: Finite<f64>, y: Finite<f64>) -> Vec<DomRoot<Element>> {
// XXX return the result of running the retargeting algorithm with context object // XXX return the result of running the retargeting algorithm with context object
// and the original result as input // and the original result as input
self.document_or_shadow_root.elements_from_point(x, y, None) self.document_or_shadow_root.elements_from_point(
x,
y,
None,
self.document.has_browsing_context(),
)
} }
/// https://dom.spec.whatwg.org/#dom-shadowroot-mode /// https://dom.spec.whatwg.org/#dom-shadowroot-mode