use document object from params instead of the one in the current scope
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, doc: Document): Node | null;
|
||||||
export function serializeNodeWithId(
|
export function serializeNodeWithId(
|
||||||
n: Node,
|
n: Node,
|
||||||
doc: Document,
|
doc: Document,
|
||||||
|
|||||||
@@ -11,19 +11,19 @@ function getTagName(n: elementNode): string {
|
|||||||
return tagName;
|
return tagName;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildNode(n: serializedNodeWithId): Node | null {
|
function buildNode(n: serializedNodeWithId, doc: Document): Node | null {
|
||||||
switch (n.type) {
|
switch (n.type) {
|
||||||
case NodeType.Document:
|
case NodeType.Document:
|
||||||
return document.implementation.createDocument(null, '', null);
|
return doc.implementation.createDocument(null, '', null);
|
||||||
case NodeType.DocumentType:
|
case NodeType.DocumentType:
|
||||||
return document.implementation.createDocumentType(
|
return doc.implementation.createDocumentType(
|
||||||
n.name,
|
n.name,
|
||||||
n.publicId,
|
n.publicId,
|
||||||
n.systemId,
|
n.systemId,
|
||||||
);
|
);
|
||||||
case NodeType.Element:
|
case NodeType.Element:
|
||||||
const tagName = getTagName(n);
|
const tagName = getTagName(n);
|
||||||
const node = document.createElement(tagName);
|
const node = doc.createElement(tagName);
|
||||||
const extraChildIndexes: number[] = [];
|
const extraChildIndexes: number[] = [];
|
||||||
for (const name in n.attributes) {
|
for (const name in n.attributes) {
|
||||||
if (n.attributes.hasOwnProperty(name)) {
|
if (n.attributes.hasOwnProperty(name)) {
|
||||||
@@ -32,7 +32,7 @@ function buildNode(n: serializedNodeWithId): Node | null {
|
|||||||
const isTextarea = tagName === 'textarea' && name === 'value';
|
const isTextarea = tagName === 'textarea' && name === 'value';
|
||||||
const isRemoteCss = tagName === 'style' && name === '_cssText';
|
const isRemoteCss = tagName === 'style' && name === '_cssText';
|
||||||
if (isTextarea || isRemoteCss) {
|
if (isTextarea || isRemoteCss) {
|
||||||
const child = document.createTextNode(value);
|
const child = doc.createTextNode(value);
|
||||||
// identify the extra child DOM we added when rebuild
|
// identify the extra child DOM we added when rebuild
|
||||||
extraChildIndexes.push(node.childNodes.length);
|
extraChildIndexes.push(node.childNodes.length);
|
||||||
node.appendChild(child);
|
node.appendChild(child);
|
||||||
@@ -53,18 +53,18 @@ function buildNode(n: serializedNodeWithId): Node | null {
|
|||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
case NodeType.Text:
|
case NodeType.Text:
|
||||||
return document.createTextNode(n.textContent);
|
return doc.createTextNode(n.textContent);
|
||||||
case NodeType.CDATA:
|
case NodeType.CDATA:
|
||||||
return document.createCDATASection(n.textContent);
|
return doc.createCDATASection(n.textContent);
|
||||||
case NodeType.Comment:
|
case NodeType.Comment:
|
||||||
return document.createComment(n.textContent);
|
return doc.createComment(n.textContent);
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function rebuild(n: serializedNodeWithId): Node | null {
|
function rebuild(n: serializedNodeWithId, doc: Document): Node | null {
|
||||||
const root = buildNode(n);
|
const root = buildNode(n, doc);
|
||||||
if (!root) {
|
if (!root) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ function rebuild(n: serializedNodeWithId): Node | null {
|
|||||||
}
|
}
|
||||||
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, doc);
|
||||||
if (!childNode) {
|
if (!childNode) {
|
||||||
console.warn('Failed to rebuild', childN);
|
console.warn('Failed to rebuild', childN);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -101,7 +101,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, document));
|
||||||
`)).replace(/\n\n/g, '');
|
`)).replace(/\n\n/g, '');
|
||||||
const result = matchSnapshot(rebuildHtml, __filename, title);
|
const result = matchSnapshot(rebuildHtml, __filename, title);
|
||||||
assert(result.pass, result.pass ? '' : result.report());
|
assert(result.pass, result.pass ? '' : result.report());
|
||||||
|
|||||||
Reference in New Issue
Block a user