-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix splitCssText again #1640
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
Merged
eoghanmurray
merged 18 commits into
rrweb-io:master
from
eoghanmurray:fix-splitCssText-again
Feb 6, 2025
Merged
Fix splitCssText again #1640
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fb694b2
Fix up the 'should replace the existing DOM nodes on iframe navigatio…
eoghanmurray 5962d03
Fix a case I previously forgot about, when there are multiple matches…
eoghanmurray 08a80ab
Failing example extracted from large files identified by Paul D'Ambra…
eoghanmurray 43df807
Fix that we weren't stopping when going from many matches to zero mat…
eoghanmurray d59c1ef
Avoid (probably) a happydom bug
eoghanmurray 21beccb
Disabliing to prove failure
eoghanmurray e789129
Make the matches go from 'many' to 'none'
eoghanmurray a72c4e1
Pick the best when there are many splits by looking at previous chunk…
eoghanmurray b3a5a12
This seems more straightforward
eoghanmurray e02e608
Rename some variables for clarity
eoghanmurray 0347a36
yarn format
eoghanmurray d0fb0c5
Add changeset
eoghanmurray e5a9550
Mention what triggers the bug
eoghanmurray 01d96bb
Solve the same bug another way; by normalization
eoghanmurray 86796ac
Apply formatting changes
eoghanmurray 3a452e3
Don't go past 100 iterations trying to find a unique substring
eoghanmurray 7a32e49
Add a tscomment to explain the `_testNoPxNorm` test-only parameter
eoghanmurray 5743c7e
Merge remote-tracking branch 'upstream/master' into fix-splitCssText-…
eoghanmurray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Solve the same bug another way; by normalization
- Loading branch information
commit 01d96bbfca61eedcadaab0df8390b36639daffe6
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,7 +80,7 @@ | |
| * Browsers sometimes incorrectly escape `@import` on `.cssText` statements. | ||
| * This function tries to correct the escaping. | ||
| * more info: https://bugs.chromium.org/p/chromium/issues/detail?id=1472259 | ||
| * @param cssImportRule | ||
|
Check warning on line 83 in packages/rrweb-snapshot/src/utils.ts
|
||
| * @returns `cssText` with browser inconsistencies fixed, or null if not applicable. | ||
| */ | ||
| export function escapeImportStatement(rule: CSSImportRule): string { | ||
|
|
@@ -450,8 +450,12 @@ | |
| * Intention is to normalize by remove spaces, semicolons and CSS comments | ||
| * so that we can compare css as authored vs. output of stringifyStylesheet | ||
| */ | ||
| export function normalizeCssString(cssText: string): string { | ||
| return cssText.replace(/(\/\*[^*]*\*\/)|[\s;]/g, ''); | ||
| export function normalizeCssString(cssText: string, _testNoPxNorm=false): string { | ||
| if (_testNoPxNorm) { | ||
| return cssText.replace(/(\/\*[^*]*\*\/)|[\s;]/g, ''); | ||
| } else { | ||
| return cssText.replace(/(\/\*[^*]*\*\/)|[\s;]/g, '').replace(/0px/g, '0'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -463,19 +467,20 @@ | |
| export function splitCssText( | ||
| cssText: string, | ||
| style: HTMLStyleElement, | ||
| _testNoPxNorm=false, | ||
| ): string[] { | ||
| const childNodes = Array.from(style.childNodes); | ||
| const splits: string[] = []; | ||
| let iterLimit = 0; | ||
| if (childNodes.length > 1 && cssText && typeof cssText === 'string') { | ||
| let cssTextNorm = normalizeCssString(cssText); | ||
| let cssTextNorm = normalizeCssString(cssText, _testNoPxNorm); | ||
| const normFactor = cssTextNorm.length / cssText.length; | ||
| for (let i = 1; i < childNodes.length; i++) { | ||
| if ( | ||
| childNodes[i].textContent && | ||
| typeof childNodes[i].textContent === 'string' | ||
| ) { | ||
| const textContentNorm = normalizeCssString(childNodes[i].textContent!); | ||
| const textContentNorm = normalizeCssString(childNodes[i].textContent!, _testNoPxNorm); | ||
| let j = 3; | ||
| for (; j < textContentNorm.length; j++) { | ||
| if ( | ||
|
|
@@ -538,7 +543,7 @@ | |
| splits.push(cssText); | ||
| return splits; | ||
| } | ||
| const normPart = normalizeCssString(cssText.substring(0, k)); | ||
| const normPart = normalizeCssString(cssText.substring(0, k), _testNoPxNorm); | ||
| if (normPart.length === splitNorm) { | ||
| splits.push(cssText.substring(0, k)); | ||
| cssText = cssText.substring(k); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i briefly tested a very naive loop parser and it was slower than regex replace - i guess because browsers/v8 are doing some magic to optimise this already
but I didn't test it over a range of inputs
this is (from my testing) at best O(n) for whitespace - and for clarity since i'm not much of a comp sci person. if you insert whitespace into the input then this gets slower the more whitespace is present
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The performance of the normalization function could be improved. I've moreso tried to ensure it's not called repeatedly on the same piece of css (with the 'binary search' style changes in #1615 ).