Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't serialize all cssRules if multiple text nodes exists #866

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,12 @@ function serializeNode(
if (isStyle && textContent) {
try {
// try to read style sheet
if ((n.parentNode as HTMLStyleElement).sheet?.cssRules) {
if (n.nextSibling || n.previousSibling) {
// This is not the only child of the stylesheet.
// We can't read all of the sheet's .cssRules and expect them
// to _only_ include the current rule(s) added by the text node.
// So we'll be conservative and keep textContent as-is.
} else if ((n.parentNode as HTMLStyleElement).sheet?.cssRules) {
textContent = stringifyStyleSheet(
(n.parentNode as HTMLStyleElement).sheet!,
);
Expand Down
55 changes: 54 additions & 1 deletion packages/rrweb-snapshot/test/snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/**
* @jest-environment jsdom
*/
import { JSDOM } from 'jsdom';
import { absoluteToStylesheet, _isBlockedElement } from '../src/snapshot';
import {
absoluteToStylesheet,
serializeNodeWithId,
_isBlockedElement,
} from '../src/snapshot';
import { serializedNodeWithId } from '../src/types';

describe('absolute url to stylesheet', () => {
const href = 'http://localhost/css/style.css';
Expand Down Expand Up @@ -126,3 +134,48 @@ describe('isBlockedElement()', () => {
).toEqual(true);
});
});

describe('style elements', () => {
const serializeNode = (node: Node): serializedNodeWithId | null => {
return serializeNodeWithId(node, {
doc: document,
map: {},
blockClass: 'blockblock',
blockSelector: null,
maskTextClass: 'maskmask',
maskTextSelector: null,
skipChild: false,
inlineStylesheet: true,
maskTextFn: undefined,
maskInputFn: undefined,
slimDOMOptions: {},
});
};

const render = (html: string): HTMLStyleElement => {
document.write(html);
return document.querySelector('style')!;
};

it('should serialize all rules of stylesheet when the sheet has a single child node', () => {
const styleEl = render(`<style>body { color: red; }</style>`);
styleEl.sheet?.insertRule('section { color: blue; }');
expect(serializeNode(styleEl.childNodes[0])).toMatchObject({
isStyle: true,
rootId: undefined,
textContent: 'section {color: blue;}body {color: red;}',
type: 3,
});
});

it('should serialize individual text nodes on stylesheets with multiple child nodes', () => {
const styleEl = render(`<style>body { color: red; }</style>`);
styleEl.append(document.createTextNode('section { color: blue; }'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a rule is also inserted here, would it be recorded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any rule added before the append would be wiped by the webbrowser. Any rule added after the append will not be picked up in rrweb-snapshot unfortunately. And I don't know of a good way of capturing those.
In rrweb these rule additions are captured by the observers.

This PR hopes that you either add css via the .sheet.inserRule api or .append(TEXT_NODE) api. It's not foolproof but it's better than what we had.

expect(serializeNode(styleEl.childNodes[1])).toMatchObject({
isStyle: true,
rootId: undefined,
textContent: 'section { color: blue; }',
type: 3,
});
});
});