{"version":3,"file":"index.cjs","sources":["../src/html/HTMLParser.ts","../src/json/JSONToHTML.ts"],"sourcesContent":["/**\n * Convert HTML to JSON\n * @author Yousuf Kalim\n */\nimport { DOMParser } from '@xmldom/xmldom';\nimport { JSONContent } from '../types';\n\n/**\n * Converts HTML Element or string to JSON\n * @param element The HTML string or element to convert to JSON.\n * @param json A boolean to indicate if the output should be a JSON string.\n * @returns {Promise<JSONContent | string>}\n */\nasync function HTMLParser(element: Element | string, json = false): Promise<JSONContent | string> {\n  return await new Promise((resolve, reject) => {\n    try {\n      const treeObject: any = {};\n      let elementToParse: Element;\n\n      // If string convert to document Node\n      if (typeof element === 'string') {\n        const parser = new DOMParser();\n        const docNode = parser.parseFromString(element, 'text/xml');\n        if (docNode.firstChild) {\n          elementToParse = docNode.firstChild as Element;\n        }\n      } else {\n        elementToParse = element;\n      }\n\n      // Recursively loop through DOM elements and assign properties to object\n      const treeHTML = (element: Element, object = treeObject): void => {\n        object.type = element.nodeName;\n        const nodeList = element.childNodes;\n        if (nodeList !== null) {\n          if (nodeList.length) {\n            object.content = [];\n            for (let i = 0; i < nodeList.length; i++) {\n              if (nodeList[i].nodeType === 3) {\n                if (nodeList[i].nodeValue) {\n                  object.content.push(nodeList[i].nodeValue);\n                }\n              } else {\n                object.content.push({});\n                treeHTML(nodeList[i] as Element, object.content[object.content.length - 1]);\n              }\n            }\n          }\n        }\n        if (element.attributes !== null) {\n          if (element.attributes.length) {\n            object.attributes = {};\n            for (let i = 0; i < element.attributes.length; i++) {\n              object.attributes[element.attributes[i].nodeName] = element.attributes[i].nodeValue;\n            }\n          }\n        }\n      };\n\n      // @ts-expect-error\n      treeHTML(elementToParse);\n\n      resolve(json ? JSON.stringify(treeObject) : treeObject);\n    } catch (e) {\n      reject(e);\n    }\n  });\n}\n\nexport default HTMLParser;\n","/**\n * Convert JSON to HTML\n * @author Yousuf Kalim\n */\nimport { DOMParser } from '@xmldom/xmldom';\nimport { JSONContent } from '../types';\n\n/**\n * Converts JSON content to HTML string or HTML Document object\n * @param content The JSON or JS object to convert to HTML Element/String\n * @param string A boolean to indicate if the output should be an HTML Element or String.\n * @returns {Promise<string | Document>}\n */\nasync function JSONToHTML(\n  content: JSONContent | string,\n  string = true, // default to returning a string representation\n): Promise<string | Document> {\n  return await new Promise((resolve, reject) => {\n    try {\n      let jsonContent = content;\n\n      // If input is a string, parse it as JSON\n      if (typeof content === 'string') {\n        jsonContent = JSON.parse(content) as JSONContent;\n      }\n\n      // Recursively construct HTML string from JSON content\n      const treeJSON = (content: JSONContent): string => {\n        let html = `<${content.type}`; // Start with opening tag\n\n        // If there are attributes, add them to the tag\n        if (content.attributes) {\n          Object.entries(content.attributes).forEach(([attribute, value]) => {\n            html += ` ${attribute}=\"${value as string}\"`;\n          });\n        }\n        html += '>';\n\n        // If there is content, process it and add it to the tag\n        if (content.content) {\n          content.content.forEach((node) => {\n            if (typeof node === 'string') {\n              html += node;\n            } else {\n              html += treeJSON(node);\n            }\n          });\n        }\n\n        // End the tag\n        html += `</${content.type}>`;\n\n        return html;\n      };\n\n      // Convert the JSON content to HTML string\n      const html = treeJSON(jsonContent as JSONContent);\n\n      // If string flag is set, return the HTML string\n      if (string) {\n        resolve(html);\n      } else {\n        // Otherwise, parse the HTML string to an Element\n        const parser = new DOMParser();\n        resolve(parser.parseFromString(html, 'text/xml'));\n      }\n    } catch (e) {\n      // Reject the Promise if there's an error\n      reject(e);\n    }\n  });\n}\n\nexport default JSONToHTML;\n"],"names":["HTMLParser","element","json","Promise","resolve","reject","elementToParse","treeObject","docNode","DOMParser","parseFromString","firstChild","treeHTML","object","type","nodeName","nodeList","childNodes","length","content","i","nodeType","nodeValue","push","attributes","_i","JSON","stringify","e","string","jsonContent","parse","html","Object","entries","forEach","_ref","attribute","value","node","treeJSON"],"mappings":"mDAaeA,SAAWC,EAA2BC,QAAI,IAAJA,IAAAA,GAAO,GAAK,IAAA,OAAAC,QAAAC,QAClD,IAAWD,QAAC,SAACC,EAASC,GACjC,IACE,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