Skip to content

Commit 1783c55

Browse files
committed
fix: teach the globalifySelector to determine combinators
1 parent e5df3a1 commit 1783c55

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

Diff for: src/modules/globalifySelector.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
const combinatorPattern = /(\s*[ >+~]\s*)(?![^[]+\])/g;
2+
13
export function globalifySelector(selector: string) {
24
return selector
35
.trim()
4-
.split(' ')
5-
.filter(Boolean)
6-
.map((selectorPart) => {
6+
.split(combinatorPattern)
7+
.map((selectorPart: string, index: number) => {
8+
// if this is the separator
9+
if (index % 2 !== 0) {
10+
return selectorPart;
11+
}
12+
13+
if (selectorPart === '') {
14+
return selectorPart;
15+
}
16+
717
if (selectorPart.startsWith(':local')) {
818
return selectorPart.replace(/:local\((.+?)\)/g, '$1');
919
}
@@ -13,5 +23,5 @@ export function globalifySelector(selector: string) {
1323

1424
return `:global(${selectorPart})`;
1525
})
16-
.join(' ');
26+
.join('');
1727
}

Diff for: test/modules.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from 'path';
22

33
import { importAny } from '../src/modules/importAny';
44
import { getIncludePaths } from '../src/modules/getIncludePaths';
5+
import { globalifySelector } from '../src/modules/globalifySelector';
56

67
describe('importAny', () => {
78
it('should throw error when none exist', () => {
@@ -49,3 +50,23 @@ describe('getIncludePaths', () => {
4950
expect(paths).toEqual(['src', 'node_modules', process.cwd(), dummyDir]);
5051
});
5152
});
53+
54+
describe('globalifySelector', () => {
55+
it('correctly treats CSS selectors with legal spaces', async () => {
56+
const selector = '[attr="with spaces"]';
57+
58+
expect(globalifySelector(selector)).toEqual(
59+
':global([attr="with spaces"])',
60+
);
61+
});
62+
63+
it('correctly treats CSS combinators', async () => {
64+
const selector = 'div > span';
65+
66+
expect(globalifySelector(selector)).toMatch(
67+
// either be :global(div > span)
68+
// or :global(div) > :global(span)
69+
/(:global\(div> span\)|:global\(div\) > :global\(span\))/,
70+
);
71+
});
72+
});

0 commit comments

Comments
 (0)