Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
"react-router-dom": "^4.3.1",
"reactjs-popup": "^1.5.0",
"redux": "^4.0.5",
"whatwg-fetch": "^1.0.0"
"whatwg-fetch": "^1.0.0",
"html-entities": "^2.6.0"
},
"resolutions": {
"**/**/lodash": "^4.17.12",
Expand Down
35 changes: 35 additions & 0 deletions src/common/utils/encodingUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { decode } from 'html-entities';

/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
Expand All @@ -18,6 +20,39 @@ export function utf8ToBase64(str) {
return btoa(binaryString);
}

export function unescapeHtml(str) {
if (typeof str !== 'string') return str;
return decode(str);
}

export function deepUnescapeStrings(obj) {
if (typeof obj === 'string') {
return unescapeHtml(obj);
}
if (Array.isArray(obj)) {
return obj.map(deepUnescapeStrings);
}
if (typeof obj === 'object' && obj !== null) {
const result = {};
for (const key of Object.keys(obj)) {
result[key] = deepUnescapeStrings(obj[key]);
}
return result;
}
return obj;
}

export function unescapeFormResourceValue(valueString) {
if (!valueString) return valueString;
try {
const parsed = JSON.parse(valueString);
const unescaped = deepUnescapeStrings(parsed);
return JSON.stringify(unescaped);
} catch (e) {
return valueString;
}
}

export function base64ToUtf8(b64) {
if (b64 === undefined || b64 === null || b64 === '') {
return '';
Expand Down
6 changes: 5 additions & 1 deletion src/form-builder/components/FormDetailContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import isEqual from 'lodash/isEqual';
import { clearTranslations } from '../actions/control';
import { formEventUpdate, saveEventUpdate } from '../actions/control';
import { Exception } from 'form-builder/helpers/Exception';
import { unescapeFormResourceValue } from 'common/utils/encodingUtils';
import {
saveFormNameTranslations,
saveTranslations,
Expand Down Expand Up @@ -97,6 +98,9 @@ export class FormDetailContainer extends Component {
`${formBuilderConstants.formUrl}/${this.props.match.params.formUuid}?${params}`
)
.then((data) => {
if (data.resources.length > 0 && data.resources[0].value) {
data.resources[0].value = unescapeFormResourceValue(data.resources[0].value);
}
const parsedFormValue =
data.resources.length > 0 ? JSON.parse(data.resources[0].value) : {};
const formDefVersion = parsedFormValue.formDefVersion || 1.0;
Expand Down Expand Up @@ -611,7 +615,7 @@ export class FormDetailContainer extends Component {
name: form.name,
id: responseObject.form.id,
dataType: responseObject.dataType,
value: responseObject.value,
value: unescapeFormResourceValue(responseObject.value),
uuid: responseObject.uuid,
};
form.resources = [formResource];
Expand Down
2 changes: 1 addition & 1 deletion src/form-builder/components/FormPreviewModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import 'bahmni-form-controls/dist/helpers.js';
import 'bahmni-form-controls/helpers.js';
import { Exception } from 'form-builder/helpers/Exception';
import { Container } from 'bahmni-form-controls';

Expand Down
Loading