PageRenderTime 37ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs//1.3.3/defiant.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 856 lines | 628 code | 37 blank | 191 comment | 52 complexity | cc1fb6ea42978507fc10ce6fbb1a899e MD5 | raw file
  1. /*
  2. * Defiant.js v1.3.2
  3. * Search JSON structures plus smart templating with XSLT and XPath.
  4. * http://defiantjs.com
  5. *
  6. * Copyright (c) 2013-2015, Hakan Bilgin <hbi@longscript.com>
  7. * Licensed under the MIT License
  8. */
  9. (function(window, undefined) {
  10. //'use strict';
  11. var x10 = {
  12. init: function() {
  13. return this;
  14. },
  15. work_handler: function(event) {
  16. var args = Array.prototype.slice.call(event.data, 1),
  17. func = event.data[0],
  18. ret = tree[func].apply(tree, args);
  19. // return process finish
  20. postMessage([func, ret]);
  21. },
  22. setup: function(tree) {
  23. var url = window.URL || window.webkitURL,
  24. script = 'var tree = {'+ this.parse(tree).join(',') +'};',
  25. blob = new Blob([script + 'self.addEventListener("message", '+ this.work_handler.toString() +', false);'],
  26. {type: 'text/javascript'}),
  27. worker = new Worker(url.createObjectURL(blob));
  28. // thread pipe
  29. worker.onmessage = function(event) {
  30. var args = Array.prototype.slice.call(event.data, 1),
  31. func = event.data[0];
  32. x10.observer.emit('x10:'+ func, args);
  33. };
  34. return worker;
  35. },
  36. call_handler: function(func, worker) {
  37. return function() {
  38. var args = Array.prototype.slice.call(arguments, 0, -1),
  39. callback = arguments[arguments.length-1];
  40. // add method name
  41. args.unshift(func);
  42. // listen for 'done'
  43. x10.observer.on('x10:'+ func, function(event) {
  44. callback(event.detail[0]);
  45. });
  46. // start worker
  47. worker.postMessage(args);
  48. };
  49. },
  50. compile: function(hash) {
  51. var worker = this.setup(typeof(hash) === 'function' ? {func: hash} : hash),
  52. obj = {},
  53. fn;
  54. // create return object
  55. if (typeof(hash) === 'function') {
  56. obj.func = this.call_handler('func', worker);
  57. return obj.func;
  58. } else {
  59. for (fn in hash) {
  60. obj[fn] = this.call_handler(fn, worker);
  61. }
  62. return obj;
  63. }
  64. },
  65. parse: function(tree, isArray) {
  66. var hash = [],
  67. key,
  68. val,
  69. v;
  70. for (key in tree) {
  71. v = tree[key];
  72. // handle null
  73. if (v === null) {
  74. hash.push(key +':null');
  75. continue;
  76. }
  77. // handle undefined
  78. if (v === undefined) {
  79. hash.push(key +':undefined');
  80. continue;
  81. }
  82. switch (v.constructor) {
  83. case Date: val = 'new Date('+ v.valueOf() +')'; break;
  84. case Object: val = '{'+ this.parse(v).join(',') +'}'; break;
  85. case Array: val = '['+ this.parse(v, true).join(',') +']'; break;
  86. case String: val = '"'+ v.replace(/"/g, '\\"') +'"'; break;
  87. case RegExp:
  88. case Function: val = v.toString(); break;
  89. default: val = v;
  90. }
  91. if (isArray) hash.push(val);
  92. else hash.push(key +':'+ val);
  93. }
  94. return hash;
  95. },
  96. // simple event emitter
  97. observer: (function() {
  98. var stack = {};
  99. return {
  100. on: function(type, fn) {
  101. if (!stack[type]) {
  102. stack[type] = [];
  103. }
  104. stack[type].unshift(fn);
  105. },
  106. off: function(type, fn) {
  107. if (!stack[type]) return;
  108. var i = stack[type].indexOf(fn);
  109. stack[type].splice(i,1);
  110. },
  111. emit: function(type, detail) {
  112. if (!stack[type]) return;
  113. var event = {
  114. type : type,
  115. detail : detail,
  116. isCanceled : false,
  117. cancelBubble : function() {
  118. this.isCanceled = true;
  119. }
  120. },
  121. len = stack[type].length;
  122. while(len--) {
  123. if (event.isCanceled) return;
  124. stack[type][len](event);
  125. }
  126. }
  127. };
  128. })()
  129. };
  130. if (typeof module === "undefined") {
  131. // publish x10
  132. window.x10 = x10.init();
  133. } else {
  134. module.exports = x10.init();
  135. }
  136. })(this);
  137. if (typeof module === "undefined") {
  138. var module = { exports: undefined };
  139. } else {
  140. // Node env adaptation goes here...
  141. }
  142. module.exports = Defiant = (function(window, undefined) {
  143. 'use strict';
  144. var Defiant = {
  145. is_ie : /(msie|trident)/i.test(navigator.userAgent),
  146. is_safari : /safari/i.test(navigator.userAgent),
  147. env : 'production',
  148. xml_decl : '<?xml version="1.0" encoding="utf-8"?>',
  149. namespace : 'xmlns:d="defiant-namespace"',
  150. tabsize : 4,
  151. render: function(template, data) {
  152. var processor = new XSLTProcessor(),
  153. span = document.createElement('span'),
  154. opt = {match: '/'},
  155. tmpltXpath,
  156. scripts,
  157. temp,
  158. sorter;
  159. // handle arguments
  160. switch (typeof(template)) {
  161. case 'object':
  162. this.extend(opt, template);
  163. if (!opt.data) opt.data = data;
  164. break;
  165. case 'string':
  166. opt.template = template;
  167. opt.data = data;
  168. break;
  169. default:
  170. throw 'error';
  171. }
  172. opt.data = JSON.toXML(opt.data);
  173. tmpltXpath = '//xsl:template[@name="'+ opt.template +'"]';
  174. if (!this.xsl_template) this.gatherTemplates();
  175. if (opt.sorter) {
  176. sorter = this.node.selectSingleNode(this.xsl_template, tmpltXpath +'//xsl:for-each//xsl:sort');
  177. if (sorter) {
  178. if (opt.sorter.order) sorter.setAttribute('order', opt.sorter.order);
  179. if (opt.sorter.select) sorter.setAttribute('select', opt.sorter.select);
  180. sorter.setAttribute('data-type', opt.sorter.type || 'text');
  181. }
  182. }
  183. temp = this.node.selectSingleNode(this.xsl_template, tmpltXpath);
  184. temp.setAttribute('match', opt.match);
  185. processor.importStylesheet(this.xsl_template);
  186. span.appendChild(processor.transformToFragment(opt.data, document));
  187. temp.removeAttribute('match');
  188. if (this.is_safari) {
  189. scripts = span.getElementsByTagName('script');
  190. for (var i=0, il=scripts.length; i<il; i++) scripts[i].defer = true;
  191. }
  192. return span.innerHTML;
  193. },
  194. gatherTemplates: function() {
  195. var scripts = document.getElementsByTagName('script'),
  196. str = '',
  197. i = 0,
  198. il = scripts.length;
  199. for (; i<il; i++) {
  200. if (scripts[i].type === 'defiant/xsl-template') str += scripts[i].innerHTML;
  201. }
  202. this.xsl_template = this.xmlFromString('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" '+ this.namespace +'>'+ str.replace(/defiant:(\w+)/g, '$1') +'</xsl:stylesheet>');
  203. },
  204. getSnapshot: function(data, callback) {
  205. return JSON.toXML(data, callback || true);
  206. },
  207. xmlFromString: function(str) {
  208. var parser,
  209. doc;
  210. str = str.replace(/>\s{1,}</g, '><');
  211. if (str.trim().match(/<\?xml/) === null) {
  212. str = this.xml_decl + str;
  213. }
  214. if (this.is_ie) {
  215. doc = new ActiveXObject('Msxml2.DOMDocument');
  216. doc.loadXML(str);
  217. if (str.indexOf('xsl:stylesheet') === -1) {
  218. doc.setProperty('SelectionLanguage', 'XPath');
  219. }
  220. } else {
  221. parser = new DOMParser();
  222. doc = parser.parseFromString(str, 'text/xml');
  223. }
  224. return doc;
  225. },
  226. extend: function(src, dest) {
  227. for (var content in dest) {
  228. if (!src[content] || typeof(dest[content]) !== 'object') {
  229. src[content] = dest[content];
  230. } else {
  231. this.extend(src[content], dest[content]);
  232. }
  233. }
  234. return src;
  235. },
  236. node: {}
  237. };
  238. return Defiant;
  239. })(this);
  240. if (typeof(XSLTProcessor) === 'undefined') {
  241. // emulating XSLT Processor (enough to be used in defiant)
  242. var XSLTProcessor = function() {};
  243. XSLTProcessor.prototype = {
  244. importStylesheet: function(xsldoc) {
  245. this.xsldoc = xsldoc;
  246. },
  247. transformToFragment: function(data, doc) {
  248. var str = data.transformNode(this.xsldoc),
  249. span = document.createElement('span');
  250. span.innerHTML = str;
  251. return span;
  252. }
  253. };
  254. } else if (typeof(XSLTProcessor) !== 'function') {
  255. // throw error
  256. throw 'XSLTProcessor transformNode not implemented';
  257. }
  258. // extending STRING
  259. if (!String.prototype.fill) {
  260. String.prototype.fill = function(i,c) {
  261. var str = this;
  262. c = c || ' ';
  263. for (; str.length<i; str+=c){}
  264. return str;
  265. };
  266. }
  267. if (!String.prototype.trim) {
  268. String.prototype.trim = function () {
  269. return this.replace(/^\s+|\s+$/gm, '');
  270. };
  271. }
  272. if (!String.prototype.xTransform) {
  273. String.prototype.xTransform = function () {
  274. var str = this;
  275. if (this.indexOf('translate(') === -1) {
  276. str = this.replace(/contains\(([^,]+),([^\\)]+)\)/g, function(c,h,n) {
  277. var a = 'abcdefghijklmnopqrstuvwxyz',
  278. q = n.trim().slice(-1);
  279. return "contains(translate("+ h +", "+ q + a.toUpperCase() + q +", "+ q + a + q +"),"+ n.toLowerCase() +")";
  280. });
  281. }
  282. return str.toString();
  283. };
  284. }
  285. if (typeof(JSON) === 'undefined') {
  286. window.JSON = {
  287. parse: function (sJSON) { return eval("(" + sJSON + ")"); },
  288. stringify: function (vContent) {
  289. if (vContent instanceof Object) {
  290. var sOutput = "";
  291. if (vContent.constructor === Array) {
  292. for (var nId = 0; nId < vContent.length; sOutput += this.stringify(vContent[nId]) + ",", nId++);
  293. return "[" + sOutput.substr(0, sOutput.length - 1) + "]";
  294. }
  295. if (vContent.toString !== Object.prototype.toString) {
  296. return "\"" + vContent.toString().replace(/"/g, "\\$&") + "\"";
  297. }
  298. for (var sProp in vContent) {
  299. sOutput += "\"" + sProp.replace(/"/g, "\\$&") + "\":" + this.stringify(vContent[sProp]) + ",";
  300. }
  301. return "{" + sOutput.substr(0, sOutput.length - 1) + "}";
  302. }
  303. return typeof vContent === "string" ? "\"" + vContent.replace(/"/g, "\\$&") + "\"" : String(vContent);
  304. }
  305. };
  306. }
  307. /* jshint ignore:end */
  308. if (!JSON.toXML) {
  309. JSON.toXML = function(tree, callback) {
  310. 'use strict';
  311. var interpreter = {
  312. map : [],
  313. rx_validate_name : /^(?!xml)[a-z_][\w\d.:]*$/i,
  314. rx_node : /<(.+?)( .*?)>/,
  315. rx_constructor : /<(.+?)( d:contr=".*?")>/,
  316. rx_namespace : / xmlns\:d="defiant\-namespace"/,
  317. rx_data : /(<.+?>)(.*?)(<\/d:data>)/i,
  318. rx_function : /function (\w+)/i,
  319. namespace : 'xmlns:d="defiant-namespace"',
  320. to_xml_str: function(tree) {
  321. return {
  322. str: this.hash_to_xml(null, tree),
  323. map: this.map
  324. };
  325. },
  326. hash_to_xml: function(name, tree, array_child) {
  327. var is_array = tree.constructor === Array,
  328. elem = [],
  329. attr = [],
  330. key,
  331. val,
  332. val_is_array,
  333. type,
  334. is_attr,
  335. cname,
  336. constr,
  337. cnName,
  338. i;
  339. for (key in tree) {
  340. val = tree[key];
  341. if (val === null || val === undefined || val.toString() === 'NaN') val = null;
  342. is_attr = key.slice(0,1) === '@';
  343. cname = array_child ? name : key;
  344. if (cname == +cname && tree.constructor !== Object) cname = 'd:item';
  345. if (val === null) {
  346. constr = null;
  347. cnName = false;
  348. } else {
  349. constr = val.constructor;
  350. cnName = constr.toString().match(this.rx_function)[1];
  351. }
  352. if (is_attr) {
  353. attr.push( cname.slice(1) +'="'+ this.escape_xml(val) +'"' );
  354. if (cnName !== 'String') attr.push( 'd:'+ cname.slice(1) +'="'+ cnName +'"' );
  355. } else if (val === null) {
  356. elem.push( this.scalar_to_xml( cname, val ) );
  357. } else {
  358. switch (constr) {
  359. case Function:
  360. // if constructor is function, then it's not a JSON structure
  361. throw 'JSON data should not contain functions. Please check jour structure.';
  362. /* falls through */
  363. case Object:
  364. elem.push( this.hash_to_xml( cname, val ) );
  365. break;
  366. case Array:
  367. if (key === cname) {
  368. val_is_array = val.constructor === Array;
  369. if (val_is_array) {
  370. i = val.length;
  371. while (i--) {
  372. if (val[i].constructor === Array) val_is_array = true;
  373. if (!val_is_array && val[i].constructor === Object) val_is_array = true;
  374. }
  375. }
  376. elem.push( this.scalar_to_xml( cname, val, val_is_array ) );
  377. break;
  378. }
  379. /* falls through */
  380. case String:
  381. if (typeof(val) === 'string') {
  382. val = val.toString().replace(/\&/g, '&amp;')
  383. .replace(/\r|\n/g, '&#13;');
  384. }
  385. if (cname === '#text') {
  386. // prepare map
  387. this.map.push(tree);
  388. attr.push('d:mi="'+ this.map.length +'"');
  389. attr.push('d:constr="'+ cnName +'"');
  390. elem.push( this.escape_xml(val) );
  391. break;
  392. }
  393. /* falls through */
  394. case Number:
  395. case Boolean:
  396. if (cname === '#text' && cnName !== 'String') {
  397. // prepare map
  398. this.map.push(tree);
  399. attr.push('d:mi="'+ this.map.length +'"');
  400. attr.push('d:constr="'+ cnName +'"');
  401. elem.push( this.escape_xml(val) );
  402. break;
  403. }
  404. elem.push( this.scalar_to_xml( cname, val ) );
  405. break;
  406. }
  407. }
  408. }
  409. if (!name) {
  410. name = 'd:data';
  411. attr.push(this.namespace);
  412. if (is_array) attr.push('d:constr="Array"');
  413. }
  414. if (name.match(this.rx_validate_name) === null) {
  415. attr.push( 'd:name="'+ name +'"' );
  416. name = 'd:name';
  417. }
  418. if (array_child) return elem.join('');
  419. // prepare map
  420. this.map.push(tree);
  421. attr.push('d:mi="'+ this.map.length +'"');
  422. return '<'+ name + (attr.length ? ' '+ attr.join(' ') : '') + (elem.length ? '>'+ elem.join('') +'</'+ name +'>' : '/>' );
  423. },
  424. scalar_to_xml: function(name, val, override) {
  425. var attr = '',
  426. text,
  427. constr,
  428. cnName;
  429. // check whether the nodename is valid
  430. if (name.match(this.rx_validate_name) === null) {
  431. attr += ' d:name="'+ name +'"';
  432. name = 'd:name';
  433. override = false;
  434. }
  435. if (val === null || val.toString() === 'NaN') val = null;
  436. if (val === null) return '<'+ name +' d:constr="null"/>';
  437. if (val.length === 1 && val[0].constructor === Object) {
  438. text = this.hash_to_xml(false, val[0]);
  439. var a1 = text.match(this.rx_node),
  440. a2 = text.match(this.rx_constructor);
  441. a1 = (a1 !== null)? a1[2]
  442. .replace(this.rx_namespace, '')
  443. .replace(/>/, '')
  444. .replace(/"\/$/, '"') : '';
  445. a2 = (a2 !== null)? a2[2] : '';
  446. text = text.match(this.rx_data);
  447. text = (text !== null)? text[2] : '';
  448. return '<'+ name + a1 +' '+ a2 +' d:type="ArrayItem">'+ text +'</'+ name +'>';
  449. } else if (val.length === 0 && val.constructor === Array) {
  450. return '<'+ name +' d:constr="Array"/>';
  451. }
  452. // else
  453. if (override) {
  454. return this.hash_to_xml( name, val, true );
  455. }
  456. constr = val.constructor;
  457. cnName = constr.toString().match(this.rx_function)[1];
  458. text = (constr === Array) ? this.hash_to_xml( 'd:item', val, true )
  459. : this.escape_xml(val);
  460. attr += ' d:constr="'+ cnName +'"';
  461. // prepare map
  462. this.map.push(val);
  463. attr += ' d:mi="'+ this.map.length +'"';
  464. return (name === '#text') ? this.escape_xml(val) : '<'+ name + attr +'>'+ text +'</'+ name +'>';
  465. },
  466. escape_xml: function(text) {
  467. return String(text) .replace(/</g, '&lt;')
  468. .replace(/>/g, '&gt;')
  469. .replace(/"/g, '&quot;')
  470. .replace(/&nbsp;/g, '&#160;');
  471. }
  472. },
  473. processed,
  474. doc,
  475. task;
  476. // depending on request
  477. switch (typeof callback) {
  478. case 'function':
  479. // compile interpreter with 'x10.js'
  480. task = x10.compile(interpreter);
  481. // parse in a dedicated thread
  482. task.to_xml_str(tree, function(processed) {
  483. // snapshot distinctly improves performance
  484. callback({
  485. doc: Defiant.xmlFromString(processed.str),
  486. src: tree,
  487. map: processed.map
  488. });
  489. });
  490. return;
  491. case 'boolean':
  492. processed = interpreter.to_xml_str.call(interpreter, tree);
  493. // return snapshot
  494. return {
  495. doc: Defiant.xmlFromString(processed.str),
  496. src: tree,
  497. map: processed.map
  498. };
  499. default:
  500. processed = interpreter.to_xml_str.call(interpreter, tree);
  501. doc = Defiant.xmlFromString(processed.str);
  502. this.search.map = processed.map;
  503. return doc;
  504. }
  505. };
  506. }
  507. if (!JSON.search) {
  508. JSON.search = function(tree, xpath, single) {
  509. 'use strict';
  510. var isSnapshot = tree.doc && tree.doc.nodeType,
  511. doc = isSnapshot ? tree.doc : JSON.toXML(tree),
  512. map = isSnapshot ? tree.map : this.search.map,
  513. src = isSnapshot ? tree.src : tree,
  514. xres = Defiant.node[ single ? 'selectSingleNode' : 'selectNodes' ](doc, xpath.xTransform()),
  515. ret = [],
  516. mapIndex,
  517. i;
  518. if (single) xres = [xres];
  519. i = xres.length;
  520. while (i--) {
  521. switch(xres[i].nodeType) {
  522. case 2:
  523. case 3:
  524. ret.unshift( xres[i].nodeValue );
  525. break;
  526. default:
  527. mapIndex = +xres[i].getAttribute('d:mi');
  528. //if (map[mapIndex-1] !== false) {
  529. ret.unshift( map[mapIndex-1] );
  530. //}
  531. }
  532. }
  533. // if environment = development, add search tracing
  534. if (Defiant.env === 'development') {
  535. this.trace = JSON.mtrace(src, ret, xres);
  536. }
  537. return ret;
  538. };
  539. }
  540. if (!JSON.mtrace) {
  541. JSON.mtrace = function(root, hits, xres) {
  542. 'use strict';
  543. var win = window,
  544. stringify = JSON.stringify,
  545. sroot = stringify( root, null, '\t' ).replace(/\t/g, ''),
  546. trace = [],
  547. i = 0,
  548. il = xres.length,
  549. od = il ? xres[i].ownerDocument.documentElement : false,
  550. map = this.search.map,
  551. hstr,
  552. cConstr,
  553. fIndex = 0,
  554. mIndex,
  555. lStart,
  556. lEnd;
  557. for (; i<il; i++) {
  558. switch (xres[i].nodeType) {
  559. case 2:
  560. cConstr = xres[i].ownerElement ? xres[i].ownerElement.getAttribute('d:'+ xres[i].nodeName) : 'String';
  561. hstr = '"@'+ xres[i].nodeName +'": '+ win[ cConstr ]( hits[i] );
  562. mIndex = sroot.indexOf(hstr);
  563. lEnd = 0;
  564. break;
  565. case 3:
  566. cConstr = xres[i].parentNode.getAttribute('d:constr');
  567. hstr = win[ cConstr ]( hits[i] );
  568. hstr = '"'+ xres[i].parentNode.nodeName +'": '+ (hstr === 'Number' ? hstr : '"'+ hstr +'"');
  569. mIndex = sroot.indexOf(hstr);
  570. lEnd = 0;
  571. break;
  572. default:
  573. if (xres[i] === od) continue;
  574. if (xres[i].getAttribute('d:constr') === 'String' || xres[i].getAttribute('d:constr') === 'Number') {
  575. cConstr = xres[i].getAttribute('d:constr');
  576. hstr = win[ cConstr ]( hits[i] );
  577. mIndex = sroot.indexOf(hstr, fIndex);
  578. hstr = '"'+ xres[i].nodeName +'": '+ (cConstr === 'Number' ? hstr : '"'+ hstr +'"');
  579. lEnd = 0;
  580. fIndex = mIndex + 1;
  581. } else {
  582. hstr = stringify( hits[i], null, '\t' ).replace(/\t/g, '');
  583. mIndex = sroot.indexOf(hstr);
  584. lEnd = hstr.match(/\n/g).length;
  585. }
  586. }
  587. lStart = sroot.substring(0,mIndex).match(/\n/g).length+1;
  588. trace.push([lStart, lEnd]);
  589. }
  590. return trace;
  591. };
  592. }
  593. Defiant.node.selectNodes = function(XNode, XPath) {
  594. if (XNode.evaluate) {
  595. var ns = XNode.createNSResolver(XNode.documentElement),
  596. qI = XNode.evaluate(XPath, XNode, ns, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null),
  597. res = [],
  598. i = 0,
  599. il = qI.snapshotLength;
  600. for (; i<il; i++) {
  601. res.push( qI.snapshotItem(i) );
  602. }
  603. return res;
  604. } else {
  605. return XNode.selectNodes(XPath);
  606. }
  607. };
  608. Defiant.node.selectSingleNode = function(XNode, XPath) {
  609. if (XNode.evaluate) {
  610. var xI = this.selectNodes(XNode, XPath);
  611. return (xI.length > 0)? xI[0] : null;
  612. } else {
  613. return XNode.selectSingleNode(XPath);
  614. }
  615. };
  616. Defiant.node.prettyPrint = function(node) {
  617. var root = Defiant,
  618. tabs = root.tabsize,
  619. decl = root.xml_decl.toLowerCase(),
  620. ser,
  621. xstr;
  622. if (root.is_ie) {
  623. xstr = node.xml;
  624. } else {
  625. ser = new XMLSerializer();
  626. xstr = ser.serializeToString(node);
  627. }
  628. if (root.env !== 'development') {
  629. // if environment is not development, remove defiant related info
  630. xstr = xstr.replace(/ \w+\:d=".*?"| d\:\w+=".*?"/g, '');
  631. }
  632. var str = xstr.trim().replace(/(>)\s*(<)(\/*)/g, '$1\n$2$3'),
  633. lines = str.split('\n'),
  634. indent = -1,
  635. i = 0,
  636. il = lines.length,
  637. start,
  638. end;
  639. for (; i<il; i++) {
  640. if (i === 0 && lines[i].toLowerCase() === decl) continue;
  641. start = lines[i].match(/<[A-Za-z_\:]+.*?>/g) !== null;
  642. //start = lines[i].match(/<[^\/]+>/g) !== null;
  643. end = lines[i].match(/<\/[\w\:]+>/g) !== null;
  644. if (lines[i].match(/<.*?\/>/g) !== null) start = end = true;
  645. if (start) indent++;
  646. lines[i] = String().fill(indent, '\t') + lines[i];
  647. if (start && end) indent--;
  648. if (!start && end) indent--;
  649. }
  650. return lines.join('\n').replace(/\t/g, String().fill(tabs, ' '));
  651. };
  652. Defiant.node.toJSON = function(xnode, stringify) {
  653. 'use strict';
  654. var interpret = function(leaf) {
  655. var obj = {},
  656. win = window,
  657. attr,
  658. type,
  659. item,
  660. cname,
  661. cConstr,
  662. cval,
  663. text,
  664. i, il, a;
  665. switch (leaf.nodeType) {
  666. case 1:
  667. cConstr = leaf.getAttribute('d:constr');
  668. if (cConstr === 'Array') obj = [];
  669. else if (cConstr === 'String' && leaf.textContent === '') obj = '';
  670. attr = leaf.attributes;
  671. i = 0;
  672. il = attr.length;
  673. for (; i<il; i++) {
  674. a = attr.item(i);
  675. if (a.nodeName.match(/\:d|d\:/g) !== null) continue;
  676. cConstr = leaf.getAttribute('d:'+ a.nodeName);
  677. if (cConstr && cConstr !== 'undefined') {
  678. if (a.nodeValue === 'null') cval = null;
  679. else cval = win[ cConstr ]( (a.nodeValue === 'false') ? '' : a.nodeValue );
  680. } else {
  681. cval = a.nodeValue;
  682. }
  683. obj['@'+ a.nodeName] = cval;
  684. }
  685. break;
  686. case 3:
  687. type = leaf.parentNode.getAttribute('d:type');
  688. cval = (type) ? win[ type ]( leaf.nodeValue === 'false' ? '' : leaf.nodeValue ) : leaf.nodeValue;
  689. obj = cval;
  690. break;
  691. }
  692. if (leaf.hasChildNodes()) {
  693. i = 0;
  694. il = leaf.childNodes.length;
  695. for(; i<il; i++) {
  696. item = leaf.childNodes.item(i);
  697. cname = item.nodeName;
  698. attr = leaf.attributes;
  699. if (cname === 'd:name') {
  700. cname = item.getAttribute('d:name');
  701. }
  702. if (cname === '#text') {
  703. cConstr = leaf.getAttribute('d:constr');
  704. if (cConstr === 'undefined') cConstr = undefined;
  705. text = item.textContent || item.text;
  706. cval = cConstr === 'Boolean' && text === 'false' ? '' : text;
  707. if (!cConstr && !attr.length) obj = cval;
  708. else if (cConstr && il === 1) {
  709. obj = win[cConstr](cval);
  710. } else if (!leaf.hasChildNodes()) {
  711. obj[cname] = (cConstr)? win[cConstr](cval) : cval;
  712. } else {
  713. if (attr.length < 3) obj = (cConstr)? win[cConstr](cval) : cval;
  714. else obj[cname] = (cConstr)? win[cConstr](cval) : cval;
  715. }
  716. } else {
  717. if (obj[cname]) {
  718. if (obj[cname].push) obj[cname].push( interpret(item) );
  719. else obj[cname] = [obj[cname], interpret(item)];
  720. continue;
  721. }
  722. cConstr = item.getAttribute('d:constr');
  723. switch (cConstr) {
  724. case 'null':
  725. if (obj.push) obj.push(null);
  726. else obj[cname] = null;
  727. break;
  728. case 'Array':
  729. //console.log( Defiant.node.prettyPrint(item) );
  730. if (item.parentNode.firstChild === item && cConstr === 'Array' && cname !== 'd:item') {
  731. if (cname === 'd:item' || cConstr === 'Array') {
  732. cval = interpret(item);
  733. obj[cname] = cval.length ? [cval] : cval;
  734. } else {
  735. obj[cname] = interpret(item);
  736. }
  737. }
  738. else if (obj.push) obj.push( interpret(item) );
  739. else obj[cname] = interpret(item);
  740. break;
  741. case 'String':
  742. case 'Number':
  743. case 'Boolean':
  744. text = item.textContent || item.text;
  745. cval = cConstr === 'Boolean' && text === 'false' ? '' : text;
  746. if (obj.push) obj.push( win[cConstr](cval) );
  747. else obj[cname] = interpret(item);
  748. break;
  749. default:
  750. if (obj.push) obj.push( interpret( item ) );
  751. else obj[cname] = interpret( item );
  752. }
  753. }
  754. }
  755. }
  756. if (leaf.nodeType === 1 && leaf.getAttribute('d:type') === 'ArrayItem') {
  757. obj = [obj];
  758. }
  759. return obj;
  760. },
  761. node = (xnode.nodeType === 9) ? xnode.documentElement : xnode,
  762. ret = interpret(node),
  763. rn = ret[node.nodeName];
  764. // exclude root, if "this" is root node
  765. if (node === node.ownerDocument.documentElement && rn && rn.constructor === Array) {
  766. ret = rn;
  767. }
  768. if (stringify && stringify.toString() === 'true') stringify = '\t';
  769. return stringify ? JSON.stringify(ret, null, stringify) : ret;
  770. };
  771. // check if jQuery is present
  772. if (typeof(jQuery) !== 'undefined') {
  773. (function ( $ ) {
  774. 'use strict';
  775. $.fn.defiant = function(template, xpath) {
  776. this.html( Defiant.render(template, xpath) );
  777. return this;
  778. };
  779. }(jQuery));
  780. }