Merge remote-tracking branch 'origin/main' into 18169-cancel-button-not-working
[arvados-workbench2.git] / src / common / xml.ts
index c810de957055d47aa5a8cab9576e24d5b1046b66..e7db3acad8d904b58b4ae0c2061023eaade227b4 100644 (file)
@@ -2,7 +2,28 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-export const getTagValue = (document: Document | Element, tagName: string, defaultValue: string) => {
+import { customDecodeURI } from "./url";
+
+export const getTagValue = (document: Document | Element, tagName: string, defaultValue: string, skipDecoding: boolean = false) => {
     const [el] = Array.from(document.getElementsByTagName(tagName));
-    return el ? el.innerHTML : defaultValue;
+    const URI = el ? htmlDecode(el.innerHTML) : defaultValue;
+
+    if (!skipDecoding) {
+        try {
+            return customDecodeURI(URI);
+        } catch(e) {}
+    }
+
+    return URI;
+};
+
+const htmlDecode = (input: string) => {
+    const out = input.split(' ').map((i) => {
+        const doc = new DOMParser().parseFromString(i, "text/html");
+        if (doc.documentElement !== null) {
+            return doc.documentElement.textContent || '';
+        }
+        return '';
+    });
+    return out.join(' ');
 };