-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: styled-components / insertRule (#621)
* fix: reimplement insertRule hook * test: add dynamic style node tests * fix: styled-components on remount * fix: early record cssRules for style rebuild * test: update style unit tests * fix: remove unused imports * fix: styled-components adding rules after remount * test: adding rules after remount * fix: recording of css rules
- Loading branch information
Showing
10 changed files
with
274 additions
and
105 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
packages/browser-vm/__tests__/__snapshots__/dynamic-style.spec.ts.snap
This file contains 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Sandbox: dynamic style append text node 1`] = ` | ||
Array [ | ||
".app .a { color: black; }.app .b { color: white; }", | ||
Array [ | ||
".app .a {color: black;}", | ||
".app .b {color: white;}", | ||
], | ||
] | ||
`; | ||
|
||
exports[`Sandbox: dynamic style insertRule 1`] = ` | ||
Array [ | ||
".app .a {color: black;}", | ||
".app .b {color: white;}", | ||
".app .c {color: white;}", | ||
] | ||
`; | ||
|
||
exports[`Sandbox: dynamic style set textContent 1`] = ` | ||
Array [ | ||
".app .a { color: black; }", | ||
Array [ | ||
".app .a {color: black;}", | ||
], | ||
] | ||
`; |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { __MockHead__ } from '@garfish/utils'; | ||
import { Sandbox } from '../src/sandbox'; | ||
import { StyleManager } from '@garfish/loader'; | ||
import { rebuildCSSRules } from '../src/dynamicNode'; | ||
|
||
describe('Sandbox: dynamic style', () => { | ||
let sandbox: Sandbox; | ||
|
||
const go = (code: string | (() => void)) => { | ||
if (typeof code === 'function') { | ||
code = `(${code})();`; | ||
} | ||
return ` | ||
const sandbox = __debug_sandbox__; | ||
const Sandbox = sandbox.constructor; | ||
const nativeWindow = Sandbox.getNativeWindow(); | ||
${code} | ||
`; | ||
}; | ||
|
||
// Mock style transformer | ||
const styleManagerProto = StyleManager.prototype; | ||
const originalTransformCode = styleManagerProto.transformCode; | ||
styleManagerProto.transformCode = function (code) { | ||
return originalTransformCode('.app ' + code); | ||
}; | ||
|
||
beforeEach(() => { | ||
document.body.innerHTML = `<div id="root">123<div ${__MockHead__}></div></div>`; | ||
sandbox = new Sandbox({ | ||
namespace: 'app', | ||
el: () => document.querySelector('#root'), | ||
baseUrl: 'http://test.app', | ||
modules: [ | ||
() => ({ | ||
recover() {}, | ||
override: { go, jest, expect }, | ||
}), | ||
], | ||
}); | ||
}); | ||
|
||
it('set textContent', (done) => { | ||
sandbox.execScript( | ||
go(() => { | ||
const style = document.createElement('style'); | ||
style.textContent = '.a { color: black; }'; | ||
document.head.appendChild(style); | ||
expect([ | ||
style.textContent, | ||
Array.from(style.sheet!.cssRules).map((x) => x.cssText), | ||
]).toMatchSnapshot(); | ||
done(); | ||
}), | ||
{ done }, | ||
); | ||
}); | ||
|
||
it('append text node', (done) => { | ||
sandbox.execScript( | ||
go(() => { | ||
const style = document.createElement('style'); | ||
style.textContent = '.a { color: black; }'; | ||
document.head.appendChild(style); | ||
style.appendChild(document.createTextNode('.b { color: white; }')); | ||
expect([ | ||
style.textContent, | ||
Array.from(style.sheet!.cssRules).map((x) => x.cssText), | ||
]).toMatchSnapshot(); | ||
done(); | ||
}), | ||
{ done }, | ||
); | ||
}); | ||
|
||
it('insertRule', (done) => { | ||
sandbox.execScript( | ||
go(() => { | ||
const style = document.createElement('style'); | ||
document.head.appendChild(style); | ||
style.sheet!.insertRule('.b { color: white; }'); | ||
style.sheet!.insertRule('.a { color: black; }'); | ||
style.sheet!.insertRule('.c { color: white; }', 2); | ||
expect( | ||
Array.from(style.sheet!.cssRules).map((x) => x.cssText), | ||
).toMatchSnapshot(); | ||
done(); | ||
}), | ||
{ done }, | ||
); | ||
}); | ||
|
||
const createStyleComponentElementWithRecord = () => { | ||
sandbox.execScript( | ||
go(() => { | ||
const styleComponentElement = document.createElement('style'); | ||
document.head.appendChild(styleComponentElement); | ||
const cssRuleExample1 = '.test1 { color: red }'; | ||
const cssRuleExample2 = '.test2 { color: red }'; | ||
styleComponentElement.sheet!.insertRule(cssRuleExample1); | ||
styleComponentElement.sheet!.insertRule(cssRuleExample2); | ||
window._styleElement = styleComponentElement; | ||
}), | ||
); | ||
|
||
const style = sandbox.global!._styleElement as HTMLStyleElement; | ||
sandbox.dynamicStyleSheetElementSet.add(style); | ||
return style; | ||
}; | ||
|
||
it('should record the css rules of styled-components correctly', () => { | ||
const styleComponentElement = createStyleComponentElementWithRecord(); | ||
const data = sandbox.styledComponentCSSRulesMap.get(styleComponentElement); | ||
expect(data!.length).toEqual(2); | ||
expect(data![0].split(' {')[0]).toEqual('.app .test2'); | ||
expect(data![1].split(' {')[0]).toEqual('.app .test1'); | ||
}); | ||
|
||
it('should rebuild the css rules of styled-components in the correct order', () => { | ||
const styleComponentElement = createStyleComponentElementWithRecord(); | ||
|
||
const parent = styleComponentElement.parentElement; | ||
styleComponentElement.remove(); | ||
parent!.appendChild(styleComponentElement); | ||
|
||
rebuildCSSRules( | ||
sandbox.dynamicStyleSheetElementSet, | ||
sandbox.styledComponentCSSRulesMap, | ||
); | ||
const cssRules = styleComponentElement.sheet!.cssRules; | ||
expect(cssRules.length).toEqual(2); | ||
expect((cssRules![0] as CSSStyleRule).selectorText).toEqual('.app .test2'); | ||
expect((cssRules![1] as CSSStyleRule).selectorText).toEqual('.app .test1'); | ||
}); | ||
|
||
it('should be able to insertRule on old sheet after remount', () => { | ||
const styleComponentElement = createStyleComponentElementWithRecord(); | ||
const oldSheet = styleComponentElement.sheet; | ||
|
||
const parent = styleComponentElement.parentElement; | ||
styleComponentElement.remove(); | ||
parent!.appendChild(styleComponentElement); | ||
|
||
rebuildCSSRules( | ||
sandbox.dynamicStyleSheetElementSet, | ||
sandbox.styledComponentCSSRulesMap, | ||
); | ||
const cssRules = styleComponentElement.sheet!.cssRules; | ||
expect(cssRules.length).toEqual(2); | ||
expect((cssRules![0] as CSSStyleRule).selectorText).toEqual('.app .test2'); | ||
expect((cssRules![1] as CSSStyleRule).selectorText).toEqual('.app .test1'); | ||
oldSheet!.insertRule('.test3 {}', 2); | ||
expect((cssRules![2] as CSSStyleRule).selectorText).toEqual('.app .test3'); | ||
}); | ||
}); |
This file contains 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.
2a14077
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.
Successfully deployed to the following URLs:
garfish – ./
garfish.vercel.app
garfish-garfish.vercel.app
www.garfishjs.org
garfish-git-main-garfish.vercel.app
garfishjs.org