Commit 0a1bb97a by kay delaney Committed by GitHub

Loki: getHighlighterExpressionsFromQuery Returns null if filter term is not quoted (#17852)

Closes #17687
parent 8f150c67
......@@ -71,17 +71,25 @@ describe('getHighlighterExpressionsFromQuery', () => {
it('returns no expressions for empty query', () => {
expect(getHighlighterExpressionsFromQuery('')).toEqual([]);
});
it('returns a single expressions for legacy query', () => {
expect(getHighlighterExpressionsFromQuery('{} x')).toEqual(['(?i)x']);
expect(getHighlighterExpressionsFromQuery('{foo="bar"} x')).toEqual(['(?i)x']);
});
it('returns an expression for query with filter', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x"')).toEqual(['x']);
});
it('returns expressions for query with filter chain', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ "y"')).toEqual(['x', 'y']);
});
it('returns drops expressions for query with negative filter chain', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" != "y"')).toEqual(['x']);
});
it('returns null if filter term is not wrapped in double quotes', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= x')).toEqual(null);
});
});
......@@ -59,12 +59,19 @@ export function getHighlighterExpressionsFromQuery(input: string): string[] {
if (filterEnd === -1) {
filterTerm = expression.trim();
} else {
filterTerm = expression.substr(0, filterEnd);
filterTerm = expression.substr(0, filterEnd).trim();
expression = expression.substr(filterEnd);
}
// Unwrap the filter term by removing quotes
results.push(filterTerm.replace(/^\s*"/g, '').replace(/"\s*$/g, ''));
const quotedTerm = filterTerm.match(/^"((?:[^\\"]|\\")*)"$/);
if (quotedTerm) {
const unwrappedFilterTerm = quotedTerm[1];
results.push(unwrappedFilterTerm);
} else {
return null;
}
}
return results;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment