mirror of
https://github.com/ansible/awx.git
synced 2026-03-05 10:41:05 -03:30
Merge pull request #414 from gconsidine/ui/fix/host-filter-parsing
Fix host filter parsing
This commit is contained in:
@@ -164,7 +164,7 @@ export default ['$stateParams', '$scope', '$state', 'GetBasePath', 'QuerySet', '
|
|||||||
let splitTerms;
|
let splitTerms;
|
||||||
|
|
||||||
if ($scope.singleSearchParam === 'host_filter') {
|
if ($scope.singleSearchParam === 'host_filter') {
|
||||||
splitTerms = SmartSearchService.splitHostIntoTerms(terms);
|
splitTerms = SmartSearchService.splitFilterIntoTerms(terms);
|
||||||
} else {
|
} else {
|
||||||
splitTerms = SmartSearchService.splitSearchIntoTerms(terms);
|
splitTerms = SmartSearchService.splitSearchIntoTerms(terms);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,20 +6,26 @@ export default [function() {
|
|||||||
* work is done to encode quotes in quoted values and the spaces within those quoted
|
* work is done to encode quotes in quoted values and the spaces within those quoted
|
||||||
* values before calling to `splitSearchIntoTerms`.
|
* values before calling to `splitSearchIntoTerms`.
|
||||||
*/
|
*/
|
||||||
splitHostIntoTerms (searchString) {
|
splitFilterIntoTerms (searchString) {
|
||||||
|
if (!searchString) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
let groups = [];
|
let groups = [];
|
||||||
let quoted;
|
let quoted;
|
||||||
|
|
||||||
searchString.split(' ').forEach(substring => {
|
searchString.split(' ').forEach(substring => {
|
||||||
if (substring.includes(':"')) {
|
if (/:"/g.test(substring)) {
|
||||||
quoted = substring;
|
if (/"$/.test(substring)) {
|
||||||
|
groups.push(this.encode(substring));
|
||||||
|
} else {
|
||||||
|
quoted = substring;
|
||||||
|
}
|
||||||
} else if (quoted) {
|
} else if (quoted) {
|
||||||
quoted += ` ${substring}`;
|
quoted += ` ${substring}`;
|
||||||
|
|
||||||
if (substring.includes('"')) {
|
if (/"/g.test(substring)) {
|
||||||
quoted = quoted.replace(/"/g, encodeURIComponent('"'));
|
groups.push(this.encode(quoted));
|
||||||
quoted = quoted.replace(/ /g, encodeURIComponent(' '));
|
|
||||||
groups.push(quoted);
|
|
||||||
quoted = undefined;
|
quoted = undefined;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -29,6 +35,11 @@ export default [function() {
|
|||||||
|
|
||||||
return this.splitSearchIntoTerms(groups.join(' '));
|
return this.splitSearchIntoTerms(groups.join(' '));
|
||||||
},
|
},
|
||||||
|
encode (string) {
|
||||||
|
string = string.replace(/'/g, '%27');
|
||||||
|
|
||||||
|
return string.replace(/("| )/g, match => encodeURIComponent(match));
|
||||||
|
},
|
||||||
splitSearchIntoTerms(searchString) {
|
splitSearchIntoTerms(searchString) {
|
||||||
return searchString.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g);
|
return searchString.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -42,4 +42,22 @@ describe('Service: SmartSearch', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('fn splitFilterIntoTerms', () => {
|
||||||
|
it('should convert the filter term to a key and value with encode quotes and spaces', () => {
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms()).toEqual(null);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('foo')).toEqual(["foo"]);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('foo bar')).toEqual(["foo", "bar"]);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:foo bar')).toEqual(["name:foo", "bar"]);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:foo description:bar')).toEqual(["name:foo", "description:bar"]);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:"foo bar"')).toEqual(["name:%22foo%20bar%22"]);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:"foo bar" description:"bar foo"')).toEqual(["name:%22foo%20bar%22", "description:%22bar%20foo%22"]);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:"foo bar" a b c')).toEqual(["name:%22foo%20bar%22", 'a', 'b', 'c']);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:"1"')).toEqual(["name:%221%22"]);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:1')).toEqual(["name:1"]);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:"foo ba\'r" a b c')).toEqual(["name:%22foo%20ba%27r%22", 'a', 'b', 'c']);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:"foobar" other:"barbaz"')).toEqual(["name:%22foobar%22", "other:%22barbaz%22"]);
|
||||||
|
expect(SmartSearchService.splitFilterIntoTerms('name:"foobar" other:"bar baz"')).toEqual(["name:%22foobar%22", "other:%22bar%20baz%22"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user