Skip to content

Commit 4f8f36a

Browse files
authored
Revert "fix: apply StyleProvider nonce to CSS variable styles (#252)"
This reverts commit ec07fb1.
1 parent 0d105b2 commit 4f8f36a

File tree

5 files changed

+15
-73
lines changed

5 files changed

+15
-73
lines changed

src/StyleContext.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ export interface StyleContextProps {
7878
linters?: Linter[];
7979
/** Wrap css in a layer to avoid global style conflict */
8080
layer?: boolean;
81+
8182
/** Hardcode here since transformer not support take effect on serialize currently */
8283
autoPrefix?: boolean;
83-
84-
/** Nonce for CSP (Content Security Policy) */
85-
nonce?: string | (() => string);
8684
}
8785

8886
const StyleContext = React.createContext<StyleContextProps>({

src/hooks/useCSSVarRegister.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import StyleContext, {
55
ATTR_TOKEN,
66
CSS_IN_JS_INSTANCE,
77
} from '../StyleContext';
8-
import { isClientSide, mergeCSSConfig, toStyleStr } from '../util';
8+
import { isClientSide, toStyleStr } from '../util';
99
import type { TokenWithCSSVar } from '../util/css-variables';
1010
import { transformToken } from '../util/css-variables';
1111
import type { ExtractStyle } from './useGlobalCache';
@@ -39,7 +39,6 @@ const useCSSVarRegister = <V, T extends Record<string, V>>(
3939
cache: { instanceId },
4040
container,
4141
hashPriority,
42-
nonce,
4342
} = useContext(StyleContext);
4443
const { _tokenKey: tokenKey } = token;
4544

@@ -71,17 +70,12 @@ const useCSSVarRegister = <V, T extends Record<string, V>>(
7170
if (!cssVarsStr) {
7271
return;
7372
}
74-
const mergedCSSConfig = mergeCSSConfig<Parameters<typeof updateCSS>[2]>(
75-
{
76-
mark: ATTR_MARK,
77-
prepend: 'queue',
78-
attachTo: container,
79-
priority: -999,
80-
},
81-
nonce,
82-
);
83-
84-
const style = updateCSS(cssVarsStr, styleId, mergedCSSConfig);
73+
const style = updateCSS(cssVarsStr, styleId, {
74+
mark: ATTR_MARK,
75+
prepend: 'queue',
76+
attachTo: container,
77+
priority: -999,
78+
});
8579

8680
(style as any)[CSS_IN_JS_INSTANCE] = instanceId;
8781

src/hooks/useCacheToken.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import StyleContext, {
77
CSS_IN_JS_INSTANCE,
88
} from '../StyleContext';
99
import type Theme from '../theme/Theme';
10-
import { flattenToken, memoResult, mergeCSSConfig, token2key, toStyleStr } from '../util';
10+
import { flattenToken, memoResult, token2key, toStyleStr } from '../util';
1111
import { transformToken } from '../util/css-variables';
1212
import type { ExtractStyle } from './useGlobalCache';
1313
import useGlobalCache from './useGlobalCache';
@@ -161,7 +161,6 @@ export default function useCacheToken<
161161
cache: { instanceId },
162162
container,
163163
hashPriority,
164-
nonce,
165164
} = useContext(StyleContext);
166165
const {
167166
salt = '',
@@ -220,17 +219,12 @@ export default function useCacheToken<
220219
if (!cssVarsStr) {
221220
return;
222221
}
223-
const mergedCSSConfig = mergeCSSConfig<Parameters<typeof updateCSS>[2]>(
224-
{
225-
mark: ATTR_MARK,
226-
prepend: 'queue',
227-
attachTo: container,
228-
priority: -999,
229-
},
230-
nonce,
231-
);
232-
233-
const style = updateCSS(cssVarsStr, hash(`css-var-${themeKey}`), mergedCSSConfig);
222+
const style = updateCSS(cssVarsStr, hash(`css-var-${themeKey}`), {
223+
mark: ATTR_MARK,
224+
prepend: 'queue',
225+
attachTo: container,
226+
priority: -999,
227+
});
234228

235229
(style as any)[CSS_IN_JS_INSTANCE] = instanceId;
236230

src/util/index.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,6 @@ export function supportLogicProps(): boolean {
155155

156156
export const isClientSide = canUseDom();
157157

158-
/**
159-
* Merge CSS injection configuration with nonce support
160-
*/
161-
/**
162-
* Merge CSS injection configuration with nonce support
163-
*/
164-
export function mergeCSSConfig<T>(
165-
config: T,
166-
nonce?: string | (() => string),
167-
): T {
168-
if (!nonce) {
169-
return config;
170-
}
171-
172-
const mergedConfig = { ...config };
173-
const nonceStr = typeof nonce === 'function' ? nonce() : nonce;
174-
if (nonceStr) {
175-
(mergedConfig as any).csp = { nonce: nonceStr };
176-
}
177-
178-
return mergedConfig;
179-
}
180158
export function unit(num: string | number) {
181159
if (typeof num === 'number') {
182160
return `${num}px`;

tests/index.spec.tsx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -507,28 +507,6 @@ describe('csssinjs', () => {
507507
test('function', () => 'bamboo');
508508
});
509509

510-
describe('StyleProvider nonce for CSS var', () => {
511-
function testWithStyleProvider(name: string, nonce: string | (() => string)) {
512-
it(name, () => {
513-
const { unmount } = render(
514-
<StyleProvider cache={createCache()} nonce={nonce}>
515-
<Box />
516-
</StyleProvider>,
517-
);
518-
519-
const styles = Array.from(document.head.querySelectorAll('style'));
520-
// Box 组件使用 useCacheToken 注册 CSS var,应该有 nonce
521-
const cssVarStyle = styles.find(s => s.innerHTML.includes('--primary-color'));
522-
expect(cssVarStyle).toBeDefined();
523-
expect(cssVarStyle?.nonce).toBe('bamboo');
524-
// unmount 后样式清理行为取决于 cache 配置
525-
});
526-
}
527-
528-
testWithStyleProvider('string', 'bamboo');
529-
testWithStyleProvider('function', () => 'bamboo');
530-
});
531-
532510
it('should not insert style with different instanceId', () => {
533511
const genDemoStyle = (token: DerivativeToken): CSSInterpolation => ({
534512
div: {

0 commit comments

Comments
 (0)