diff options
author | Till Schneidereit <till@tillschneidereit.net> | 2015-09-02 14:16:34 +0200 |
---|---|---|
committer | Till Schneidereit <till@tillschneidereit.net> | 2015-10-17 22:47:01 +0200 |
commit | 4c1c05fac67903ca5e1d06a1e5be04a90a1766e2 (patch) | |
tree | 0f8dbc8e4872457c9582a70d3d0cfd082c6cb267 | |
parent | 499a84714139a351e8ac9c08b5a8e26440c77209 (diff) | |
download | servo-4c1c05fac67903ca5e1d06a1e5be04a90a1766e2.tar.gz servo-4c1c05fac67903ca5e1d06a1e5be04a90a1766e2.zip |
Improve implementation of DOMRect and implement DOMRectReadOnly
Passes most tests from test-css.
-rw-r--r-- | components/script/devtools.rs | 4 | ||||
-rw-r--r-- | components/script/dom/domrect.rs | 83 | ||||
-rw-r--r-- | components/script/dom/domrectreadonly.rs | 102 | ||||
-rw-r--r-- | components/script/dom/element.rs | 20 | ||||
-rw-r--r-- | components/script/dom/mod.rs | 1 | ||||
-rw-r--r-- | components/script/dom/webidls/DOMRect.webidl | 17 | ||||
-rw-r--r-- | components/script/dom/webidls/DOMRectReadOnly.webidl | 29 | ||||
-rw-r--r-- | tests/wpt/metadata-css/geometry-1_dev/html/DOMRect-001.htm.ini | 41 | ||||
-rw-r--r-- | tests/wpt/metadata-css/geometry-1_dev/xhtml1/DOMRect-001.xht.ini | 42 | ||||
-rw-r--r-- | tests/wpt/metadata-css/geometry-1_dev/xhtml1print/DOMRect-001.xht.ini | 42 | ||||
-rw-r--r-- | tests/wpt/mozilla/tests/mozilla/interfaces.html | 1 |
11 files changed, 198 insertions, 184 deletions
diff --git a/components/script/devtools.rs b/components/script/devtools.rs index 0e892f32e6a..2356a4896f9 100644 --- a/components/script/devtools.rs +++ b/components/script/devtools.rs @@ -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(); } diff --git a/components/script/dom/domrect.rs b/components/script/dom/domrect.rs index 6706642b5c5..fa1dcc1b165 100644 --- a/components/script/dom/domrect.rs +++ b/components/script/dom/domrect.rs @@ -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); } } diff --git a/components/script/dom/domrectreadonly.rs b/components/script/dom/domrectreadonly.rs new file mode 100644 index 00000000000..a6ba99ead2a --- /dev/null +++ b/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 } + } +} diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 651142df713..089d3199058 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -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; @@ -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) } @@ -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 diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs index 05d89c232df..c8179c3f0e3 100644 --- a/components/script/dom/mod.rs +++ b/components/script/dom/mod.rs @@ -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; diff --git a/components/script/dom/webidls/DOMRect.webidl b/components/script/dom/webidls/DOMRect.webidl index 6e0fe24b57d..9ea5933c3f9 100644 --- a/components/script/dom/webidls/DOMRect.webidl +++ b/components/script/dom/webidls/DOMRect.webidl @@ -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; }; diff --git a/components/script/dom/webidls/DOMRectReadOnly.webidl b/components/script/dom/webidls/DOMRectReadOnly.webidl new file mode 100644 index 00000000000..937ed4eb478 --- /dev/null +++ b/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; +}; diff --git a/tests/wpt/metadata-css/geometry-1_dev/html/DOMRect-001.htm.ini b/tests/wpt/metadata-css/geometry-1_dev/html/DOMRect-001.htm.ini index d2c334283b1..582463bd683 100644 --- a/tests/wpt/metadata-css/geometry-1_dev/html/DOMRect-001.htm.ini +++ b/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 diff --git a/tests/wpt/metadata-css/geometry-1_dev/xhtml1/DOMRect-001.xht.ini b/tests/wpt/metadata-css/geometry-1_dev/xhtml1/DOMRect-001.xht.ini index a1345a2bfbf..d23e08618a6 100644 --- a/tests/wpt/metadata-css/geometry-1_dev/xhtml1/DOMRect-001.xht.ini +++ b/tests/wpt/metadata-css/geometry-1_dev/xhtml1/DOMRect-001.xht.ini @@ -1,53 +1,11 @@ [DOMRect-001.xht] 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] - expected: FAIL - diff --git a/tests/wpt/metadata-css/geometry-1_dev/xhtml1print/DOMRect-001.xht.ini b/tests/wpt/metadata-css/geometry-1_dev/xhtml1print/DOMRect-001.xht.ini index a1345a2bfbf..d23e08618a6 100644 --- a/tests/wpt/metadata-css/geometry-1_dev/xhtml1print/DOMRect-001.xht.ini +++ b/tests/wpt/metadata-css/geometry-1_dev/xhtml1print/DOMRect-001.xht.ini @@ -1,53 +1,11 @@ [DOMRect-001.xht] 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] - expected: FAIL - diff --git a/tests/wpt/mozilla/tests/mozilla/interfaces.html b/tests/wpt/mozilla/tests/mozilla/interfaces.html index 5d20c9220c3..ef3ae24d9fe 100644 --- a/tests/wpt/mozilla/tests/mozilla/interfaces.html +++ b/tests/wpt/mozilla/tests/mozilla/interfaces.html @@ -82,6 +82,7 @@ var interfaceNamesInGlobalScope = [ "DOMPoint", "DOMPointReadOnly", "DOMRect", + "DOMRectReadOnly", "Comment", "Console", "CustomEvent", |