2018-08-08 14:53:35 +03:00
|
|
|
import {helper} from '@ember/component/helper';
|
|
|
|
import {isArray} from '@ember/array';
|
|
|
|
|
|
|
|
export function cleanBasicHtml(html = '') {
|
|
|
|
if (isArray(html)) {
|
2018-08-08 15:50:42 +03:00
|
|
|
html = html[0] || '';
|
2018-08-08 14:53:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let cleanHtml = html
|
|
|
|
.replace(/<br>/g, ' ')
|
|
|
|
.replace(/(\s| ){2,}/g, ' ')
|
|
|
|
.trim()
|
|
|
|
.replace(/^ | $/g, '')
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
// remove any elements that have a blank textContent
|
|
|
|
if (cleanHtml) {
|
|
|
|
let doc = new DOMParser().parseFromString(cleanHtml, 'text/html');
|
|
|
|
|
|
|
|
doc.body.querySelectorAll('*').forEach((element) => {
|
|
|
|
if (!element.textContent.trim()) {
|
|
|
|
element.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
cleanHtml = doc.body.innerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleanHtml;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default helper(cleanBasicHtml);
|