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

Cascader: escape special characters for regexp #12248

Merged
merged 1 commit into from
Aug 7, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '\\$&');