Cascader: escape special characters for regexp (#12248)

pull/12260/head
Jikkai Xiao 2018-08-07 10:36:49 +08:00 committed by hetech
parent cb93645728
commit fbe58a4015
3 changed files with 7 additions and 6 deletions

View File

@ -70,7 +70,7 @@ import emitter from 'element-ui/src/mixins/emitter';
import Locale from 'element-ui/src/mixins/locale'; import Locale from 'element-ui/src/mixins/locale';
import { t } from 'element-ui/src/locale'; import { t } from 'element-ui/src/locale';
import debounce from 'throttle-debounce/debounce'; 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 = { const popperMixin = {
props: { props: {
@ -337,7 +337,8 @@ export default {
} }
let filteredFlatOptions = flatOptions.filter(optionsStack => { 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) { if (filteredFlatOptions.length > 0) {

View File

@ -17,7 +17,7 @@
<script type="text/babel"> <script type="text/babel">
import Emitter from 'element-ui/src/mixins/emitter'; 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 { export default {
mixins: [Emitter], mixins: [Emitter],
@ -129,9 +129,7 @@
}, },
queryChange(query) { queryChange(query) {
// query this.visible = new RegExp(escapeRegexpString(query), 'i').test(this.currentLabel) || this.created;
let parsedQuery = String(query).replace(/(\^|\(|\)|\[|\]|\$|\*|\+|\.|\?|\\|\{|\}|\|)/g, '\\$1');
this.visible = new RegExp(parsedQuery, 'i').test(this.currentLabel) || this.created;
if (!this.visible) { if (!this.visible) {
this.select.filteredOptionsCount--; this.select.filteredOptionsCount--;
} }

View File

@ -82,3 +82,5 @@ export const valueEquals = (a, b) => {
} }
return true; return true;
}; };
export const escapeRegexpString = value => String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');