From 63d6a2edc9054b0f44f973328efb81fa5b8d8f54 Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Thu, 22 Aug 2024 15:43:56 +0800 Subject: [PATCH] fix: root selector with single & --- src/hooks/useStyleRegister.tsx | 5 ++++- tests/index.spec.tsx | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/hooks/useStyleRegister.tsx b/src/hooks/useStyleRegister.tsx index 822379e..b2feec8 100644 --- a/src/hooks/useStyleRegister.tsx +++ b/src/hooks/useStyleRegister.tsx @@ -236,6 +236,9 @@ export const parseStyle = ( if (mergedKey.startsWith('@')) { // 略过媒体查询,交给子节点继续插入 hashId subInjectHash = true; + } else if (mergedKey === '&') { + // 抹掉 root selector 上的单个 & + mergedKey = injectSelectorHash('', hashId, hashPriority); } else { // 注入 hashId mergedKey = injectSelectorHash(key, hashId, hashPriority); @@ -251,7 +254,7 @@ export const parseStyle = ( // and finally we will get `{color:red;}` as css, which is wrong. // So we need to remove key in root, and treat child `{ a: { color: 'red' } }` as root. mergedKey = ''; - nextRoot = true; + nextRoot = !hashId; } const [parsedStr, childEffectStyle] = parseStyle( diff --git a/tests/index.spec.tsx b/tests/index.spec.tsx index 028b882..a2ad1dd 100644 --- a/tests/index.spec.tsx +++ b/tests/index.spec.tsx @@ -778,7 +778,7 @@ describe('csssinjs', () => { }); }); - it('hash & nest style', () => { + it.only('hash & nest style', () => { const genHashStyle = (): CSSInterpolation => ({ '&': { a: {