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

fix: Set style properties directly in mutation #211

Draft
wants to merge 3 commits into
base: sentry-v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
inDom,
getShadowHost,
closestElementOfNode,
splitStyleAttributes,
} from '../utils';

type DoubleLinkedListNode = {
Expand Down Expand Up @@ -365,13 +366,13 @@
};

while (this.mapRemoves.length) {
this.mirror.removeNodeFromMap(this.mapRemoves.shift()!);

Check warning on line 369 in packages/rrweb/src/record/mutation.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/mutation.ts#L369

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
}

for (const n of this.movedSet) {
if (
isParentRemoved(this.removes, n, this.mirror) &&
!this.movedSet.has(n.parentNode!)

Check warning on line 375 in packages/rrweb/src/record/mutation.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/mutation.ts#L375

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
) {
continue;
}
Expand Down Expand Up @@ -659,7 +660,16 @@
}
const old = this.unattachedDoc.createElement('span');
if (m.oldValue) {
old.setAttribute('style', m.oldValue);
console.log('yalc version 3');
chargome marked this conversation as resolved.
Show resolved Hide resolved
// Split the style string into individual style rules
const styleAttributes = splitStyleAttributes(m.oldValue);

// set each style property individually to avoid csp error
styleAttributes.forEach(({ property, value }) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
old.style[property] = value;
});
}
for (const pname of Array.from(target.style)) {
const newValue = target.style.getPropertyValue(pname);
Expand Down
25 changes: 25 additions & 0 deletions packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'now you can use replayer.getMirror() to access the mirror instance of a replayer,' +
'\r\n' +
'or you can use record.mirror to access the mirror instance during recording.';
/** @deprecated */

Check warning on line 37 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L37

[tsdoc/syntax] tsdoc-missing-deprecation-message: The @deprecated block must include a deprecation message, e.g. describing the recommended alternative
export let _mirror: DeprecatedMirror = {
map: {},
getId() {
Expand Down Expand Up @@ -118,7 +118,7 @@
set(value) {
// put hooked setter into event loop to avoid of set latency
setTimeout(() => {
d.set!.call(this, value);

Check warning on line 121 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L121

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
}, 0);
if (original && original.set) {
original.set.call(this, value);
Expand All @@ -131,7 +131,7 @@

// copy from https://github.com/getsentry/sentry-javascript/blob/b2109071975af8bf0316d3b5b38f519bdaf5dc15/packages/utils/src/object.ts
export function patch(
source: { [key: string]: any },

Check warning on line 134 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L134

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
name: string,
replacement: (...args: unknown[]) => unknown,
): () => void {
Expand Down Expand Up @@ -670,3 +670,28 @@
): ReturnType<typeof window.clearTimeout> {
return getImplementation('clearTimeout')(...rest);
}

/**
* Takes a styles attribute string and converts it to key value pairs
* @param styles - The full stype attributes string
chargome marked this conversation as resolved.
Show resolved Hide resolved
*
*/
export function splitStyleAttributes(styles: string) {
const splitStyles = styles.split(';');
return splitStyles
.filter((value) => value.split(':').length == 2)
Copy link
Member

Choose a reason for hiding this comment

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

Q: Is this safe? Could styles not include colons in other places? Not 100% sure, just want to double check!

Copy link
Member Author

Choose a reason for hiding this comment

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

That will probably be an issue sooner or later, you're right

.map((style) => {
let [property, value] = style.trim().split(':');

property = property.trim();
value = value.trim();

// Convert kebab-case to camelCase
const camelCasedProperty = property.replace(
/-([a-z])/g,
(match, letter: string) => letter.toUpperCase(),
);

return { property: camelCasedProperty, value };
});
}
14 changes: 14 additions & 0 deletions packages/rrweb/test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
inDom,
shadowHostInDom,
getShadowHost,
splitStyleAttributes,
} from '../src/utils';

describe('Utilities for other modules', () => {
Expand Down Expand Up @@ -142,5 +143,18 @@ describe('Utilities for other modules', () => {
expect(shadowHostInDom(a.childNodes[0])).toBeTruthy();
expect(inDom(a.childNodes[0])).toBeTruthy();
});

it('should split styles attributes', () => {
const styleAttribute =
'background-color: peachpuff; padding: 20px; margin-top: 0;';

const splitStyles = splitStyleAttributes(styleAttribute);

expect(splitStyles).toEqual([
{ property: 'backgroundColor', value: 'peachpuff' },
{ property: 'padding', value: '20px' },
{ property: 'marginTop', value: '0' },
]);
});
});
});
Loading