Implemented option `escapeUnicode`

This commit is contained in:
jos 2016-10-28 14:00:44 +02:00
parent 8a3fffcd24
commit 9cbb7574c0
8 changed files with 17 additions and 9 deletions

View File

@ -158,7 +158,7 @@ export default class JSONNode extends Component {
const editable = !isIndex && (!options.isPropertyEditable || options.isPropertyEditable(this.getPath())) const editable = !isIndex && (!options.isPropertyEditable || options.isPropertyEditable(this.getPath()))
if (editable) { if (editable) {
const escapedProp = escapeHTML(prop) const escapedProp = escapeHTML(prop, options.escapeUnicode)
return h('div', { return h('div', {
class: 'jsoneditor-property' + (prop.length === 0 ? ' jsoneditor-empty' : ''), class: 'jsoneditor-property' + (prop.length === 0 ? ' jsoneditor-empty' : ''),
@ -180,7 +180,7 @@ export default class JSONNode extends Component {
} }
renderValue (value, options) { renderValue (value, options) {
const escapedValue = escapeHTML(value) const escapedValue = escapeHTML(value, options.escapeUnicode)
const type = valueType (value) const type = valueType (value)
const itsAnUrl = isUrl(value) const itsAnUrl = isUrl(value)
const isEmpty = escapedValue.length === 0 const isEmpty = escapedValue.length === 0

View File

@ -31,7 +31,7 @@ export default class JSONNodeForm extends JSONNode {
}, prop) }, prop)
} }
else { // object property else { // object property
const escapedProp = escapeHTML(prop) const escapedProp = escapeHTML(prop, options.escapeUnicode)
return h('div', { return h('div', {
class: 'jsoneditor-property' + (prop.length === 0 ? ' jsoneditor-empty' : '') class: 'jsoneditor-property' + (prop.length === 0 ? ' jsoneditor-empty' : '')

View File

@ -14,7 +14,7 @@ export default class JSONNodeView extends JSONNodeForm {
// render a readonly value // render a readonly value
renderValue (value) { renderValue (value) {
const escapedValue = escapeHTML(value) const escapedValue = escapeHTML(value, options.escapeUnicode)
const type = valueType (value) const type = valueType (value)
const isEmpty = escapedValue.length === 0 const isEmpty = escapedValue.length === 0
const itsAnUrl = isUrl(value) const itsAnUrl = isUrl(value)

View File

@ -1,5 +1,6 @@
import { h, Component } from 'preact' import { h, Component } from 'preact'
import { parseJSON } from '../utils/jsonUtils' import { parseJSON } from '../utils/jsonUtils'
import { escapeUnicodeChars } from '../utils/stringUtils'
import { jsonToData, dataToJson, patchData } from '../jsonData' import { jsonToData, dataToJson, patchData } from '../jsonData'
import ModeButton from './menu/ModeButton' import ModeButton from './menu/ModeButton'
@ -190,7 +191,11 @@ export default class TextMode extends Component {
* @param {string} text * @param {string} text
*/ */
setText (text) { setText (text) {
this.setState({ text }) this.setState({
text: this.props.options.escapeUnicode
? escapeUnicodeChars(text)
: text
})
} }
/** /**

View File

@ -1,6 +1,6 @@
import { h, Component } from 'preact' 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 { expand, jsonToData, dataToJson, toDataPath, patchData } from '../jsonData'
import { parseJSON } from '../utils/jsonUtils' import { parseJSON } from '../utils/jsonUtils'
import { import {

View File

@ -60,7 +60,8 @@
}, },
mode: mode, mode: mode,
modes: ['text', 'code', 'tree', 'form', 'view'], modes: ['text', 'code', 'tree', 'form', 'view'],
indentation: 4 indentation: 4,
escapeUnicode: true
} }
const editor = jsoneditor(container, options) const editor = jsoneditor(container, options)
const json = { const json = {
@ -72,6 +73,7 @@
'number': 123, 'number': 123,
'object': {'a': 'b', 'c': 'd', 'e': [{"first": true}, {"second": true}]}, 'object': {'a': 'b', 'c': 'd', 'e': [{"first": true}, {"second": true}]},
'string': 'Hello World', 'string': 'Hello World',
'unicode': 'A unicode character: \u260E',
'url': 'http://jsoneditoronline.org' 'url': 'http://jsoneditoronline.org'
} }
editor.set(json, { editor.set(json, {

View File

@ -40,7 +40,8 @@
* onChangeMode?: function (mode: string, prevMode: string), * onChangeMode?: function (mode: string, prevMode: string),
* onError?: function (err: Error), * onError?: function (err: Error),
* isPropertyEditable?: function (Path) : boolean * isPropertyEditable?: function (Path) : boolean
* isValueEditable?: function (Path) : boolean * isValueEditable?: function (Path) : boolean,
* escapeUnicode:? boolean
* }} Options * }} Options
* *
* @typedef {{ * @typedef {{

View File

@ -31,7 +31,7 @@ export function escapeHTML (text, escapeUnicode = false) {
* @param {string} text * @param {string} text
* @return {string} * @return {string}
*/ */
function escapeUnicodeChars (text) { export function escapeUnicodeChars (text) {
// see https://www.wikiwand.com/en/UTF-16 // see https://www.wikiwand.com/en/UTF-16
// note: we leave surrogate pairs as two individual chars, // note: we leave surrogate pairs as two individual chars,
// as JSON doesn't interpret them as a single unicode char. // as JSON doesn't interpret them as a single unicode char.