@@ -4,11 +4,9 @@ import {
44 NodeType ,
55 tagMap ,
66 elementNode ,
7- idNodeMap ,
8- INode ,
97 BuildCache ,
108} from './types' ;
11- import { isElement } from './utils' ;
9+ import { isElement , Mirror } from './utils' ;
1210
1311const tagMap : tagMap = {
1412 script : 'noscript' ,
@@ -215,7 +213,10 @@ function buildNode(
215213 n . attributes . rr_dataURL
216214 ) {
217215 // backup original img srcset
218- node . setAttribute ( 'rrweb-original-srcset' , n . attributes . srcset as string ) ;
216+ node . setAttribute (
217+ 'rrweb-original-srcset' ,
218+ n . attributes . srcset as string ,
219+ ) ;
219220 } else {
220221 node . setAttribute ( name , value ) ;
221222 }
@@ -307,16 +308,16 @@ export function buildNodeWithSN(
307308 n : serializedNodeWithId ,
308309 options : {
309310 doc : Document ;
310- map : idNodeMap ;
311+ mirror : Mirror ;
311312 skipChild ?: boolean ;
312313 hackCss : boolean ;
313- afterAppend ?: ( n : INode ) => unknown ;
314+ afterAppend ?: ( n : Node ) => unknown ;
314315 cache : BuildCache ;
315316 } ,
316- ) : INode | null {
317+ ) : Node | null {
317318 const {
318319 doc,
319- map ,
320+ mirror ,
320321 skipChild = false ,
321322 hackCss = true ,
322323 afterAppend,
@@ -328,7 +329,7 @@ export function buildNodeWithSN(
328329 }
329330 if ( n . rootId ) {
330331 console . assert (
331- ( ( map [ n . rootId ] as unknown ) as Document ) === doc ,
332+ ( mirror . getNode ( n . rootId ) as Document ) === doc ,
332333 'Target document should has the same root id.' ,
333334 ) ;
334335 }
@@ -362,8 +363,7 @@ export function buildNodeWithSN(
362363 node = doc ;
363364 }
364365
365- ( node as INode ) . __sn = n ;
366- map [ n . id ] = node as INode ;
366+ mirror . add ( node , n ) ;
367367
368368 if (
369369 ( n . type === NodeType . Document || n . type === NodeType . Element ) &&
@@ -372,7 +372,7 @@ export function buildNodeWithSN(
372372 for ( const childN of n . childNodes ) {
373373 const childNode = buildNodeWithSN ( childN , {
374374 doc,
375- map ,
375+ mirror ,
376376 skipChild : false ,
377377 hackCss,
378378 afterAppend,
@@ -394,27 +394,27 @@ export function buildNodeWithSN(
394394 }
395395 }
396396
397- return node as INode ;
397+ return node ;
398398}
399399
400- function visit ( idNodeMap : idNodeMap , onVisit : ( node : INode ) => void ) {
401- function walk ( node : INode ) {
400+ function visit ( mirror : Mirror , onVisit : ( node : Node ) => void ) {
401+ function walk ( node : Node ) {
402402 onVisit ( node ) ;
403403 }
404404
405- for ( const key in idNodeMap ) {
406- if ( idNodeMap [ key ] ) {
407- walk ( idNodeMap [ key ] ) ;
405+ for ( const id of mirror . getIds ( ) ) {
406+ if ( mirror . has ( id ) ) {
407+ walk ( mirror . getNode ( id ) ! ) ;
408408 }
409409 }
410410}
411411
412- function handleScroll ( node : INode ) {
413- const n = node . __sn ;
414- if ( n . type !== NodeType . Element ) {
412+ function handleScroll ( node : Node , mirror : Mirror ) {
413+ const n = mirror . getMeta ( node ) ;
414+ if ( n ? .type !== NodeType . Element ) {
415415 return ;
416416 }
417- const el = ( node as Node ) as HTMLElement ;
417+ const el = node as HTMLElement ;
418418 for ( const name in n . attributes ) {
419419 if ( ! ( n . attributes . hasOwnProperty ( name ) && name . startsWith ( 'rr_' ) ) ) {
420420 continue ;
@@ -433,29 +433,36 @@ function rebuild(
433433 n : serializedNodeWithId ,
434434 options : {
435435 doc : Document ;
436- onVisit ?: ( node : INode ) => unknown ;
436+ onVisit ?: ( node : Node ) => unknown ;
437437 hackCss ?: boolean ;
438- afterAppend ?: ( n : INode ) => unknown ;
438+ afterAppend ?: ( n : Node ) => unknown ;
439439 cache : BuildCache ;
440+ mirror : Mirror ;
440441 } ,
441- ) : [ Node | null , idNodeMap ] {
442- const { doc, onVisit, hackCss = true , afterAppend, cache } = options ;
443- const idNodeMap : idNodeMap = { } ;
442+ ) : Node | null {
443+ const {
444+ doc,
445+ onVisit,
446+ hackCss = true ,
447+ afterAppend,
448+ cache,
449+ mirror = new Mirror ( ) ,
450+ } = options ;
444451 const node = buildNodeWithSN ( n , {
445452 doc,
446- map : idNodeMap ,
453+ mirror ,
447454 skipChild : false ,
448455 hackCss,
449456 afterAppend,
450457 cache,
451458 } ) ;
452- visit ( idNodeMap , ( visitedNode ) => {
459+ visit ( mirror , ( visitedNode ) => {
453460 if ( onVisit ) {
454461 onVisit ( visitedNode ) ;
455462 }
456- handleScroll ( visitedNode ) ;
463+ handleScroll ( visitedNode , mirror ) ;
457464 } ) ;
458- return [ node , idNodeMap ] ;
465+ return node ;
459466}
460467
461468export default rebuild ;
0 commit comments