Skip to content

Commit

Permalink
Improve implementation of DOMRect and implement DOMRectReadOnly
Browse files Browse the repository at this point in the history
Passes most tests from test-css.
  • Loading branch information
tschneidereit committed Oct 17, 2015
1 parent 499a847 commit 4c1c05f
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 184 deletions.
4 changes: 2 additions & 2 deletions components/script/devtools.rs
Expand Up @@ -102,8 +102,8 @@ pub fn handle_get_layout(page: &Rc<Page>,
let node = find_node_by_unique_id(&*page, pipeline, node_id);
let elem = ElementCast::to_ref(node.r()).expect("should be getting layout of element");
let rect = elem.GetBoundingClientRect();
let width = *rect.r().Width();
let height = *rect.r().Height();
let width = rect.Width() as f32;
let height = rect.Height() as f32;
reply.send(ComputedNodeLayout { width: width, height: height }).unwrap();
}

Expand Down
83 changes: 42 additions & 41 deletions components/script/dom/domrect.rs
Expand Up @@ -2,74 +2,75 @@
* 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 app_units::Au;
use dom::bindings::codegen::Bindings::DOMRectBinding;
use dom::bindings::codegen::Bindings::DOMRectBinding::DOMRectMethods;
use dom::bindings::codegen::Bindings::DOMRectReadOnlyBinding::DOMRectReadOnlyMethods;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::num::Finite;
use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::window::Window;
use dom::bindings::utils::reflect_dom_object;
use dom::domrectreadonly::DOMRectReadOnly;

#[dom_struct]
pub struct DOMRect {
reflector_: Reflector,
top: f32,
bottom: f32,
left: f32,
right: f32,
rect: DOMRectReadOnly,
}

impl DOMRect {
fn new_inherited(top: Au, bottom: Au,
left: Au, right: Au) -> DOMRect {
fn new_inherited(x: f64, y: f64, width: f64, height: f64) -> DOMRect {
DOMRect {
top: top.to_nearest_px() as f32,
bottom: bottom.to_nearest_px() as f32,
left: left.to_nearest_px() as f32,
right: right.to_nearest_px() as f32,
reflector_: Reflector::new(),
rect: DOMRectReadOnly::new_inherited(x, y, width, height),
}
}

pub fn new(window: &Window,
top: Au, bottom: Au,
left: Au, right: Au) -> Root<DOMRect> {
reflect_dom_object(box DOMRect::new_inherited(top, bottom, left, right),
GlobalRef::Window(window), DOMRectBinding::Wrap)
pub fn new(global: GlobalRef, x: f64, y: f64, width: f64, height: f64) -> Root<DOMRect> {
reflect_dom_object(box DOMRect::new_inherited(x, y, width, height), global, DOMRectBinding::Wrap)
}

pub fn Constructor(global: GlobalRef,
x: f64, y: f64, width: f64, height: f64) -> Fallible<Root<DOMRect>> {
Ok(DOMRect::new(global, x, y, width, height))
}
}

impl DOMRectMethods for DOMRect {
// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-top
fn Top(&self) -> Finite<f32> {
Finite::wrap(self.top)
// https://drafts.fxtf.org/geometry/#dom-domrect-x
fn X(&self) -> f64 {
self.rect.X()
}

// https://drafts.fxtf.org/geometry/#dom-domrect-x
fn SetX(&self, value: f64) {
self.rect.set_x(value);
}

// https://drafts.fxtf.org/geometry/#dom-domrect-y
fn Y(&self) -> f64 {
self.rect.Y()
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-bottom
fn Bottom(&self) -> Finite<f32> {
Finite::wrap(self.bottom)
// https://drafts.fxtf.org/geometry/#dom-domrect-y
fn SetY(&self, value: f64) {
self.rect.set_y(value);
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-left
fn Left(&self) -> Finite<f32> {
Finite::wrap(self.left)
// https://drafts.fxtf.org/geometry/#dom-domrect-width
fn Width(&self) -> f64 {
self.rect.Width()
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-right
fn Right(&self) -> Finite<f32> {
Finite::wrap(self.right)
// https://drafts.fxtf.org/geometry/#dom-domrect-width
fn SetWidth(&self, value: f64) {
self.rect.set_width(value);
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-width
fn Width(&self) -> Finite<f32> {
let result = (self.right - self.left).abs();
Finite::wrap(result)
// https://drafts.fxtf.org/geometry/#dom-domrect-height
fn Height(&self) -> f64 {
self.rect.Height()
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-height
fn Height(&self) -> Finite<f32> {
let result = (self.bottom - self.top).abs();
Finite::wrap(result)
// https://drafts.fxtf.org/geometry/#dom-domrect-height
fn SetHeight(&self, value: f64) {
self.rect.set_height(value);
}
}
102 changes: 102 additions & 0 deletions components/script/dom/domrectreadonly.rs
@@ -0,0 +1,102 @@
/* 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 dom::bindings::codegen::Bindings::DOMRectReadOnlyBinding::{DOMRectReadOnlyMethods, Wrap};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::utils::{Reflector, reflect_dom_object};
use std::cell::Cell;

#[dom_struct]
pub struct DOMRectReadOnly {
reflector_: Reflector,
x: Cell<f64>,
y: Cell<f64>,
width: Cell<f64>,
height: Cell<f64>,
}

impl DOMRectReadOnly {
pub fn new_inherited(x: f64, y: f64, width: f64, height: f64) -> DOMRectReadOnly {
DOMRectReadOnly {
x: Cell::new(x),
y: Cell::new(y),
width: Cell::new(width),
height: Cell::new(height),
reflector_: Reflector::new(),
}
}

pub fn new(global: GlobalRef, x: f64, y: f64, width: f64, height: f64) -> Root<DOMRectReadOnly> {
reflect_dom_object(box DOMRectReadOnly::new_inherited(x, y, width, height), global, Wrap)
}

pub fn Constructor(global: GlobalRef,
x: f64, y: f64, width: f64, height: f64) -> Fallible<Root<DOMRectReadOnly>> {
Ok(DOMRectReadOnly::new(global, x, y, width, height))
}

pub fn set_x(&self, value: f64) {
self.x.set(value);
}

pub fn set_y(&self, value: f64) {
self.y.set(value);
}

pub fn set_width(&self, value: f64) {
self.width.set(value);
}

pub fn set_height(&self, value: f64) {
self.height.set(value);
}
}

impl DOMRectReadOnlyMethods for DOMRectReadOnly {
// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-x
fn X(&self) -> f64 {
self.x.get()
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-y
fn Y(&self) -> f64 {
self.y.get()
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-width
fn Width(&self) -> f64 {
self.width.get()
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-height
fn Height(&self) -> f64 {
self.height.get()
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-top
fn Top(&self) -> f64 {
let height = self.height.get();
if height >= 0f64 { self.y.get() } else { self.y.get() + height }
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-right
fn Right(&self) -> f64 {
let width = self.width.get();
if width < 0f64 { self.x.get() } else { self.x.get() + width }
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-bottom
fn Bottom(&self) -> f64 {
let height = self.height.get();
if height < 0f64 { self.y.get() } else { self.y.get() + height }
}

// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-left
fn Left(&self) -> f64 {
let width = self.width.get();
if width >= 0f64 { self.x.get() } else { self.x.get() + width }
}
}
20 changes: 11 additions & 9 deletions components/script/dom/element.rs
Expand Up @@ -30,6 +30,7 @@ use dom::bindings::codegen::InheritTypes::{HTMLTemplateElementCast, HTMLTextArea
use dom::bindings::codegen::InheritTypes::{NodeCast, NodeTypeId, TextCast};
use dom::bindings::codegen::UnionTypes::NodeOrString;
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, LayoutJS, MutNullableHeap};
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::utils::XMLName::InvalidXMLName;
Expand Down Expand Up @@ -1249,9 +1250,11 @@ impl ElementMethods for Element {
let node = NodeCast::from_ref(self);
let raw_rects = node.get_content_boxes();
let rects = raw_rects.iter().map(|rect| {
DOMRect::new(win.r(),
rect.origin.y, rect.origin.y + rect.size.height,
rect.origin.x, rect.origin.x + rect.size.width)
DOMRect::new(GlobalRef::Window(win.r()),
rect.origin.x.to_f64_px(),
rect.origin.y.to_f64_px(),
rect.size.width.to_f64_px(),
rect.size.height.to_f64_px())
});
DOMRectList::new(win.r(), rects)
}
Expand All @@ -1261,12 +1264,11 @@ impl ElementMethods for Element {
let win = window_from_node(self);
let node = NodeCast::from_ref(self);
let rect = node.get_bounding_content_box();
DOMRect::new(
win.r(),
rect.origin.y,
rect.origin.y + rect.size.height,
rect.origin.x,
rect.origin.x + rect.size.width)
DOMRect::new(GlobalRef::Window(win.r()),
rect.origin.x.to_f64_px(),
rect.origin.y.to_f64_px(),
rect.size.width.to_f64_px(),
rect.size.height.to_f64_px())
}

// https://drafts.csswg.org/cssom-view/#dom-element-clienttop
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/mod.rs
Expand Up @@ -220,6 +220,7 @@ pub mod dompoint;
pub mod dompointreadonly;
pub mod domrect;
pub mod domrectlist;
pub mod domrectreadonly;
pub mod domstringmap;
pub mod domtokenlist;
pub mod element;
Expand Down
17 changes: 9 additions & 8 deletions components/script/dom/webidls/DOMRect.webidl
Expand Up @@ -3,12 +3,13 @@
* 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/. */

// http://dev.w3.org/fxtf/geometry/#DOMRect
interface DOMRect {
readonly attribute float top;
readonly attribute float right;
readonly attribute float bottom;
readonly attribute float left;
readonly attribute float width;
readonly attribute float height;
[Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double width = 0, optional unrestricted double height = 0),
/*Exposed=(Window,Worker)*/]
// https://drafts.fxtf.org/geometry/#domrect
interface DOMRect : DOMRectReadOnly {
inherit attribute unrestricted double x;
inherit attribute unrestricted double y;
inherit attribute unrestricted double width;
inherit attribute unrestricted double height;
};
29 changes: 29 additions & 0 deletions components/script/dom/webidls/DOMRectReadOnly.webidl
@@ -0,0 +1,29 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

[Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double width = 0, optional unrestricted double height = 0),
/*Exposed=(Window,Worker)*/]
// https://drafts.fxtf.org/geometry/#domrect
interface DOMRectReadOnly {
// [NewObject] static DOMRectReadOnly fromRect(optional DOMRectInit other);

readonly attribute unrestricted double x;
readonly attribute unrestricted double y;
readonly attribute unrestricted double width;
readonly attribute unrestricted double height;
readonly attribute unrestricted double top;
readonly attribute unrestricted double right;
readonly attribute unrestricted double bottom;
readonly attribute unrestricted double left;
};

// https://drafts.fxtf.org/geometry/#dictdef-domrectinit
dictionary DOMRectInit {
unrestricted double x = 0;
unrestricted double y = 0;
unrestricted double width = 0;
unrestricted double height = 0;
};
41 changes: 1 addition & 40 deletions tests/wpt/metadata-css/geometry-1_dev/html/DOMRect-001.htm.ini
@@ -1,53 +1,14 @@
[DOMRect-001.htm]
type: testharness
[testConstructor0]
expected: FAIL

[testConstructor4]
expected: FAIL

[testConstructor5]
expected: FAIL

[testConstructorNegativeWidth]
expected: FAIL

[testConstructorNegativeHeight]
expected: FAIL

[testConstructorNegativeWidthHeight]
expected: FAIL

[testConstructorUndefined1]
expected: FAIL

[testConstructorUndefined2]
expected: FAIL

[testConstructorString1]
expected: FAIL

[testConstructorString2]
expected: FAIL

[testConstructorIllegal1]
expected: FAIL

[testConstructorIllegal2]
expected: FAIL

[testSetReadOnlyAttributes]
expected: FAIL

[testSetAttributes]
expected: FAIL

[testConstructor1]
expected: FAIL

[testConstructor2]
expected: FAIL

[testConstructor3]
[testReadOnlyConstructorUndefined1]
expected: FAIL

0 comments on commit 4c1c05f

Please sign in to comment.