dom.ts 642 B

12345678910111213141516171819202122232425
  1. export const canUseDOM = (): boolean =>
  2. !!(typeof window !== 'undefined' && window.document);
  3. export const xmlParser = (xmlString: string): Document => {
  4. const parser = new window.DOMParser();
  5. const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
  6. return xmlDoc;
  7. };
  8. export const getElementsByTagName = (
  9. elements: ChildNode,
  10. tagname: string,
  11. ): NodeList | null => {
  12. const arr = Array.prototype.slice.call(elements.childNodes);
  13. let element: NodeList | null = null;
  14. arr.forEach((ele) => {
  15. if (ele.tagName === tagname) {
  16. element = ele.childNodes;
  17. }
  18. });
  19. return element;
  20. };
  21. export default canUseDOM;