Skip to content

Commit

Permalink
Some more fixes related to unicode and escape chars
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Dec 24, 2015
1 parent 9cdff41 commit 0b234f6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/js/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
};

/**
Expand All @@ -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, ' ')
Expand Down
10 changes: 7 additions & 3 deletions test/test_build.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,26 @@
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"},
"string": "Hello World"
};

editor = new JSONEditor(container, options, json);

console.log(JSON.stringify(json));
</script>
</body>
</html>

0 comments on commit 0b234f6

Please sign in to comment.