Skip to content

Commit

Permalink
feat: Add fromRect method
Browse files Browse the repository at this point in the history
  • Loading branch information
btea committed Apr 14, 2024
1 parent c29f36c commit 58b9b4a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/happy-dom/src/nodes/element/DOMRect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,22 @@ export default class DOMRect {
this.y = y || 0;
this.width = width || 0;
this.height = height || 0;
this.top = this.y;
this.right = this.x + this.width;
this.bottom = this.y + this.height;
this.left = this.x;
}
static fromRect(rectangle: {

Check failure on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Missing accessibility modifier on method definition fromRect

Check failure on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Missing return type on function

Check warning on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Missing JSDoc comment

Check warning on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `⏎↹↹`

Check failure on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Missing accessibility modifier on method definition fromRect

Check failure on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Missing return type on function

Check warning on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Missing JSDoc comment

Check warning on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `⏎↹↹`

Check failure on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Missing accessibility modifier on method definition fromRect

Check failure on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Missing return type on function

Check warning on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Missing JSDoc comment

Check warning on line 34 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `⏎↹↹`
x: number;

Check warning on line 35 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `↹`

Check warning on line 35 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `↹`

Check warning on line 35 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `↹`
y: number;

Check warning on line 36 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `↹`

Check warning on line 36 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `↹`

Check warning on line 36 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `↹`
width: number;

Check warning on line 37 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `↹`

Check warning on line 37 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `↹`

Check warning on line 37 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `↹`
height: number;

Check warning on line 38 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `↹`

Check warning on line 38 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `↹`

Check warning on line 38 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `↹`
} = {

Check warning on line 39 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `↹`

Check warning on line 39 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `↹`

Check warning on line 39 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `↹`
x: 0,

Check warning on line 40 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `↹`

Check warning on line 40 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `↹`

Check warning on line 40 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `↹`
y: 0,

Check warning on line 41 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `↹`

Check warning on line 41 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `↹`

Check warning on line 41 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `↹`
width: 0,

Check warning on line 42 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `↹`

Check warning on line 42 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `↹`

Check warning on line 42 in packages/happy-dom/src/nodes/element/DOMRect.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `↹`
height: 0
}) {
return new DOMRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
}
25 changes: 25 additions & 0 deletions packages/happy-dom/test/nodes/element/Element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,31 @@ describe('Element', () => {
});
});

describe('creates a new DOMRect object', () => {
it('with the correct values.', () => {
const domRect = new DOMRect(1, 2, 3, 4);
expect(domRect.x).toBe(1);
expect(domRect.y).toBe(2);
expect(domRect.width).toBe(3);
expect(domRect.height).toBe(4);
expect(domRect.top).toBe(2);
expect(domRect.right).toBe(4);
expect(domRect.bottom).toBe(6);
expect(domRect.left).toBe(1);
});
it('with the correct values when using the fromRect method.', () => {
const domRect = DOMRect.fromRect({ x: 1, y: 2, width: 3, height: 4 });
expect(domRect.x).toBe(1);
expect(domRect.y).toBe(2);
expect(domRect.width).toBe(3);
expect(domRect.height).toBe(4);
expect(domRect.top).toBe(2);
expect(domRect.right).toBe(4);
expect(domRect.bottom).toBe(6);
expect(domRect.left).toBe(1);
});
})

describe('cloneNode()', () => {
it('Clones the properties of the element when cloned.', () => {
const child = document.createElement('div');
Expand Down

0 comments on commit 58b9b4a

Please sign in to comment.