Merge branch '19177-sharing-urls-ui-config'. Closes #19177
[arvados-workbench2.git] / src / common / xml.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { customDecodeURI } from "./url";
6
7 export const getTagValue = (document: Document | Element, tagName: string, defaultValue: string, skipDecoding: boolean = false) => {
8     const [el] = Array.from(document.getElementsByTagName(tagName));
9     const URI = el ? htmlDecode(el.innerHTML) : defaultValue;
10
11     if (!skipDecoding) {
12         try {
13             return customDecodeURI(URI);
14         } catch(e) {}
15     }
16
17     return URI;
18 };
19
20 const htmlDecode = (input: string) => {
21     const out = input.split(' ').map((i) => {
22         const doc = new DOMParser().parseFromString(i, "text/html");
23         if (doc.documentElement !== null) {
24             return doc.documentElement.textContent || '';
25         }
26         return '';
27     });
28     return out.join(' ');
29 };