Skip to content

Commit c9d6b72

Browse files
TimothyGudomenic
authored andcommitted
Implement DOMRect and DOMRectReadOnly
Extracted from #2926. Part of #2716.
1 parent 8d7155a commit c9d6b72

File tree

7 files changed

+176
-1
lines changed

7 files changed

+176
-1
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use strict";
2+
const DOMRectReadOnlyImpl = require("./DOMRectReadOnly-impl").implementation;
3+
const DOMRect = require("../generated/DOMRect");
4+
5+
class DOMRectImpl extends DOMRectReadOnlyImpl {
6+
static fromRect(globalObject, other) {
7+
return DOMRect.createImpl(globalObject, [other.x, other.y, other.width, other.height]);
8+
}
9+
10+
get x() {
11+
return super.x;
12+
}
13+
set x(newX) {
14+
this._x = newX;
15+
}
16+
17+
get y() {
18+
return super.y;
19+
}
20+
set y(newY) {
21+
this._y = newY;
22+
}
23+
24+
get width() {
25+
return super.width;
26+
}
27+
set width(newWidth) {
28+
this._width = newWidth;
29+
}
30+
31+
get height() {
32+
return super.height;
33+
}
34+
set height(newHeight) {
35+
this._height = newHeight;
36+
}
37+
}
38+
39+
exports.implementation = DOMRectImpl;
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://drafts.fxtf.org/geometry/#domrect
2+
3+
// Commented out for https://github.com/w3c/svgwg/issues/706
4+
// [LegacyWindowAlias=SVGRect]
5+
[Exposed=(Window,Worker),
6+
Serializable]
7+
interface DOMRect : DOMRectReadOnly {
8+
constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
9+
optional unrestricted double width = 0, optional unrestricted double height = 0);
10+
11+
[NewObject, WebIDL2JSCallWithGlobal] static DOMRect fromRect(optional DOMRectInit other = {});
12+
13+
inherit attribute unrestricted double x;
14+
inherit attribute unrestricted double y;
15+
inherit attribute unrestricted double width;
16+
inherit attribute unrestricted double height;
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"use strict";
2+
const DOMRectReadOnly = require("../generated/DOMRectReadOnly");
3+
4+
class DOMRectReadOnlyImpl {
5+
constructor(globalObject, [x = 0, y = 0, width = 0, height = 0]) {
6+
this._globalObject = globalObject;
7+
this._x = x;
8+
this._y = y;
9+
this._width = width;
10+
this._height = height;
11+
}
12+
13+
static fromRect(globalObject, other) {
14+
return DOMRectReadOnly.createImpl(globalObject, [other.x, other.y, other.width, other.height]);
15+
}
16+
17+
get x() {
18+
return this._x;
19+
}
20+
21+
get y() {
22+
return this._y;
23+
}
24+
25+
get width() {
26+
return this._width;
27+
}
28+
29+
get height() {
30+
return this._height;
31+
}
32+
33+
get top() {
34+
const { height, y } = this;
35+
// We use Math.min's built-in NaN handling: https://github.com/w3c/fxtf-drafts/issues/222
36+
return Math.min(y, y + height);
37+
}
38+
39+
get right() {
40+
const { width, x } = this;
41+
// We use Math.max's built-in NaN handling: https://github.com/w3c/fxtf-drafts/issues/222
42+
return Math.max(x, x + width);
43+
}
44+
45+
get bottom() {
46+
const { height, y } = this;
47+
// We use Math.max's built-in NaN handling: https://github.com/w3c/fxtf-drafts/issues/222
48+
return Math.max(y, y + height);
49+
}
50+
51+
get left() {
52+
const { width, x } = this;
53+
// We use Math.min's built-in NaN handling: https://github.com/w3c/fxtf-drafts/issues/222
54+
return Math.min(x, x + width);
55+
}
56+
57+
// Could be removed after https://github.com/jsdom/webidl2js/issues/185 gets fixed.
58+
toJSON() {
59+
return {
60+
x: this.x,
61+
y: this.y,
62+
width: this.width,
63+
height: this.height,
64+
top: this.top,
65+
right: this.right,
66+
bottom: this.bottom,
67+
left: this.left
68+
};
69+
}
70+
}
71+
72+
exports.implementation = DOMRectReadOnlyImpl;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://drafts.fxtf.org/geometry/#domrectreadonly
2+
[Exposed=(Window,Worker),
3+
Serializable]
4+
interface DOMRectReadOnly {
5+
constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
6+
optional unrestricted double width = 0, optional unrestricted double height = 0);
7+
8+
[NewObject, WebIDL2JSCallWithGlobal] static DOMRectReadOnly fromRect(optional DOMRectInit other = {});
9+
10+
readonly attribute unrestricted double x;
11+
readonly attribute unrestricted double y;
12+
readonly attribute unrestricted double width;
13+
readonly attribute unrestricted double height;
14+
readonly attribute unrestricted double top;
15+
readonly attribute unrestricted double right;
16+
readonly attribute unrestricted double bottom;
17+
readonly attribute unrestricted double left;
18+
19+
// https://github.com/jsdom/webidl2js/issues/185
20+
[Default] object toJSON();
21+
};
22+
23+
dictionary DOMRectInit {
24+
unrestricted double x = 0;
25+
unrestricted double y = 0;
26+
unrestricted double width = 0;
27+
unrestricted double height = 0;
28+
};

lib/jsdom/living/interfaces.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ const generatedInterfaces = {
190190

191191
Headers: require("./generated/Headers"),
192192
AbortController: require("./generated/AbortController"),
193-
AbortSignal: require("./generated/AbortSignal")
193+
AbortSignal: require("./generated/AbortSignal"),
194+
195+
DOMRectReadOnly: require("./generated/DOMRectReadOnly"),
196+
DOMRect: require("./generated/DOMRect")
194197
};
195198

196199
function install(window, name, interfaceConstructor) {

scripts/webidl/convert.js

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ addDir("../../lib/jsdom/living/domparsing");
152152
addDir("../../lib/jsdom/living/events");
153153
addDir("../../lib/jsdom/living/fetch");
154154
addDir("../../lib/jsdom/living/file-api");
155+
addDir("../../lib/jsdom/living/geometry");
155156
addDir("../../lib/jsdom/living/hr-time");
156157
addDir("../../lib/jsdom/living/mutation-observer");
157158
addDir("../../lib/jsdom/living/navigator");

test/web-platform-tests/to-run.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,21 @@ ttwf-js-cssomview-getclientrects-length.html: [fail, Unknown]
132132
window-screen-height.html: [fail, Test not applicable - no output device]
133133
window-screen-width.html: [fail, Test not applicable - no output device]
134134

135+
136+
---
137+
138+
DIR: css/geometry
139+
140+
DOMMatrix*: [fail, not implemented]
141+
DOMPoint*: [fail, not implemented]
142+
DOMQuad*: [fail, not implemented]
143+
DOMRectList.html: [fail, not implemented]
144+
WebKitCSSMatrix.html: [fail, not implemented]
145+
historical.html: [fail, needs DOMPoint DOMQuad DOMMatrix]
146+
idlharness.any.html: [fail, Depends on Fetch]
147+
spec-examples.html: [fail, needs DOMPoint DOMQuad DOMMatrix]
148+
structured-serialization.html: [fail, need MessageChannel]
149+
135150
---
136151

137152
DIR: custom-elements

0 commit comments

Comments
 (0)