-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml-serializer.js
2 lines (2 loc) · 1.43 KB
/
xml-serializer.js
1
2
function e(e){return t(e,"")}function t(e,n){const o=e.nodeType;if(1==o){const o=e;n+="<"+o.tagName,o.prefix?(o.getAttributeNS("http://www.w3.org/2000/xmlns/",o.prefix)??o.parentNode?.lookupNamespaceURI(o.prefix))!==o.namespaceURI&&(n+=` xmlns:${o.prefix}="${o.namespaceURI||""}"`):o.namespaceURI&&(o.getAttributeNS("http://www.w3.org/2000/xmlns/","xmlns")??o.getAttributeNS(null,"xmlns")??o.parentNode?.lookupNamespaceURI(null))!==o.namespaceURI&&(n+=` xmlns="${o.namespaceURI}"`);for(const e of o.attributes){let t=e.localName;e.prefix&&(e.lookupNamespaceURI(e.prefix)!==e.namespaceURI&&(n+=` xmlns:${e.prefix}="${e.namespaceURI||""}"`),t=e.prefix+":"+e.localName),n+=` ${t}="${r(e.value)}"`}if(o.hasChildNodes()){n+=">";for(const e of o.childNodes)n=t(e,n);return n+`</${o.tagName}>`}return n+"/>"}if(3==o)return n+r(e.textContent);if(4==o)return n+`<[CDATA[${e.textContent}]]>`;if(7==o){const t=e;return n+`<?${t.target} ${t.data}?>`}if(8==o)return n+`<!--${e.textContent}-->`;if(9==o||11==o){const r=e;for(const e of r.childNodes)n=t(e,n);return n}if(10==o){const t=e;return n+="<!DOCTYPE "+t.name,t.publicId&&(n+=" "+t.publicId),t.systemId&&(n+=" "+t.systemId),t.internalSubset?n+=` [${t.internalSubset}]>`:n+=">",n}throw Error("Unexpected node type: "+o)}const n={"<":"<",">":">","&":"&",'"':""","'":"'"};function r(e){return e.replace(/[<>&"']/g,(e=>n[e]))}export{e as serializeToString};
//# sourceMappingURL=xml-serializer.js.map