mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Logs: Fixed incorrect highlighting on empty line filter (#52214)
* fixed hightlighting searchwords * do not add empty searchWords
This commit is contained in:
@@ -12,6 +12,36 @@ describe('getHighlighterExpressionsFromQuery', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('')).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns no expression for query with empty filter ', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= ``')).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns no expression for query with empty filter and parser', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= `` | json count="counter" | __error__=``')).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns no expression for query with empty filter and chained filter', () => {
|
||||
expect(
|
||||
getHighlighterExpressionsFromQuery('{foo="bar"} |= `` |= `highlight` | json count="counter" | __error__=``')
|
||||
).toEqual(['highlight']);
|
||||
});
|
||||
|
||||
it('returns no expression for query with empty filter, chained and regex filter', () => {
|
||||
expect(
|
||||
getHighlighterExpressionsFromQuery(
|
||||
'{foo="bar"} |= `` |= `highlight` |~ `high.ight` | json count="counter" | __error__=``'
|
||||
)
|
||||
).toEqual(['highlight', 'high.ight']);
|
||||
});
|
||||
|
||||
it('returns no expression for query with empty filter, chained and regex quotes filter', () => {
|
||||
expect(
|
||||
getHighlighterExpressionsFromQuery(
|
||||
'{foo="bar"} |= `` |= `highlight` |~ "highlight\\\\d" | json count="counter" | __error__=``'
|
||||
)
|
||||
).toEqual(['highlight', 'highlight\\d']);
|
||||
});
|
||||
|
||||
it('returns an expression for query with filter using quotes', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x"')).toEqual(['x']);
|
||||
});
|
||||
|
||||
@@ -32,8 +32,8 @@ export function getHighlighterExpressionsFromQuery(input: string): string[] {
|
||||
if (skip) {
|
||||
continue;
|
||||
}
|
||||
// Check if there is more chained
|
||||
const filterEnd = expression.search(/\|=|\|~|!=|!~/);
|
||||
// Check if there is more chained, by just looking for the next pipe-operator
|
||||
const filterEnd = expression.search(/\|/);
|
||||
let filterTerm;
|
||||
if (filterEnd === -1) {
|
||||
filterTerm = expression.trim();
|
||||
@@ -50,14 +50,20 @@ export function getHighlighterExpressionsFromQuery(input: string): string[] {
|
||||
const unwrappedFilterTerm = term[1];
|
||||
const regexOperator = filterOperator === '|~';
|
||||
|
||||
let resultTerm = '';
|
||||
|
||||
// Only filter expressions with |~ operator are treated as regular expressions
|
||||
if (regexOperator) {
|
||||
// When using backticks, Loki doesn't require to escape special characters and we can just push regular expression to highlights array
|
||||
// When using quotes, we have extra backslash escaping and we need to replace \\ with \
|
||||
results.push(backtickedTerm ? unwrappedFilterTerm : unwrappedFilterTerm.replace(/\\\\/g, '\\'));
|
||||
resultTerm = backtickedTerm ? unwrappedFilterTerm : unwrappedFilterTerm.replace(/\\\\/g, '\\');
|
||||
} else {
|
||||
// We need to escape this string so it is not matched as regular expression
|
||||
results.push(escapeRegExp(unwrappedFilterTerm));
|
||||
resultTerm = escapeRegExp(unwrappedFilterTerm);
|
||||
}
|
||||
|
||||
if (resultTerm) {
|
||||
results.push(resultTerm);
|
||||
}
|
||||
} else {
|
||||
return results;
|
||||
|
||||
Reference in New Issue
Block a user