Skip to content

Commit

Permalink
fix: remove dangerouslySetInnerHTML (#4181)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaanus committed Jul 7, 2023
1 parent f995a0e commit 5388eaf
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions frontend/src/component/common/Highlighter/Highlighter.tsx
@@ -1,4 +1,4 @@
import { VFC } from 'react';
import { Fragment, VFC } from 'react';
import { safeRegExp } from '@server/util/escape-regex';
import { styled } from '@mui/material';

Expand Down Expand Up @@ -29,11 +29,18 @@ export const Highlighter: VFC<IHighlighterProps> = ({

const regex = safeRegExp(search, caseSensitive ? 'g' : 'gi');

return (
<StyledSpan
dangerouslySetInnerHTML={{
__html: children?.replaceAll(regex, '<mark>$&</mark>') || '',
}}
/>
const parts = children.split(regex);

const highlightedText = parts.map((part, index) =>
index < parts.length - 1 ? (
<Fragment key={index}>
{part}
<mark>{search}</mark>
</Fragment>
) : (
part
)
);

return <StyledSpan>{highlightedText}</StyledSpan>;
};

0 comments on commit 5388eaf

Please sign in to comment.