dom.ts 619 B

123456789101112131415161718192021222324
  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 => {
  12. const arr = Array.prototype.slice.call(elements.childNodes);
  13. let element: any = null;
  14. arr.forEach(ele => {
  15. if (ele.tagName === tagname) {
  16. element = ele.childNodes;
  17. }
  18. });
  19. return element;
  20. };
  21. export default canUseDOM;