From dff31ed755830812ca1db812c51f04965676895e Mon Sep 17 00:00:00 2001 From: Anton Dosov Date: Mon, 14 Mar 2022 15:35:23 +0100 Subject: [PATCH] fix ff color editor crash (#127292) --- .../common/converters/color.test.ts | 19 +++++++++++++++++++ .../field_formats/common/converters/color.tsx | 6 +++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/plugins/field_formats/common/converters/color.test.ts b/src/plugins/field_formats/common/converters/color.test.ts index 994c6d802ae3b..617945b3d1cdc 100644 --- a/src/plugins/field_formats/common/converters/color.test.ts +++ b/src/plugins/field_formats/common/converters/color.test.ts @@ -112,5 +112,24 @@ describe('Color Format', () => { expect(converter('<', HTML_CONTEXT_TYPE)).toBe('<'); }); + + test('returns original value (escaped) on regex with syntax error', () => { + const colorer = new ColorFormat( + { + fieldType: 'string', + colors: [ + { + regex: 'nogroup(', + text: 'blue', + background: 'yellow', + }, + ], + }, + jest.fn() + ); + const converter = colorer.getConverterFor(HTML_CONTEXT_TYPE) as Function; + + expect(converter('<', HTML_CONTEXT_TYPE)).toBe('<'); + }); }); }); diff --git a/src/plugins/field_formats/common/converters/color.tsx b/src/plugins/field_formats/common/converters/color.tsx index 3e5ff97830479..197468fc1592a 100644 --- a/src/plugins/field_formats/common/converters/color.tsx +++ b/src/plugins/field_formats/common/converters/color.tsx @@ -35,7 +35,11 @@ export class ColorFormat extends FieldFormat { switch (this.param('fieldType')) { case 'string': return findLast(this.param('colors'), (colorParam: typeof DEFAULT_CONVERTER_COLOR) => { - return new RegExp(colorParam.regex).test(val as string); + try { + return new RegExp(colorParam.regex).test(val as string); + } catch (e) { + return false; + } }); case 'number':