Skip to content

Commit

Permalink
Implement DOMRect and DOMRectReadOnly
Browse files Browse the repository at this point in the history
Extracted from #2926. Part of #2716.
  • Loading branch information
TimothyGu authored and domenic committed May 27, 2023
1 parent 8d7155a commit c9d6b72
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 1 deletion.
39 changes: 39 additions & 0 deletions lib/jsdom/living/geometry/DOMRect-impl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
const DOMRectReadOnlyImpl = require("./DOMRectReadOnly-impl").implementation;
const DOMRect = require("../generated/DOMRect");

class DOMRectImpl extends DOMRectReadOnlyImpl {
static fromRect(globalObject, other) {
return DOMRect.createImpl(globalObject, [other.x, other.y, other.width, other.height]);
}

get x() {
return super.x;
}
set x(newX) {
this._x = newX;
}

get y() {
return super.y;
}
set y(newY) {
this._y = newY;
}

get width() {
return super.width;
}
set width(newWidth) {
this._width = newWidth;
}

get height() {
return super.height;
}
set height(newHeight) {
this._height = newHeight;
}
}

exports.implementation = DOMRectImpl;
17 changes: 17 additions & 0 deletions lib/jsdom/living/geometry/DOMRect.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://drafts.fxtf.org/geometry/#domrect

// Commented out for https://github.com/w3c/svgwg/issues/706
// [LegacyWindowAlias=SVGRect]
[Exposed=(Window,Worker),
Serializable]
interface DOMRect : DOMRectReadOnly {
constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double width = 0, optional unrestricted double height = 0);

[NewObject, WebIDL2JSCallWithGlobal] static DOMRect fromRect(optional DOMRectInit other = {});

inherit attribute unrestricted double x;
inherit attribute unrestricted double y;
inherit attribute unrestricted double width;
inherit attribute unrestricted double height;
};
72 changes: 72 additions & 0 deletions lib/jsdom/living/geometry/DOMRectReadOnly-impl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use strict";
const DOMRectReadOnly = require("../generated/DOMRectReadOnly");

class DOMRectReadOnlyImpl {
constructor(globalObject, [x = 0, y = 0, width = 0, height = 0]) {
this._globalObject = globalObject;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
}

static fromRect(globalObject, other) {
return DOMRectReadOnly.createImpl(globalObject, [other.x, other.y, other.width, other.height]);
}

get x() {
return this._x;
}

get y() {
return this._y;
}

get width() {
return this._width;
}

get height() {
return this._height;
}

get top() {
const { height, y } = this;
// We use Math.min's built-in NaN handling: https://github.com/w3c/fxtf-drafts/issues/222
return Math.min(y, y + height);
}

get right() {
const { width, x } = this;
// We use Math.max's built-in NaN handling: https://github.com/w3c/fxtf-drafts/issues/222
return Math.max(x, x + width);
}

get bottom() {
const { height, y } = this;
// We use Math.max's built-in NaN handling: https://github.com/w3c/fxtf-drafts/issues/222
return Math.max(y, y + height);
}

get left() {
const { width, x } = this;
// We use Math.min's built-in NaN handling: https://github.com/w3c/fxtf-drafts/issues/222
return Math.min(x, x + width);
}

// Could be removed after https://github.com/jsdom/webidl2js/issues/185 gets fixed.
toJSON() {
return {
x: this.x,
y: this.y,
width: this.width,
height: this.height,
top: this.top,
right: this.right,
bottom: this.bottom,
left: this.left
};
}
}

exports.implementation = DOMRectReadOnlyImpl;
28 changes: 28 additions & 0 deletions lib/jsdom/living/geometry/DOMRectReadOnly.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// https://drafts.fxtf.org/geometry/#domrectreadonly
[Exposed=(Window,Worker),
Serializable]
interface DOMRectReadOnly {
constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double width = 0, optional unrestricted double height = 0);

[NewObject, WebIDL2JSCallWithGlobal] 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://github.com/jsdom/webidl2js/issues/185
[Default] object toJSON();
};

dictionary DOMRectInit {
unrestricted double x = 0;
unrestricted double y = 0;
unrestricted double width = 0;
unrestricted double height = 0;
};
5 changes: 4 additions & 1 deletion lib/jsdom/living/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ const generatedInterfaces = {

Headers: require("./generated/Headers"),
AbortController: require("./generated/AbortController"),
AbortSignal: require("./generated/AbortSignal")
AbortSignal: require("./generated/AbortSignal"),

DOMRectReadOnly: require("./generated/DOMRectReadOnly"),
DOMRect: require("./generated/DOMRect")
};

function install(window, name, interfaceConstructor) {
Expand Down
1 change: 1 addition & 0 deletions scripts/webidl/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ addDir("../../lib/jsdom/living/domparsing");
addDir("../../lib/jsdom/living/events");
addDir("../../lib/jsdom/living/fetch");
addDir("../../lib/jsdom/living/file-api");
addDir("../../lib/jsdom/living/geometry");
addDir("../../lib/jsdom/living/hr-time");
addDir("../../lib/jsdom/living/mutation-observer");
addDir("../../lib/jsdom/living/navigator");
Expand Down
15 changes: 15 additions & 0 deletions test/web-platform-tests/to-run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ ttwf-js-cssomview-getclientrects-length.html: [fail, Unknown]
window-screen-height.html: [fail, Test not applicable - no output device]
window-screen-width.html: [fail, Test not applicable - no output device]


---

DIR: css/geometry

DOMMatrix*: [fail, not implemented]
DOMPoint*: [fail, not implemented]
DOMQuad*: [fail, not implemented]
DOMRectList.html: [fail, not implemented]
WebKitCSSMatrix.html: [fail, not implemented]
historical.html: [fail, needs DOMPoint DOMQuad DOMMatrix]
idlharness.any.html: [fail, Depends on Fetch]
spec-examples.html: [fail, needs DOMPoint DOMQuad DOMMatrix]
structured-serialization.html: [fail, need MessageChannel]

---

DIR: custom-elements
Expand Down

0 comments on commit c9d6b72

Please sign in to comment.