Skip to content

Commit 18b68e5

Browse files
committed
Don't ignore CDATA nodes, record their textContent; I don't believe any further escaping is needed
1 parent 2c67e53 commit 18b68e5

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

packages/rrweb-snapshot/src/snapshot.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -465,18 +465,16 @@ function serializeNode(
465465
newlyAddedElement,
466466
rootId,
467467
});
468-
case n.TEXT_NODE:
469-
return serializeTextNode(n as Text, {
470-
doc,
471-
needsMask,
472-
maskTextFn,
473-
rootId,
474-
cssCaptured,
475-
});
476468
case n.CDATA_SECTION_NODE:
469+
case n.TEXT_NODE:
477470
return {
478-
type: NodeType.CDATA,
479-
textContent: '',
471+
type: n.nodeType === n.TEXT_NODE ? NodeType.Text : NodeType.CDATA,
472+
textContent: serializeTextContent(n as Text, {
473+
doc,
474+
needsMask,
475+
maskTextFn,
476+
cssCaptured,
477+
}),
480478
rootId,
481479
};
482480
case n.COMMENT_NODE:
@@ -496,17 +494,16 @@ function getRootId(doc: Document, mirror: Mirror): number | undefined {
496494
return docId === 1 ? undefined : docId;
497495
}
498496

499-
function serializeTextNode(
500-
n: Text,
497+
function serializeTextContent(
498+
n: Text | CDATASection,
501499
options: {
502500
doc: Document;
503501
needsMask: boolean;
504502
maskTextFn: MaskTextFn | undefined;
505-
rootId: number | undefined;
506503
cssCaptured?: boolean;
507504
},
508-
): serializedNode {
509-
const { needsMask, maskTextFn, rootId, cssCaptured } = options;
505+
): string {
506+
const { needsMask, maskTextFn, cssCaptured } = options;
510507
// The parent node may not be a html element which has a tagName attribute.
511508
// So just let it be undefined which is ok in this use case.
512509
const parent = dom.parentNode(n);
@@ -531,12 +528,7 @@ function serializeTextNode(
531528
? maskTextFn(textContent, dom.parentElement(n))
532529
: textContent.replace(/[\S]/g, '*');
533530
}
534-
535-
return {
536-
type: NodeType.Text,
537-
textContent: textContent || '',
538-
rootId,
539-
};
531+
return textContent || '';
540532
}
541533

542534
function serializeElementNode(

packages/rrweb-snapshot/test/__snapshots__/cdata.svg.snap.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<body>
77
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
88
<style>.Icon &gt; span { color: red; }</style>
9+
<div>&lt;/svg&gt;&amp; this is not markup&lt;svg/&gt;</div>
910
</svg>
1011
</body>
1112
</html>

packages/rrweb-snapshot/test/__snapshots__/cdata.svg.snap.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@
5757
],
5858
"isSVG": true,
5959
"id": 8
60+
},
61+
{
62+
"type": 2,
63+
"tagName": "div",
64+
"attributes": {},
65+
"childNodes": [
66+
{
67+
"type": 4,
68+
"textContent": "</svg>& this is not markup<svg/>",
69+
"id": 11
70+
}
71+
],
72+
"isSVG": true,
73+
"id": 10
6074
}
6175
],
6276
"isSVG": true,

packages/rrweb-snapshot/test/integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ const regularStyle = document.createElement('style');
433433
regularStyle.innerText = '.Icon > span{ color: blue; }'
434434
document.head.append(regularStyle);
435435
const defsSvg = (new window.DOMParser()).parseFromString(
436-
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><style><![CDATA[.Icon > span{ color: red; }]]></style></svg>', 'image/svg+xml');
436+
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><style><![CDATA[.Icon > span{ color: red; }]]></style><div><![CDATA[</svg>& this is not markup<svg/>]]></div></svg>', 'image/svg+xml');
437437
document.body.appendChild(defsSvg.documentElement);
438438
`);
439439
await waitForRAF(page); // a small wait

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ export type textNode = {
791791

792792
export type cdataNode = {
793793
type: NodeType.CDATA;
794-
textContent: '';
794+
textContent: string;
795795
};
796796

797797
export type commentNode = {

0 commit comments

Comments
 (0)