Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-8395: html in error message not evaluating #5632

Merged
merged 5 commits into from
Jun 11, 2024
Merged
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: 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 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';

Check warning on line 6 in src/components/select/Select.js

View workflow job for this annotation

GitHub Actions / setup

'unescapeHTML' is defined but never used

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

Expand Down Expand Up @@ -1820,7 +1820,7 @@
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
Loading