diff --git a/src/devtools/views/Components/NativeStyleEditor/StyleEditor.js b/src/devtools/views/Components/NativeStyleEditor/StyleEditor.js index 94d6923c37..9a23be92fb 100644 --- a/src/devtools/views/Components/NativeStyleEditor/StyleEditor.js +++ b/src/devtools/views/Components/NativeStyleEditor/StyleEditor.js @@ -178,7 +178,7 @@ function Row({ const validateAndSetLocalValue = value => { let isValid = false; try { - JSON.parse(value); + JSON.parse(sanitizeForParse(value)); isValid = true; } catch (error) {} @@ -198,7 +198,7 @@ function Row({ const submitValueChange = () => { if (isValueValid) { - const parsedLocalValue = JSON.parse(localValue); + const parsedLocalValue = JSON.parse(sanitizeForParse(localValue)); if (value !== parsedLocalValue) { changeValue(attribute, parsedLocalValue); } @@ -282,3 +282,16 @@ function Field({ /> ); } + +// We use JSON.parse to parse string values +// e.g. 'foo' is not valid JSON but it is a valid string +// so this method replaces e.g. 'foo' with "foo" +function sanitizeForParse(value: any) { + if (typeof value === 'string') { + if (value.charAt(0) === "'" && value.charAt(value.length - 1) === "'") { + return '"' + value.substr(1, value.length - 2) + '"'; + } + } + + return value; +}