@wildpeaks/snapshot-dom
Version:
Converts a DOM element to a JSON tree
1 lines • 501 B
JavaScript
;function toJSON(e){const t={};if("object"==typeof e&&null!==e){e.tagName?t.tagName=e.tagName.toLowerCase():e.nodeName&&(t.nodeName=e.nodeName),e.nodeValue&&(t.nodeValue=e.nodeValue);const o=e.attributes;if(o){const e=o.length;if(e>0){const n={};for(let t=0;t<e;t++){const e=o[t];n[e.nodeName]=e.nodeValue}t.attributes=n}}const{childNodes:n}=e;if(n){const e=n.length;if(e>0){const o=new Array(e);for(let t=0;t<e;t++)o[t]=toJSON(n[t]);t.childNodes=o}}}return t}module.exports.toJSON=toJSON;