@web3r/flowerkit
Version:
Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).
1 lines • 2.29 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":[""],"sourcesContent":["import { parse } from \"node-html-parser\";\r\n\r\nexport type TGetHTMLFromStrArgs = Parameters<typeof getHTMLFromStr>;\r\n\r\nexport type TGetHTMLFromStrReturn = ReturnType<typeof getHTMLFromStr>;\r\n\r\n/**\r\n * Get parsed HTML from string and returns NodeList that include elements and text\r\n * @param str{String} source string\r\n * @param type{DOMParserSupportedType} content type (\"application/xhtml+xml\", \"application/xml\", \"image/svg+xml\", \"text/html\" (by default) or \"text/xml\"\r\n * @returns {NodeList}\r\n * @throws {TypeError} getHTMLFromStr: str must be a non-empty string\r\n * @throws {TypeError} getHTMLFromStr: type must be a supported DOMParser type\r\n * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser\r\n * @example\r\n * // How to get parsed HTML elements from string?\r\n * Array.from(getHTMLFromStr(`\r\n * <p>Hello world!</p>\r\n * <p>Hello world!</p>\r\n * `)); // returns array of two paragraph nodes\r\n */\r\nexport const getHTMLFromStr = (str: string = \"\", type: DOMParserSupportedType = \"text/html\"): NodeList => {\r\n if (typeof str !== \"string\" || str.length === 0) {\r\n throw new TypeError(\"getHTMLFromStr: str must be a non-empty string\");\r\n }\r\n const allowed: DOMParserSupportedType[] = [ \"application/xhtml+xml\", \"application/xml\", \"image/svg+xml\", \"text/html\", \"text/xml\" ];\r\n if (!allowed.includes(type)) {\r\n throw new TypeError(\"getHTMLFromStr: type must be a supported DOMParser type\");\r\n }\r\n if (typeof DOMParser === \"undefined\") {\r\n return (parse(str) as unknown as HTMLElement).querySelectorAll(\"*\");\r\n } else {\r\n return new DOMParser().parseFromString(str, type).body.childNodes;\r\n }\r\n};\r\n"],"names":["getHTMLFromStr","str","type","length","TypeError","allowed","includes","DOMParser","parse","querySelectorAll","parseFromString","body","childNodes"],"mappings":";;;;;;;;;;;;;;;GAqBO,MAAMA,eAAiBA,CAACC,IAAc,GAAIC,KAA+B,eAC9E,UAAWD,MAAQ,UAAYA,IAAIE,SAAW,EAC5C,MAAM,IAAIC,UAAU,kDAEtB,MAAMC,QAAoC,CAAE,wBAAyB,kBAAmB,gBAAiB,YAAa,YACtH,IAAKA,QAAQC,SAASJ,MACpB,MAAM,IAAIE,UAAU,2DAEtB,UAAWG,YAAc,YACvB,OAAQC,MAAMP,KAAgCQ,iBAAiB,UAE/D,OAAO,IAAIF,WAAYG,gBAAgBT,IAAKC,MAAMS,KAAKC"}