Skip to content

Commit

Permalink
fix: teach the globalifySelector to determine combinators
Browse files Browse the repository at this point in the history
  • Loading branch information
illright committed Jun 6, 2020
1 parent e5df3a1 commit 1783c55
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/modules/globalifySelector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
const combinatorPattern = /(\s*[ >+~]\s*)(?![^[]+\])/g;

export function globalifySelector(selector: string) {
return selector
.trim()
.split(' ')
.filter(Boolean)
.map((selectorPart) => {
.split(combinatorPattern)
.map((selectorPart: string, index: number) => {
// if this is the separator
if (index % 2 !== 0) {
return selectorPart;
}

if (selectorPart === '') {
return selectorPart;
}

if (selectorPart.startsWith(':local')) {
return selectorPart.replace(/:local\((.+?)\)/g, '$1');
}
Expand All @@ -13,5 +23,5 @@ export function globalifySelector(selector: string) {

return `:global(${selectorPart})`;
})
.join(' ');
.join('');
}
21 changes: 21 additions & 0 deletions test/modules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { resolve } from 'path';

import { importAny } from '../src/modules/importAny';
import { getIncludePaths } from '../src/modules/getIncludePaths';
import { globalifySelector } from '../src/modules/globalifySelector';

describe('importAny', () => {
it('should throw error when none exist', () => {
Expand Down Expand Up @@ -49,3 +50,23 @@ describe('getIncludePaths', () => {
expect(paths).toEqual(['src', 'node_modules', process.cwd(), dummyDir]);
});
});

describe('globalifySelector', () => {
it('correctly treats CSS selectors with legal spaces', async () => {
const selector = '[attr="with spaces"]';

expect(globalifySelector(selector)).toEqual(
':global([attr="with spaces"])',
);
});

it('correctly treats CSS combinators', async () => {
const selector = 'div > span';

expect(globalifySelector(selector)).toMatch(
// either be :global(div > span)
// or :global(div) > :global(span)
/(:global\(div> span\)|:global\(div\) > :global\(span\))/,
);
});
});

0 comments on commit 1783c55

Please sign in to comment.