-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
bugfix: Sort attributes to make rr_* attributes handled last
#970
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
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ad24bfd
Sort attributes to make `rr_*` attributes handled last
Juice10 a5e998e
Update packages/rrweb-snapshot/src/rebuild.ts
Juice10 d067a97
Merge branch 'master' of https://github.com/rrweb-io/rrweb into attri…
Juice10 4ecf3b9
Refactor handling of rr_* attributes
Juice10 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
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
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 |
|---|---|---|
| @@ -1,92 +1,127 @@ | ||
| /** | ||
| * @jest-environment jsdom | ||
| */ | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import { addHoverClass, createCache } from '../src/rebuild'; | ||
| import { addHoverClass, buildNodeWithSN, createCache } from '../src/rebuild'; | ||
| import { NodeType } from '../src/types'; | ||
| import { createMirror, Mirror } from '../src/utils'; | ||
|
|
||
| function getDuration(hrtime: [number, number]) { | ||
| const [seconds, nanoseconds] = hrtime; | ||
| return seconds * 1000 + nanoseconds / 1000000; | ||
| } | ||
|
|
||
| describe('add hover class to hover selector related rules', function () { | ||
| describe('rebuild', function () { | ||
| let cache: ReturnType<typeof createCache>; | ||
| let mirror: Mirror; | ||
|
|
||
| beforeEach(() => { | ||
| mirror = createMirror(); | ||
| cache = createCache(); | ||
| }); | ||
|
|
||
| it('will do nothing to css text without :hover', () => { | ||
| const cssText = 'body { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual(cssText); | ||
| describe('rr_dataURL', function () { | ||
| it('should rebuild dataURL', function () { | ||
| const dataURI = | ||
| 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='; | ||
| const node = buildNodeWithSN( | ||
| { | ||
| id: 1, | ||
| tagName: 'img', | ||
| type: NodeType.Element, | ||
| attributes: { | ||
| rr_dataURL: dataURI, | ||
| src: 'http://example.com/image.png', | ||
| }, | ||
| childNodes: [], | ||
| }, | ||
| { | ||
| doc: document, | ||
| mirror, | ||
| hackCss: false, | ||
| cache, | ||
| }, | ||
| ) as HTMLImageElement; | ||
| expect(node?.src).toBe(dataURI); | ||
| }); | ||
| }); | ||
|
|
||
| it('can add hover class to css text', () => { | ||
| const cssText = '.a:hover { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| '.a:hover, .a.\\:hover { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('can add hover class when there is multi selector', () => { | ||
| const cssText = '.a, .b:hover, .c { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| '.a, .b:hover, .b.\\:hover, .c { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('can add hover class when there is a multi selector with the same prefix', () => { | ||
| const cssText = '.a:hover, .a:hover::after { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| '.a:hover, .a.\\:hover, .a:hover::after, .a.\\:hover::after { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('can add hover class when :hover is not the end of selector', () => { | ||
| const cssText = 'div:hover::after { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| 'div:hover::after, div.\\:hover::after { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('can add hover class when the selector has multi :hover', () => { | ||
| const cssText = 'a:hover b:hover { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| 'a:hover b:hover, a.\\:hover b.\\:hover { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('will ignore :hover in css value', () => { | ||
| const cssText = '.a::after { content: ":hover" }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual(cssText); | ||
| }); | ||
|
|
||
| it('benchmark', () => { | ||
| const cssText = fs.readFileSync( | ||
| path.resolve(__dirname, './css/benchmark.css'), | ||
| 'utf8', | ||
| ); | ||
| const start = process.hrtime(); | ||
| addHoverClass(cssText, cache); | ||
| const end = process.hrtime(start); | ||
| const duration = getDuration(end); | ||
| expect(duration).toBeLessThan(100); | ||
| }); | ||
|
|
||
| it('should be a lot faster to add a hover class to a previously processed css string', () => { | ||
| const factor = 100; | ||
|
|
||
| let cssText = fs.readFileSync( | ||
| path.resolve(__dirname, './css/benchmark.css'), | ||
| 'utf8', | ||
| ); | ||
|
|
||
| const start = process.hrtime(); | ||
| addHoverClass(cssText, cache); | ||
| const end = process.hrtime(start); | ||
|
|
||
| const cachedStart = process.hrtime(); | ||
| addHoverClass(cssText, cache); | ||
| const cachedEnd = process.hrtime(cachedStart); | ||
|
|
||
| expect(getDuration(cachedEnd) * factor).toBeLessThan(getDuration(end)); | ||
| describe('add hover class to hover selector related rules', function () { | ||
| it('will do nothing to css text without :hover', () => { | ||
| const cssText = 'body { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual(cssText); | ||
| }); | ||
|
|
||
| it('can add hover class to css text', () => { | ||
| const cssText = '.a:hover { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| '.a:hover, .a.\\:hover { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('can add hover class when there is multi selector', () => { | ||
| const cssText = '.a, .b:hover, .c { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| '.a, .b:hover, .b.\\:hover, .c { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('can add hover class when there is a multi selector with the same prefix', () => { | ||
| const cssText = '.a:hover, .a:hover::after { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| '.a:hover, .a.\\:hover, .a:hover::after, .a.\\:hover::after { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('can add hover class when :hover is not the end of selector', () => { | ||
| const cssText = 'div:hover::after { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| 'div:hover::after, div.\\:hover::after { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('can add hover class when the selector has multi :hover', () => { | ||
| const cssText = 'a:hover b:hover { color: white }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual( | ||
| 'a:hover b:hover, a.\\:hover b.\\:hover { color: white }', | ||
| ); | ||
| }); | ||
|
|
||
| it('will ignore :hover in css value', () => { | ||
| const cssText = '.a::after { content: ":hover" }'; | ||
| expect(addHoverClass(cssText, cache)).toEqual(cssText); | ||
| }); | ||
|
|
||
| it('benchmark', () => { | ||
| const cssText = fs.readFileSync( | ||
| path.resolve(__dirname, './css/benchmark.css'), | ||
| 'utf8', | ||
| ); | ||
| const start = process.hrtime(); | ||
| addHoverClass(cssText, cache); | ||
| const end = process.hrtime(start); | ||
| const duration = getDuration(end); | ||
| expect(duration).toBeLessThan(100); | ||
| }); | ||
|
|
||
| it('should be a lot faster to add a hover class to a previously processed css string', () => { | ||
| const factor = 100; | ||
|
|
||
| let cssText = fs.readFileSync( | ||
| path.resolve(__dirname, './css/benchmark.css'), | ||
| 'utf8', | ||
| ); | ||
|
|
||
| const start = process.hrtime(); | ||
| addHoverClass(cssText, cache); | ||
| const end = process.hrtime(start); | ||
|
|
||
| const cachedStart = process.hrtime(); | ||
| addHoverClass(cssText, cache); | ||
| const cachedEnd = process.hrtime(cachedStart); | ||
|
|
||
| expect(getDuration(cachedEnd) * factor).toBeLessThan(getDuration(end)); | ||
| }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.