File tree Expand file tree Collapse file tree 8 files changed +717
-16
lines changed Expand file tree Collapse file tree 8 files changed +717
-16
lines changed Original file line number Diff line number Diff line change 5
5
dist
6
6
es
7
7
lib
8
+ temp
Original file line number Diff line number Diff line change 7
7
idNodeMap ,
8
8
INode ,
9
9
} from './types' ;
10
+ import { isElement } from './utils' ;
10
11
11
12
const tagMap : tagMap = {
12
13
script : 'noscript' ,
@@ -177,6 +178,25 @@ function buildNode(
177
178
}
178
179
}
179
180
}
181
+ if ( n . isShadowHost ) {
182
+ /**
183
+ * Since node is newly rebuilt, it should be a normal element
184
+ * without shadowRoot.
185
+ * But if there are some weird situations that has defined
186
+ * custom element in the scope before we rebuild node, it may
187
+ * register the shadowRoot earlier.
188
+ * The logic in the 'else' block is just a try-my-best solution
189
+ * for the corner case, please let we know if it is wrong and
190
+ * we can remove it.
191
+ */
192
+ if ( ! node . shadowRoot ) {
193
+ node . attachShadow ( { mode : 'open' } ) ;
194
+ } else {
195
+ while ( node . shadowRoot . firstChild ) {
196
+ node . shadowRoot . removeChild ( node . shadowRoot . firstChild ) ;
197
+ }
198
+ }
199
+ }
180
200
return node ;
181
201
case NodeType . Text :
182
202
return doc . createTextNode (
@@ -240,7 +260,11 @@ export function buildNodeWithSN(
240
260
continue ;
241
261
}
242
262
243
- node . appendChild ( childNode ) ;
263
+ if ( childN . isShadow && isElement ( node ) && node . shadowRoot ) {
264
+ node . shadowRoot . appendChild ( childNode ) ;
265
+ } else {
266
+ node . appendChild ( childNode ) ;
267
+ }
244
268
if ( afterAppend ) {
245
269
afterAppend ( childNode ) ;
246
270
}
Original file line number Diff line number Diff line change 8
8
MaskInputOptions ,
9
9
SlimDOMOptions ,
10
10
} from './types' ;
11
+ import { isElement } from './utils' ;
11
12
12
13
let _id = 1 ;
13
14
const tagNameRegex = RegExp ( '[^a-z0-9-_]' ) ;
@@ -622,26 +623,38 @@ export function serializeNodeWithId(
622
623
) {
623
624
preserveWhiteSpace = false ;
624
625
}
626
+ const bypassOptions = {
627
+ doc,
628
+ map,
629
+ blockClass,
630
+ blockSelector,
631
+ skipChild,
632
+ inlineStylesheet,
633
+ maskInputOptions,
634
+ slimDOMOptions,
635
+ recordCanvas,
636
+ preserveWhiteSpace,
637
+ onSerialize,
638
+ onIframeLoad,
639
+ iframeLoadTimeout,
640
+ } ;
625
641
for ( const childN of Array . from ( n . childNodes ) ) {
626
- const serializedChildNode = serializeNodeWithId ( childN , {
627
- doc,
628
- map,
629
- blockClass,
630
- blockSelector,
631
- skipChild,
632
- inlineStylesheet,
633
- maskInputOptions,
634
- slimDOMOptions,
635
- recordCanvas,
636
- preserveWhiteSpace,
637
- onSerialize,
638
- onIframeLoad,
639
- iframeLoadTimeout,
640
- } ) ;
642
+ const serializedChildNode = serializeNodeWithId ( childN , bypassOptions ) ;
641
643
if ( serializedChildNode ) {
642
644
serializedNode . childNodes . push ( serializedChildNode ) ;
643
645
}
644
646
}
647
+
648
+ if ( isElement ( n ) && n . shadowRoot ) {
649
+ serializedNode . isShadowHost = true ;
650
+ for ( const childN of Array . from ( n . shadowRoot . childNodes ) ) {
651
+ const serializedChildNode = serializeNodeWithId ( childN , bypassOptions ) ;
652
+ if ( serializedChildNode ) {
653
+ serializedChildNode . isShadow = true ;
654
+ serializedNode . childNodes . push ( serializedChildNode ) ;
655
+ }
656
+ }
657
+ }
645
658
}
646
659
647
660
if (
Original file line number Diff line number Diff line change @@ -56,6 +56,8 @@ export type serializedNode = (
56
56
| commentNode
57
57
) & {
58
58
rootId ?: number ;
59
+ isShadowHost ?: boolean ;
60
+ isShadow ?: boolean ;
59
61
} ;
60
62
61
63
export type serializedNodeWithId = serializedNode & { id : number } ;
Original file line number Diff line number Diff line change
1
+ import { INode } from './types' ;
2
+
3
+ export function isElement ( n : Node | INode ) : n is Element {
4
+ return n . nodeType === n . ELEMENT_NODE ;
5
+ }
You can’t perform that action at this time.
0 commit comments