Skip to content

Commit baf24db

Browse files
committed
fix: πŸ› prevent globalify to wrongly split escaped selectors
BREAKING CHANGE: 🧨 Uses Lookbehind assertions, so Node 9.11.2+ is needed βœ… Closes: Closes #191
1 parent 3ab63ac commit baf24db

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

β€Žsrc/modules/globalifySelector.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
const combinatorPattern = /(\s*[ >+~,]\s*)(?![^[]+\])/g;
1+
/*
2+
* Split a selector string (ex: div > foo ~ .potato) by
3+
* separators: space, >, +, ~ and comma (maybe not needed)
4+
* We use a negative lookbehind assertion to prevent matching
5+
* escaped combinators like `\~`.
6+
*/
7+
const combinatorPattern = /(?<!\\)(?:\\\\)*([ >+~,]\s*)(?![^[]+\])/g;
28

39
export function globalifySelector(selector: string) {
4-
return selector
5-
.trim()
6-
.split(combinatorPattern)
10+
const parts = selector.trim().split(combinatorPattern);
11+
const modifiedSelector = parts
712
.map((selectorPart: string, index: number) => {
813
// if this is the separator
914
if (index % 2 !== 0) {
@@ -25,4 +30,6 @@ export function globalifySelector(selector: string) {
2530
return `:global(${selectorPart})`;
2631
})
2732
.join('');
33+
34+
return modifiedSelector;
2835
}

β€Žtest/modules.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ describe('globalifySelector', () => {
7171
);
7272
expect(globalifySelector(selector2)).toEqual(':global(div), :global(span)');
7373
});
74+
75+
it('correctly treats selectors with escaped combinator characters', async () => {
76+
const selector1 = '.\\~positive.\\!normal ~ .\\+foo';
77+
78+
expect(globalifySelector(selector1)).toEqual(
79+
':global(.\\~positive.\\!normal) ~ :global(.\\+foo)',
80+
);
81+
});
7482
});
7583

7684
describe(`parse svelte file`, () => {

0 commit comments

Comments
Β (0)