Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug 1585087 - Escape search user input for RegExp. #1404

Merged
merged 1 commit into from Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/package.json
Expand Up @@ -20,6 +20,7 @@
"linkify-it": "2.1.0",
"lodash.clonedeep": "4.5.0",
"lodash.debounce": "4.0.8",
"lodash.escaperegexp": "^4.1.2",
"lodash.flattendeep": "4.4.0",
"lodash.isequal": "4.5.0",
"nprogress": "0.2.0",
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/modules/search/withSearch.js
Expand Up @@ -2,6 +2,7 @@

import * as React from 'react';
import shortid from 'shortid';
import escapeRegExp from 'lodash.escaperegexp';
import { mark } from 'react-content-marker';


Expand All @@ -25,7 +26,7 @@ export function markSearchTerms(base: string, search: string){
});

for (let searchTerm of searchTerms) {
const rule = new RegExp(searchTerm, 'i');
const rule = new RegExp(escapeRegExp(searchTerm), 'i');
const tag = (x: string) =>
<mark className='search' key={ shortid.generate() }>{ x }</mark>;

Expand Down
14 changes: 14 additions & 0 deletions frontend/src/modules/search/withSearch.test.js
@@ -0,0 +1,14 @@
import React from 'react';
import { shallow } from 'enzyme';

import { markSearchTerms } from './withSearch';


describe('markSearchTerms', () => {
it('does not break on regexp special characters', () => {
const marked = markSearchTerms('foo (bar)', '(bar');
const wrapper = shallow(<p>{ marked }</p>);
expect(wrapper.find('mark')).toHaveLength(1);
expect(wrapper.find('mark').text()).toEqual('(bar');
});
});
5 changes: 5 additions & 0 deletions frontend/yarn.lock
Expand Up @@ -6344,6 +6344,11 @@ lodash.escape@^4.0.1:
resolved "https://registry.yarnpkg.com/lodash.escape/-/lodash.escape-4.0.1.tgz#c9044690c21e04294beaa517712fded1fa88de98"
integrity sha1-yQRGkMIeBClL6qUXcS/e0fqI3pg=

lodash.escaperegexp@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz#64762c48618082518ac3df4ccf5d5886dae20347"
integrity sha1-ZHYsSGGAglGKw99Mz11YhtriA0c=

lodash.flattendeep@4.4.0, lodash.flattendeep@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2"
Expand Down