Skip to content

Commit 1b595ef

Browse files
authored
Don't use regex to search website nav (#20003)
1 parent 1633178 commit 1b595ef

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

  • apps/public-docsite/src/components/Nav

apps/public-docsite/src/components/Nav/Nav.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as styles from './Nav.module.scss';
2424
import { isLocal } from '../../utilities/index';
2525

2626
export interface INavState {
27+
/** Search query as typed by the user. May contain special characters (don't use as a regex). */
2728
searchQuery: string;
2829
defaultSortState: keyof typeof NavSortType;
2930
sortState: keyof typeof NavSortType;
@@ -152,17 +153,16 @@ export class Nav extends React.Component<INavProps, INavState> {
152153
};
153154

154155
private _renderLink = (page: INavPage, linkIndex: number): JSX.Element => {
155-
const { searchQuery } = this.state;
156+
const searchQuery = this.state.searchQuery.toLowerCase();
156157
const childLinks = page.pages ? this._renderLinkList(page.pages, true) : null;
157158
const ariaLabel = page.pages ? 'Hit enter to open sub menu, tab to access sub menu items.' : '';
158159
const title = page.title === 'Fabric' ? 'Home page' : page.title;
159-
const searchRegEx = new RegExp(searchQuery, 'i');
160160
const text = page.title;
161161
let linkText = <>{text}</>;
162162

163163
// Highlight search query within link.
164164
if (searchQuery) {
165-
const matchIndex = text.toLowerCase().indexOf(searchQuery.toLowerCase());
165+
const matchIndex = text.toLowerCase().indexOf(searchQuery);
166166
if (matchIndex >= 0) {
167167
const before = text.slice(0, matchIndex);
168168
const match = text.slice(matchIndex, matchIndex + searchQuery.length);
@@ -188,7 +188,7 @@ export class Nav extends React.Component<INavProps, INavState> {
188188
)}
189189
key={linkIndex + page.url}
190190
>
191-
{(!page.isUhfLink || isLocal) && searchRegEx.test(page.title) && (
191+
{(!page.isUhfLink || isLocal) && page.title.toLowerCase().indexOf(searchQuery) !== -1 && (
192192
<Link
193193
href={page.url}
194194
onClick={this._onLinkClick}
@@ -320,9 +320,9 @@ export class Nav extends React.Component<INavProps, INavState> {
320320
};
321321

322322
private _hasMatchChild = (page: INavPage): boolean => {
323-
const { searchQuery } = this.state;
324-
const searchRegEx = new RegExp(searchQuery, 'i');
325-
const checkPage = (pg: INavPage) => searchRegEx.test(pg.title) || (!!pg.pages && pg.pages.some(checkPage));
323+
const searchQuery = this.state.searchQuery.toLowerCase();
324+
const checkPage = (pg: INavPage) =>
325+
pg.title.toLowerCase().indexOf(searchQuery) !== -1 || (!!pg.pages && pg.pages.some(checkPage));
326326
return checkPage(page);
327327
};
328328

0 commit comments

Comments
 (0)