Make a bunch of layout queries morally safer

This commit is contained in:
Anthony Ramine 2019-01-09 14:11:13 +01:00
parent ddef6211b3
commit 3ccda7f90c
7 changed files with 60 additions and 68 deletions

View file

@ -403,7 +403,7 @@ impl HTMLElementMethods for HTMLElement {
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (element, _) = window.offset_parent_query(node.to_trusted_node_address());
let (element, _) = window.offset_parent_query(node);
element
}
@ -416,7 +416,7 @@ impl HTMLElementMethods for HTMLElement {
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (_, rect) = window.offset_parent_query(node.to_trusted_node_address());
let (_, rect) = window.offset_parent_query(node);
rect.origin.y.to_nearest_px()
}
@ -429,7 +429,7 @@ impl HTMLElementMethods for HTMLElement {
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (_, rect) = window.offset_parent_query(node.to_trusted_node_address());
let (_, rect) = window.offset_parent_query(node);
rect.origin.x.to_nearest_px()
}
@ -438,7 +438,7 @@ impl HTMLElementMethods for HTMLElement {
fn OffsetWidth(&self) -> i32 {
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (_, rect) = window.offset_parent_query(node.to_trusted_node_address());
let (_, rect) = window.offset_parent_query(node);
rect.size.width.to_nearest_px()
}
@ -447,7 +447,7 @@ impl HTMLElementMethods for HTMLElement {
fn OffsetHeight(&self) -> i32 {
let node = self.upcast::<Node>();
let window = window_from_node(self);
let (_, rect) = window.offset_parent_query(node.to_trusted_node_address());
let (_, rect) = window.offset_parent_query(node);
rect.size.height.to_nearest_px()
}

View file

@ -1467,10 +1467,8 @@ impl VirtualMethods for HTMLInputElement {
// now.
if let Some(point_in_target) = mouse_event.point_in_target() {
let window = window_from_node(self);
let TextIndexResponse(index) = window.text_index_query(
self.upcast::<Node>().to_trusted_node_address(),
point_in_target,
);
let TextIndexResponse(index) =
window.text_index_query(self.upcast::<Node>(), point_in_target);
if let Some(i) = index {
self.textinput.borrow_mut().set_edit_point_index(i as usize);
// trigger redraw

View file

@ -334,6 +334,10 @@ impl Node {
UntrustedNodeAddress(self.reflector().get_jsobject().get() as *const c_void)
}
pub fn to_opaque(&self) -> OpaqueNode {
OpaqueNode(self.reflector().get_jsobject().get() as usize)
}
pub fn as_custom_element(&self) -> Option<DomRoot<Element>> {
self.downcast::<Element>().and_then(|element| {
if element.get_custom_element_definition().is_some() {
@ -639,7 +643,7 @@ impl Node {
/// Returns the rendered bounding content box if the element is rendered,
/// and none otherwise.
pub fn bounding_content_box(&self) -> Option<Rect<Au>> {
window_from_node(self).content_box_query(self.to_trusted_node_address())
window_from_node(self).content_box_query(self)
}
pub fn bounding_content_box_or_zero(&self) -> Rect<Au> {
@ -647,11 +651,11 @@ impl Node {
}
pub fn content_boxes(&self) -> Vec<Rect<Au>> {
window_from_node(self).content_boxes_query(self.to_trusted_node_address())
window_from_node(self).content_boxes_query(self)
}
pub fn client_rect(&self) -> Rect<i32> {
window_from_node(self).client_rect_query(self.to_trusted_node_address())
window_from_node(self).client_rect_query(self)
}
// https://drafts.csswg.org/cssom-view/#dom-element-scrollwidth
@ -668,7 +672,7 @@ impl Node {
.downcast::<HTMLBodyElement>()
.map_or(false, |e| e.is_the_html_body_element());
let scroll_area = window.scroll_area_query(self.to_trusted_node_address());
let scroll_area = window.scroll_area_query(self);
match (
document != window.Document(),

View file

@ -1571,31 +1571,31 @@ impl Window {
&*self.layout_rpc
}
pub fn content_box_query(&self, content_box_request: TrustedNodeAddress) -> Option<Rect<Au>> {
if !self.layout_reflow(QueryMsg::ContentBoxQuery(content_box_request)) {
pub fn content_box_query(&self, node: &Node) -> Option<Rect<Au>> {
if !self.layout_reflow(QueryMsg::ContentBoxQuery(node.to_opaque())) {
return None;
}
let ContentBoxResponse(rect) = self.layout_rpc.content_box();
rect
}
pub fn content_boxes_query(&self, content_boxes_request: TrustedNodeAddress) -> Vec<Rect<Au>> {
if !self.layout_reflow(QueryMsg::ContentBoxesQuery(content_boxes_request)) {
pub fn content_boxes_query(&self, node: &Node) -> Vec<Rect<Au>> {
if !self.layout_reflow(QueryMsg::ContentBoxesQuery(node.to_opaque())) {
return vec![];
}
let ContentBoxesResponse(rects) = self.layout_rpc.content_boxes();
rects
}
pub fn client_rect_query(&self, node_geometry_request: TrustedNodeAddress) -> Rect<i32> {
if !self.layout_reflow(QueryMsg::NodeGeometryQuery(node_geometry_request)) {
pub fn client_rect_query(&self, node: &Node) -> Rect<i32> {
if !self.layout_reflow(QueryMsg::NodeGeometryQuery(node.to_opaque())) {
return Rect::zero();
}
self.layout_rpc.node_geometry().client_rect
}
pub fn scroll_area_query(&self, node: TrustedNodeAddress) -> Rect<i32> {
if !self.layout_reflow(QueryMsg::NodeScrollGeometryQuery(node)) {
pub fn scroll_area_query(&self, node: &Node) -> Rect<i32> {
if !self.layout_reflow(QueryMsg::NodeScrollGeometryQuery(node.to_opaque())) {
return Rect::zero();
}
self.layout_rpc.node_scroll_area().client_rect
@ -1652,14 +1652,13 @@ impl Window {
}
#[allow(unsafe_code)]
pub fn offset_parent_query(
&self,
node: TrustedNodeAddress,
) -> (Option<DomRoot<Element>>, Rect<Au>) {
if !self.layout_reflow(QueryMsg::OffsetParentQuery(node)) {
pub fn offset_parent_query(&self, node: &Node) -> (Option<DomRoot<Element>>, Rect<Au>) {
if !self.layout_reflow(QueryMsg::OffsetParentQuery(node.to_opaque())) {
return (None, Rect::zero());
}
// FIXME(nox): Layout can reply with a garbage value which doesn't
// actually correspond to an element, that's unsound.
let response = self.layout_rpc.offset_parent();
let js_runtime = self.js_runtime.borrow();
let js_runtime = js_runtime.as_ref().unwrap();
@ -1677,12 +1676,8 @@ impl Window {
self.layout_rpc.style().0
}
pub fn text_index_query(
&self,
node: TrustedNodeAddress,
point_in_node: Point2D<f32>,
) -> TextIndexResponse {
if !self.layout_reflow(QueryMsg::TextIndexQuery(node, point_in_node)) {
pub fn text_index_query(&self, node: &Node, point_in_node: Point2D<f32>) -> TextIndexResponse {
if !self.layout_reflow(QueryMsg::TextIndexQuery(node.to_opaque(), point_in_node)) {
return TextIndexResponse(None);
}
self.layout_rpc.text_index()