Skip to content

Commit

Permalink
FIO-8395: html in error message not evaluating (#5632)
Browse files Browse the repository at this point in the history
* Added test for unescapeHTML function and reorganized test

* unescapeHTML function no longer escapes html tags

* Removed code causing html tags to be removed from error messages

* getTemplateValue now uses removeHTML

* Added removeHTML function and removeHTML tests
  • Loading branch information
ZenMasterJacob20011 authored Jun 11, 2024
1 parent 8d03ab2 commit cc68bdf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
3 changes: 0 additions & 3 deletions src/components/_classes/component/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2156,9 +2156,6 @@ export default class Component extends Element {

if (this.refs.messageContainer) {
this.setContent(this.refs.messageContainer, messages.map((message) => {
if (message.message && typeof message.message === 'string') {
message.message = message.message.replaceAll('<', '&lt;').replaceAll('>', '&gt;');
}
return this.renderTemplate('message', { ...message });
}
).join(''));
Expand Down
4 changes: 2 additions & 2 deletions src/components/select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Formio } from '../../Formio';
import ListComponent from '../_classes/list/ListComponent';
import Input from '../_classes/input/Input';
import Form from '../../Form';
import { getRandomComponentId, boolValue, isPromise, componentValueTypes, getComponentSavedTypes, unescapeHTML, isSelectResourceWithObjectValue } from '../../utils/utils';
import { getRandomComponentId, boolValue, isPromise, componentValueTypes, getComponentSavedTypes, unescapeHTML, isSelectResourceWithObjectValue, removeHTML } from '../../utils/utils';

import Choices from '../../utils/ChoicesWrapper';

Expand Down Expand Up @@ -1820,7 +1820,7 @@ export default class SelectComponent extends ListComponent {
const getTemplateValue = (v) => {
const itemTemplate = this.itemTemplate(v);
return options.csv && itemTemplate
? unescapeHTML(itemTemplate)
? removeHTML(itemTemplate)
: itemTemplate;
};

Expand Down
16 changes: 13 additions & 3 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,16 @@ export function setActionProperty(component, action, result, row, data, instance
return component;
}

/**
* Removes HTML tags from string e.g. <div>Hello World</div> => Hello World
* @param {string} str
* @returns {string}
*/
export function removeHTML(str) {
const doc = new window.DOMParser().parseFromString(str, 'text/html');
return (doc.body.textContent || '').trim();
}

/**
* Unescape HTML characters like &lt, &gt, &amp and etc.
* @param str
Expand All @@ -457,9 +467,9 @@ export function unescapeHTML(str) {
if (typeof window === 'undefined' || !('DOMParser' in window)) {
return str;
}

const doc = new window.DOMParser().parseFromString(str, 'text/html');
return doc.documentElement.textContent;
const elem = document.createElement('textarea');
elem.innerHTML = str;
return elem.value;
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/utils/utils.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,13 +820,27 @@ describe('Util Tests', () => {
done(error);
}
});
});

describe('unescapeHTML', () => {
it('should not remove html tags from string', () => {
const unescapedString = utils.unescapeHTML('<div><p>This is a paragraph.</p> <p>This is another paragraph.</p></div>');
expect(unescapedString).to.equal('<div><p>This is a paragraph.</p> <p>This is another paragraph.</p></div>');
});

it('Should return string without HTML characters', () => {
it('should return string without HTML characters', () => {
const unescapedString = utils.unescapeHTML('&lt;p&gt;ampersand &amp; &#34;quotes&#34; test&lt;&#47;p&gt;');
expect(unescapedString).to.equal('<p>ampersand & "quotes" test</p>');
});
});

describe('removeHTML', () => {
it('should remove html tags from string', () => {
const removedHTML = utils.removeHTML('<div><p> Hello</p> <p>World</p></div>');
expect(removedHTML).to.equal('Hello World');
});
});

describe('getCurrencyAffixes', () => {
it('USD en', (done) => {
try {
Expand Down

0 comments on commit cc68bdf

Please sign in to comment.