From 4ae691e88aebf47cba1058adf30e65ab051d5f67 Mon Sep 17 00:00:00 2001 From: cahrens Date: Wed, 13 Feb 2013 17:41:52 -0500 Subject: [PATCH] Convert JSON that doesn't parse to String (unless object, array, or single quote). --- cms/static/js/views/settings/advanced_view.js | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/cms/static/js/views/settings/advanced_view.js b/cms/static/js/views/settings/advanced_view.js index a3b592914c..4faf5cc616 100644 --- a/cms/static/js/views/settings/advanced_view.js +++ b/cms/static/js/views/settings/advanced_view.js @@ -62,14 +62,36 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ }, onBlur: function (mirror) { var key = $(mirror.getWrapperElement()).closest('.row').children('.key').attr('id'); - var quotedValue = mirror.getValue(); + var stringValue = $.trim(mirror.getValue()); + // update CodeMirror to show the trimmed value. + mirror.setValue(stringValue); + var JSONValue = undefined; try { - var JSONValue = JSON.parse(quotedValue); - self.model.set(key, JSONValue, {validate: true}); + JSONValue = JSON.parse(stringValue); + } catch (e) { + // If it didn't parse, try converting non-arrays/non-objects to a String. + // But don't convert single-quote strings, which are most likely errors. + var firstNonWhite = stringValue.substring(0, 1); + if (firstNonWhite !== "{" && firstNonWhite !== "[" && firstNonWhite !== "'") { + try { + stringValue = '"'+stringValue +'"'; + JSONValue = JSON.parse(stringValue); + mirror.setValue(stringValue); + } catch(quotedE) { + // TODO: validation error + console.log("Error with JSON, even after converting to String.") + console.log(quotedE); + JSONValue = undefined; + } + } + else { + // TODO: validation error + console.log("Error with JSON, but will not convert to String.") + console.log(e); + } } - catch (e) { - // TODO: error checking - console.log(e); + if (JSONValue !== undefined) { + self.model.set(key, JSONValue, {validate: true}); } } });