Skip to content

Commit

Permalink
Create an option to opt-out of string escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
joaompneves committed Dec 24, 2024
1 parent 459d60b commit 6436d72
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import ReactJsonView from '@microlink/react-json-view'
| `quotesOnKeys` | `boolean` | `true` | Set to `false` to remove quotes from keys (e.g., `"name":` vs. `name:`). |
| `validationMessage` | `string` | "Validation Error" | Custom message for validation failures to `onEdit`, `onAdd`, or `onDelete` callbacks. |
| `displayArrayKey` | `boolean` | `true` | When set to `true`, the index of the elements prefix values. |
| `escapeStrings` | `boolean` | `true` | When set to `true`, strings sequences such as \n, \t, \r, \f will be escaped. |

#### Callbacks

Expand Down
10 changes: 6 additions & 4 deletions src/js/components/DataTypes/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ export default class extends React.PureComponent {
const type_name = 'string'
const { collapsed } = this.state
const { props } = this
const { collapseStringsAfterLength, theme } = props
const { collapseStringsAfterLength, theme, escapeStrings } = props
let { value } = props
const collapsible = toType(collapseStringsAfterLength) === 'integer'
const style = { style: { cursor: 'default' } }

value = escapeString(value)

if (escapeStrings) {
value = escapeString(value)
}

if (collapsible && value.length > collapseStringsAfterLength) {
style.style.cursor = 'pointer'
if (this.state.collapsed) {
if (collapsed) {
value = (
<span>
{value.substring(0, collapseStringsAfterLength)}
Expand Down
1 change: 1 addition & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ReactJsonView extends React.PureComponent {
groupArraysAfterLength: 100,
indentWidth: 4,
enableClipboard: true,
escapeStrings: true,
displayObjectSize: true,
displayDataTypes: true,
onEdit: false,
Expand Down
14 changes: 14 additions & 0 deletions test/tests/js/components/DataTypes/String-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,25 @@ describe('<JsonString />', function () {
const props = {
value: '\\\n\t\r\f\\n',
displayDataTypes: false,
escapeStrings: true,
theme: 'rjv-default'
}
const component = mount(<JsonString {...props} />).render()
expect(component.find('.string-value').text()).to.equal(
'"\\\\\\n\\t\\r\\f\\\\n"'
)
})

it('string with special escape sequences is not escaped', function () {
const props = {
value: '\\\n\t\r\f\\n',
displayDataTypes: false,
escapeStrings: false,
theme: 'rjv-default'
}
const component = mount(<JsonString {...props} />).render()
expect(component.find('.string-value').text()).to.equal(
'"\\\n\t\n\f\\n"'
)
})
})

0 comments on commit 6436d72

Please sign in to comment.