return id node map when rebuild
This commit is contained in:
2
index.d.ts
vendored
2
index.d.ts
vendored
@@ -2,7 +2,7 @@ import { serializedNodeWithId, idNodeMap } from './src/types';
|
|||||||
export * from './src/types';
|
export * from './src/types';
|
||||||
|
|
||||||
export function snapshot(n: Document): [serializedNodeWithId | null, idNodeMap];
|
export function snapshot(n: Document): [serializedNodeWithId | null, idNodeMap];
|
||||||
export function rebuild(n: serializedNodeWithId): Node | null;
|
export function rebuild(n: serializedNodeWithId): [Node | null, idNodeMap];
|
||||||
export function serializeNodeWithId(
|
export function serializeNodeWithId(
|
||||||
n: Node,
|
n: Node,
|
||||||
doc: Document,
|
doc: Document,
|
||||||
|
|||||||
@@ -1,4 +1,11 @@
|
|||||||
import { serializedNodeWithId, NodeType, tagMap, elementNode } from './types';
|
import {
|
||||||
|
serializedNodeWithId,
|
||||||
|
NodeType,
|
||||||
|
tagMap,
|
||||||
|
elementNode,
|
||||||
|
idNodeMap,
|
||||||
|
INode,
|
||||||
|
} from './types';
|
||||||
|
|
||||||
const tagMap: tagMap = {
|
const tagMap: tagMap = {
|
||||||
script: 'noscript',
|
script: 'noscript',
|
||||||
@@ -54,14 +61,16 @@ function buildNode(n: serializedNodeWithId): Node | null {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function rebuild(n: serializedNodeWithId): Node | null {
|
function _rebuild(n: serializedNodeWithId, map: idNodeMap): Node | null {
|
||||||
const root = buildNode(n);
|
const root = buildNode(n);
|
||||||
if (!root) {
|
if (!root) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
(root as INode).__sn = n;
|
||||||
|
map[n.id] = root as INode;
|
||||||
if (n.type === NodeType.Document || n.type === NodeType.Element) {
|
if (n.type === NodeType.Document || n.type === NodeType.Element) {
|
||||||
for (const childN of n.childNodes) {
|
for (const childN of n.childNodes) {
|
||||||
const childNode = rebuild(childN);
|
const childNode = _rebuild(childN, map);
|
||||||
if (!childNode) {
|
if (!childNode) {
|
||||||
console.warn('Failed to rebuild', childN);
|
console.warn('Failed to rebuild', childN);
|
||||||
} else {
|
} else {
|
||||||
@@ -72,4 +81,9 @@ function rebuild(n: serializedNodeWithId): Node | null {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function rebuild(n: serializedNodeWithId): [Node | null, idNodeMap] {
|
||||||
|
const idNodeMap: idNodeMap = {};
|
||||||
|
return [_rebuild(n, idNodeMap), idNodeMap];
|
||||||
|
}
|
||||||
|
|
||||||
export default rebuild;
|
export default rebuild;
|
||||||
|
|||||||
@@ -4,3 +4,6 @@ body {
|
|||||||
p {
|
p {
|
||||||
color: red;
|
color: red;
|
||||||
}
|
}
|
||||||
|
body > p {
|
||||||
|
color: yellow;
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
<title>with style sheet</title>
|
<title>with style sheet</title>
|
||||||
<style>body { margin: 0px; }p { color: red; }</style>
|
<style>body { margin: 0px; }p { color: red; }body > p { color: yellow; }</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ describe('integration tests', () => {
|
|||||||
const rebuildHtml = (await page.evaluate(`${this.code}
|
const rebuildHtml = (await page.evaluate(`${this.code}
|
||||||
const x = new XMLSerializer();
|
const x = new XMLSerializer();
|
||||||
const [snap] = rrweb.snapshot(document);
|
const [snap] = rrweb.snapshot(document);
|
||||||
x.serializeToString(rrweb.rebuild(snap));
|
x.serializeToString(rrweb.rebuild(snap)[0]);
|
||||||
`)).replace(/\n\n/g, '');
|
`)).replace(/\n\n/g, '');
|
||||||
await page.goto(`http://localhost:3030/html`);
|
await page.goto(`http://localhost:3030/html`);
|
||||||
await page.setContent(html.dest);
|
await page.setContent(html.dest);
|
||||||
|
|||||||
Reference in New Issue
Block a user