Skip to content

Commit 6436d72

Browse files
committed
Create an option to opt-out of string escaping
1 parent 459d60b commit 6436d72

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import ReactJsonView from '@microlink/react-json-view'
8181
| `quotesOnKeys` | `boolean` | `true` | Set to `false` to remove quotes from keys (e.g., `"name":` vs. `name:`). |
8282
| `validationMessage` | `string` | "Validation Error" | Custom message for validation failures to `onEdit`, `onAdd`, or `onDelete` callbacks. |
8383
| `displayArrayKey` | `boolean` | `true` | When set to `true`, the index of the elements prefix values. |
84+
| `escapeStrings` | `boolean` | `true` | When set to `true`, strings sequences such as \n, \t, \r, \f will be escaped. |
8485

8586
#### Callbacks
8687

Diff for: src/js/components/DataTypes/String.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,18 @@ export default class extends React.PureComponent {
4141
const type_name = 'string'
4242
const { collapsed } = this.state
4343
const { props } = this
44-
const { collapseStringsAfterLength, theme } = props
44+
const { collapseStringsAfterLength, theme, escapeStrings } = props
4545
let { value } = props
4646
const collapsible = toType(collapseStringsAfterLength) === 'integer'
4747
const style = { style: { cursor: 'default' } }
4848

49-
value = escapeString(value)
50-
49+
if (escapeStrings) {
50+
value = escapeString(value)
51+
}
52+
5153
if (collapsible && value.length > collapseStringsAfterLength) {
5254
style.style.cursor = 'pointer'
53-
if (this.state.collapsed) {
55+
if (collapsed) {
5456
value = (
5557
<span>
5658
{value.substring(0, collapseStringsAfterLength)}

Diff for: src/js/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ReactJsonView extends React.PureComponent {
4646
groupArraysAfterLength: 100,
4747
indentWidth: 4,
4848
enableClipboard: true,
49+
escapeStrings: true,
4950
displayObjectSize: true,
5051
displayDataTypes: true,
5152
onEdit: false,

Diff for: test/tests/js/components/DataTypes/String-test.js

+14
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,25 @@ describe('<JsonString />', function () {
5858
const props = {
5959
value: '\\\n\t\r\f\\n',
6060
displayDataTypes: false,
61+
escapeStrings: true,
6162
theme: 'rjv-default'
6263
}
6364
const component = mount(<JsonString {...props} />).render()
6465
expect(component.find('.string-value').text()).to.equal(
6566
'"\\\\\\n\\t\\r\\f\\\\n"'
6667
)
6768
})
69+
70+
it('string with special escape sequences is not escaped', function () {
71+
const props = {
72+
value: '\\\n\t\r\f\\n',
73+
displayDataTypes: false,
74+
escapeStrings: false,
75+
theme: 'rjv-default'
76+
}
77+
const component = mount(<JsonString {...props} />).render()
78+
expect(component.find('.string-value').text()).to.equal(
79+
'"\\\n\t\n\f\\n"'
80+
)
81+
})
6882
})

0 commit comments

Comments
 (0)