17337: Added another edge case handling
[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 export const getTagValue = (document: Document | Element, tagName: string, defaultValue: string) => {
6     const [el] = Array.from(document.getElementsByTagName(tagName));
7     const URI = el ? htmlDecode(el.innerHTML) : defaultValue;
8
9     try {
10         return decodeURI(URI);
11     } catch(e) {}
12
13     return URI;
14 };
15
16 const htmlDecode = (input: string) => {
17     const out = input.split(' ').map((i) => {
18         const doc = new DOMParser().parseFromString(i, "text/html");
19         if (doc.documentElement !== null) {
20             return doc.documentElement.textContent || '';
21         }
22         return '';
23     });
24     return out.join(' ');
25 };