Skip to content

Commit

Permalink
fix(core): escape custom classes for query selectors (fix #674)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grsmto committed Oct 8, 2023
1 parent 0b4047b commit 10d0d98
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/examples/stories/SimpleBarReact.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ WithScale.args = {
style: { transform: 'scale(0.5)' },
};

export const WithCustomClassNames = Template.bind({});
WithCustomClassNames.args = {
...SimpleBar.defaultOptions,
classNames: {
contentEl: 'simplebar-content max-h-[1rem]',
},
style: { height: 300 },
};

const WithImageTemplate: StoryFn<{
className: string;
style: React.CSSProperties;
Expand Down
6 changes: 5 additions & 1 deletion packages/simplebar-core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ export function removeClasses(el: HTMLElement | null, classes: string): void {
}

export function classNamesToQuery(classNames: string): string {
return `.${classNames.split(' ').join('.')}`;
return `.${classNames.split(' ').map(escapeForQuerySelector).join('.')}`;
}

export function escapeForQuerySelector(str: string): string {
return str.replace(/([!\"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~])/g, '\\$1');
}

export function mergeClassNames(
Expand Down

0 comments on commit 10d0d98

Please sign in to comment.