|
| 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; |
0 commit comments