/* * Defiant.js v1.2.5 * Serch JSON structures plus smart templating with XSLT and XPath. * http://defiantjs.com * * Copyright (c) 2013-2015, Hakan Bilgin * Licensed under the MIT License * * NOTE: * Robbie Antenesse edited line 165's RegExp to search global and case-insensitive in case of multiple contains() groups. */ if("undefined"==typeof module)var module={exports:void 0};if(module.exports=Defiant=function(t,e){"use strict";var r={is_ie:/msie/i.test(navigator.userAgent),is_safari:/safari/i.test(navigator.userAgent),env:"production",xml_decl:'',namespace:'xmlns:d="defiant-namespace"',tabsize:4,render:function(t,e){var r,n,a,s,i=new XSLTProcessor,o=document.createElement("span"),l={match:"/"};switch(typeof t){case"object":this.extend(l,t),l.data||(l.data=e);break;case"string":l.template=t,l.data=e;break;default:throw"error"}if(l.data=JSON.toXML(l.data),r='//xsl:template[@name="'+l.template+'"]',this.xsl_template||this.gatherTemplates(),l.sorter&&(s=this.node.selectSingleNode(this.xsl_template,r+"//xsl:for-each//xsl:sort"),s&&(l.sorter.order&&s.setAttribute("order",l.sorter.order),l.sorter.select&&s.setAttribute("select",l.sorter.select),s.setAttribute("data-type",l.sorter.type||"text"))),a=this.node.selectSingleNode(this.xsl_template,r),a.setAttribute("match",l.match),i.importStylesheet(this.xsl_template),o.appendChild(i.transformToFragment(l.data,document)),a.removeAttribute("match"),this.is_safari){n=o.getElementsByTagName("script");for(var c=0,u=n.length;u>c;c++)n[c].defer=!0}return o.innerHTML},gatherTemplates:function(){for(var t=document.getElementsByTagName("script"),e="",r=0,n=t.length;n>r;r++)"defiant/xsl-template"===t[r].type&&(e+=t[r].innerHTML);this.xsl_template=this.xmlFromString('"+e.replace(/defiant:(\w+)/g,"$1")+"")},getSnapshot:function(t){return JSON.toXML(t,!0)},xmlFromString:function(t){var e,r;return t=t.replace(/>\s{1,}<"),null===t.trim().match(/<\?xml/)&&(t=this.xml_decl+t),this.is_ie?(r=new ActiveXObject("Msxml2.DOMDocument"),r.loadXML(t),-1===t.indexOf("xsl:stylesheet")&&r.setProperty("SelectionLanguage","XPath")):(e=new DOMParser,r=e.parseFromString(t,"text/xml")),r},extend:function(t,e){for(var r in e)t[r]&&"object"==typeof e[r]?this.extend(t[r],e[r]):t[r]=e[r];return t},node:{}};return r}(this),"undefined"==typeof XSLTProcessor){var XSLTProcessor=function(){};XSLTProcessor.prototype={importStylesheet:function(t){this.xsldoc=t},transformToFragment:function(t,e){var r=t.transformNode(this.xsldoc),n=document.createElement("span");return n.innerHTML=r,n}}}String.prototype.fill||(String.prototype.fill=function(t,e){var r=this;for(e=e||" ";r.length/,rx_constructor:/<(.+?)( d:contr=".*?")>/,rx_namespace:/ xmlns\:d="defiant\-namespace"/,rx_data:/(<.+?>)(.*?)(<\/d:data>)/i,rx_function:/function (\w+)/i,to_xml:function(t){var e=this.hash_to_xml(null,t);return Defiant.xmlFromString(e)},hash_to_xml:function(t,e,r){var n,a,s,i,o,l,c,u,h=e.constructor===Array,d=[],m=[];for(n in e)if(a=e[n],null!==a&&void 0!==a&&"NaN"!==a.toString()||(a=null),i="@"===n.slice(0,1),o=r?t:n,o==+o&&e.constructor!==Object&&(o="d:item"),null===a?(l=null,c=!1):(l=a.constructor,c=l.toString().match(this.rx_function)[1]),i)m.push(o.slice(1)+'="'+this.escape_xml(a)+'"'),"String"!==c&&m.push("d:"+o.slice(1)+'="'+c+'"');else if(null===a)d.push(this.scalar_to_xml(o,a));else switch(l){case Function:break;case Object:d.push(this.hash_to_xml(o,a));break;case Array:if(n===o){if(s=a.constructor===Array)for(u=a.length;u--;)a[u].constructor===Array&&(s=!0),s||a[u].constructor!==Object||(s=!0);d.push(this.scalar_to_xml(o,a,s));break}case String:if("string"==typeof a&&(a=a.toString().replace(/\&/g,"&").replace(/\r|\n/g," ")),"#text"===o){this.map.push(e),m.push('d:mi="'+this.map.length+'"'),m.push('d:constr="'+c+'"'),d.push(this.escape_xml(a));break}case Number:case Boolean:if("#text"===o&&"String"!==c){this.map.push(e),m.push('d:mi="'+this.map.length+'"'),m.push('d:constr="'+c+'"'),d.push(this.escape_xml(a));break}d.push(this.scalar_to_xml(o,a))}return t||(t="d:data",m.push(Defiant.namespace),h&&m.push('d:constr="Array"')),null===t.match(this.rx_validate_name)&&(m.push('d:name="'+t+'"'),t="d:name"),r?d.join(""):(this.map.push(e),m.push('d:mi="'+this.map.length+'"'),"<"+t+(m.length?" "+m.join(" "):"")+(d.length?">"+d.join("")+"":"/>"))},scalar_to_xml:function(t,e,r){var n,a,s,i="";if(null===t.match(this.rx_validate_name)&&(i+=' d:name="'+t+'"',t="d:name",r=!1),null!==e&&"NaN"!==e.toString()||(e=null),null===e)return"<"+t+' d:constr="null"/>';if(1===e.length&&e[0].constructor===Object){n=this.hash_to_xml(!1,e[0]);var o=n.match(this.rx_node),l=n.match(this.rx_constructor);return o=null!==o?o[2].replace(this.rx_namespace,"").replace(/>/,"").replace(/"\/$/,'"'):"",l=null!==l?l[2]:"",n=n.match(this.rx_data),n=null!==n?n[2]:"","<"+t+o+" "+l+' d:type="ArrayItem">'+n+""}return 0===e.length&&e.constructor===Array?"<"+t+' d:constr="Array"/>':r?this.hash_to_xml(t,e,!0):(a=e.constructor,s=a.toString().match(this.rx_function)[1],n=a===Array?this.hash_to_xml("d:item",e,!0):this.escape_xml(e),i+=' d:constr="'+s+'"',this.map.push(e),i+=' d:mi="'+this.map.length+'"',"#text"===t?this.escape_xml(e):"<"+t+i+">"+n+"")},escape_xml:function(t){return String(t).replace(//g,">").replace(/"/g,""").replace(/ /g," ")}},n=r.to_xml.call(r,t);return e?{doc:n,src:t,map:r.map}:(this.search.map=r.map,n)}),JSON.search||(JSON.search=function(t,e,r){"use strict";var n,a,s=t.doc&&t.doc.nodeType,i=s?t.doc:JSON.toXML(t),o=s?t.map:this.search.map,l=s?t.src:t,c=Defiant.node[r?"selectSingleNode":"selectNodes"](i,e.xTransform()),u=[];for(r&&(c=[c]),a=c.length;a--;)switch(c[a].nodeType){case 2:case 3:u.unshift(c[a].nodeValue);break;default:n=+c[a].getAttribute("d:mi"),o[n-1]&&u.unshift(o[n-1])}return"development"===Defiant.env&&(this.trace=JSON.mtrace(l,u,c)),u}),JSON.mtrace||(JSON.mtrace=function(t,e,r){"use strict";for(var n,a,s,i,o,l=window,c=JSON.stringify,u=c(t,null," ").replace(/\t/g,""),h=[],d=0,m=r.length,p=m?r[d].ownerDocument.documentElement:!1,f=(this.search.map,0);m>d;d++){switch(r[d].nodeType){case 2:a=r[d].ownerElement?r[d].ownerElement.getAttribute("d:"+r[d].nodeName):"String",n='"@'+r[d].nodeName+'": '+l[a](e[d]),s=u.indexOf(n),o=0;break;case 3:a=r[d].parentNode.getAttribute("d:constr"),n=l[a](e[d]),n='"'+r[d].parentNode.nodeName+'": '+("Number"===n?n:'"'+n+'"'),s=u.indexOf(n),o=0;break;default:if(r[d]===p)continue;"String"===r[d].getAttribute("d:constr")?(a=r[d].getAttribute("d:constr"),n=l[a](e[d]),n='"'+r[d].nodeName+'": '+("Number"===n?n:'"'+n+'"'),s=u.indexOf(n,f),o=0,f=s+1):(n=c(e[d],null," ").replace(/\t/g,""),s=u.indexOf(n),o=n.match(/\n/g).length)}i=u.substring(0,s).match(/\n/g).length+1,h.push([i,o])}return h}),Defiant.node.selectNodes=function(t,e){if(t.evaluate){for(var r=t.createNSResolver(t.documentElement),n=t.evaluate(e,t,r,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null),a=[],s=0,i=n.snapshotLength;i>s;s++)a.push(n.snapshotItem(s));return a}return t.selectNodes(e)},Defiant.node.selectSingleNode=function(t,e){if(t.evaluate){var r=this.selectNodes(t,e);return r.length>0?r[0]:null}return t.selectSingleNode(e)},Defiant.node.prettyPrint=function(t){var e,r,n=Defiant,a=n.tabsize,s=n.xml_decl.toLowerCase();n.is_ie?r=t.xml:(e=new XMLSerializer,r=e.serializeToString(t)),"development"!==n.env&&(r=r.replace(/ \w+\:d=".*?"| d\:\w+=".*?"/g,""));for(var i,o,l=r.trim().replace(/(>)\s*(<)(\/*)/g,"$1\n$2$3"),c=l.split("\n"),u=-1,h=0,d=c.length;d>h;h++)0===h&&c[h].toLowerCase()===s||(i=null!==c[h].match(/<[A-Za-z_\:]+.*?>/g),o=null!==c[h].match(/<\/[\w\:]+>/g),null!==c[h].match(/<.*?\/>/g)&&(i=o=!0),i&&u++,c[h]=String().fill(u," ")+c[h],i&&o&&u--,!i&&o&&u--);return c.join("\n").replace(/\t/g,String().fill(a," "))},Defiant.node.toJSON=function(t,e){"use strict";var r=function(t){var e,n,a,s,i,o,l,c,u,h,d={},m=window;switch(t.nodeType){case 1:for(i=t.getAttribute("d:constr"),"Array"===i?d=[]:"String"===i&&""===t.textContent&&(d=""),e=t.attributes,c=0,u=e.length;u>c;c++)h=e.item(c),null===h.nodeName.match(/\:d|d\:/g)&&(i=t.getAttribute("d:"+h.nodeName),o=i&&"undefined"!==i?"null"===h.nodeValue?null:m[i]("false"===h.nodeValue?"":h.nodeValue):h.nodeValue,d["@"+h.nodeName]=o);break;case 3:n=t.parentNode.getAttribute("d:type"),o=n?m[n]("false"===t.nodeValue?"":t.nodeValue):t.nodeValue,d=o}if(t.hasChildNodes())for(c=0,u=t.childNodes.length;u>c;c++)if(a=t.childNodes.item(c),s=a.nodeName,e=t.attributes,"d:name"===s&&(s=a.getAttribute("d:name")),"#text"===s)i=t.getAttribute("d:constr"),"undefined"===i&&(i=void 0),l=a.textContent||a.text,o="Boolean"===i&&"false"===l?"":l,i||e.length?i&&1===u?d=m[i](o):t.hasChildNodes()&&e.length<3?d=i?m[i](o):o:d[s]=i?m[i](o):o:d=o;else{if(d[s]){d[s].push?d[s].push(r(a)):d[s]=[d[s],r(a)];continue}switch(i=a.getAttribute("d:constr")){case"null":d.push?d.push(null):d[s]=null;break;case"Array":a.parentNode.firstChild===a&&"Array"===i&&"d:item"!==s?"d:item"===s||"Array"===i?(o=r(a),d[s]=o.length?[o]:o):d[s]=r(a):d.push?d.push(r(a)):d[s]=r(a);break;case"String":case"Number":case"Boolean":l=a.textContent||a.text,o="Boolean"===i&&"false"===l?"":l,d.push?d.push(m[i](o)):d[s]=r(a);break;default:d.push?d.push(r(a)):d[s]=r(a)}}return 1===t.nodeType&&"ArrayItem"===t.getAttribute("d:type")&&(d=[d]),d},n=9===t.nodeType?t.documentElement:t,a=r(n),s=a[n.nodeName];return n===n.ownerDocument.documentElement&&s&&s.constructor===Array&&(a=s),e&&"true"===e.toString()&&(e=" "),e?JSON.stringify(a,null,e):a},"undefined"!=typeof jQuery&&!function(t){"use strict";t.fn.defiant=function(t,e){return this.html(Defiant.render(t,e)),this}}(jQuery);