mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[6.6] [KQL] Support matching field names with newlines (#30858)
* Use 's' instead of 'm' to match newlines instead of doing multi-line * Fix autocomplete suggestions to handle newlines and tabs in field names * Add line feed to escaped whitespace
This commit is contained in:
parent
c0c81857d4
commit
edc8768337
5 changed files with 90 additions and 56 deletions
|
@ -203,33 +203,35 @@ module.exports = (function() {
|
|||
peg$c39 = "*",
|
||||
peg$c40 = { type: "literal", value: "*", description: "\"*\"" },
|
||||
peg$c41 = function() { return wildcardSymbol; },
|
||||
peg$c42 = "\\n",
|
||||
peg$c43 = { type: "literal", value: "\\n", description: "\"\\\\n\"" },
|
||||
peg$c44 = function() { return '\n'; },
|
||||
peg$c45 = "\\t",
|
||||
peg$c46 = { type: "literal", value: "\\t", description: "\"\\\\t\"" },
|
||||
peg$c47 = function() { return '\t'; },
|
||||
peg$c48 = function(keyword) { return keyword; },
|
||||
peg$c49 = /^[\\():<>"*]/,
|
||||
peg$c50 = { type: "class", value: "[\\\\():<>\"*]", description: "[\\\\():<>\"*]" },
|
||||
peg$c51 = "<=",
|
||||
peg$c52 = { type: "literal", value: "<=", description: "\"<=\"" },
|
||||
peg$c53 = function() { return 'lte'; },
|
||||
peg$c54 = ">=",
|
||||
peg$c55 = { type: "literal", value: ">=", description: "\">=\"" },
|
||||
peg$c56 = function() { return 'gte'; },
|
||||
peg$c57 = "<",
|
||||
peg$c58 = { type: "literal", value: "<", description: "\"<\"" },
|
||||
peg$c59 = function() { return 'lt'; },
|
||||
peg$c60 = ">",
|
||||
peg$c61 = { type: "literal", value: ">", description: "\">\"" },
|
||||
peg$c62 = function() { return 'gt'; },
|
||||
peg$c63 = /^[ \t\r\n]/,
|
||||
peg$c64 = { type: "class", value: "[\\ \\t\\r\\n]", description: "[\\ \\t\\r\\n]" },
|
||||
peg$c65 = function() { return parseCursor; },
|
||||
peg$c66 = "@kuery-cursor@",
|
||||
peg$c67 = { type: "literal", value: "@kuery-cursor@", description: "\"@kuery-cursor@\"" },
|
||||
peg$c68 = function() { return cursorSymbol; },
|
||||
peg$c42 = "\\t",
|
||||
peg$c43 = { type: "literal", value: "\\t", description: "\"\\\\t\"" },
|
||||
peg$c44 = function() { return '\t'; },
|
||||
peg$c45 = "\\r",
|
||||
peg$c46 = { type: "literal", value: "\\r", description: "\"\\\\r\"" },
|
||||
peg$c47 = "\\n",
|
||||
peg$c48 = { type: "literal", value: "\\n", description: "\"\\\\n\"" },
|
||||
peg$c49 = function() { return '\n'; },
|
||||
peg$c50 = function(keyword) { return keyword; },
|
||||
peg$c51 = /^[\\():<>"*]/,
|
||||
peg$c52 = { type: "class", value: "[\\\\():<>\"*]", description: "[\\\\():<>\"*]" },
|
||||
peg$c53 = "<=",
|
||||
peg$c54 = { type: "literal", value: "<=", description: "\"<=\"" },
|
||||
peg$c55 = function() { return 'lte'; },
|
||||
peg$c56 = ">=",
|
||||
peg$c57 = { type: "literal", value: ">=", description: "\">=\"" },
|
||||
peg$c58 = function() { return 'gte'; },
|
||||
peg$c59 = "<",
|
||||
peg$c60 = { type: "literal", value: "<", description: "\"<\"" },
|
||||
peg$c61 = function() { return 'lt'; },
|
||||
peg$c62 = ">",
|
||||
peg$c63 = { type: "literal", value: ">", description: "\">\"" },
|
||||
peg$c64 = function() { return 'gt'; },
|
||||
peg$c65 = /^[ \t\r\n]/,
|
||||
peg$c66 = { type: "class", value: "[\\ \\t\\r\\n]", description: "[\\ \\t\\r\\n]" },
|
||||
peg$c67 = function() { return parseCursor; },
|
||||
peg$c68 = "@kuery-cursor@",
|
||||
peg$c69 = { type: "literal", value: "@kuery-cursor@", description: "\"@kuery-cursor@\"" },
|
||||
peg$c70 = function() { return cursorSymbol; },
|
||||
|
||||
peg$currPos = 0,
|
||||
peg$savedPos = 0,
|
||||
|
@ -1475,9 +1477,24 @@ module.exports = (function() {
|
|||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$c47();
|
||||
s1 = peg$c44();
|
||||
}
|
||||
s0 = s1;
|
||||
if (s0 === peg$FAILED) {
|
||||
s0 = peg$currPos;
|
||||
if (input.substr(peg$currPos, 2) === peg$c47) {
|
||||
s1 = peg$c47;
|
||||
peg$currPos += 2;
|
||||
} else {
|
||||
s1 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c48); }
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$c49();
|
||||
}
|
||||
s0 = s1;
|
||||
}
|
||||
}
|
||||
|
||||
return s0;
|
||||
|
@ -1551,7 +1568,7 @@ module.exports = (function() {
|
|||
}
|
||||
if (s2 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$c48(s2);
|
||||
s1 = peg$c50(s2);
|
||||
s0 = s1;
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
|
@ -1582,12 +1599,12 @@ module.exports = (function() {
|
|||
function peg$parseSpecialCharacter() {
|
||||
var s0;
|
||||
|
||||
if (peg$c49.test(input.charAt(peg$currPos))) {
|
||||
if (peg$c51.test(input.charAt(peg$currPos))) {
|
||||
s0 = input.charAt(peg$currPos);
|
||||
peg$currPos++;
|
||||
} else {
|
||||
s0 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c50); }
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c52); }
|
||||
}
|
||||
|
||||
return s0;
|
||||
|
@ -1597,58 +1614,58 @@ module.exports = (function() {
|
|||
var s0, s1;
|
||||
|
||||
s0 = peg$currPos;
|
||||
if (input.substr(peg$currPos, 2) === peg$c51) {
|
||||
s1 = peg$c51;
|
||||
if (input.substr(peg$currPos, 2) === peg$c53) {
|
||||
s1 = peg$c53;
|
||||
peg$currPos += 2;
|
||||
} else {
|
||||
s1 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c52); }
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c54); }
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$c53();
|
||||
s1 = peg$c55();
|
||||
}
|
||||
s0 = s1;
|
||||
if (s0 === peg$FAILED) {
|
||||
s0 = peg$currPos;
|
||||
if (input.substr(peg$currPos, 2) === peg$c54) {
|
||||
s1 = peg$c54;
|
||||
if (input.substr(peg$currPos, 2) === peg$c56) {
|
||||
s1 = peg$c56;
|
||||
peg$currPos += 2;
|
||||
} else {
|
||||
s1 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c55); }
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c57); }
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$c56();
|
||||
s1 = peg$c58();
|
||||
}
|
||||
s0 = s1;
|
||||
if (s0 === peg$FAILED) {
|
||||
s0 = peg$currPos;
|
||||
if (input.charCodeAt(peg$currPos) === 60) {
|
||||
s1 = peg$c57;
|
||||
s1 = peg$c59;
|
||||
peg$currPos++;
|
||||
} else {
|
||||
s1 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c58); }
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c60); }
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$c59();
|
||||
s1 = peg$c61();
|
||||
}
|
||||
s0 = s1;
|
||||
if (s0 === peg$FAILED) {
|
||||
s0 = peg$currPos;
|
||||
if (input.charCodeAt(peg$currPos) === 62) {
|
||||
s1 = peg$c60;
|
||||
s1 = peg$c62;
|
||||
peg$currPos++;
|
||||
} else {
|
||||
s1 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c61); }
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c63); }
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$c62();
|
||||
s1 = peg$c64();
|
||||
}
|
||||
s0 = s1;
|
||||
}
|
||||
|
@ -1661,12 +1678,12 @@ module.exports = (function() {
|
|||
function peg$parseSpace() {
|
||||
var s0;
|
||||
|
||||
if (peg$c63.test(input.charAt(peg$currPos))) {
|
||||
if (peg$c65.test(input.charAt(peg$currPos))) {
|
||||
s0 = input.charAt(peg$currPos);
|
||||
peg$currPos++;
|
||||
} else {
|
||||
s0 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c64); }
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c66); }
|
||||
}
|
||||
|
||||
return s0;
|
||||
|
@ -1677,23 +1694,23 @@ module.exports = (function() {
|
|||
|
||||
s0 = peg$currPos;
|
||||
peg$savedPos = peg$currPos;
|
||||
s1 = peg$c65();
|
||||
s1 = peg$c67();
|
||||
if (s1) {
|
||||
s1 = void 0;
|
||||
} else {
|
||||
s1 = peg$FAILED;
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
if (input.substr(peg$currPos, 14) === peg$c66) {
|
||||
s2 = peg$c66;
|
||||
if (input.substr(peg$currPos, 14) === peg$c68) {
|
||||
s2 = peg$c68;
|
||||
peg$currPos += 14;
|
||||
} else {
|
||||
s2 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c67); }
|
||||
if (peg$silentFails === 0) { peg$fail(peg$c69); }
|
||||
}
|
||||
if (s2 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$c68();
|
||||
s1 = peg$c70();
|
||||
s0 = s1;
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
|
|
|
@ -251,8 +251,9 @@ OptionalSpace
|
|||
/ Space*
|
||||
|
||||
EscapedWhitespace
|
||||
= '\\n' { return '\n'; }
|
||||
/ '\\t' { return '\t'; }
|
||||
= '\\t' { return '\t'; }
|
||||
/ '\\r' { return '\t'; }
|
||||
/ '\\n' { return '\n'; }
|
||||
|
||||
EscapedSpecialCharacter
|
||||
= '\\' char:SpecialCharacter { return char; }
|
||||
|
|
|
@ -48,7 +48,7 @@ export function test(node, string) {
|
|||
.split(wildcardSymbol)
|
||||
.map(escapeRegExp)
|
||||
.join('.*');
|
||||
const regexp = new RegExp(`^${regex}$`, 'm');
|
||||
const regexp = new RegExp(`^${regex}$`, 's');
|
||||
return regexp.test(string);
|
||||
}
|
||||
|
||||
|
|
|
@ -49,4 +49,10 @@ describe('Kuery escape', function () {
|
|||
const expected = 'Hello, world, \\and \\<nice\\> to meet you!';
|
||||
expect(escapeKuery(value)).to.be(expected);
|
||||
});
|
||||
|
||||
it('should escape newlines and tabs', () => {
|
||||
const value = 'This\nhas\tnewlines\r\nwith\ttabs';
|
||||
const expected = 'This\\nhas\\tnewlines\\r\\nwith\\ttabs';
|
||||
expect(escapeKuery(value)).to.be(expected);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -4,11 +4,13 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { flow } from 'lodash';
|
||||
|
||||
export function escapeQuotes(string) {
|
||||
return string.replace(/"/g, '\\"');
|
||||
}
|
||||
|
||||
export const escapeKuery = (string) => escapeNot(escapeAndOr(escapeSpecialCharacters(string)));
|
||||
export const escapeKuery = flow(escapeSpecialCharacters, escapeAndOr, escapeNot, escapeWhitespace);
|
||||
|
||||
// See the SpecialCharacter rule in kuery.peg
|
||||
function escapeSpecialCharacters(string) {
|
||||
|
@ -23,3 +25,11 @@ function escapeAndOr(string) {
|
|||
function escapeNot(string) {
|
||||
return string.replace(/not(\s+)/ig, '\\$&');
|
||||
}
|
||||
|
||||
// See the Space rule in kuery.peg
|
||||
function escapeWhitespace(string) {
|
||||
return string
|
||||
.replace(/\t/g, '\\t')
|
||||
.replace(/\r/g, '\\r')
|
||||
.replace(/\n/g, '\\n');
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue