Skip to content

Commit

Permalink
fix: πŸ› prevent globalify to wrongly split escaped selectors
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 🧨 Uses Lookbehind assertions, so Node 9.11.2+ is needed

βœ… Closes: Closes #191
  • Loading branch information
kaisermann committed Jul 5, 2020
1 parent 28a49d6 commit f461320
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/modules/globalifySelector.ts
@@ -1,9 +1,14 @@
const combinatorPattern = /(\s*[ >+~,]\s*)(?![^[]+\])/g;
/*
* Split a selector string (ex: div > foo ~ .potato) by
* separators: space, >, +, ~ and comma (maybe not needed)
* We use a negative lookbehind assertion to prevent matching
* escaped combinators like `\~`.
*/
const combinatorPattern = /(?<!\\)(?:\\\\)*([ >+~,]\s*)(?![^[]+\])/g;

export function globalifySelector(selector: string) {
return selector
.trim()
.split(combinatorPattern)
const parts = selector.trim().split(combinatorPattern);
const modifiedSelector = parts
.map((selectorPart: string, index: number) => {
// if this is the separator
if (index % 2 !== 0) {
Expand All @@ -25,4 +30,6 @@ export function globalifySelector(selector: string) {
return `:global(${selectorPart})`;
})
.join('');

return modifiedSelector;
}
8 changes: 8 additions & 0 deletions test/modules.test.ts
Expand Up @@ -71,6 +71,14 @@ describe('globalifySelector', () => {
);
expect(globalifySelector(selector2)).toEqual(':global(div), :global(span)');
});

it('correctly treats selectors with escaped combinator characters', async () => {
const selector1 = '.\\~positive.\\!normal ~ .\\+foo';

expect(globalifySelector(selector1)).toEqual(
':global(.\\~positive.\\!normal) ~ :global(.\\+foo)',
);
});
});

describe(`parse svelte file`, () => {
Expand Down

0 comments on commit f461320

Please sign in to comment.