Skip to content

Commit 1067900

Browse files
authored
fix: use clientWidth/clientHeight instead of getBoundingClientRect (#1395)
* fix: use clientWidth/clientHeight instead of getBoundingClientRect * fix(test): properly mock clientWidth and clientHeight
1 parent 3ce44e0 commit 1067900

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/svg/Svg.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,19 +330,19 @@ export class Svg {
330330
}
331331

332332
/**
333-
* Get element height using `getBoundingClientRect`
333+
* Get element height using `clientHeight`
334334
* @return The elements height in pixels
335335
*/
336336
height() {
337-
return this._node.getBoundingClientRect().height;
337+
return this._node.clientHeight;
338338
}
339339

340340
/**
341-
* Get element width using `getBoundingClientRect`
341+
* Get element width using `clientWidth`
342342
* @return The elements width in pixels
343343
*/
344344
width() {
345-
return this._node.getBoundingClientRect().width;
345+
return this._node.clientWidth;
346346
}
347347

348348
/**

test/mock/dom.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,32 @@ export function mockDomRects() {
4242
bottom: 0,
4343
left: 0
4444
});
45+
46+
Object.defineProperties(SVGElement.prototype, {
47+
clientWidth: {
48+
configurable: true,
49+
get: () => 500
50+
},
51+
clientHeight: {
52+
configurable: true,
53+
get: () => 500
54+
}
55+
});
4556
}
4657

4758
export function destroyMockDomRects() {
4859
SVGElement.prototype.getBoundingClientRect = getBoundingClientRect;
60+
61+
// Redefine clientWidth and clientHeight properties from the prototype of SVGElement
62+
const ElementPrototype = Object.getPrototypeOf(SVGElement.prototype);
63+
Object.defineProperties(SVGElement.prototype, {
64+
clientWidth: Object.getOwnPropertyDescriptor(
65+
ElementPrototype,
66+
'clientWidth'
67+
)!,
68+
clientHeight: Object.getOwnPropertyDescriptor(
69+
ElementPrototype,
70+
'clientHeight'
71+
)!
72+
});
4973
}

0 commit comments

Comments
 (0)