UNPKG

fast-xml-parser

Version:

Validate XML, Parse XML, Build XML without C/C++ based libraries

2 lines 7.79 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.XMLBuilder=e():t.XMLBuilder=e()}(this,()=>(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function i(t,e){let i="";return e.format&&e.indentBy.length>0&&(i="\n"),s(t,e,"",i)}function s(t,e,i,p){let u="",h=!1;if(!Array.isArray(t)){if(null!=t){let i=t.toString();return i=a(i,e),i}return""}for(let l=0;l<t.length;l++){const d=t[l],c=n(d);if(void 0===c)continue;let f="";if(f=0===i.length?c:`${i}.${c}`,c===e.textNodeName){let t=d[c];r(f,e)||(t=e.tagValueProcessor(c,t),t=a(t,e)),h&&(u+=p),u+=t,h=!1;continue}if(c===e.cdataPropName){h&&(u+=p),u+=`<![CDATA[${d[c][0][e.textNodeName]}]]>`,h=!1;continue}if(c===e.commentPropName){u+=p+`\x3c!--${d[c][0][e.textNodeName]}--\x3e`,h=!0;continue}if("?"===c[0]){const t=o(d[":@"],e),i="?xml"===c?"":p;let s=d[c][0][e.textNodeName];s=0!==s.length?" "+s:"",u+=i+`<${c}${s}${t}?>`,h=!0;continue}let g=p;""!==g&&(g+=e.indentBy);const b=p+`<${c}${o(d[":@"],e)}`,N=s(d[c],e,f,g);-1!==e.unpairedTags.indexOf(c)?e.suppressUnpairedNode?u+=b+">":u+=b+"/>":N&&0!==N.length||!e.suppressEmptyNode?N&&N.endsWith(">")?u+=b+`>${N}${p}</${c}>`:(u+=b+">",N&&""!==p&&(N.includes("/>")||N.includes("</"))?u+=p+e.indentBy+N+p:u+=N,u+=`</${c}>`):u+=b+"/>",h=!0}return u}function n(t){const e=Object.keys(t);for(let i=0;i<e.length;i++){const s=e[i];if(Object.prototype.hasOwnProperty.call(t,s)&&":@"!==s)return s}}function o(t,e){let i="";if(t&&!e.ignoreAttributes)for(let s in t){if(!Object.prototype.hasOwnProperty.call(t,s))continue;let n=e.attributeValueProcessor(s,t[s]);n=a(n,e),!0===n&&e.suppressBooleanAttributes?i+=` ${s.substr(e.attributeNamePrefix.length)}`:i+=` ${s.substr(e.attributeNamePrefix.length)}="${n}"`}return i}function r(t,e){let i=(t=t.substr(0,t.length-e.textNodeName.length-1)).substr(t.lastIndexOf(".")+1);for(let s in e.stopNodes)if(e.stopNodes[s]===t||e.stopNodes[s]==="*."+i)return!0;return!1}function a(t,e){if(t&&t.length>0&&e.processEntities)for(let i=0;i<e.entities.length;i++){const s=e.entities[i];t=t.replace(s.regex,s.val)}return t}t.r(e),t.d(e,{default:()=>c});const p={attributeNamePrefix:"@_",attributesGroupName:!1,textNodeName:"#text",ignoreAttributes:!0,cdataPropName:!1,format:!1,indentBy:" ",suppressEmptyNode:!1,suppressUnpairedNode:!0,suppressBooleanAttributes:!0,tagValueProcessor:function(t,e){return e},attributeValueProcessor:function(t,e){return e},preserveOrder:!1,commentPropName:!1,unpairedTags:[],entities:[{regex:new RegExp("&","g"),val:"&amp;"},{regex:new RegExp(">","g"),val:"&gt;"},{regex:new RegExp("<","g"),val:"&lt;"},{regex:new RegExp("'","g"),val:"&apos;"},{regex:new RegExp('"',"g"),val:"&quot;"}],processEntities:!0,stopNodes:[],oneListGroup:!1};function u(t){var e;this.options=Object.assign({},p,t),!0===this.options.ignoreAttributes||this.options.attributesGroupName?this.isAttribute=function(){return!1}:(this.ignoreAttributesFn="function"==typeof(e=this.options.ignoreAttributes)?e:Array.isArray(e)?t=>{for(const i of e){if("string"==typeof i&&t===i)return!0;if(i instanceof RegExp&&i.test(t))return!0}}:()=>!1,this.attrPrefixLen=this.options.attributeNamePrefix.length,this.isAttribute=d),this.processTextOrObjNode=h,this.options.format?(this.indentate=l,this.tagEndChar=">\n",this.newLine="\n"):(this.indentate=function(){return""},this.tagEndChar=">",this.newLine="")}function h(t,e,i,s){const n=this.j2x(t,i+1,s.concat(e));return void 0!==t[this.options.textNodeName]&&1===Object.keys(t).length?this.buildTextValNode(t[this.options.textNodeName],e,n.attrStr,i):this.buildObjectNode(n.val,e,n.attrStr,i)}function l(t){return this.options.indentBy.repeat(t)}function d(t){return!(!t.startsWith(this.options.attributeNamePrefix)||t===this.options.textNodeName)&&t.substr(this.attrPrefixLen)}u.prototype.build=function(t){return this.options.preserveOrder?i(t,this.options):(Array.isArray(t)&&this.options.arrayNodeName&&this.options.arrayNodeName.length>1&&(t={[this.options.arrayNodeName]:t}),this.j2x(t,0,[]).val)},u.prototype.j2x=function(t,e,i){let s="",n="";const o=i.join(".");for(let r in t)if(Object.prototype.hasOwnProperty.call(t,r))if(void 0===t[r])this.isAttribute(r)&&(n+="");else if(null===t[r])this.isAttribute(r)||r===this.options.cdataPropName?n+="":"?"===r[0]?n+=this.indentate(e)+"<"+r+"?"+this.tagEndChar:n+=this.indentate(e)+"<"+r+"/"+this.tagEndChar;else if(t[r]instanceof Date)n+=this.buildTextValNode(t[r],r,"",e);else if("object"!=typeof t[r]){const i=this.isAttribute(r);if(i&&!this.ignoreAttributesFn(i,o))s+=this.buildAttrPairStr(i,""+t[r]);else if(!i)if(r===this.options.textNodeName){let e=this.options.tagValueProcessor(r,""+t[r]);n+=this.replaceEntitiesValue(e)}else n+=this.buildTextValNode(t[r],r,"",e)}else if(Array.isArray(t[r])){const s=t[r].length;let o="",a="";for(let p=0;p<s;p++){const s=t[r][p];if(void 0===s);else if(null===s)"?"===r[0]?n+=this.indentate(e)+"<"+r+"?"+this.tagEndChar:n+=this.indentate(e)+"<"+r+"/"+this.tagEndChar;else if("object"==typeof s)if(this.options.oneListGroup){const t=this.j2x(s,e+1,i.concat(r));o+=t.val,this.options.attributesGroupName&&s.hasOwnProperty(this.options.attributesGroupName)&&(a+=t.attrStr)}else o+=this.processTextOrObjNode(s,r,e,i);else if(this.options.oneListGroup){let t=this.options.tagValueProcessor(r,s);t=this.replaceEntitiesValue(t),o+=t}else o+=this.buildTextValNode(s,r,"",e)}this.options.oneListGroup&&(o=this.buildObjectNode(o,r,a,e)),n+=o}else if(this.options.attributesGroupName&&r===this.options.attributesGroupName){const e=Object.keys(t[r]),i=e.length;for(let n=0;n<i;n++)s+=this.buildAttrPairStr(e[n],""+t[r][e[n]])}else n+=this.processTextOrObjNode(t[r],r,e,i);return{attrStr:s,val:n}},u.prototype.buildAttrPairStr=function(t,e){return e=this.options.attributeValueProcessor(t,""+e),e=this.replaceEntitiesValue(e),this.options.suppressBooleanAttributes&&"true"===e?" "+t:" "+t+'="'+e+'"'},u.prototype.buildObjectNode=function(t,e,i,s){if(""===t)return"?"===e[0]?this.indentate(s)+"<"+e+i+"?"+this.tagEndChar:this.indentate(s)+"<"+e+i+this.closeTag(e)+this.tagEndChar;{let n="</"+e+this.tagEndChar,o="";return"?"===e[0]&&(o="?",n=""),!i&&""!==i||-1!==t.indexOf("<")?!1!==this.options.commentPropName&&e===this.options.commentPropName&&0===o.length?this.indentate(s)+`\x3c!--${t}--\x3e`+this.newLine:this.indentate(s)+"<"+e+i+o+this.tagEndChar+t+this.indentate(s)+n:this.indentate(s)+"<"+e+i+o+">"+t+n}},u.prototype.closeTag=function(t){let e="";return-1!==this.options.unpairedTags.indexOf(t)?this.options.suppressUnpairedNode||(e="/"):e=this.options.suppressEmptyNode?"/":`></${t}`,e},u.prototype.buildTextValNode=function(t,e,i,s){if(!1!==this.options.cdataPropName&&e===this.options.cdataPropName)return this.indentate(s)+`<![CDATA[${t}]]>`+this.newLine;if(!1!==this.options.commentPropName&&e===this.options.commentPropName)return this.indentate(s)+`\x3c!--${t}--\x3e`+this.newLine;if("?"===e[0])return this.indentate(s)+"<"+e+i+"?"+this.tagEndChar;{let n=this.options.tagValueProcessor(e,t);return n=this.replaceEntitiesValue(n),""===n?this.indentate(s)+"<"+e+i+this.closeTag(e)+this.tagEndChar:this.indentate(s)+"<"+e+i+">"+n+"</"+e+this.tagEndChar}},u.prototype.replaceEntitiesValue=function(t){if(t&&t.length>0&&this.options.processEntities)for(let e=0;e<this.options.entities.length;e++){const i=this.options.entities[e];t=t.replace(i.regex,i.val)}return t};const c=u;return e})()); //# sourceMappingURL=fxbuilder.min.js.map