Skip to content

add extractStyleNode function to extract style as ReactNode. - #100

Open
tonny008 wants to merge 3 commits into
ant-design:masterfrom
tonny008:patch-extractStyleNode
Open

add extractStyleNode function to extract style as ReactNode.#100
tonny008 wants to merge 3 commits into
ant-design:masterfrom
tonny008:patch-extractStyleNode

Conversation

@tonny008

@tonny008 tonny008 commented Mar 25, 2023

Copy link
Copy Markdown

这样使得 SSR 更容易在 React 中直接渲染。例如在 nextjs 中:

  useServerInsertedHTML(() => extractStyleNode(cache));

Summary by CodeRabbit

  • 新功能
    • 新增了样式节点导出能力,便于在渲染或样式提取场景中直接获取可用的样式内容。
    • 相关能力已通过统一入口对外提供,使用更方便。

@danialzivehdadr

This comment was marked as abuse.

@afc163

afc163 commented Jun 20, 2026

Copy link
Copy Markdown
Member

冲突

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

useStyleRegister.tsx 中新增导出函数 extractStyleNode(cache),用于从缓存中筛选出样式相关条目并生成对应的 <style> React 节点数组。同时更新 src/index.ts,导入并对外导出该新函数。

Changes

extractStyleNode 导出

Layer / File(s) Summary
实现并导出 extractStyleNode
src/hooks/useStyleRegister.tsx, src/index.ts
新增函数遍历缓存中以 style% 开头的条目,解析出 styleStr/tokenKey/styleId 并生成带 ATTR_TOKEN/ATTR_MARK 属性的 <style> 节点数组;入口文件同步导入并导出该函数。

Estimated code review effort: 1 (Trivial) | ~5 minutes

Poem

缓存深处翻一翻,
拽出样式小蒲团,
贴上标记来分辨,
兔子欢呼跳三圈! 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了新增 extractStyleNode 的核心变化,并清楚说明了其用于将样式提取为 ReactNode。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/hooks/useStyleRegister.tsx

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

src/index.ts

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/hooks/useStyleRegister.tsx`:
- Around line 578-593: extractStyleNode is destructuring StyleCacheValue
incorrectly, so it uses the wrong tuple slots for token/style metadata and skips
the clientOnly/effectStyle handling. Update extractStyleNode to follow the same
destructuring and filtering logic as extract, using the proper StyleCacheValue
order from cache.cache.get(key)![1], and ensure only non-client-only styles are
emitted with the correct ATTR_TOKEN, ATTR_MARK, key, and innerHTML values.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c6349cd0-385e-41d1-affe-2e8c357ad5fb

📥 Commits

Reviewing files that changed from the base of the PR and between 1160994 and 08518ec.

📒 Files selected for processing (2)
  • src/hooks/useStyleRegister.tsx
  • src/index.ts

Comment on lines +578 to +593
export function extractStyleNode(cache: Cache) {
const styleKeys = Array.from(cache.cache.keys()).filter((key) =>
key.startsWith('style%')
);

return styleKeys.map((key) => {
const [styleStr, tokenKey, styleId]: [string, string, string] =
cache.cache.get(key)![1];
const props = {
[ATTR_TOKEN]: tokenKey,
[ATTR_MARK]: styleId,
dangerouslySetInnerHTML: { __html: styleStr },
};
return <style key={styleId} {...props} />;
});
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
rg -n -B2 -A10 'StyleCacheValue' src/hooks/useStyleRegister.tsx

Repository: ant-design/cssinjs

Length of output: 1287


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '560,620p' src/hooks/useStyleRegister.tsx
printf '\n---\n'
rg -n -B3 -A8 'cache\.cache\.get\(key\)!?\[1\]|STYLE_PREFIX|useGlobalCache<StyleCacheValue>|type Cache|interface Cache' src/hooks/useStyleRegister.tsx

Repository: ant-design/cssinjs

Length of output: 2078


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Show the cache type / entry shape and how extractStyleNode is used.
ast-grep outline src/hooks/useStyleRegister.tsx --view expanded
printf '\n---\n'
rg -n -A6 -B6 'extractStyleNode\(|cache\.cache\.set\(|cache\.cache\.get\(|useGlobalCache<StyleCacheValue>|type Cache|interface Cache|type CachePath|type CacheValue' src/hooks/useStyleRegister.tsx

Repository: ant-design/cssinjs

Length of output: 2594


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -A4 -B4 'extractStyleNode\(|cache\.set\(|cache\.get\(|class Cache|interface Cache|type Cache' src
printf '\n---\n'
rg -n -A6 -B6 'style%|ATTR_TOKEN|ATTR_MARK|clientOnly|effectStyle' src/hooks/useStyleRegister.tsx src

Repository: ant-design/cssinjs

Length of output: 50374


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '1,140p' src/Cache.ts
printf '\n---\n'
sed -n '1,220p' src/hooks/useGlobalCache.tsx

Repository: ant-design/cssinjs

Length of output: 4734


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n 'extractStyleNode\(' src .
printf '\n---\n'
sed -n '1,220p' src/extractStyle.ts

Repository: ant-design/cssinjs

Length of output: 3313


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '536,548p' src/hooks/useStyleRegister.tsx
printf '\n---\n'
rg -n 'tokenKey' src/hooks/useStyleRegister.tsx

Repository: ant-design/cssinjs

Length of output: 686


修正 extractStyleNodeStyleCacheValue 解构
StyleCacheValue 实际顺序是 [styleStr, styleId, effectStyle, clientOnly, order],这里把第 2/3 项当成了 tokenKey/styleId,会把 effectStyle 误写进 data-css-hashkey,同时漏掉 clientOnly 过滤和 effectStyle 的输出。建议直接复用 extract 的解构与处理逻辑。

🧰 Tools
🪛 ast-grep (0.44.1)

[warning] 588-588: Usage of dangerouslySetInnerHTML detected. This bypasses React's built-in XSS protection. Always sanitize HTML content using libraries like DOMPurify before injecting it into the DOM to prevent XSS attacks.
Context: dangerouslySetInnerHTML
Note: [CWE-79] Improper Neutralization of Input During Web Page Generation

(react-unsafe-html-injection)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/hooks/useStyleRegister.tsx` around lines 578 - 593, extractStyleNode is
destructuring StyleCacheValue incorrectly, so it uses the wrong tuple slots for
token/style metadata and skips the clientOnly/effectStyle handling. Update
extractStyleNode to follow the same destructuring and filtering logic as
extract, using the proper StyleCacheValue order from cache.cache.get(key)![1],
and ensure only non-client-only styles are emitted with the correct ATTR_TOKEN,
ATTR_MARK, key, and innerHTML values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants