Browse Source

fix: CP nonce should work on cssinjs (#41482)

pull/41501/head
二货爱吃白萝卜 2 years ago
committed by GitHub
parent
commit
f0d3e43faa
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      components/config-provider/__tests__/nonce.test.tsx
  2. 2
      components/config-provider/index.tsx
  3. 12
      components/config-provider/style/index.ts
  4. 75
      components/theme/util/genComponentStyleHook.ts
  5. 2
      package.json

21
components/config-provider/__tests__/icon.test.tsx → components/config-provider/__tests__/nonce.test.tsx

@ -1,8 +1,10 @@
import { createCache, StyleProvider } from '@ant-design/cssinjs';
import { SmileOutlined } from '@ant-design/icons'; import { SmileOutlined } from '@ant-design/icons';
import IconContext from '@ant-design/icons/lib/components/Context'; import IconContext from '@ant-design/icons/lib/components/Context';
import React from 'react'; import React from 'react';
import { render } from '../../../tests/utils';
import ConfigProvider from '..'; import ConfigProvider from '..';
import { render } from '../../../tests/utils';
import Button from '../../button';
describe('ConfigProvider.Icon', () => { describe('ConfigProvider.Icon', () => {
beforeEach(() => { beforeEach(() => {
@ -62,4 +64,21 @@ describe('ConfigProvider.Icon', () => {
expect(styleNode?.nonce).toEqual('light'); expect(styleNode?.nonce).toEqual('light');
expect(container.querySelector('#csp')?.innerHTML).toEqual('light'); expect(container.querySelector('#csp')?.innerHTML).toEqual('light');
}); });
it('cssinjs should support nonce', () => {
render(
<StyleProvider cache={createCache()}>
<ConfigProvider csp={{ nonce: 'bamboo' }}>
<Button />
</ConfigProvider>
</StyleProvider>,
);
const styleList = Array.from(document.querySelectorAll('style'));
expect(styleList.length).toBeTruthy();
styleList.forEach((style) => {
expect(style.nonce).toEqual('bamboo');
});
});
}); });

2
components/config-provider/index.tsx

@ -206,7 +206,7 @@ const ProviderChildren: React.FC<ProviderChildrenProps> = (props) => {
const shouldWrapSSR = iconPrefixCls !== parentContext.iconPrefixCls; const shouldWrapSSR = iconPrefixCls !== parentContext.iconPrefixCls;
const csp = customCsp || parentContext.csp; const csp = customCsp || parentContext.csp;
const wrapSSR = useStyle(iconPrefixCls); const wrapSSR = useStyle(iconPrefixCls, csp);
const mergedTheme = useTheme(theme, parentContext.theme); const mergedTheme = useTheme(theme, parentContext.theme);

12
components/config-provider/style/index.ts

@ -1,12 +1,20 @@
import { useStyleRegister } from '@ant-design/cssinjs'; import { useStyleRegister } from '@ant-design/cssinjs';
import type { CSPConfig } from '..';
import { resetIcon } from '../../style'; import { resetIcon } from '../../style';
import { useToken } from '../../theme/internal'; import { useToken } from '../../theme/internal';
const useStyle = (iconPrefixCls: string) => { const useStyle = (iconPrefixCls: string, csp?: CSPConfig) => {
const [theme, token] = useToken(); const [theme, token] = useToken();
// Generate style for icons // Generate style for icons
return useStyleRegister( return useStyleRegister(
{ theme, token, hashId: '', path: ['ant-design-icons', iconPrefixCls] }, {
theme,
token,
hashId: '',
path: ['ant-design-icons', iconPrefixCls],
nonce: () => csp?.nonce!,
},
() => [ () => [
{ {
[`.${iconPrefixCls}`]: { [`.${iconPrefixCls}`]: {

75
components/theme/util/genComponentStyleHook.ts

@ -2,11 +2,11 @@
import type { CSSInterpolation } from '@ant-design/cssinjs'; import type { CSSInterpolation } from '@ant-design/cssinjs';
import { useStyleRegister } from '@ant-design/cssinjs'; import { useStyleRegister } from '@ant-design/cssinjs';
import { useContext } from 'react'; import { useContext } from 'react';
import { genCommonStyle, genLinkStyle } from '../../style';
import { ConfigContext } from '../../config-provider/context'; import { ConfigContext } from '../../config-provider/context';
import { genCommonStyle, genLinkStyle } from '../../style';
import type { ComponentTokenMap, GlobalToken } from '../interface';
import type { UseComponentStyleResult } from '../internal'; import type { UseComponentStyleResult } from '../internal';
import { mergeToken, statisticToken, useToken } from '../internal'; import { mergeToken, statisticToken, useToken } from '../internal';
import type { ComponentTokenMap, GlobalToken } from '../interface';
export type OverrideTokenWithoutDerivative = ComponentTokenMap; export type OverrideTokenWithoutDerivative = ComponentTokenMap;
export type OverrideComponent = keyof OverrideTokenWithoutDerivative; export type OverrideComponent = keyof OverrideTokenWithoutDerivative;
@ -44,11 +44,19 @@ export default function genComponentStyleHook<ComponentName extends OverrideComp
) { ) {
return (prefixCls: string): UseComponentStyleResult => { return (prefixCls: string): UseComponentStyleResult => {
const [theme, token, hashId] = useToken(); const [theme, token, hashId] = useToken();
const { getPrefixCls, iconPrefixCls } = useContext(ConfigContext); const { getPrefixCls, iconPrefixCls, csp } = useContext(ConfigContext);
const rootPrefixCls = getPrefixCls(); const rootPrefixCls = getPrefixCls();
// Shared config
const sharedConfig: Omit<Parameters<typeof useStyleRegister>[0], 'path'> = {
theme,
token,
hashId,
nonce: () => csp?.nonce!,
};
// Generate style for all a tags in antd component. // Generate style for all a tags in antd component.
useStyleRegister({ theme, token, hashId, path: ['Shared', rootPrefixCls] }, () => [ useStyleRegister({ ...sharedConfig, path: ['Shared', rootPrefixCls] }, () => [
{ {
// Link // Link
'&': genLinkStyle(token), '&': genLinkStyle(token),
@ -56,40 +64,37 @@ export default function genComponentStyleHook<ComponentName extends OverrideComp
]); ]);
return [ return [
useStyleRegister( useStyleRegister({ ...sharedConfig, path: [component, prefixCls, iconPrefixCls] }, () => {
{ theme, token, hashId, path: [component, prefixCls, iconPrefixCls] }, const { token: proxyToken, flush } = statisticToken(token);
() => {
const { token: proxyToken, flush } = statisticToken(token);
const defaultComponentToken = const defaultComponentToken =
typeof getDefaultToken === 'function' ? getDefaultToken(proxyToken) : getDefaultToken; typeof getDefaultToken === 'function' ? getDefaultToken(proxyToken) : getDefaultToken;
const mergedComponentToken = { ...defaultComponentToken, ...token[component] }; const mergedComponentToken = { ...defaultComponentToken, ...token[component] };
const componentCls = `.${prefixCls}`; const componentCls = `.${prefixCls}`;
const mergedToken = mergeToken< const mergedToken = mergeToken<
TokenWithCommonCls<GlobalTokenWithComponent<OverrideComponent>> TokenWithCommonCls<GlobalTokenWithComponent<OverrideComponent>>
>( >(
proxyToken, proxyToken,
{ {
componentCls, componentCls,
prefixCls,
iconCls: `.${iconPrefixCls}`,
antCls: `.${rootPrefixCls}`,
},
mergedComponentToken,
);
const styleInterpolation = styleFn(mergedToken as unknown as FullToken<ComponentName>, {
hashId,
prefixCls, prefixCls,
rootPrefixCls, iconCls: `.${iconPrefixCls}`,
iconPrefixCls, antCls: `.${rootPrefixCls}`,
overrideComponentToken: token[component], },
}); mergedComponentToken,
flush(component, mergedComponentToken); );
return [genCommonStyle(token, prefixCls), styleInterpolation];
}, const styleInterpolation = styleFn(mergedToken as unknown as FullToken<ComponentName>, {
), hashId,
prefixCls,
rootPrefixCls,
iconPrefixCls,
overrideComponentToken: token[component],
});
flush(component, mergedComponentToken);
return [genCommonStyle(token, prefixCls), styleInterpolation];
}),
hashId, hashId,
]; ];
}; };

2
package.json

@ -108,7 +108,7 @@
], ],
"dependencies": { "dependencies": {
"@ant-design/colors": "^7.0.0", "@ant-design/colors": "^7.0.0",
"@ant-design/cssinjs": "^1.5.6", "@ant-design/cssinjs": "^1.7.1",
"@ant-design/icons": "^5.0.0", "@ant-design/icons": "^5.0.0",
"@ant-design/react-slick": "~1.0.0", "@ant-design/react-slick": "~1.0.0",
"@babel/runtime": "^7.18.3", "@babel/runtime": "^7.18.3",

Loading…
Cancel
Save