1
1
import type { Options } from './types'
2
2
import { clonePseudoElements } from './clone-pseudos'
3
- import { createImage , toArray } from './util'
3
+ import { createImage , toArray , isInstanceOfElement } from './util'
4
4
import { getMimeType } from './mimes'
5
5
import { resourceToDataURL } from './dataurl'
6
6
@@ -49,15 +49,15 @@ async function cloneSingleNode<T extends HTMLElement>(
49
49
node : T ,
50
50
options : Options ,
51
51
) : Promise < HTMLElement > {
52
- if ( node instanceof HTMLCanvasElement ) {
52
+ if ( isInstanceOfElement ( node , HTMLCanvasElement ) ) {
53
53
return cloneCanvasElement ( node )
54
54
}
55
55
56
- if ( node instanceof HTMLVideoElement ) {
56
+ if ( isInstanceOfElement ( node , HTMLVideoElement ) ) {
57
57
return cloneVideoElement ( node , options )
58
58
}
59
59
60
- if ( node instanceof HTMLIFrameElement ) {
60
+ if ( isInstanceOfElement ( node , HTMLIFrameElement ) ) {
61
61
return cloneIFrameElement ( node )
62
62
}
63
63
@@ -72,12 +72,23 @@ async function cloneChildren<T extends HTMLElement>(
72
72
clonedNode : T ,
73
73
options : Options ,
74
74
) : Promise < T > {
75
- const children =
76
- isSlotElement ( nativeNode ) && nativeNode . assignedNodes
77
- ? toArray < T > ( nativeNode . assignedNodes ( ) )
78
- : toArray < T > ( ( nativeNode . shadowRoot ?? nativeNode ) . childNodes )
75
+ let children : T [ ] = [ ]
76
+
77
+ if ( isSlotElement ( nativeNode ) && nativeNode . assignedNodes ) {
78
+ children = toArray < T > ( nativeNode . assignedNodes ( ) )
79
+ } else if (
80
+ isInstanceOfElement ( nativeNode , HTMLIFrameElement ) &&
81
+ nativeNode . contentDocument ?. body
82
+ ) {
83
+ children = toArray < T > ( nativeNode . contentDocument . body . childNodes )
84
+ } else {
85
+ children = toArray < T > ( ( nativeNode . shadowRoot ?? nativeNode ) . childNodes )
86
+ }
79
87
80
- if ( children . length === 0 || nativeNode instanceof HTMLVideoElement ) {
88
+ if (
89
+ children . length === 0 ||
90
+ isInstanceOfElement ( nativeNode , HTMLVideoElement )
91
+ ) {
81
92
return clonedNode
82
93
}
83
94
@@ -114,9 +125,19 @@ function cloneCSSStyle<T extends HTMLElement>(nativeNode: T, clonedNode: T) {
114
125
Math . floor ( parseFloat ( value . substring ( 0 , value . length - 2 ) ) ) - 0.1
115
126
value = `${ reducedFont } px`
116
127
}
128
+
129
+ if (
130
+ isInstanceOfElement ( nativeNode , HTMLIFrameElement ) &&
131
+ name === 'display' &&
132
+ value === 'inline'
133
+ ) {
134
+ value = 'block'
135
+ }
136
+
117
137
if ( name === 'd' && clonedNode . getAttribute ( 'd' ) ) {
118
138
value = `path(${ clonedNode . getAttribute ( 'd' ) } )`
119
139
}
140
+
120
141
targetStyle . setProperty (
121
142
name ,
122
143
value ,
@@ -127,17 +148,17 @@ function cloneCSSStyle<T extends HTMLElement>(nativeNode: T, clonedNode: T) {
127
148
}
128
149
129
150
function cloneInputValue < T extends HTMLElement > ( nativeNode : T , clonedNode : T ) {
130
- if ( nativeNode instanceof HTMLTextAreaElement ) {
151
+ if ( isInstanceOfElement ( nativeNode , HTMLTextAreaElement ) ) {
131
152
clonedNode . innerHTML = nativeNode . value
132
153
}
133
154
134
- if ( nativeNode instanceof HTMLInputElement ) {
155
+ if ( isInstanceOfElement ( nativeNode , HTMLInputElement ) ) {
135
156
clonedNode . setAttribute ( 'value' , nativeNode . value )
136
157
}
137
158
}
138
159
139
160
function cloneSelectValue < T extends HTMLElement > ( nativeNode : T , clonedNode : T ) {
140
- if ( nativeNode instanceof HTMLSelectElement ) {
161
+ if ( isInstanceOfElement ( nativeNode , HTMLSelectElement ) ) {
141
162
const clonedSelect = clonedNode as any as HTMLSelectElement
142
163
const selectedOption = Array . from ( clonedSelect . children ) . find (
143
164
( child ) => nativeNode . value === child . getAttribute ( 'value' ) ,
@@ -150,7 +171,7 @@ function cloneSelectValue<T extends HTMLElement>(nativeNode: T, clonedNode: T) {
150
171
}
151
172
152
173
function decorate < T extends HTMLElement > ( nativeNode : T , clonedNode : T ) : T {
153
- if ( clonedNode instanceof Element ) {
174
+ if ( isInstanceOfElement ( clonedNode , Element ) ) {
154
175
cloneCSSStyle ( nativeNode , clonedNode )
155
176
clonePseudoElements ( nativeNode , clonedNode )
156
177
cloneInputValue ( nativeNode , clonedNode )
0 commit comments