Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/application/components/HighlightMatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ interface HighlightMatchProps {
}

const HighlightMatch = ({ searchTerm, value }: HighlightMatchProps) => {
const regex = new RegExp(searchTerm, "i");
if (!searchTerm) {
return <>{value}</>;
}

const escapedSearchTerm = RegExp.escape(searchTerm);

let regex: RegExp;

try {
regex = new RegExp(escapedSearchTerm, "i");
} catch {
return <>{value}</>;
}

const match = regex.exec(value);

if (!match) {
Expand All @@ -17,9 +30,9 @@ const HighlightMatch = ({ searchTerm, value }: HighlightMatchProps) => {
<span className="bg-searchHighlight dark:bg-searchHighlight-dark text-inverted dark:text-inverted-dark">
{match[0]}
</span>
{value.slice(match.index + searchTerm.length)}
{value.slice(match.index + match[0].length)}
</span>
);
};

export default HighlightMatch;
export default HighlightMatch;
6 changes: 4 additions & 2 deletions src/application/components/ObjectViewer/ObjectKey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export const ObjectKey = customRenderable(
({ className, value, softWrapCharacters }: ObjectKeyProps) => {
const uniqueChars = new Set(softWrapCharacters);
const regex = softWrapCharacters
? new RegExp(`(${softWrapCharacters.join("|")})`)
? new RegExp(
`(${softWrapCharacters.map((character) => RegExp.escape(character)).join("|")})`
)
: null;

return (
Expand All @@ -35,4 +37,4 @@ export const ObjectKey = customRenderable(
...props,
value: parentProps.value,
})
);
);
Loading