Skip to content
Draft
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
36 changes: 36 additions & 0 deletions src/common/utils/encodingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@ export function utf8ToBase64(str) {
return btoa(binaryString);
}

export function unescapeHtml(str) {
if (typeof str !== 'string') return str;
return str
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&');
}

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 @@ -28,6 +28,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 @@ -86,6 +87,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 @@ -597,7 +601,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
@@ -1,7 +1,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