Lexiconga/js/min/defiant.js

1 line
10 KiB
JavaScript
Raw Normal View History

2016-03-02 19:18:52 +01:00
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:'<?xml version="1.0" encoding="utf-8"?>',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('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" '+this.namespace+">"+e.replace(/defiant:(\w+)/g,"$1")+"</xsl:stylesheet>")},getSnapshot:function(t){return JSON.toXML(t,!0)},xmlFromString:function(t){var e,r;return t=t.replace(/>\s{1,}</g,"><"),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<t;r+=e);return r}),String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/gm,"")}),String.prototype.xTransform||(String.prototype.xTransform=function(){var t=this;return-1===this.indexOf("translate(")&&(t=this.replace(/contains\(([^,]+),([^\\)]+)\)/gi,function(t,e,r){var n="abcdefghijklmnopqrstuvwxyz",a=r.trim().slice(-1);return"contains(translate("+e+", "+a+n.toUpperCase()+a+", "+a+n+a+"),"+r.toLowerCase()+")"})),t.toString()}),"undefined"==typeof JSON&&(window.JSON={parse:function(sJSON){return eval("("+sJSON+")")},stringify:function(t){if(t instanceof Object){var e="";if(t.constructor===Array){for(var r=0;r<t.length;e+=this.stringify(t[r])+",",r++);return"["+e.substr(0,e.length-1)+"]"}if(t.toString!==Object.prototype.toString)return'"'+t.toString().replace(/"/g,"\\$&")+'"';for(var n in t)e+='"'+n.replace(/"/g,"\\$&")+'":'+this.stringify(t[n])+",";return"{"+e.substr(0,e.length-1)+"}"}return"string"==typeof t?'"'+t.replace(/"/g,"\\$&")+'"':String(t)}}),JSON.toXML||(JSON.toXML=function(t,e){"use strict";var r={map:[],rx_validate_name:/^(?!xml)[a-z_][\w\d.:]*$/i,rx_node:/<(.+?)( .*?)>/,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.constructo