MCPcopy
hub / github.com/rrweb-io/rrweb / createOrGetNode

Function createOrGetNode

packages/rrdom/src/diff.ts:534–582  ·  view source on GitHub ↗
(
  rrNode: IRRNode,
  domMirror: NodeMirror,
  rrnodeMirror: Mirror,
)

Source from the content-addressed store, hash-verified

532}
533
534export function createOrGetNode(
535 rrNode: IRRNode,
536 domMirror: NodeMirror,
537 rrnodeMirror: Mirror,
538): Node {
539 const nodeId = rrnodeMirror.getId(rrNode);
540 const sn = rrnodeMirror.getMeta(rrNode);
541 let node: Node | null = null;
542 // negative ids shouldn't be compared accross mirrors
543 if (nodeId > -1) node = domMirror.getNode(nodeId);
544 if (node !== null && sameNodeType(node, rrNode)) return node;
545 switch (rrNode.RRNodeType) {
546 case RRNodeType.Document:
547 node = new Document();
548 break;
549 case RRNodeType.DocumentType:
550 node = document.implementation.createDocumentType(
551 (rrNode as IRRDocumentType).name,
552 (rrNode as IRRDocumentType).publicId,
553 (rrNode as IRRDocumentType).systemId,
554 );
555 break;
556 case RRNodeType.Element: {
557 let tagName = (rrNode as IRRElement).tagName.toLowerCase();
558 tagName = SVGTagMap[tagName] || tagName;
559 if (sn && 'isSVG' in sn && sn?.isSVG) {
560 node = document.createElementNS(NAMESPACES['svg'], tagName);
561 } else node = document.createElement((rrNode as IRRElement).tagName);
562 break;
563 }
564 case RRNodeType.Text:
565 node = document.createTextNode((rrNode as IRRText).data);
566 break;
567 case RRNodeType.Comment:
568 node = document.createComment((rrNode as IRRComment).data);
569 break;
570 case RRNodeType.CDATA:
571 node = document.createCDATASection((rrNode as IRRCDATASection).data);
572 break;
573 }
574
575 if (sn) domMirror.add(node, { ...sn });
576 try {
577 createdNodeSet?.add(node);
578 } catch (e) {
579 // Just for safety concern.
580 }
581 return node;
582}
583
584/**
585 * To check whether two nodes are the same type of node. If they are both Elements, check wether their tagNames are same or not.

Callers 4

diff.test.tsFile · 0.90
constructorMethod · 0.90
diffChildrenFunction · 0.85

Calls 11

sameNodeTypeFunction · 0.85
getIdMethod · 0.65
getMetaMethod · 0.65
getNodeMethod · 0.65
createDocumentTypeMethod · 0.65
createElementNSMethod · 0.65
createElementMethod · 0.65
createTextNodeMethod · 0.65
createCommentMethod · 0.65
createCDATASectionMethod · 0.65
addMethod · 0.65

Tested by

no test coverage detected