Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"typescript": "^5.1.6"
},
"dependencies": {
"@ant-design/cssinjs": "^2.1.0",
"@ant-design/cssinjs": "^2.1.2",
"@babel/runtime": "^7.23.2",
"@rc-component/util": "^1.4.0"
},
Expand Down
5 changes: 4 additions & 1 deletion src/util/genStyleUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useMemo } from 'react';
import type React from 'react';
import { useMemo } from 'react';

import type { CSSInterpolation, CSSObject, TokenType } from '@ant-design/cssinjs';

Expand Down Expand Up @@ -251,6 +252,7 @@ function genStyleUtils<

return (rootCls: string | string[]) => {
const { cssVar, realToken } = useToken();
const csp = useCSP();

useCSSVarRegister(
{
Expand All @@ -261,6 +263,7 @@ function genStyleUtils<
ignore,
token: realToken,
scope: rootCls,
nonce: () => csp.nonce!,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The non-null assertion operator ! on csp.nonce! can be misleading here. The useCSP hook, especially its default implementation useDefaultCSP, can return an object where nonce is undefined. While () => undefined might be handled gracefully by useCSSVarRegister, it's generally better to avoid ! when nullability is possible to improve type safety clarity. Consider passing csp.nonce directly or providing a default if useCSSVarRegister strictly expects a string when the nonce property is present.

Suggested change
nonce: () => csp.nonce!,
nonce: () => csp.nonce,

},
() => {
const defaultToken = getDefaultComponentToken<CompTokenMap, AliasToken, C>(
Expand Down
49 changes: 49 additions & 0 deletions tests/genStyleUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,53 @@ describe('genStyleUtils', () => {
expect(usePrefix).not.toHaveBeenCalled();
});
});

describe('nonce support', () => {
it('should pass nonce to useCSSVarRegister', () => {
const testNonce = 'test-nonce-12345';
const config = {
usePrefix: jest.fn().mockReturnValue({
rootPrefixCls: 'ant',
iconPrefixCls: 'anticon',
}),
useToken: jest.fn().mockReturnValue({
theme: {},
realToken: { colorPrimary: '#1890ff' },
hashId: 'hash',
token: { colorPrimary: '#1890ff' },
cssVar: {
prefix: 'ant',
key: 'test-key',
},
}),
useCSP: jest.fn().mockReturnValue({ nonce: testNonce }),
};

const { genStyleHooks: gen } = genStyleUtils<TestCompTokenMap, object, object>(config);

const useStyle = gen(
'TestComponent',
() => ({}),
() => ({}),
);

const TestComponent: React.FC<{ prefixCls: string }> = ({ prefixCls }) => {
useStyle(prefixCls);
return <div data-testid="test-component">Test</div>;
};

render(
<StyleProvider cache={createCache()}>
<TestComponent prefixCls="test-prefix" />
</StyleProvider>,
);

// Check that style tags have the nonce attribute
const styleTags = document.querySelectorAll('style');
const hasNonce = Array.from(styleTags).some(
(style) => style.getAttribute('nonce') === testNonce,
);
expect(hasNonce).toBe(true);
});
});
});