Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
1. add an enum type for NodeType
2. rename nodeType from rrweb-snapshot to RRNodeType
3. rename notSerializedId to unserializedId
4. add comments for some confusing variables
  • Loading branch information
YunFeng0817 committed Mar 21, 2022
1 parent 23c53e1 commit d69970d
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 137 deletions.
30 changes: 15 additions & 15 deletions packages/rrdom/src/diff.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { elementNode, INode, NodeType } from 'rrweb-snapshot';
import { elementNode, INode, NodeType as RRNodeType } from 'rrweb-snapshot';
import type {
canvasMutationData,
incrementalSnapshotEvent,
Expand Down Expand Up @@ -92,11 +92,11 @@ export function diff(
let inputDataToApply = null,
scrollDataToApply = null;
switch (newTree.RRNodeType) {
case NodeType.Document:
case RRNodeType.Document:
const newRRDocument = newTree as IRRDocument;
scrollDataToApply = (newRRDocument as RRDocument).scrollData;
break;
case NodeType.Element:
case RRNodeType.Element:
const oldElement = (oldTree as Node) as HTMLElement;
const newRRElement = newTree as IRRElement;
diffProps(oldElement, newRRElement);
Expand Down Expand Up @@ -148,9 +148,9 @@ export function diff(
);
}
break;
case NodeType.Text:
case NodeType.Comment:
case NodeType.CDATA:
case RRNodeType.Text:
case RRNodeType.Comment:
case RRNodeType.CDATA:
if (
oldTree.textContent !==
(newTree as IRRText | IRRComment | IRRCDATASection).data
Expand All @@ -174,7 +174,7 @@ export function diff(
}
// IFrame element doesn't have child nodes.
if (
newTree.RRNodeType === NodeType.Element &&
newTree.RRNodeType === RRNodeType.Element &&
(newTree as IRRElement).tagName === 'IFRAME'
) {
const oldContentDocument = (((oldTree as Node) as HTMLIFrameElement)
Expand Down Expand Up @@ -288,8 +288,8 @@ function diffChildren(
* We should delete it before insert a serialized one. Otherwise, an error 'Only one element on document allowed' will be thrown.
*/
if (
parentNode.__sn.type === NodeType.Document &&
newNode.__sn.type === NodeType.Element &&
parentNode.__sn.type === RRNodeType.Document &&
newNode.__sn.type === RRNodeType.Element &&
((parentNode as Node) as Document).documentElement
) {
parentNode.removeChild(
Expand Down Expand Up @@ -335,17 +335,17 @@ export function createOrGetNode(rrNode: IRRNode, mirror: Mirror): INode {
let node = mirror.getNode(rrNode.__sn.id);
if (node !== null) return node;
switch (rrNode.RRNodeType) {
case NodeType.Document:
case RRNodeType.Document:
node = (new Document() as unknown) as INode;
break;
case NodeType.DocumentType:
case RRNodeType.DocumentType:
node = (document.implementation.createDocumentType(
(rrNode as IRRDocumentType).name,
(rrNode as IRRDocumentType).publicId,
(rrNode as IRRDocumentType).systemId,
) as unknown) as INode;
break;
case NodeType.Element:
case RRNodeType.Element:
let tagName = (rrNode as IRRElement).tagName.toLowerCase();
tagName = SVGTagMap[tagName] || tagName;
if ((rrNode.__sn as elementNode).isSVG) {
Expand All @@ -358,17 +358,17 @@ export function createOrGetNode(rrNode: IRRNode, mirror: Mirror): INode {
(rrNode as IRRElement).tagName,
) as unknown) as INode;
break;
case NodeType.Text:
case RRNodeType.Text:
node = (document.createTextNode(
(rrNode as IRRText).data,
) as unknown) as INode;
break;
case NodeType.Comment:
case RRNodeType.Comment:
node = (document.createComment(
(rrNode as IRRComment).data,
) as unknown) as INode;
break;
case NodeType.CDATA:
case RRNodeType.CDATA:
node = (document.createCDATASection(
(rrNode as IRRCDATASection).data,
) as unknown) as INode;
Expand Down
22 changes: 10 additions & 12 deletions packages/rrdom/src/document-nodejs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeType } from 'rrweb-snapshot';
import { NodeType as RRNodeType } from 'rrweb-snapshot';
import { NWSAPI } from 'nwsapi';
import type { CSSStyleDeclaration as CSSStyleDeclarationType } from 'cssstyle';
import {
Expand Down Expand Up @@ -53,15 +53,13 @@ export class RRDocument
}

get documentElement(): RRElement | null {
return super.documentElement as RRElement | null;
}

get body(): RRElement | null {
return super.body as RRElement | null;
}

get head() {
return super.head as RRElement | null;
return (
(this.childNodes.find(
(node) =>
node.RRNodeType === RRNodeType.Element &&
(node as RRElement).tagName === 'HTML',
) as RRElement) || null
);
}

get implementation(): RRDocument {
Expand Down Expand Up @@ -231,7 +229,7 @@ export class RRElement extends BaseRRElementImpl(RRNode) {

get firstElementChild(): RRElement | null {
for (let child of this.childNodes)
if (child.RRNodeType === NodeType.Element) return child as RRElement;
if (child.RRNodeType === RRNodeType.Element) return child as RRElement;
return null;
}

Expand Down Expand Up @@ -327,7 +325,7 @@ export class RRStyleElement extends RRElement {
if (!this._sheet) {
let result = '';
for (let child of this.childNodes)
if (child.nodeType === NodeType.Text)
if (child.RRNodeType === RRNodeType.Text)
result += (child as RRText).textContent;
this._sheet = cssom.parse(result);
}
Expand Down
Loading

0 comments on commit d69970d

Please sign in to comment.