{"version":3,"file":"escaping.cjs","sources":["../../src/escaping.ts"],"sourcesContent":["// NOTE: these two functions are similar to the escapeLabelValueIn* functions\n// in language_utils.ts, but they are not exactly the same algorithm, and we found\n\nimport { type QueryVariableModel, type CustomVariableModel } from '@grafana/data';\nimport { config } from '@grafana/runtime';\n\nexport function interpolateQueryExpr(\n  value: string | string[] = [],\n  variable: QueryVariableModel | CustomVariableModel\n) {\n  // if no multi or include all do not regexEscape\n  if (!variable.multi && !variable.includeAll) {\n    return prometheusRegularEscape(value);\n  }\n\n  if (typeof value === 'string') {\n    return prometheusSpecialRegexEscape(value);\n  }\n\n  const escapedValues = value.map((val) => prometheusSpecialRegexEscape(val));\n\n  if (escapedValues.length === 1) {\n    return escapedValues[0];\n  }\n\n  return '(' + escapedValues.join('|') + ')';\n}\n\n// no way to reuse one in the another or vice versa.\nexport function prometheusRegularEscape<T>(value: T) {\n  if (typeof value !== 'string') {\n    return value;\n  }\n\n  if (config.featureToggles.prometheusSpecialCharsInLabelValues) {\n    // if the string looks like a complete label matcher (e.g. 'job=\"grafana\"' or 'job=~\"grafana\"'),\n    // don't escape the encapsulating quotes\n    if (/^\\w+(=|!=|=~|!~)\".*\"$/.test(value)) {\n      return value;\n    }\n\n    return value\n      .replace(/\\\\/g, '\\\\\\\\') // escape backslashes\n      .replace(/\"/g, '\\\\\"'); // escape double quotes\n  }\n\n  // classic behavior\n  return value\n    .replace(/\\\\/g, '\\\\\\\\') // escape backslashes\n    .replace(/'/g, \"\\\\\\\\'\"); // escape single quotes\n}\n\nexport function prometheusSpecialRegexEscape<T>(value: T) {\n  if (typeof value !== 'string') {\n    return value;\n  }\n\n  if (config.featureToggles.prometheusSpecialCharsInLabelValues) {\n    return value\n      .replace(/\\\\/g, '\\\\\\\\\\\\\\\\') // escape backslashes\n      .replace(/\"/g, '\\\\\\\\\\\\\"') // escape double quotes\n      .replace(/[$^*{}\\[\\]\\'+?.()|]/g, '\\\\\\\\$&'); // escape regex metacharacters\n  }\n\n  // classic behavior\n  return value\n    .replace(/\\\\/g, '\\\\\\\\\\\\\\\\') // escape backslashes\n    .replace(/[$^*{}\\[\\]+?.()|]/g, '\\\\\\\\$&'); // escape regex metacharacters\n}\n\n// NOTE: the following 2 exported functions are very similar to the prometheus*Escape\n// functions in datasource.ts, but they are not exactly the same algorithm, and we found\n// no way to reuse one in the another or vice versa.\n\n// Prometheus regular-expressions use the RE2 syntax (https://github.com/google/re2/wiki/Syntax),\n// so every character that matches something in that list has to be escaped.\n// the list of metacharacters is: *+?()|\\.[]{}^$\n// we make a javascript regular expression that matches those characters:\nconst RE2_METACHARACTERS = /[*+?()|\\\\.\\[\\]{}^$]/g;\n\nfunction escapePrometheusRegexp(value: string): string {\n  return value.replace(RE2_METACHARACTERS, '\\\\$&');\n}\n\n// based on the openmetrics-documentation, the 3 symbols we have to handle are:\n// - \\n ... the newline character\n// - \\  ... the backslash character\n// - \"  ... the double-quote character\nexport function escapeLabelValueInExactSelector(labelValue: string): string {\n  return labelValue.replace(/\\\\/g, '\\\\\\\\').replace(/\\n/g, '\\\\n').replace(/\"/g, '\\\\\"');\n}\n\nexport function escapeLabelValueInRegexSelector(labelValue: string): string {\n  return escapeLabelValueInExactSelector(escapePrometheusRegexp(labelValue));\n}\n"],"names":["config"],"mappings":";;;;;;;AAMO,SAAS,oBAAA,CACd,KAAA,GAA2B,EAAC,EAC5B,QAAA,EACA;AAEA,EAAA,IAAI,CAAC,QAAA,CAAS,KAAA,IAAS,CAAC,SAAS,UAAA,EAAY;AAC3C,IAAA,OAAO,wBAAwB,KAAK,CAAA;AAAA,EACtC;AAEA,EAAA,IAAI,OAAO,UAAU,QAAA,EAAU;AAC7B,IAAA,OAAO,6BAA6B,KAAK,CAAA;AAAA,EAC3C;AAEA,EAAA,MAAM,gBAAgB,KAAA,CAAM,GAAA,CAAI,CAAC,GAAA,KAAQ,4BAAA,CAA6B,GAAG,CAAC,CAAA;AAE1E,EAAA,IAAI,aAAA,CAAc,WAAW,CAAA,EAAG;A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