feat(home): use regexp for tags filtering (#4112)

Avoid substring false positives.

Fixes #4087
This commit is contained in:
Enishowk 2019-04-16 10:31:39 +02:00 committed by Julien Fontanet
parent 41ca853e03
commit d82c951db6
4 changed files with 32 additions and 3 deletions

View File

@ -3,6 +3,7 @@
### Enhancements ### Enhancements
- [Self/New VM] Display confirmation modal when user will use a large amount of resources [#4044](https://github.com/vatesfr/xen-orchestra/issues/4044) (PR [#4127](https://github.com/vatesfr/xen-orchestra/pull/4127)) - [Self/New VM] Display confirmation modal when user will use a large amount of resources [#4044](https://github.com/vatesfr/xen-orchestra/issues/4044) (PR [#4127](https://github.com/vatesfr/xen-orchestra/pull/4127))
- [Home] No more false positives when select Tag on Home page [#4087](https://github.com/vatesfr/xen-orchestra/issues/4087) (PR [#4112](https://github.com/vatesfr/xen-orchestra/pull/4112))
### Bug fixes ### Bug fixes
@ -10,5 +11,6 @@
### Released packages ### Released packages
- complex-matcher v0.6.0
- xo-server v5.40.0 - xo-server v5.40.0
- xo-web v5.40.0 - xo-web v5.40.0

View File

@ -599,6 +599,13 @@ export const parse = parser.parse.bind(parser)
// ------------------------------------------------------------------- // -------------------------------------------------------------------
const _extractStringFromRegexp = child => {
const unescapedRegexp = child.re.source.replace(/^(\^)|\\|\$$/g, '')
if (child.re.source === `^${escapeRegExp(unescapedRegexp)}$`) {
return unescapedRegexp
}
}
const _getPropertyClauseStrings = ({ child }) => { const _getPropertyClauseStrings = ({ child }) => {
if (child instanceof Or) { if (child instanceof Or) {
const strings = [] const strings = []
@ -606,6 +613,12 @@ const _getPropertyClauseStrings = ({ child }) => {
if (child instanceof StringNode) { if (child instanceof StringNode) {
strings.push(child.value) strings.push(child.value)
} }
if (child instanceof RegExpNode) {
const unescapedRegexp = _extractStringFromRegexp(child)
if (unescapedRegexp !== undefined) {
strings.push(unescapedRegexp)
}
}
}) })
return strings return strings
} }
@ -613,6 +626,12 @@ const _getPropertyClauseStrings = ({ child }) => {
if (child instanceof StringNode) { if (child instanceof StringNode) {
return [child.value] return [child.value]
} }
if (child instanceof RegExpNode) {
const unescapedRegexp = _extractStringFromRegexp(child)
if (unescapedRegexp !== undefined) {
return [unescapedRegexp]
}
}
return [] return []
} }

View File

@ -12,10 +12,13 @@ import {
} from './' } from './'
it('getPropertyClausesStrings', () => { it('getPropertyClausesStrings', () => {
const tmp = getPropertyClausesStrings(parse('foo bar:baz baz:|(foo bar)')) const tmp = getPropertyClausesStrings(
parse('foo bar:baz baz:|(foo bar /^boo$/ /^far$/) foo:/^bar$/')
)
expect(tmp).toEqual({ expect(tmp).toEqual({
bar: ['baz'], bar: ['baz'],
baz: ['foo', 'bar'], baz: ['foo', 'bar', 'boo', 'far'],
foo: ['bar'],
}) })
}) })

View File

@ -20,6 +20,7 @@ import { Card, CardHeader, CardBlock } from 'card'
import { import {
ceil, ceil,
debounce, debounce,
escapeRegExp,
filter, filter,
find, find,
forEach, forEach,
@ -747,7 +748,11 @@ export default class Home extends Component {
filter, filter,
'tags', 'tags',
new ComplexMatcher.Or( new ComplexMatcher.Or(
map(tags, tag => new ComplexMatcher.String(tag.id)) map(
tags,
tag =>
new ComplexMatcher.RegExp(`^${escapeRegExp(tag.id)}$`)
)
) )
) )
: ComplexMatcher.setPropertyClause(filter, 'tags', undefined) : ComplexMatcher.setPropertyClause(filter, 'tags', undefined)