Skip to content
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
21 changes: 0 additions & 21 deletions src/bundle/Resources/public/js/scripts/helpers/dom.helper.js

This file was deleted.

33 changes: 33 additions & 0 deletions src/bundle/Resources/public/js/scripts/helpers/dom.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { escapeHTML } from './text.helper';

const safelySetInnerHTML = (node: HTMLElement, text: string): void => {
// eslint-disable-next-line no-param-reassign
node.innerHTML = escapeHTML(text);
};

const dangerouslySetInnerHTML = (node: HTMLElement, text: string): void => {
// eslint-disable-next-line no-param-reassign
node.innerHTML = text;
};

const dangerouslyInsertAdjacentHTML = (node: HTMLElement, position: InsertPosition, text: string): void => {
const escapedText = text;

node.insertAdjacentHTML(position, escapedText);
};

const dangerouslyAppend = (node: HTMLElement, nodeOrText: Node | string): void => {
node.append(nodeOrText);
};

const notNullQuerySelector = function <T extends HTMLElement>(node: HTMLElement | Document, selector: string): T {
const selectedNode = node.querySelector<T>(selector);

if (!selectedNode) {
throw new Error(`Element for selector '${selector}' not found!`);
}

return selectedNode;
};

export { safelySetInnerHTML, dangerouslySetInnerHTML, dangerouslyInsertAdjacentHTML, dangerouslyAppend, notNullQuerySelector };
24 changes: 0 additions & 24 deletions src/bundle/Resources/public/js/scripts/helpers/text.helper.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/bundle/Resources/public/js/scripts/helpers/text.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { document: doc } = window;

const escapeHTML = (str: string): string => {
const stringTempNode = doc.createElement('div');

stringTempNode.appendChild(doc.createTextNode(str));

return stringTempNode.innerHTML;
};

const escapeHTMLAttribute = (str: unknown): string => {
if (str === null || str === undefined) {
return '';
}

// eslint-disable-next-line @typescript-eslint/no-base-to-string
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;');
};

export { escapeHTML, escapeHTMLAttribute };
Loading