Skip to content

Commit

Permalink
✨ isolation for all the grouping selector (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Aug 7, 2020
1 parent db31e9d commit a191d51
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
14 changes: 12 additions & 2 deletions src/sandbox/patchers/__tests__/css.test.ts
Expand Up @@ -112,6 +112,16 @@ test('should rewrite root-level correctly [2]', () => {
expect(removeWs(styleNode.textContent)).toBe(removeWs(expectValue));
});

test('should rewrite root-level correctly [3]', () => {
const actualValue = `html button[type="reset"] {color: #fff}`;
const expectValue = `div[data-qiankun=react15] button[type="reset"]{color:#fff;}`;

const styleNode = fakeStyleNode(actualValue);
CSSProcessor.process(styleNode, 'div[data-qiankun=react15]');

expect(removeWs(styleNode.textContent)).toBe(removeWs(expectValue));
});

test('should not replace body/html if body is part of class [1]', () => {
const actualValue = '.ant-card-body {color: #eee}';
const expectValue = 'div[data-qiankun=react15] .ant-card-body {color: #eee;}';
Expand Down Expand Up @@ -204,7 +214,7 @@ test('should handle root-level grouping selector [1]', () => {

test('should handle root-level grouping selector [2]', () => {
const actualValue = 'body,html,div {color: #eee;}';
const expectValue = 'div[data-qiankun=react15],div {color: #eee;}';
const expectValue = 'div[data-qiankun=react15],div[data-qiankun=react15]div{color:#eee;}';

const styleNode = fakeStyleNode(actualValue);
CSSProcessor.process(styleNode, 'div[data-qiankun=react15]');
Expand All @@ -214,7 +224,7 @@ test('should handle root-level grouping selector [2]', () => {

test('should handle root-level grouping selector [3]', () => {
const actualValue = 'a,body,html,div {color: #eee;}';
const expectValue = 'a,div[data-qiankun=react15],div {color: #eee;}';
const expectValue = 'div[data-qiankun=react15]a,div[data-qiankun=react15],div[data-qiankun=react15]div{color:#eee;}';

const styleNode = fakeStyleNode(actualValue);
CSSProcessor.process(styleNode, 'div[data-qiankun=react15]');
Expand Down
33 changes: 21 additions & 12 deletions src/sandbox/patchers/css.ts
Expand Up @@ -134,20 +134,29 @@ class ScopedCSS {
}
}

if (rootSelectorRE.test(rule.selectorText)) {
// handle div,body,span { ... }
return cssText.replace(rootSelectorRE, m => {
// do not discard valid previous character, such as body,html or *:not(:root)
const whitePrevChars = [',', '('];

if (m && whitePrevChars.includes(m[0])) {
return `${m[0]}${prefix}`;
// handle grouping selector, a,span,p,div { ... }
cssText = cssText.replace(/^[^]+{/, selectors =>
selectors.replace(/(^|,\n?)([^,]+)/g, (item, p, s) => {
// handle div,body,span { ... }
if (rootSelectorRE.test(item)) {
return item.replace(rootSelectorRE, m => {
// do not discard valid previous character, such as body,html or *:not(:root)
const whitePrevChars = [',', '('];

if (m && whitePrevChars.includes(m[0])) {
return `${m[0]}${prefix}`;
}

// replace root selector with prefix
return prefix;
});
}
return prefix;
});
}

return `${prefix} ${cssText}`;
return `${p}${prefix} ${s}`;
}),
);

return cssText;
}

// handle case:
Expand Down

1 comment on commit a191d51

@github-actions
Copy link

Choose a reason for hiding this comment

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

Deploy preview for qiankun ready!

✅ Preview
https://qiankun-2fe2jxl0q.vercel.app

Built with commit a191d51.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.