Skip to content

Commit c78b260

Browse files
committed
fix: πŸ› nth-child not being correctly globalified
βœ… Closes: #224
1 parent 1bee2db commit c78b260

File tree

3 files changed

+75
-33
lines changed

3 files changed

+75
-33
lines changed

Diff for: β€Žsrc/modules/globalifySelector.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* We use a negative lookbehind assertion to prevent matching
55
* escaped combinators like `\~`.
66
*/
7-
const combinatorPattern = /(?<!\\)(?:\\\\)*([ >+~,]\s*)(?![^[]+\])/g;
7+
const combinatorPattern = /(?<!\\)(?:\\\\)*([ >+~,]\s*)(?![^[]+\]|\d)/g;
88

99
export function globalifySelector(selector: string) {
1010
const parts = selector.trim().split(combinatorPattern);
11+
1112
const modifiedSelector = parts
1213
.map((selectorPart: string, index: number) => {
1314
// if this is the separator

Diff for: β€Žtest/modules/globalifySelector.test.ts

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { globalifySelector } from '../../src/modules/globalifySelector';
2+
3+
describe('globalifySelector', () => {
4+
it('correctly treats CSS selectors with legal spaces', async () => {
5+
const selector = '[attr="with spaces"]';
6+
7+
expect(globalifySelector(selector)).toEqual(
8+
':global([attr="with spaces"])',
9+
);
10+
});
11+
12+
it('works with combinators', async () => {
13+
expect(globalifySelector('ul + p')).toEqual(`:global(ul) + :global(p)`);
14+
expect(globalifySelector('p > a')).toEqual(`:global(p) > :global(a)`);
15+
expect(globalifySelector('p + p')).toEqual(`:global(p) + :global(p)`);
16+
expect(globalifySelector('li a')).toEqual(`:global(li) :global(a)`);
17+
expect(globalifySelector('div ~ a')).toEqual(`:global(div) ~ :global(a)`);
18+
expect(globalifySelector('div, a')).toEqual(`:global(div), :global(a)`);
19+
});
20+
21+
it('correctly treats selectors with escaped combinator characters', async () => {
22+
const selector1 = '.\\~positive.\\!normal ~ .\\+foo';
23+
24+
expect(globalifySelector(selector1)).toEqual(
25+
':global(.\\~positive.\\!normal) ~ :global(.\\+foo)',
26+
);
27+
});
28+
29+
it('works with nth-child', async () => {
30+
expect(globalifySelector('tr:nth-child(odd)')).toEqual(
31+
`:global(tr:nth-child(odd))`,
32+
);
33+
expect(globalifySelector('tr:nth-child(2n+1)')).toEqual(
34+
`:global(tr:nth-child(2n+1))`,
35+
);
36+
expect(globalifySelector('tr:nth-child(even)')).toEqual(
37+
`:global(tr:nth-child(even))`,
38+
);
39+
expect(globalifySelector('tr:nth-child(2n)')).toEqual(
40+
`:global(tr:nth-child(2n))`,
41+
);
42+
expect(globalifySelector(':nth-child(7)')).toEqual(
43+
`:global(:nth-child(7))`,
44+
);
45+
expect(globalifySelector(':nth-child(5n)')).toEqual(
46+
`:global(:nth-child(5n))`,
47+
);
48+
expect(globalifySelector(':nth-child(n+7)')).toEqual(
49+
`:global(:nth-child(n+7))`,
50+
);
51+
expect(globalifySelector(':nth-child(3n+4)')).toEqual(
52+
`:global(:nth-child(3n+4))`,
53+
);
54+
expect(globalifySelector(':nth-child(-n+3)')).toEqual(
55+
`:global(:nth-child(-n+3))`,
56+
);
57+
expect(globalifySelector('p:nth-child(n)')).toEqual(
58+
`:global(p:nth-child(n))`,
59+
);
60+
expect(globalifySelector('p:nth-child(1)')).toEqual(
61+
`:global(p:nth-child(1))`,
62+
);
63+
expect(globalifySelector('p:nth-child(0n+1)')).toEqual(
64+
`:global(p:nth-child(0n+1))`,
65+
);
66+
expect(globalifySelector('p:nth-child(n+8):nth-child(-n+15)')).toEqual(
67+
`:global(p:nth-child(n+8):nth-child(-n+15))`,
68+
);
69+
});
70+
});

Diff for: β€Žtest/modules.test.ts renamed to β€Žtest/modules/modules.test.ts

+3-32
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { resolve } from 'path';
22

3-
import { getTestAppFilename, getFixtureContent } from './utils';
4-
import { getTagInfo } from '../src/modules/tagInfo';
3+
import { getTestAppFilename, getFixtureContent } from '../utils';
4+
import { getTagInfo } from '../../src/modules/tagInfo';
55
import {
66
importAny,
77
getIncludePaths,
88
hasDepInstalled,
9-
} from '../src/modules/utils';
10-
import { globalifySelector } from '../src/modules/globalifySelector';
9+
} from '../../src/modules/utils';
1110

1211
describe('importAny', () => {
1312
it('should throw error when none exist', () => {
@@ -56,34 +55,6 @@ describe('getIncludePaths', () => {
5655
});
5756
});
5857

59-
describe('globalifySelector', () => {
60-
it('correctly treats CSS selectors with legal spaces', async () => {
61-
const selector = '[attr="with spaces"]';
62-
63-
expect(globalifySelector(selector)).toEqual(
64-
':global([attr="with spaces"])',
65-
);
66-
});
67-
68-
it('correctly treats CSS combinators', async () => {
69-
const selector1 = 'div > span';
70-
const selector2 = 'div, span';
71-
72-
expect(globalifySelector(selector1)).toEqual(
73-
':global(div) > :global(span)',
74-
);
75-
expect(globalifySelector(selector2)).toEqual(':global(div), :global(span)');
76-
});
77-
78-
it('correctly treats selectors with escaped combinator characters', async () => {
79-
const selector1 = '.\\~positive.\\!normal ~ .\\+foo';
80-
81-
expect(globalifySelector(selector1)).toEqual(
82-
':global(.\\~positive.\\!normal) ~ :global(.\\+foo)',
83-
);
84-
});
85-
});
86-
8758
describe(`get tag information`, () => {
8859
it('should only include src files if content is empty', async () => {
8960
let parsedFile = await getTagInfo({

0 commit comments

Comments
Β (0)