-
Notifications
You must be signed in to change notification settings - Fork 27
/
jxon.min.js
39 lines (39 loc) · 5.23 KB
/
jxon.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* JXON framework - Copyleft 2011 by Mozilla Developer Network
*
* Revision #1 - September 5, 2014
*
* https://developer.mozilla.org/en-US/docs/JXON
*
* This framework is released under the GNU Public License, version 3 or later.
* http://www.gnu.org/licenses/gpl-3.0-standalone.html
*
* small modifications performed by the iD project:
* https://github.com/openstreetmap/iD/commits/18aa33ba97b52cacf454e95c65d154000e052a1f/js/lib/jxon.js
*
* small modifications performed by user @bugreport0
* https://github.com/tyrasd/JXON/pull/2/commits
*
* some additions and modifications by user @igord
* https://github.com/tyrasd/JXON/pull/5/commits
*
* bugfixes and code cleanup by user @laubstein
* https://github.com/tyrasd/jxon/pull/32
*
* adapted for nodejs and npm by @tyrasd (Martin Raifer <[email protected]>)
*/
(function(e,t){if(typeof define==="function"&&define.amd){
// AMD. Register as an anonymous module.
define([],t(window))}else if(typeof exports==="object"){if(typeof window==="object"&&window.DOMImplementation){
// Browserify. hardcode usage of browser's own XMLDom implementation
// see https://github.com/tyrasd/jxon/issues/18
module.exports=t(window)}else{
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports=t(require("xmldom"),true)}}else{
// Browser globals (root is window)
e.JXON=t(window)}})(this,function(e,t){var r={valueKey:"_",attrKey:"$",attrPrefix:"$",lowerCaseTags:false,trueIsEmpty:false,autoDate:false,ignorePrefixedNodes:false,parseValues:false};var n=[];var i=/^\s*$/;var o=/^(?:true|false)$/i;var a;return new function(){this.config=function(t){for(var n in t){r[n]=t[n]}if(r.parserErrorHandler){a=new e.DOMParser({errorHandler:r.parserErrorHandler,locator:{}})}};function s(e){if(!r.parseValues){return e}if(i.test(e)){return null}if(o.test(e)){return e.toLowerCase()==="true"}if(isFinite(e)){return parseFloat(e)}if(r.autoDate&&isFinite(Date.parse(e))){return new Date(e)}return e}function l(){}l.prototype.toString=function(){return"null"};l.prototype.valueOf=function(){return null};function f(e){return e===null?new l:e instanceof Object?e:new e.constructor(e)}function u(e,t,i,o){var a=4,l=3,c=1,d=n.length,p=e.hasChildNodes(),m=e.nodeType===e.ELEMENT_NODE&&e.hasAttributes(),h=Boolean(t&2),g=0,x="",v=h?{}:/* put here the default value for empty nodes: */r.trueIsEmpty?true:"",w,y;if(p){for(var T,S=0;S<e.childNodes.length;S++){T=e.childNodes.item(S);if(T.nodeType===a){x+=T.nodeValue}else if(T.nodeType===l){x+=T.nodeValue.trim()}else if(T.nodeType===c&&!(r.ignorePrefixedNodes&&T.prefix)){n.push(T)}}}var N=n.length,b=s(x);if(!h&&(p||m)){v=t===0?f(b):{}}for(var E=d;E<N;E++){w=n[E].nodeName;if(r.lowerCaseTags){w=w.toLowerCase()}y=u(n[E],t,i,o);if(v.hasOwnProperty(w)){if(v[w].constructor!==Array){v[w]=[v[w]]}v[w].push(y)}else{v[w]=y;g++}}if(m){var O=e.attributes.length,C=o?"":r.attrPrefix,D=o?{}:v;for(var P,j,I=0;I<O;g++,I++){P=e.attributes.item(I);j=P.name;if(r.lowerCaseTags){j=j.toLowerCase()}D[C+j]=s(P.value.trim())}if(o){if(i){Object.freeze(D)}v[r.attrKey]=D;g-=O-1}}if(t===3||(t===2||t===1&&g>0)&&x){v[r.valueKey]=b}else if(!h&&g===0&&x){v=b}if(i&&(h||g>0)){Object.freeze(v)}n.length=d;return v}function c(e,n,i){var o,a,s;if(i.constructor===String||i.constructor===Number||i.constructor===Boolean){n.appendChild(e.createTextNode(i.toString()));/* verbosity level is 0 or 1 */
if(i===i.valueOf()){return}}else if(i.constructor===Date){n.appendChild(e.createTextNode(i.toISOString()))}for(var l in i){o=i[l];if(o===null){o={}}if(isFinite(l)||o instanceof Function){continue}/* verbosity level is 0 */
if(l===r.valueKey){if(o!==null&&o!==true){n.appendChild(e.createTextNode(o.constructor===Date?o.toISOString():String(o)))}}else if(l===r.attrKey){/* verbosity level is 3 */
for(var f in o){n.setAttribute(f,o[f])}}else if(l===r.attrPrefix+"xmlns"){if(t){n.setAttribute(l.slice(1),o)}}else if(l.charAt(0)===r.attrPrefix){n.setAttribute(l.slice(1),o)}else if(o.constructor===Array){for(var u in o){s=o[u]&&o[u][r.attrPrefix+"xmlns"]||n.namespaceURI;if(s){a=e.createElementNS(s,l)}else{a=e.createElement(l)}c(e,a,o[u]||{});n.appendChild(a)}}else{s=(o||{})[r.attrPrefix+"xmlns"]||n.namespaceURI;if(s){a=e.createElementNS(s,l)}else{a=e.createElement(l)}if(o instanceof Object){c(e,a,o)}else if(o!==null&&o!==true){a.appendChild(e.createTextNode(o.toString()))}else if(!r.trueIsEmpty&&o===true){a.appendChild(e.createTextNode(o.toString()))}n.appendChild(a)}}}this.xmlToJs=this.build=function(e,t,r,n){var i=arguments.length>1&&typeof t==="number"?t&3:1;return u(e,i,r||false,arguments.length>3?n:i===3)};this.jsToXml=this.unbuild=function(t,r,n,i){var o=e.document&&e.document.implementation||new e.DOMImplementation;var a=o.createDocument(r||null,n||"",i||null);c(a,a.documentElement||a,t);return a};this.stringToXml=function(t){if(!a){a=new e.DOMParser}return a.parseFromString(t,"application/xml")};this.xmlToString=function(t){if(typeof t.xml!=="undefined"){return t.xml}else{return(new e.XMLSerializer).serializeToString(t)}};this.stringToJs=function(e){var t=this.stringToXml(e);return this.xmlToJs(t)};this.jsToString=this.stringify=function(e,t,r,n){return this.xmlToString(this.jsToXml(e,t,r,n))};this.each=function(e,t,r){if(e instanceof Array){e.forEach(t,r)}else{[e].forEach(t,r)}}}});