From 9cbb7574c06200eb19ccc1f9651e673974f9506a Mon Sep 17 00:00:00 2001 From: jos Date: Fri, 28 Oct 2016 14:00:44 +0200 Subject: [PATCH] Implemented option `escapeUnicode` --- src/components/JSONNode.js | 4 ++-- src/components/JSONNodeForm.js | 2 +- src/components/JSONNodeView.js | 2 +- src/components/TextMode.js | 7 ++++++- src/components/TreeMode.js | 2 +- src/develop.html | 4 +++- src/typedef.js | 3 ++- src/utils/stringUtils.js | 2 +- 8 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/components/JSONNode.js b/src/components/JSONNode.js index ffae751..ceda5e5 100644 --- a/src/components/JSONNode.js +++ b/src/components/JSONNode.js @@ -158,7 +158,7 @@ export default class JSONNode extends Component { const editable = !isIndex && (!options.isPropertyEditable || options.isPropertyEditable(this.getPath())) if (editable) { - const escapedProp = escapeHTML(prop) + const escapedProp = escapeHTML(prop, options.escapeUnicode) return h('div', { class: 'jsoneditor-property' + (prop.length === 0 ? ' jsoneditor-empty' : ''), @@ -180,7 +180,7 @@ export default class JSONNode extends Component { } renderValue (value, options) { - const escapedValue = escapeHTML(value) + const escapedValue = escapeHTML(value, options.escapeUnicode) const type = valueType (value) const itsAnUrl = isUrl(value) const isEmpty = escapedValue.length === 0 diff --git a/src/components/JSONNodeForm.js b/src/components/JSONNodeForm.js index 0d826a7..eacccb9 100644 --- a/src/components/JSONNodeForm.js +++ b/src/components/JSONNodeForm.js @@ -31,7 +31,7 @@ export default class JSONNodeForm extends JSONNode { }, prop) } else { // object property - const escapedProp = escapeHTML(prop) + const escapedProp = escapeHTML(prop, options.escapeUnicode) return h('div', { class: 'jsoneditor-property' + (prop.length === 0 ? ' jsoneditor-empty' : '') diff --git a/src/components/JSONNodeView.js b/src/components/JSONNodeView.js index e4253fe..1dfe936 100644 --- a/src/components/JSONNodeView.js +++ b/src/components/JSONNodeView.js @@ -14,7 +14,7 @@ export default class JSONNodeView extends JSONNodeForm { // render a readonly value renderValue (value) { - const escapedValue = escapeHTML(value) + const escapedValue = escapeHTML(value, options.escapeUnicode) const type = valueType (value) const isEmpty = escapedValue.length === 0 const itsAnUrl = isUrl(value) diff --git a/src/components/TextMode.js b/src/components/TextMode.js index a3a4f38..40a6608 100644 --- a/src/components/TextMode.js +++ b/src/components/TextMode.js @@ -1,5 +1,6 @@ import { h, Component } from 'preact' import { parseJSON } from '../utils/jsonUtils' +import { escapeUnicodeChars } from '../utils/stringUtils' import { jsonToData, dataToJson, patchData } from '../jsonData' import ModeButton from './menu/ModeButton' @@ -190,7 +191,11 @@ export default class TextMode extends Component { * @param {string} text */ setText (text) { - this.setState({ text }) + this.setState({ + text: this.props.options.escapeUnicode + ? escapeUnicodeChars(text) + : text + }) } /** diff --git a/src/components/TreeMode.js b/src/components/TreeMode.js index 3e0aeff..e788dd4 100644 --- a/src/components/TreeMode.js +++ b/src/components/TreeMode.js @@ -1,6 +1,6 @@ import { h, Component } from 'preact' -import { setIn, updateIn } from '../utils/immutabilityHelpers' +import { updateIn } from '../utils/immutabilityHelpers' import { expand, jsonToData, dataToJson, toDataPath, patchData } from '../jsonData' import { parseJSON } from '../utils/jsonUtils' import { diff --git a/src/develop.html b/src/develop.html index a1b5db9..cab087b 100644 --- a/src/develop.html +++ b/src/develop.html @@ -60,7 +60,8 @@ }, mode: mode, modes: ['text', 'code', 'tree', 'form', 'view'], - indentation: 4 + indentation: 4, + escapeUnicode: true } const editor = jsoneditor(container, options) const json = { @@ -72,6 +73,7 @@ 'number': 123, 'object': {'a': 'b', 'c': 'd', 'e': [{"first": true}, {"second": true}]}, 'string': 'Hello World', + 'unicode': 'A unicode character: \u260E', 'url': 'http://jsoneditoronline.org' } editor.set(json, { diff --git a/src/typedef.js b/src/typedef.js index debd535..0ed96f2 100644 --- a/src/typedef.js +++ b/src/typedef.js @@ -40,7 +40,8 @@ * onChangeMode?: function (mode: string, prevMode: string), * onError?: function (err: Error), * isPropertyEditable?: function (Path) : boolean - * isValueEditable?: function (Path) : boolean + * isValueEditable?: function (Path) : boolean, + * escapeUnicode:? boolean * }} Options * * @typedef {{ diff --git a/src/utils/stringUtils.js b/src/utils/stringUtils.js index 80f9146..93dfb1c 100644 --- a/src/utils/stringUtils.js +++ b/src/utils/stringUtils.js @@ -31,7 +31,7 @@ export function escapeHTML (text, escapeUnicode = false) { * @param {string} text * @return {string} */ -function escapeUnicodeChars (text) { +export function escapeUnicodeChars (text) { // see https://www.wikiwand.com/en/UTF-16 // note: we leave surrogate pairs as two individual chars, // as JSON doesn't interpret them as a single unicode char.