Improve implementation of DOMRect and implement DOMRectReadOnly

Passes most tests from test-css.
This commit is contained in:
Till Schneidereit 2015-09-02 14:16:34 +02:00
parent 499a847141
commit 4c1c05fac6
11 changed files with 198 additions and 184 deletions

View file

@ -102,8 +102,8 @@ pub fn handle_get_layout(page: &Rc<Page>,
let node = find_node_by_unique_id(&*page, pipeline, node_id); 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 elem = ElementCast::to_ref(node.r()).expect("should be getting layout of element");
let rect = elem.GetBoundingClientRect(); let rect = elem.GetBoundingClientRect();
let width = *rect.r().Width(); let width = rect.Width() as f32;
let height = *rect.r().Height(); let height = rect.Height() as f32;
reply.send(ComputedNodeLayout { width: width, height: height }).unwrap(); reply.send(ComputedNodeLayout { width: width, height: height }).unwrap();
} }

View file

@ -2,74 +2,75 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * 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;
use dom::bindings::codegen::Bindings::DOMRectBinding::DOMRectMethods; 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::global::GlobalRef;
use dom::bindings::js::Root; use dom::bindings::js::Root;
use dom::bindings::num::Finite; use dom::bindings::utils::reflect_dom_object;
use dom::bindings::utils::{Reflector, reflect_dom_object}; use dom::domrectreadonly::DOMRectReadOnly;
use dom::window::Window;
#[dom_struct] #[dom_struct]
pub struct DOMRect { pub struct DOMRect {
reflector_: Reflector, rect: DOMRectReadOnly,
top: f32,
bottom: f32,
left: f32,
right: f32,
} }
impl DOMRect { impl DOMRect {
fn new_inherited(top: Au, bottom: Au, fn new_inherited(x: f64, y: f64, width: f64, height: f64) -> DOMRect {
left: Au, right: Au) -> DOMRect {
DOMRect { DOMRect {
top: top.to_nearest_px() as f32, rect: DOMRectReadOnly::new_inherited(x, y, width, height),
bottom: bottom.to_nearest_px() as f32,
left: left.to_nearest_px() as f32,
right: right.to_nearest_px() as f32,
reflector_: Reflector::new(),
} }
} }
pub fn new(window: &Window, pub fn new(global: GlobalRef, x: f64, y: f64, width: f64, height: f64) -> Root<DOMRect> {
top: Au, bottom: Au, reflect_dom_object(box DOMRect::new_inherited(x, y, width, height), global, DOMRectBinding::Wrap)
left: Au, right: Au) -> Root<DOMRect> { }
reflect_dom_object(box DOMRect::new_inherited(top, bottom, left, right),
GlobalRef::Window(window), 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 { impl DOMRectMethods for DOMRect {
// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-top // https://drafts.fxtf.org/geometry/#dom-domrect-x
fn Top(&self) -> Finite<f32> { fn X(&self) -> f64 {
Finite::wrap(self.top) self.rect.X()
} }
// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-bottom // https://drafts.fxtf.org/geometry/#dom-domrect-x
fn Bottom(&self) -> Finite<f32> { fn SetX(&self, value: f64) {
Finite::wrap(self.bottom) self.rect.set_x(value);
} }
// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-left // https://drafts.fxtf.org/geometry/#dom-domrect-y
fn Left(&self) -> Finite<f32> { fn Y(&self) -> f64 {
Finite::wrap(self.left) self.rect.Y()
} }
// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-right // https://drafts.fxtf.org/geometry/#dom-domrect-y
fn Right(&self) -> Finite<f32> { fn SetY(&self, value: f64) {
Finite::wrap(self.right) self.rect.set_y(value);
} }
// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-width // https://drafts.fxtf.org/geometry/#dom-domrect-width
fn Width(&self) -> Finite<f32> { fn Width(&self) -> f64 {
let result = (self.right - self.left).abs(); self.rect.Width()
Finite::wrap(result)
} }
// https://drafts.fxtf.org/geometry/#dom-domrectreadonly-height // https://drafts.fxtf.org/geometry/#dom-domrect-width
fn Height(&self) -> Finite<f32> { fn SetWidth(&self, value: f64) {
let result = (self.bottom - self.top).abs(); self.rect.set_width(value);
Finite::wrap(result) }
// https://drafts.fxtf.org/geometry/#dom-domrect-height
fn Height(&self) -> f64 {
self.rect.Height()
}
// https://drafts.fxtf.org/geometry/#dom-domrect-height
fn SetHeight(&self, value: f64) {
self.rect.set_height(value);
} }
} }

View file

@ -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 }
}
}

View file

@ -30,6 +30,7 @@ use dom::bindings::codegen::InheritTypes::{HTMLTemplateElementCast, HTMLTextArea
use dom::bindings::codegen::InheritTypes::{NodeCast, NodeTypeId, TextCast}; use dom::bindings::codegen::InheritTypes::{NodeCast, NodeTypeId, TextCast};
use dom::bindings::codegen::UnionTypes::NodeOrString; use dom::bindings::codegen::UnionTypes::NodeOrString;
use dom::bindings::error::{Error, ErrorResult, Fallible}; use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, LayoutJS, MutNullableHeap}; use dom::bindings::js::{JS, LayoutJS, MutNullableHeap};
use dom::bindings::js::{Root, RootedReference}; use dom::bindings::js::{Root, RootedReference};
use dom::bindings::utils::XMLName::InvalidXMLName; use dom::bindings::utils::XMLName::InvalidXMLName;
@ -1249,9 +1250,11 @@ impl ElementMethods for Element {
let node = NodeCast::from_ref(self); let node = NodeCast::from_ref(self);
let raw_rects = node.get_content_boxes(); let raw_rects = node.get_content_boxes();
let rects = raw_rects.iter().map(|rect| { let rects = raw_rects.iter().map(|rect| {
DOMRect::new(win.r(), DOMRect::new(GlobalRef::Window(win.r()),
rect.origin.y, rect.origin.y + rect.size.height, rect.origin.x.to_f64_px(),
rect.origin.x, rect.origin.x + rect.size.width) rect.origin.y.to_f64_px(),
rect.size.width.to_f64_px(),
rect.size.height.to_f64_px())
}); });
DOMRectList::new(win.r(), rects) DOMRectList::new(win.r(), rects)
} }
@ -1261,12 +1264,11 @@ impl ElementMethods for Element {
let win = window_from_node(self); let win = window_from_node(self);
let node = NodeCast::from_ref(self); let node = NodeCast::from_ref(self);
let rect = node.get_bounding_content_box(); let rect = node.get_bounding_content_box();
DOMRect::new( DOMRect::new(GlobalRef::Window(win.r()),
win.r(), rect.origin.x.to_f64_px(),
rect.origin.y, rect.origin.y.to_f64_px(),
rect.origin.y + rect.size.height, rect.size.width.to_f64_px(),
rect.origin.x, rect.size.height.to_f64_px())
rect.origin.x + rect.size.width)
} }
// https://drafts.csswg.org/cssom-view/#dom-element-clienttop // https://drafts.csswg.org/cssom-view/#dom-element-clienttop

View file

@ -220,6 +220,7 @@ pub mod dompoint;
pub mod dompointreadonly; pub mod dompointreadonly;
pub mod domrect; pub mod domrect;
pub mod domrectlist; pub mod domrectlist;
pub mod domrectreadonly;
pub mod domstringmap; pub mod domstringmap;
pub mod domtokenlist; pub mod domtokenlist;
pub mod element; pub mod element;

View file

@ -3,12 +3,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// http://dev.w3.org/fxtf/geometry/#DOMRect [Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
interface DOMRect { optional unrestricted double width = 0, optional unrestricted double height = 0),
readonly attribute float top; /*Exposed=(Window,Worker)*/]
readonly attribute float right; // https://drafts.fxtf.org/geometry/#domrect
readonly attribute float bottom; interface DOMRect : DOMRectReadOnly {
readonly attribute float left; inherit attribute unrestricted double x;
readonly attribute float width; inherit attribute unrestricted double y;
readonly attribute float height; inherit attribute unrestricted double width;
inherit attribute unrestricted double height;
}; };

View file

@ -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;
};

View file

@ -1,53 +1,14 @@
[DOMRect-001.htm] [DOMRect-001.htm]
type: testharness type: testharness
[testConstructor0]
expected: FAIL
[testConstructor4]
expected: FAIL
[testConstructor5]
expected: FAIL
[testConstructorNegativeWidth]
expected: FAIL
[testConstructorNegativeHeight]
expected: FAIL
[testConstructorNegativeWidthHeight]
expected: FAIL
[testConstructorUndefined1] [testConstructorUndefined1]
expected: FAIL expected: FAIL
[testConstructorUndefined2]
expected: FAIL
[testConstructorString1]
expected: FAIL
[testConstructorString2]
expected: FAIL
[testConstructorIllegal1] [testConstructorIllegal1]
expected: FAIL expected: FAIL
[testConstructorIllegal2] [testConstructorIllegal2]
expected: FAIL expected: FAIL
[testSetReadOnlyAttributes] [testReadOnlyConstructorUndefined1]
expected: FAIL
[testSetAttributes]
expected: FAIL
[testConstructor1]
expected: FAIL
[testConstructor2]
expected: FAIL
[testConstructor3]
expected: FAIL expected: FAIL

View file

@ -1,53 +1,11 @@
[DOMRect-001.xht] [DOMRect-001.xht]
type: testharness type: testharness
[testConstructor0]
expected: FAIL
[testConstructor4]
expected: FAIL
[testConstructor5]
expected: FAIL
[testConstructorNegativeWidth]
expected: FAIL
[testConstructorNegativeHeight]
expected: FAIL
[testConstructorNegativeWidthHeight]
expected: FAIL
[testConstructorUndefined1] [testConstructorUndefined1]
expected: FAIL expected: FAIL
[testConstructorUndefined2]
expected: FAIL
[testConstructorString1]
expected: FAIL
[testConstructorString2]
expected: FAIL
[testConstructorIllegal1] [testConstructorIllegal1]
expected: FAIL expected: FAIL
[testConstructorIllegal2] [testConstructorIllegal2]
expected: FAIL expected: FAIL
[testSetReadOnlyAttributes]
expected: FAIL
[testSetAttributes]
expected: FAIL
[testConstructor1]
expected: FAIL
[testConstructor2]
expected: FAIL
[testConstructor3]
expected: FAIL

View file

@ -1,53 +1,11 @@
[DOMRect-001.xht] [DOMRect-001.xht]
type: testharness type: testharness
[testConstructor0]
expected: FAIL
[testConstructor4]
expected: FAIL
[testConstructor5]
expected: FAIL
[testConstructorNegativeWidth]
expected: FAIL
[testConstructorNegativeHeight]
expected: FAIL
[testConstructorNegativeWidthHeight]
expected: FAIL
[testConstructorUndefined1] [testConstructorUndefined1]
expected: FAIL expected: FAIL
[testConstructorUndefined2]
expected: FAIL
[testConstructorString1]
expected: FAIL
[testConstructorString2]
expected: FAIL
[testConstructorIllegal1] [testConstructorIllegal1]
expected: FAIL expected: FAIL
[testConstructorIllegal2] [testConstructorIllegal2]
expected: FAIL expected: FAIL
[testSetReadOnlyAttributes]
expected: FAIL
[testSetAttributes]
expected: FAIL
[testConstructor1]
expected: FAIL
[testConstructor2]
expected: FAIL
[testConstructor3]
expected: FAIL

View file

@ -82,6 +82,7 @@ var interfaceNamesInGlobalScope = [
"DOMPoint", "DOMPoint",
"DOMPointReadOnly", "DOMPointReadOnly",
"DOMRect", "DOMRect",
"DOMRectReadOnly",
"Comment", "Comment",
"Console", "Console",
"CustomEvent", "CustomEvent",