Skip to content

Commit

Permalink
Cascader: escape special characters for regexp (#12248)
Browse files Browse the repository at this point in the history
  • Loading branch information
jikkai authored and ziyoung committed Aug 7, 2018
1 parent cb93645 commit fbe58a4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/cascader/src/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import emitter from 'element-ui/src/mixins/emitter';
import Locale from 'element-ui/src/mixins/locale';
import { t } from 'element-ui/src/locale';
import debounce from 'throttle-debounce/debounce';
import { generateId } from 'element-ui/src/utils/util';
import { generateId, escapeRegexpString } from 'element-ui/src/utils/util';
const popperMixin = {
props: {
Expand Down Expand Up @@ -337,7 +337,8 @@ export default {
}
let filteredFlatOptions = flatOptions.filter(optionsStack => {
return optionsStack.some(option => new RegExp(value, 'i').test(option[this.labelKey]));
return optionsStack.some(option => new RegExp(escapeRegexpString(value), 'i')
.test(option[this.labelKey]));
});
if (filteredFlatOptions.length > 0) {
Expand Down
6 changes: 2 additions & 4 deletions packages/select/src/option.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<script type="text/babel">
import Emitter from 'element-ui/src/mixins/emitter';
import { getValueByPath } from 'element-ui/src/utils/util';
import { getValueByPath, escapeRegexpString } from 'element-ui/src/utils/util';
export default {
mixins: [Emitter],
Expand Down Expand Up @@ -129,9 +129,7 @@
},
queryChange(query) {
// query 里如果有正则中的特殊字符,需要先将这些字符转义
let parsedQuery = String(query).replace(/(\^|\(|\)|\[|\]|\$|\*|\+|\.|\?|\\|\{|\}|\|)/g, '\\$1');
this.visible = new RegExp(parsedQuery, 'i').test(this.currentLabel) || this.created;
this.visible = new RegExp(escapeRegexpString(query), 'i').test(this.currentLabel) || this.created;
if (!this.visible) {
this.select.filteredOptionsCount--;
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ export const valueEquals = (a, b) => {
}
return true;
};

export const escapeRegexpString = value => String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');

0 comments on commit fbe58a4

Please sign in to comment.