From d8675a9557d875aea6afcd3b647123d0f76398f2 Mon Sep 17 00:00:00 2001 From: StefanDimov Date: Mon, 16 Oct 2017 19:41:19 +0300 Subject: [PATCH] Removed declaration of `module` object while exporting --- src/defiant.js | 213 +++++++++++++++++++++++++------------------------ 1 file changed, 109 insertions(+), 104 deletions(-) diff --git a/src/defiant.js b/src/defiant.js index 1153a4b..2404328 100755 --- a/src/defiant.js +++ b/src/defiant.js @@ -1,111 +1,116 @@ - -(function(window, module, undefined) { - 'use strict'; - - var Defiant = { - is_ie : /(msie|trident)/i.test(navigator.userAgent), - is_safari : /safari/i.test(navigator.userAgent), - env : 'production', - xml_decl : '', - namespace : 'xmlns:d="defiant-namespace"', - tabsize : 4, - render: function(template, data) { - var processor = new XSLTProcessor(), - span = document.createElement('span'), - opt = {match: '/'}, - tmpltXpath, - scripts, - temp, - sorter; - // handle arguments - switch (typeof(template)) { - case 'object': - this.extend(opt, template); - if (!opt.data) opt.data = data; - break; - case 'string': - opt.template = template; - opt.data = data; - break; - default: - throw 'error'; - } - opt.data = JSON.toXML(opt.data); - tmpltXpath = '//xsl:template[@name="'+ opt.template +'"]'; - - if (!this.xsl_template) this.gatherTemplates(); - - if (opt.sorter) { - sorter = this.node.selectSingleNode(this.xsl_template, tmpltXpath +'//xsl:for-each//xsl:sort'); - if (sorter) { - if (opt.sorter.order) sorter.setAttribute('order', opt.sorter.order); - if (opt.sorter.select) sorter.setAttribute('select', opt.sorter.select); - sorter.setAttribute('data-type', opt.sorter.type || 'text'); +(function () { + var Defiant = (function() { + 'use strict'; + + var Defiant = { + is_ie : /(msie|trident)/i.test(navigator.userAgent), + is_safari : /safari/i.test(navigator.userAgent), + env : 'production', + xml_decl : '', + namespace : 'xmlns:d="defiant-namespace"', + tabsize : 4, + render: function(template, data) { + var processor = new XSLTProcessor(), + span = document.createElement('span'), + opt = {match: '/'}, + tmpltXpath, + scripts, + temp, + sorter; + // handle arguments + switch (typeof(template)) { + case 'object': + this.extend(opt, template); + if (!opt.data) opt.data = data; + break; + case 'string': + opt.template = template; + opt.data = data; + break; + default: + throw 'error'; } - } - - temp = this.node.selectSingleNode(this.xsl_template, tmpltXpath); - temp.setAttribute('match', opt.match); - processor.importStylesheet(this.xsl_template); - span.appendChild(processor.transformToFragment(opt.data, document)); - temp.removeAttribute('match'); - - if (this.is_safari) { - scripts = span.getElementsByTagName('script'); - for (var i=0, il=scripts.length; i'+ str.replace(/defiant:(\w+)/g, '$1') +''); - }, - getSnapshot: function(data, callback) { - return JSON.toXML(data, callback || true); - }, - xmlFromString: function(str) { - var parser, - doc; - str = str.replace(/>\s{1,}<'); - if (str.trim().match(/<\?xml/) === null) { - str = this.xml_decl + str; - } - if ( 'ActiveXObject' in window ) { - doc = new ActiveXObject('Msxml2.DOMDocument'); - doc.loadXML(str); - doc.setProperty('SelectionNamespaces', this.namespace); - if (str.indexOf('xsl:stylesheet') === -1) { - doc.setProperty('SelectionLanguage', 'XPath'); + opt.data = JSON.toXML(opt.data); + tmpltXpath = '//xsl:template[@name="'+ opt.template +'"]'; + + if (!this.xsl_template) this.gatherTemplates(); + + if (opt.sorter) { + sorter = this.node.selectSingleNode(this.xsl_template, tmpltXpath +'//xsl:for-each//xsl:sort'); + if (sorter) { + if (opt.sorter.order) sorter.setAttribute('order', opt.sorter.order); + if (opt.sorter.select) sorter.setAttribute('select', opt.sorter.select); + sorter.setAttribute('data-type', opt.sorter.type || 'text'); + } + } + + temp = this.node.selectSingleNode(this.xsl_template, tmpltXpath); + temp.setAttribute('match', opt.match); + processor.importStylesheet(this.xsl_template); + span.appendChild(processor.transformToFragment(opt.data, document)); + temp.removeAttribute('match'); + + if (this.is_safari) { + scripts = span.getElementsByTagName('script'); + for (var i=0, il=scripts.length; i'+ str.replace(/defiant:(\w+)/g, '$1') +''); + }, + getSnapshot: function(data, callback) { + return JSON.toXML(data, callback || true); + }, + xmlFromString: function(str) { + var parser, + doc; + str = str.replace(/>\s{1,}<'); + if (str.trim().match(/<\?xml/) === null) { + str = this.xml_decl + str; + } + if ( 'ActiveXObject' in window ) { + doc = new ActiveXObject('Msxml2.DOMDocument'); + doc.loadXML(str); + doc.setProperty('SelectionNamespaces', this.namespace); + if (str.indexOf('xsl:stylesheet') === -1) { + doc.setProperty('SelectionLanguage', 'XPath'); + } } else { - this.extend(src[content], dest[content]); + parser = new DOMParser(); + doc = parser.parseFromString(str, 'text/xml'); + } + return doc; + }, + extend: function(src, dest) { + for (var content in dest) { + if (!src[content] || typeof(dest[content]) !== 'object') { + src[content] = dest[content]; + } else { + this.extend(src[content], dest[content]); + } } - } - return src; - }, - node: {} - }; + return src; + }, + node: {} + }; + return Defiant; + })(); // Export - window.Defiant = module.exports = Defiant; + var isInBrowser = typeof window !== 'undefined' + var isInNode = typeof module !== 'undefined' -})( - typeof window !== 'undefined' ? window : {}, - typeof module !== 'undefined' ? module : {} -); + if (isInBrowser) { + window.Defiant = Defiant; + } else if (isInNode) { + module.exports = Defiant; + } +})();