@samitouri / QOS-React-2 / commits / 024bac4263

Support single-quote strings in style editor

Brian Vaughn committed Aug 16, 2019 at 13:35 UTC 024bac4263f401f63a92070164ae6a257c7ff890
1 file changed +15 -2
src/devtools/views/Components/NativeStyleEditor/StyleEditor.js
+15 -2
@@ -178,7 +178,7 @@ function Row({
178 const validateAndSetLocalValue = value => {
179 let isValid = false;
180 try {
181 - JSON.parse(value);
181 + JSON.parse(sanitizeForParse(value));
182 isValid = true;
183 } catch (error) {}
184
@@ -198,7 +198,7 @@ function Row({
198
199 const submitValueChange = () => {
200 if (isValueValid) {
201 - const parsedLocalValue = JSON.parse(localValue);
201 + const parsedLocalValue = JSON.parse(sanitizeForParse(localValue));
202 if (value !== parsedLocalValue) {
203 changeValue(attribute, parsedLocalValue);
204 }
@@ -282,3 +282,16 @@ function Field({
282 />
283 );
284 }
285 +
286 +// We use JSON.parse to parse string values
287 +// e.g. 'foo' is not valid JSON but it is a valid string
288 +// so this method replaces e.g. 'foo' with "foo"
289 +function sanitizeForParse(value: any) {
290 + if (typeof value === 'string') {
291 + if (value.charAt(0) === "'" && value.charAt(value.length - 1) === "'") {
292 + return '"' + value.substr(1, value.length - 2) + '"';
293 + }
294 + }
295 +
296 + return value;
297 +}