diff --git a/src/bundle/Resources/public/js/scripts/helpers/dom.helper.js b/src/bundle/Resources/public/js/scripts/helpers/dom.helper.js deleted file mode 100644 index bd6c35c43f..0000000000 --- a/src/bundle/Resources/public/js/scripts/helpers/dom.helper.js +++ /dev/null @@ -1,21 +0,0 @@ -import { escapeHTML } from './text.helper'; - -const safelySetInnerHTML = (node, text) => { - node.innerHTML = escapeHTML(text); -}; - -const dangerouslySetInnerHTML = (node, text) => { - node.innerHTML = text; -}; - -const dangerouslyInsertAdjacentHTML = (node, position, text) => { - const escapedText = text; - - node.insertAdjacentHTML(position, escapedText); -}; - -const dangerouslyAppend = (node, nodeOrText) => { - node.append(nodeOrText); -}; - -export { safelySetInnerHTML, dangerouslySetInnerHTML, dangerouslyInsertAdjacentHTML, dangerouslyAppend }; diff --git a/src/bundle/Resources/public/js/scripts/helpers/dom.helper.ts b/src/bundle/Resources/public/js/scripts/helpers/dom.helper.ts new file mode 100644 index 0000000000..4bc81bf444 --- /dev/null +++ b/src/bundle/Resources/public/js/scripts/helpers/dom.helper.ts @@ -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 (node: HTMLElement | Document, selector: string): T { + const selectedNode = node.querySelector(selector); + + if (!selectedNode) { + throw new Error(`Element for selector '${selector}' not found!`); + } + + return selectedNode; +}; + +export { safelySetInnerHTML, dangerouslySetInnerHTML, dangerouslyInsertAdjacentHTML, dangerouslyAppend, notNullQuerySelector }; diff --git a/src/bundle/Resources/public/js/scripts/helpers/text.helper.js b/src/bundle/Resources/public/js/scripts/helpers/text.helper.js deleted file mode 100644 index ac5f88cc3c..0000000000 --- a/src/bundle/Resources/public/js/scripts/helpers/text.helper.js +++ /dev/null @@ -1,24 +0,0 @@ -const { document: doc } = window; - -const escapeHTML = (string) => { - const stringTempNode = doc.createElement('div'); - - stringTempNode.appendChild(doc.createTextNode(string)); - - return stringTempNode.innerHTML; -}; - -const escapeHTMLAttribute = (string) => { - if (string === null) { - return ''; - } - - return String(string) - .replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"') - .replace(/'/g, '''); -}; - -export { escapeHTML, escapeHTMLAttribute }; diff --git a/src/bundle/Resources/public/js/scripts/helpers/text.helper.ts b/src/bundle/Resources/public/js/scripts/helpers/text.helper.ts new file mode 100644 index 0000000000..994c33b193 --- /dev/null +++ b/src/bundle/Resources/public/js/scripts/helpers/text.helper.ts @@ -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, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, '''); +}; + +export { escapeHTML, escapeHTMLAttribute };