@@ -34,6 +34,39 @@ export function addPolyfill(windowObj = window) {
34
34
}
35
35
}
36
36
37
+ /**
38
+ * @param {globalThis } windowObj?
39
+ */
40
+ export function patchAdoptNode ( windowObj = window ) {
41
+ if ( ! windowObj . Node . prototype . getBoxQuads ) {
42
+ //@ts -ignore
43
+ windowObj . Node . prototype . getBoxQuads = function ( options ) {
44
+ return getBoxQuads ( this , options )
45
+ }
46
+ }
47
+
48
+ if ( ! windowObj . Node . prototype . convertQuadFromNode ) {
49
+ //@ts -ignore
50
+ windowObj . Node . prototype . convertQuadFromNode = function ( quad , from , options ) {
51
+ return convertQuadFromNode ( this , quad , from , options )
52
+ }
53
+ }
54
+
55
+ if ( ! windowObj . Node . prototype . convertRectFromNode ) {
56
+ //@ts -ignore
57
+ windowObj . Node . prototype . convertRectFromNode = function ( rect , from , options ) {
58
+ return convertRectFromNode ( this , rect , from , options )
59
+ }
60
+ }
61
+
62
+ if ( ! windowObj . Node . prototype . convertPointFromNode ) {
63
+ //@ts -ignore
64
+ windowObj . Node . prototype . convertPointFromNode = function ( point , from , options ) {
65
+ return convertPointFromNode ( this , point , from , options )
66
+ }
67
+ }
68
+ }
69
+
37
70
/**
38
71
* @param {Node } node
39
72
* @param {DOMQuadInit } quad
@@ -48,7 +81,7 @@ export function convertQuadFromNode(node, quad, from, options) {
48
81
quad = new DOMQuad ( transformPointBox ( quad . p1 , options . fromBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( from ) , - 1 ) , transformPointBox ( quad . p2 , options . fromBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( from ) , - 1 ) , transformPointBox ( quad . p3 , options . fromBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( from ) , - 1 ) , transformPointBox ( quad . p4 , options . fromBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( from ) , - 1 ) )
49
82
}
50
83
let res = new DOMQuad ( m2 . transformPoint ( m1 . transformPoint ( quad . p1 ) ) , m2 . transformPoint ( m1 . transformPoint ( quad . p2 ) ) , m2 . transformPoint ( m1 . transformPoint ( quad . p3 ) ) , m2 . transformPoint ( m1 . transformPoint ( quad . p4 ) ) ) ;
51
- if ( options ?. toBox && options ?. toBox !== 'border' && node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) {
84
+ if ( options ?. toBox && options ?. toBox !== 'border' && ( node instanceof Element || node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) ) {
52
85
res = new DOMQuad ( transformPointBox ( res . p1 , options . toBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) , - 1 ) , transformPointBox ( res . p2 , options . toBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) , - 1 ) , transformPointBox ( res . p3 , options . toBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) , - 1 ) , transformPointBox ( res . p4 , options . toBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) , - 1 ) )
53
86
}
54
87
return res ;
@@ -70,7 +103,7 @@ export function convertRectFromNode(node, rect, from, options) {
70
103
rect = new DOMRect ( p . x , p . y , rect . width , rect . height ) ;
71
104
}
72
105
let res = new DOMQuad ( m2 . transformPoint ( m1 . transformPoint ( new DOMPoint ( rect . x , rect . y ) ) ) , m2 . transformPoint ( m1 . transformPoint ( new DOMPoint ( rect . x + rect . width , rect . y ) ) ) , m2 . transformPoint ( m1 . transformPoint ( new DOMPoint ( rect . x + rect . width , rect . y + rect . height ) ) ) , m2 . transformPoint ( m1 . transformPoint ( new DOMPoint ( rect . x , rect . y + rect . height ) ) ) ) ;
73
- if ( options ?. toBox && options ?. toBox !== 'border' && node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) {
106
+ if ( options ?. toBox && options ?. toBox !== 'border' && ( node instanceof Element || node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) ) {
74
107
res = new DOMQuad ( transformPointBox ( res . p1 , options . toBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) , - 1 ) , transformPointBox ( res . p2 , options . toBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) , - 1 ) , transformPointBox ( res . p3 , options . toBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) , - 1 ) , transformPointBox ( res . p4 , options . toBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) , - 1 ) )
75
108
}
76
109
return res ;
@@ -90,7 +123,7 @@ export function convertPointFromNode(node, point, from, options) {
90
123
point = transformPointBox ( point , options . fromBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( from ) , 1 ) ;
91
124
}
92
125
let res = m2 . transformPoint ( m1 . transformPoint ( point ) ) ;
93
- if ( options ?. toBox && options ?. toBox !== 'border' && node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) {
126
+ if ( options ?. toBox && options ?. toBox !== 'border' && ( node instanceof Element || node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) ) {
94
127
res = transformPointBox ( res , options . toBox , ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) , - 1 ) ;
95
128
}
96
129
return res ;
@@ -165,7 +198,7 @@ export function getBoxQuads(node, options) {
165
198
166
199
/** @type {{x: number, y:number}[] } */
167
200
let o = null ;
168
- if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) {
201
+ if ( ( node instanceof Element || node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) ) {
169
202
if ( options ?. box === 'margin' ) {
170
203
const cs = ( node . ownerDocument . defaultView ?? window ) . getComputedStyle ( node ) ;
171
204
o = [ { x : parseFloat ( cs . marginLeft ) , y : parseFloat ( cs . marginTop ) } , { x : - parseFloat ( cs . marginRight ) , y : parseFloat ( cs . marginTop ) } , { x : - parseFloat ( cs . marginRight ) , y : - parseFloat ( cs . marginBottom ) } , { x : parseFloat ( cs . marginLeft ) , y : - parseFloat ( cs . marginBottom ) } ] ;
@@ -228,21 +261,21 @@ function as2DPoint(point) {
228
261
export function getElementSize ( node , matrix ) {
229
262
let width = 0 ;
230
263
let height = 0 ;
231
- if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . HTMLElement ) {
264
+ if ( ( node instanceof HTMLElement || node instanceof ( node . ownerDocument . defaultView ?? window ) . HTMLElement ) ) {
232
265
width = node . offsetWidth ;
233
266
height = node . offsetHeight ;
234
- } else if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . SVGSVGElement ) {
267
+ } else if ( ( node instanceof SVGSVGElement || node instanceof ( node . ownerDocument . defaultView ?? window ) . SVGSVGElement ) ) {
235
268
width = node . width . baseVal . value
236
269
height = node . height . baseVal . value
237
- } else if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . SVGGraphicsElement ) {
270
+ } else if ( ( node instanceof SVGGraphicsElement || node instanceof ( node . ownerDocument . defaultView ?? window ) . SVGGraphicsElement ) ) {
238
271
const bbox = node . getBBox ( )
239
272
width = bbox . width ;
240
273
height = bbox . height ;
241
- } else if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . MathMLElement ) {
274
+ } else if ( ( node instanceof MathMLElement || node instanceof ( node . ownerDocument . defaultView ?? window ) . MathMLElement ) ) {
242
275
const bbox = node . getBoundingClientRect ( )
243
276
width = bbox . width / ( matrix ?. a ?? 1 ) ;
244
277
height = bbox . height / ( matrix ?. d ?? 1 ) ;
245
- } else if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . Text ) {
278
+ } else if ( ( node instanceof Text || node instanceof ( node . ownerDocument . defaultView ?? window ) . Text ) ) {
246
279
const range = document . createRange ( ) ;
247
280
range . selectNodeContents ( node ) ;
248
281
const targetRect = range . getBoundingClientRect ( ) ;
@@ -258,9 +291,9 @@ export function getElementSize(node, matrix) {
258
291
* @param {HTMLIFrameElement[] } iframes
259
292
*/
260
293
function getElementOffsetsInContainer ( node , includeScroll , iframes ) {
261
- if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . HTMLElement ) {
294
+ if ( ( node instanceof HTMLElement || node instanceof ( node . ownerDocument . defaultView ?? window ) . HTMLElement ) ) {
262
295
return new DOMPoint ( node . offsetLeft - ( includeScroll ? node . scrollLeft : 0 ) , node . offsetTop - ( includeScroll ? node . scrollTop : 0 ) ) ;
263
- } else if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . Text ) {
296
+ } else if ( ( node instanceof Text || node instanceof ( node . ownerDocument . defaultView ?? window ) . Text ) ) {
264
297
const range = document . createRange ( ) ;
265
298
range . selectNodeContents ( node ) ;
266
299
const r1 = range . getBoundingClientRect ( ) ;
@@ -271,8 +304,8 @@ function getElementOffsetsInContainer(node, includeScroll, iframes) {
271
304
const zX = parent . offsetWidth / r2 . width ;
272
305
const zY = parent . offsetHeight / r2 . height ;
273
306
return new DOMPoint ( ( r1 . x - r2 . x ) * zX , ( r1 . y - r2 . y ) * zY ) ;
274
- } else if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) {
275
- if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . SVGGraphicsElement && ! ( node instanceof ( node . ownerDocument . defaultView ?? window ) . SVGSVGElement ) ) {
307
+ } else if ( ( node instanceof Element || node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) ) {
308
+ if ( ( node instanceof SVGGraphicsElement || node instanceof ( node . ownerDocument . defaultView ?? window ) . SVGGraphicsElement ) && ! ( ( node instanceof SVGSVGElement || node instanceof ( node . ownerDocument . defaultView ?? window ) . SVGSVGElement ) ) ) {
276
309
const bb = node . getBBox ( ) ;
277
310
return new DOMPoint ( bb . x , bb . y ) ;
278
311
}
@@ -339,8 +372,8 @@ export function getResultingTransformationBetweenElementAndAllAncestors(node, an
339
372
}
340
373
else {
341
374
342
- if ( actualElement instanceof ( actualElement . ownerDocument . defaultView ?? window ) . HTMLElement ) {
343
- if ( lastOffsetParent !== actualElement . offsetParent && ! ( actualElement instanceof ( actualElement . ownerDocument . defaultView ?? window ) . HTMLSlotElement ) ) {
375
+ if ( ( actualElement instanceof HTMLElement || actualElement instanceof ( actualElement . ownerDocument . defaultView ?? window ) . HTMLElement ) ) {
376
+ if ( lastOffsetParent !== actualElement . offsetParent && ! ( ( actualElement instanceof HTMLSlotElement || actualElement instanceof ( actualElement . ownerDocument . defaultView ?? window ) . HTMLSlotElement ) ) ) {
344
377
const offsets = getElementOffsetsInContainer ( actualElement , actualElement !== node , iframes ) ;
345
378
lastOffsetParent = actualElement . offsetParent ;
346
379
const mvMat = new DOMMatrix ( ) . translate ( offsets . x , offsets . y ) ;
@@ -389,14 +422,14 @@ export function getResultingTransformationBetweenElementAndAllAncestors(node, an
389
422
* @returns {Element }
390
423
*/
391
424
function getParentElementIncludingSlots ( node , iframes ) {
392
- if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . Element && node . assignedSlot )
425
+ if ( ( node instanceof Element || node instanceof ( node . ownerDocument . defaultView ?? window ) . Element ) && node . assignedSlot )
393
426
return node . assignedSlot ;
394
427
if ( node . parentElement == null ) {
395
- if ( node . parentNode instanceof ( node . ownerDocument . defaultView ?? window ) . ShadowRoot ) {
428
+ if ( ( node . parentNode instanceof ShadowRoot || node . parentNode instanceof ( node . ownerDocument . defaultView ?? window ) . ShadowRoot ) ) {
396
429
return node . parentNode . host ;
397
430
}
398
431
}
399
- if ( node instanceof ( node . ownerDocument . defaultView ?? window ) . HTMLHtmlElement ) {
432
+ if ( ( node instanceof HTMLHtmlElement || node instanceof ( node . ownerDocument . defaultView ?? window ) . HTMLHtmlElement ) ) {
400
433
if ( iframes ) {
401
434
for ( const f of iframes )
402
435
if ( f ?. contentDocument == node . ownerDocument )
@@ -411,7 +444,7 @@ function getParentElementIncludingSlots(node, iframes) {
411
444
* @param {HTMLIFrameElement[]= } iframes
412
445
*/
413
446
export function getElementCombinedTransform ( element , iframes ) {
414
- if ( element instanceof ( element . ownerDocument . defaultView ?? window ) . Text )
447
+ if ( ( element instanceof Text || element instanceof ( element . ownerDocument . defaultView ?? window ) . Text ) )
415
448
return new DOMMatrix ;
416
449
417
450
//https://www.w3.org/TR/css-transforms-2/#ctm
0 commit comments