Skip to content

Commit

Permalink
fix: safely escape strings passed to RegExp in InputSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
wp-aberg committed Dec 11, 2024
1 parent 0cb0ae6 commit 66fc814
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/kit/src/input-search/InputSearchListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ export const ListItem = ({ item, state }: ListItemProps) => {
);

let highlighted;

const escape = (string) => {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
};

if (typeof item.rendered === "string") {
highlighted = item.rendered.replace(
new RegExp((state as ComboBoxState<object>).inputValue, "gi"),
(match) => (match ? `<mark>${match}</mark>` : "")
const val = escape((state as ComboBoxState<object>).inputValue);
highlighted = item.rendered.replace(new RegExp(val, "gi"), (match) =>
match ? `<mark>${match}</mark>` : ""
);
}

Expand Down

0 comments on commit 66fc814

Please sign in to comment.