html-to-json-parser
Version:
This library converts HTML to JSON or JSON to HTML tree
3 lines (2 loc) • 1.32 kB
JavaScript
import{DOMParser as t}from"@xmldom/xmldom";var e=function(e,r){void 0===r&&(r=!1);try{return Promise.resolve(new Promise(function(n,o){try{var i,a={};if("string"==typeof e){var s=(new t).parseFromString(e,"text/xml");s.firstChild&&(i=s.firstChild)}else i=e;!function t(e,r){void 0===r&&(r=a),r.type=e.nodeName;var n=e.childNodes;if(null!==n&&n.length){r.content=[];for(var o=0;o<n.length;o++)3===n[o].nodeType?n[o].nodeValue&&r.content.push(n[o].nodeValue):(r.content.push({}),t(n[o],r.content[r.content.length-1]))}if(null!==e.attributes&&e.attributes.length){r.attributes={};for(var i=0;i<e.attributes.length;i++)r.attributes[e.attributes[i].nodeName]=e.attributes[i].nodeValue}}(i),n(r?JSON.stringify(a):a)}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}},r=function(e,r){void 0===r&&(r=!0);try{return Promise.resolve(new Promise(function(n,o){try{var i=e;"string"==typeof e&&(i=JSON.parse(e));var a=function t(e){var r="<"+e.type;return e.attributes&&Object.entries(e.attributes).forEach(function(t){r+=" "+t[0]+'="'+t[1]+'"'}),r+=">",e.content&&e.content.forEach(function(e){r+="string"==typeof e?e:t(e)}),r+="</"+e.type+">"}(i);n(r?a:(new t).parseFromString(a,"text/xml"))}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}};export{e as HTMLToJSON,r as JSONToHTML};
//# sourceMappingURL=index.module.js.map