Skip to content

Commit

Permalink
fix: use clientWidth/clientHeight instead of getBoundingClientRect (#…
Browse files Browse the repository at this point in the history
…1395)

* fix: use clientWidth/clientHeight instead of getBoundingClientRect

* fix(test): properly mock clientWidth and clientHeight
  • Loading branch information
evertheylen committed Jan 31, 2023
1 parent 3ce44e0 commit 1067900
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/svg/Svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,19 +330,19 @@ export class Svg {
}

/**
* Get element height using `getBoundingClientRect`
* Get element height using `clientHeight`
* @return The elements height in pixels
*/
height() {
return this._node.getBoundingClientRect().height;
return this._node.clientHeight;
}

/**
* Get element width using `getBoundingClientRect`
* Get element width using `clientWidth`
* @return The elements width in pixels
*/
width() {
return this._node.getBoundingClientRect().width;
return this._node.clientWidth;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test/mock/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,32 @@ export function mockDomRects() {
bottom: 0,
left: 0
});

Object.defineProperties(SVGElement.prototype, {
clientWidth: {
configurable: true,
get: () => 500
},
clientHeight: {
configurable: true,
get: () => 500
}
});
}

export function destroyMockDomRects() {
SVGElement.prototype.getBoundingClientRect = getBoundingClientRect;

// Redefine clientWidth and clientHeight properties from the prototype of SVGElement
const ElementPrototype = Object.getPrototypeOf(SVGElement.prototype);
Object.defineProperties(SVGElement.prototype, {
clientWidth: Object.getOwnPropertyDescriptor(
ElementPrototype,
'clientWidth'
)!,
clientHeight: Object.getOwnPropertyDescriptor(
ElementPrototype,
'clientHeight'
)!
});
}

0 comments on commit 1067900

Please sign in to comment.