Some more fixes related to unicode and escape chars

This commit is contained in:
jos 2015-12-24 17:22:01 +01:00
parent 9cdff41afa
commit 0b234f6375
2 changed files with 15 additions and 6 deletions

View File

@ -984,7 +984,6 @@ Node.prototype._getDomValue = function(silent) {
try {
// retrieve the value
var value;
console.log('getValue', this.valueInnerText, this.valueInnerText.length)
if (this.type == 'string') {
value = this._unescapeHTML(this.valueInnerText);
}
@ -2858,13 +2857,16 @@ Node.prototype._stringCast = function(str) {
* @private
*/
Node.prototype._escapeHTML = function (text) {
return String(text)
var htmlEscaped = String(text)
.replace(/&/g, '&') // must be replaced first!
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/ /g, ' &nbsp;') // replace double space with an nbsp and space
.replace(/^ /, '&nbsp;') // space at start
.replace(/ $/, '&nbsp;'); // space at end
var json = JSON.stringify(htmlEscaped);
return json.substring(1, json.length - 1);
};
/**
@ -2874,7 +2876,10 @@ Node.prototype._escapeHTML = function (text) {
* @private
*/
Node.prototype._unescapeHTML = function (escapedText) {
return this._escapeJSON(escapedText)
var json = '"' + this._escapeJSON(escapedText) + '"';
var htmlEscaped = util.parse(json);
return htmlEscaped
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&nbsp;|\u00A0/g, ' ')

View File

@ -45,15 +45,17 @@
modes: ['code', 'form', 'text', 'tree', 'view'], // allowed modes
error: function (err) {
alert(err.toString());
}
},
indentation: 4
};
json = {
"array": [1, 2, 3],
"boolean": true,
"htmlcode": '&quot;',
"unicode": '\\u20b9',
"invalid": '\u20b9',
"escaped_unicode": '\\u20b9',
"unicode": '\u20b9',
"return": '\n',
"null": null,
"number": 123,
"object": {"a": "b", "c": "d"},
@ -61,6 +63,8 @@
};
editor = new JSONEditor(container, options, json);
console.log(JSON.stringify(json));
</script>
</body>
</html>