Fixed #129: code is sanitized before throwing an error
This commit is contained in:
parent
cc1b12effc
commit
330e5bb6d1
|
@ -3,6 +3,13 @@
|
||||||
https://github.com/josdejong/jsoneditor
|
https://github.com/josdejong/jsoneditor
|
||||||
|
|
||||||
|
|
||||||
|
## not yet released, version 3.1.3
|
||||||
|
|
||||||
|
- Before an error is thrown because of invalid text, the editor first tries to
|
||||||
|
sanitize the text (replace JavaScript notation with JSON notation), and only
|
||||||
|
after that throws the error.
|
||||||
|
|
||||||
|
|
||||||
## 2014-09-03, version 3.1.2
|
## 2014-09-03, version 3.1.2
|
||||||
|
|
||||||
- Some fixes/improvements in `parseJS` (to parse a JSON object from a JavaScript
|
- Some fixes/improvements in `parseJS` (to parse a JSON object from a JavaScript
|
||||||
|
|
|
@ -23,8 +23,8 @@
|
||||||
* Copyright (c) 2011-2014 Jos de Jong, http://jsoneditoronline.org
|
* Copyright (c) 2011-2014 Jos de Jong, http://jsoneditoronline.org
|
||||||
*
|
*
|
||||||
* @author Jos de Jong, <wjosdejong@gmail.com>
|
* @author Jos de Jong, <wjosdejong@gmail.com>
|
||||||
* @version 3.1.2
|
* @version 3.1.3-SNAPSHOT
|
||||||
* @date 2014-09-03
|
* @date 2014-09-13
|
||||||
*/
|
*/
|
||||||
(function webpackUniversalModuleDefinition(root, factory) {
|
(function webpackUniversalModuleDefinition(root, factory) {
|
||||||
if(typeof exports === 'object' && typeof module === 'object')
|
if(typeof exports === 'object' && typeof module === 'object')
|
||||||
|
@ -1264,16 +1264,18 @@ return /******/ (function(modules) { // webpackBootstrap
|
||||||
* Compact the code in the formatter
|
* Compact the code in the formatter
|
||||||
*/
|
*/
|
||||||
textmode.compact = function () {
|
textmode.compact = function () {
|
||||||
var json = util.parse(this.getText());
|
var json = this.get();
|
||||||
this.setText(JSON.stringify(json));
|
var text = JSON.stringify(json);
|
||||||
|
this.setText(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format the code in the formatter
|
* Format the code in the formatter
|
||||||
*/
|
*/
|
||||||
textmode.format = function () {
|
textmode.format = function () {
|
||||||
var json = util.parse(this.getText());
|
var json = this.get();
|
||||||
this.setText(JSON.stringify(json, null, this.indentation));
|
var text = JSON.stringify(json, null, this.indentation);
|
||||||
|
this.setText(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1311,7 +1313,22 @@ return /******/ (function(modules) { // webpackBootstrap
|
||||||
* @return {Object} json
|
* @return {Object} json
|
||||||
*/
|
*/
|
||||||
textmode.get = function() {
|
textmode.get = function() {
|
||||||
return util.parse(this.getText());
|
var text = this.getText();
|
||||||
|
var json;
|
||||||
|
|
||||||
|
try {
|
||||||
|
json = util.parse(text); // this can throw an error
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
// try to sanitize json, replace JavaScript notation with JSON notation
|
||||||
|
text = util.sanitize(text);
|
||||||
|
this.setText(text);
|
||||||
|
|
||||||
|
// try to parse again
|
||||||
|
json = util.parse(text); // this can throw an error
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1379,30 +1396,23 @@ return /******/ (function(modules) { // webpackBootstrap
|
||||||
return JSON.parse(jsonString);
|
return JSON.parse(jsonString);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
// try to load as JavaScript instead of JSON (like "{a: 2}" instead of "{"a": 2}"
|
// try to throw a more detailed error message using validate
|
||||||
try {
|
util.validate(jsonString);
|
||||||
return util.parseJS(jsonString);
|
|
||||||
}
|
|
||||||
catch(err2) {
|
|
||||||
// ok no luck loading as JavaScript
|
|
||||||
|
|
||||||
// try to throw a more detailed error message using validate
|
// rethrow the original error
|
||||||
util.validate(jsonString);
|
throw err;
|
||||||
|
|
||||||
// rethrow the original error
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a string containing an object in JavaScript notation into a JSON.
|
* Sanitize a JSON-like string containing. For example changes JavaScript
|
||||||
* Throws an error when not successful. This function can for example parse
|
* notation into JSON notation.
|
||||||
* a string like "{a: 2, 'b': {c: 'd'}".
|
* This function for example changes a string like "{a: 2, 'b': {c: 'd'}"
|
||||||
|
* into '{"a": 2, "b": {"c": "d"}'
|
||||||
* @param {string} jsString
|
* @param {string} jsString
|
||||||
* @returns {JSON} json
|
* @returns {string} json
|
||||||
*/
|
*/
|
||||||
util.parseJS = function (jsString) {
|
util.sanitize = function (jsString) {
|
||||||
// escape all single and double quotes inside strings
|
// escape all single and double quotes inside strings
|
||||||
var chars = [];
|
var chars = [];
|
||||||
var inString = false;
|
var inString = false;
|
||||||
|
@ -1443,7 +1453,7 @@ return /******/ (function(modules) { // webpackBootstrap
|
||||||
return $1 + '"' + $2 + '"' + $3;
|
return $1 + '"' + $2 + '"' + $3;
|
||||||
});
|
});
|
||||||
|
|
||||||
return JSON.parse(jsonString);
|
return jsonString;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -195,16 +195,18 @@ define(['./modeswitcher', './util'], function (modeswitcher, util) {
|
||||||
* Compact the code in the formatter
|
* Compact the code in the formatter
|
||||||
*/
|
*/
|
||||||
textmode.compact = function () {
|
textmode.compact = function () {
|
||||||
var json = util.parse(this.getText());
|
var json = this.get();
|
||||||
this.setText(JSON.stringify(json));
|
var text = JSON.stringify(json);
|
||||||
|
this.setText(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format the code in the formatter
|
* Format the code in the formatter
|
||||||
*/
|
*/
|
||||||
textmode.format = function () {
|
textmode.format = function () {
|
||||||
var json = util.parse(this.getText());
|
var json = this.get();
|
||||||
this.setText(JSON.stringify(json, null, this.indentation));
|
var text = JSON.stringify(json, null, this.indentation);
|
||||||
|
this.setText(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -242,7 +244,22 @@ define(['./modeswitcher', './util'], function (modeswitcher, util) {
|
||||||
* @return {Object} json
|
* @return {Object} json
|
||||||
*/
|
*/
|
||||||
textmode.get = function() {
|
textmode.get = function() {
|
||||||
return util.parse(this.getText());
|
var text = this.getText();
|
||||||
|
var json;
|
||||||
|
|
||||||
|
try {
|
||||||
|
json = util.parse(text); // this can throw an error
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
// try to sanitize json, replace JavaScript notation with JSON notation
|
||||||
|
text = util.sanitize(text);
|
||||||
|
this.setText(text);
|
||||||
|
|
||||||
|
// try to parse again
|
||||||
|
json = util.parse(text); // this can throw an error
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -14,30 +14,23 @@ define(function () {
|
||||||
return JSON.parse(jsonString);
|
return JSON.parse(jsonString);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
// try to load as JavaScript instead of JSON (like "{a: 2}" instead of "{"a": 2}"
|
// try to throw a more detailed error message using validate
|
||||||
try {
|
util.validate(jsonString);
|
||||||
return util.parseJS(jsonString);
|
|
||||||
}
|
|
||||||
catch(err2) {
|
|
||||||
// ok no luck loading as JavaScript
|
|
||||||
|
|
||||||
// try to throw a more detailed error message using validate
|
// rethrow the original error
|
||||||
util.validate(jsonString);
|
throw err;
|
||||||
|
|
||||||
// rethrow the original error
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a string containing an object in JavaScript notation into a JSON.
|
* Sanitize a JSON-like string containing. For example changes JavaScript
|
||||||
* Throws an error when not successful. This function can for example parse
|
* notation into JSON notation.
|
||||||
* a string like "{a: 2, 'b': {c: 'd'}".
|
* This function for example changes a string like "{a: 2, 'b': {c: 'd'}"
|
||||||
|
* into '{"a": 2, "b": {"c": "d"}'
|
||||||
* @param {string} jsString
|
* @param {string} jsString
|
||||||
* @returns {JSON} json
|
* @returns {string} json
|
||||||
*/
|
*/
|
||||||
util.parseJS = function (jsString) {
|
util.sanitize = function (jsString) {
|
||||||
// escape all single and double quotes inside strings
|
// escape all single and double quotes inside strings
|
||||||
var chars = [];
|
var chars = [];
|
||||||
var inString = false;
|
var inString = false;
|
||||||
|
@ -78,7 +71,7 @@ define(function () {
|
||||||
return $1 + '"' + $2 + '"' + $3;
|
return $1 + '"' + $2 + '"' + $3;
|
||||||
});
|
});
|
||||||
|
|
||||||
return JSON.parse(jsonString);
|
return jsonString;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue