From 0bed9dc67bf55a0e12ee66e16fbb15ecb5d1bb10 Mon Sep 17 00:00:00 2001 From: binduak Date: Sun, 15 Mar 2026 22:55:30 +0530 Subject: [PATCH 1/2] Bindu | BAH-4360 | Fix Form Scripts saving issue due to html escape from payload --- src/common/utils/encodingUtils.js | 36 +++++++++++++++++++ .../components/FormDetailContainer.jsx | 6 +++- .../components/FormPreviewModal.jsx | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/common/utils/encodingUtils.js b/src/common/utils/encodingUtils.js index 396b9583..8fb1126a 100644 --- a/src/common/utils/encodingUtils.js +++ b/src/common/utils/encodingUtils.js @@ -18,6 +18,42 @@ export function utf8ToBase64(str) { return btoa(binaryString); } +export function unescapeHtml(str) { + if (typeof str !== 'string') return str; + return str + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/&/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 ''; diff --git a/src/form-builder/components/FormDetailContainer.jsx b/src/form-builder/components/FormDetailContainer.jsx index ba155f2c..3fb6baf2 100644 --- a/src/form-builder/components/FormDetailContainer.jsx +++ b/src/form-builder/components/FormDetailContainer.jsx @@ -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, @@ -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; @@ -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]; diff --git a/src/form-builder/components/FormPreviewModal.jsx b/src/form-builder/components/FormPreviewModal.jsx index 8cc9ea79..33677c0d 100644 --- a/src/form-builder/components/FormPreviewModal.jsx +++ b/src/form-builder/components/FormPreviewModal.jsx @@ -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'; From 64ad1874c7b376c458ed332d813507c470f50f67 Mon Sep 17 00:00:00 2001 From: binduak Date: Thu, 5 Nov 2026 14:03:29 +0530 Subject: [PATCH 2/2] Bindu | BAH-4360 | handle feedback changes - Use html-entities to encode HTML entities instead of a custom method. --- package.json | 3 ++- src/common/utils/encodingUtils.js | 7 +++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 755e4c3e..e1d31388 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/common/utils/encodingUtils.js b/src/common/utils/encodingUtils.js index 8fb1126a..8746e59b 100644 --- a/src/common/utils/encodingUtils.js +++ b/src/common/utils/encodingUtils.js @@ -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 @@ -20,10 +22,7 @@ export function utf8ToBase64(str) { export function unescapeHtml(str) { if (typeof str !== 'string') return str; - return str - .replace(/</g, '<') - .replace(/>/g, '>') - .replace(/&/g, '&'); + return decode(str); } export function deepUnescapeStrings(obj) {