PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/ajax/libs//1.3.1/defiant.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 851 lines | 626 code | 35 blank | 190 comment | 50 complexity | 4e9555344a5ec7bb1359bd5bb2fe7092 MD5 | raw file
  1. /*
  2. * Defiant.js v1.3.1
  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. }
  255. // extending STRING
  256. if (!String.prototype.fill) {
  257. String.prototype.fill = function(i,c) {
  258. var str = this;
  259. c = c || ' ';
  260. for (; str.length<i; str+=c){}
  261. return str;
  262. };
  263. }
  264. if (!String.prototype.trim) {
  265. String.prototype.trim = function () {
  266. return this.replace(/^\s+|\s+$/gm, '');
  267. };
  268. }
  269. if (!String.prototype.xTransform) {
  270. String.prototype.xTransform = function () {
  271. var str = this;
  272. if (this.indexOf('translate(') === -1) {
  273. str = this.replace(/contains\(([^,]+),([^\\)]+)\)/g, function(c,h,n) {
  274. var a = 'abcdefghijklmnopqrstuvwxyz',
  275. q = n.trim().slice(-1);
  276. return "contains(translate("+ h +", "+ q + a.toUpperCase() + q +", "+ q + a + q +"),"+ n.toLowerCase() +")";
  277. });
  278. }
  279. return str.toString();
  280. };
  281. }
  282. if (typeof(JSON) === 'undefined') {
  283. window.JSON = {
  284. parse: function (sJSON) { return eval("(" + sJSON + ")"); },
  285. stringify: function (vContent) {
  286. if (vContent instanceof Object) {
  287. var sOutput = "";
  288. if (vContent.constructor === Array) {
  289. for (var nId = 0; nId < vContent.length; sOutput += this.stringify(vContent[nId]) + ",", nId++);
  290. return "[" + sOutput.substr(0, sOutput.length - 1) + "]";
  291. }
  292. if (vContent.toString !== Object.prototype.toString) {
  293. return "\"" + vContent.toString().replace(/"/g, "\\$&") + "\"";
  294. }
  295. for (var sProp in vContent) {
  296. sOutput += "\"" + sProp.replace(/"/g, "\\$&") + "\":" + this.stringify(vContent[sProp]) + ",";
  297. }
  298. return "{" + sOutput.substr(0, sOutput.length - 1) + "}";
  299. }
  300. return typeof vContent === "string" ? "\"" + vContent.replace(/"/g, "\\$&") + "\"" : String(vContent);
  301. }
  302. };
  303. }
  304. /* jshint ignore:end */
  305. if (!JSON.toXML) {
  306. JSON.toXML = function(tree, callback) {
  307. 'use strict';
  308. var interpreter = {
  309. map : [],
  310. rx_validate_name : /^(?!xml)[a-z_][\w\d.:]*$/i,
  311. rx_node : /<(.+?)( .*?)>/,
  312. rx_constructor : /<(.+?)( d:contr=".*?")>/,
  313. rx_namespace : / xmlns\:d="defiant\-namespace"/,
  314. rx_data : /(<.+?>)(.*?)(<\/d:data>)/i,
  315. rx_function : /function (\w+)/i,
  316. namespace : 'xmlns:d="defiant-namespace"',
  317. to_xml_str: function(tree) {
  318. return {
  319. str: this.hash_to_xml(null, tree),
  320. map: this.map
  321. };
  322. },
  323. hash_to_xml: function(name, tree, array_child) {
  324. var is_array = tree.constructor === Array,
  325. elem = [],
  326. attr = [],
  327. key,
  328. val,
  329. val_is_array,
  330. type,
  331. is_attr,
  332. cname,
  333. constr,
  334. cnName,
  335. i;
  336. for (key in tree) {
  337. val = tree[key];
  338. if (val === null || val === undefined || val.toString() === 'NaN') val = null;
  339. is_attr = key.slice(0,1) === '@';
  340. cname = array_child ? name : key;
  341. if (cname == +cname && tree.constructor !== Object) cname = 'd:item';
  342. if (val === null) {
  343. constr = null;
  344. cnName = false;
  345. } else {
  346. constr = val.constructor;
  347. cnName = constr.toString().match(this.rx_function)[1];
  348. }
  349. if (is_attr) {
  350. attr.push( cname.slice(1) +'="'+ this.escape_xml(val) +'"' );
  351. if (cnName !== 'String') attr.push( 'd:'+ cname.slice(1) +'="'+ cnName +'"' );
  352. } else if (val === null) {
  353. elem.push( this.scalar_to_xml( cname, val ) );
  354. } else {
  355. switch (constr) {
  356. case Function:
  357. // if constructor is function, then it's not a JSON structure
  358. throw 'JSON data should not contain functions. Please check jour structure.';
  359. /* falls through */
  360. case Object:
  361. elem.push( this.hash_to_xml( cname, val ) );
  362. break;
  363. case Array:
  364. if (key === cname) {
  365. val_is_array = val.constructor === Array;
  366. if (val_is_array) {
  367. i = val.length;
  368. while (i--) {
  369. if (val[i].constructor === Array) val_is_array = true;
  370. if (!val_is_array && val[i].constructor === Object) val_is_array = true;
  371. }
  372. }
  373. elem.push( this.scalar_to_xml( cname, val, val_is_array ) );
  374. break;
  375. }
  376. /* falls through */
  377. case String:
  378. if (typeof(val) === 'string') {
  379. val = val.toString().replace(/\&/g, '&amp;')
  380. .replace(/\r|\n/g, '&#13;');
  381. }
  382. if (cname === '#text') {
  383. // prepare map
  384. this.map.push(tree);
  385. attr.push('d:mi="'+ this.map.length +'"');
  386. attr.push('d:constr="'+ cnName +'"');
  387. elem.push( this.escape_xml(val) );
  388. break;
  389. }
  390. /* falls through */
  391. case Number:
  392. case Boolean:
  393. if (cname === '#text' && cnName !== 'String') {
  394. // prepare map
  395. this.map.push(tree);
  396. attr.push('d:mi="'+ this.map.length +'"');
  397. attr.push('d:constr="'+ cnName +'"');
  398. elem.push( this.escape_xml(val) );
  399. break;
  400. }
  401. elem.push( this.scalar_to_xml( cname, val ) );
  402. break;
  403. }
  404. }
  405. }
  406. if (!name) {
  407. name = 'd:data';
  408. attr.push(this.namespace);
  409. if (is_array) attr.push('d:constr="Array"');
  410. }
  411. if (name.match(this.rx_validate_name) === null) {
  412. attr.push( 'd:name="'+ name +'"' );
  413. name = 'd:name';
  414. }
  415. if (array_child) return elem.join('');
  416. // prepare map
  417. this.map.push(tree);
  418. attr.push('d:mi="'+ this.map.length +'"');
  419. return '<'+ name + (attr.length ? ' '+ attr.join(' ') : '') + (elem.length ? '>'+ elem.join('') +'</'+ name +'>' : '/>' );
  420. },
  421. scalar_to_xml: function(name, val, override) {
  422. var attr = '',
  423. text,
  424. constr,
  425. cnName;
  426. // check whether the nodename is valid
  427. if (name.match(this.rx_validate_name) === null) {
  428. attr += ' d:name="'+ name +'"';
  429. name = 'd:name';
  430. override = false;
  431. }
  432. if (val === null || val.toString() === 'NaN') val = null;
  433. if (val === null) return '<'+ name +' d:constr="null"/>';
  434. if (val.length === 1 && val[0].constructor === Object) {
  435. text = this.hash_to_xml(false, val[0]);
  436. var a1 = text.match(this.rx_node),
  437. a2 = text.match(this.rx_constructor);
  438. a1 = (a1 !== null)? a1[2]
  439. .replace(this.rx_namespace, '')
  440. .replace(/>/, '')
  441. .replace(/"\/$/, '"') : '';
  442. a2 = (a2 !== null)? a2[2] : '';
  443. text = text.match(this.rx_data);
  444. text = (text !== null)? text[2] : '';
  445. return '<'+ name + a1 +' '+ a2 +' d:type="ArrayItem">'+ text +'</'+ name +'>';
  446. } else if (val.length === 0 && val.constructor === Array) {
  447. return '<'+ name +' d:constr="Array"/>';
  448. }
  449. // else
  450. if (override) {
  451. return this.hash_to_xml( name, val, true );
  452. }
  453. constr = val.constructor;
  454. cnName = constr.toString().match(this.rx_function)[1];
  455. text = (constr === Array) ? this.hash_to_xml( 'd:item', val, true )
  456. : this.escape_xml(val);
  457. attr += ' d:constr="'+ cnName +'"';
  458. // prepare map
  459. this.map.push(val);
  460. attr += ' d:mi="'+ this.map.length +'"';
  461. return (name === '#text') ? this.escape_xml(val) : '<'+ name + attr +'>'+ text +'</'+ name +'>';
  462. },
  463. escape_xml: function(text) {
  464. return String(text) .replace(/</g, '&lt;')
  465. .replace(/>/g, '&gt;')
  466. .replace(/"/g, '&quot;')
  467. .replace(/&nbsp;/g, '&#160;');
  468. }
  469. },
  470. processed,
  471. doc,
  472. task;
  473. // depending on request
  474. switch (typeof callback) {
  475. case 'function':
  476. // compile interpreter with 'x10.js'
  477. task = x10.compile(interpreter);
  478. // parse in a dedicated thread
  479. task.to_xml_str(tree, function(processed) {
  480. // snapshot distinctly improves performance
  481. callback({
  482. doc: Defiant.xmlFromString(processed.str),
  483. src: tree,
  484. map: processed.map
  485. });
  486. });
  487. return;
  488. case 'boolean':
  489. processed = interpreter.to_xml_str.call(interpreter, tree);
  490. // return snapshot
  491. return {
  492. doc: Defiant.xmlFromString(processed.str),
  493. src: tree,
  494. map: processed.map
  495. };
  496. default:
  497. processed = interpreter.to_xml_str.call(interpreter, tree);
  498. doc = Defiant.xmlFromString(processed.str);
  499. this.search.map = processed.map;
  500. return doc;
  501. }
  502. };
  503. }
  504. if (!JSON.search) {
  505. JSON.search = function(tree, xpath, single) {
  506. 'use strict';
  507. var isSnapshot = tree.doc && tree.doc.nodeType,
  508. doc = isSnapshot ? tree.doc : JSON.toXML(tree),
  509. map = isSnapshot ? tree.map : this.search.map,
  510. src = isSnapshot ? tree.src : tree,
  511. xres = Defiant.node[ single ? 'selectSingleNode' : 'selectNodes' ](doc, xpath.xTransform()),
  512. ret = [],
  513. mapIndex,
  514. i;
  515. if (single) xres = [xres];
  516. i = xres.length;
  517. while (i--) {
  518. switch(xres[i].nodeType) {
  519. case 2:
  520. case 3:
  521. ret.unshift( xres[i].nodeValue );
  522. break;
  523. default:
  524. mapIndex = +xres[i].getAttribute('d:mi');
  525. //if (map[mapIndex-1] !== false) {
  526. ret.unshift( map[mapIndex-1] );
  527. //}
  528. }
  529. }
  530. // if environment = development, add search tracing
  531. if (Defiant.env === 'development') {
  532. this.trace = JSON.mtrace(src, ret, xres);
  533. }
  534. return ret;
  535. };
  536. }
  537. if (!JSON.mtrace) {
  538. JSON.mtrace = function(root, hits, xres) {
  539. 'use strict';
  540. var win = window,
  541. stringify = JSON.stringify,
  542. sroot = stringify( root, null, '\t' ).replace(/\t/g, ''),
  543. trace = [],
  544. i = 0,
  545. il = xres.length,
  546. od = il ? xres[i].ownerDocument.documentElement : false,
  547. map = this.search.map,
  548. hstr,
  549. cConstr,
  550. fIndex = 0,
  551. mIndex,
  552. lStart,
  553. lEnd;
  554. for (; i<il; i++) {
  555. switch (xres[i].nodeType) {
  556. case 2:
  557. cConstr = xres[i].ownerElement ? xres[i].ownerElement.getAttribute('d:'+ xres[i].nodeName) : 'String';
  558. hstr = '"@'+ xres[i].nodeName +'": '+ win[ cConstr ]( hits[i] );
  559. mIndex = sroot.indexOf(hstr);
  560. lEnd = 0;
  561. break;
  562. case 3:
  563. cConstr = xres[i].parentNode.getAttribute('d:constr');
  564. hstr = win[ cConstr ]( hits[i] );
  565. hstr = '"'+ xres[i].parentNode.nodeName +'": '+ (hstr === 'Number' ? hstr : '"'+ hstr +'"');
  566. mIndex = sroot.indexOf(hstr);
  567. lEnd = 0;
  568. break;
  569. default:
  570. if (xres[i] === od) continue;
  571. if (xres[i].getAttribute('d:constr') === 'String' || xres[i].getAttribute('d:constr') === 'Number') {
  572. cConstr = xres[i].getAttribute('d:constr');
  573. hstr = win[ cConstr ]( hits[i] );
  574. mIndex = sroot.indexOf(hstr, fIndex);
  575. hstr = '"'+ xres[i].nodeName +'": '+ (cConstr === 'Number' ? hstr : '"'+ hstr +'"');
  576. lEnd = 0;
  577. fIndex = mIndex + 1;
  578. } else {
  579. hstr = stringify( hits[i], null, '\t' ).replace(/\t/g, '');
  580. mIndex = sroot.indexOf(hstr);
  581. lEnd = hstr.match(/\n/g).length;
  582. }
  583. }
  584. lStart = sroot.substring(0,mIndex).match(/\n/g).length+1;
  585. trace.push([lStart, lEnd]);
  586. }
  587. return trace;
  588. };
  589. }
  590. Defiant.node.selectNodes = function(XNode, XPath) {
  591. if (XNode.evaluate) {
  592. var ns = XNode.createNSResolver(XNode.documentElement),
  593. qI = XNode.evaluate(XPath, XNode, ns, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null),
  594. res = [],
  595. i = 0,
  596. il = qI.snapshotLength;
  597. for (; i<il; i++) {
  598. res.push( qI.snapshotItem(i) );
  599. }
  600. return res;
  601. } else {
  602. return XNode.selectNodes(XPath);
  603. }
  604. };
  605. Defiant.node.selectSingleNode = function(XNode, XPath) {
  606. if (XNode.evaluate) {
  607. var xI = this.selectNodes(XNode, XPath);
  608. return (xI.length > 0)? xI[0] : null;
  609. } else {
  610. return XNode.selectSingleNode(XPath);
  611. }
  612. };
  613. Defiant.node.prettyPrint = function(node) {
  614. var root = Defiant,
  615. tabs = root.tabsize,
  616. decl = root.xml_decl.toLowerCase(),
  617. ser,
  618. xstr;
  619. if (root.is_ie) {
  620. xstr = node.xml;
  621. } else {
  622. ser = new XMLSerializer();
  623. xstr = ser.serializeToString(node);
  624. }
  625. if (root.env !== 'development') {
  626. // if environment is not development, remove defiant related info
  627. xstr = xstr.replace(/ \w+\:d=".*?"| d\:\w+=".*?"/g, '');
  628. }
  629. var str = xstr.trim().replace(/(>)\s*(<)(\/*)/g, '$1\n$2$3'),
  630. lines = str.split('\n'),
  631. indent = -1,
  632. i = 0,
  633. il = lines.length,
  634. start,
  635. end;
  636. for (; i<il; i++) {
  637. if (i === 0 && lines[i].toLowerCase() === decl) continue;
  638. start = lines[i].match(/<[A-Za-z_\:]+.*?>/g) !== null;
  639. //start = lines[i].match(/<[^\/]+>/g) !== null;
  640. end = lines[i].match(/<\/[\w\:]+>/g) !== null;
  641. if (lines[i].match(/<.*?\/>/g) !== null) start = end = true;
  642. if (start) indent++;
  643. lines[i] = String().fill(indent, '\t') + lines[i];
  644. if (start && end) indent--;
  645. if (!start && end) indent--;
  646. }
  647. return lines.join('\n').replace(/\t/g, String().fill(tabs, ' '));
  648. };
  649. Defiant.node.toJSON = function(xnode, stringify) {
  650. 'use strict';
  651. var interpret = function(leaf) {
  652. var obj = {},
  653. win = window,
  654. attr,
  655. type,
  656. item,
  657. cname,
  658. cConstr,
  659. cval,
  660. text,
  661. i, il, a;
  662. switch (leaf.nodeType) {
  663. case 1:
  664. cConstr = leaf.getAttribute('d:constr');
  665. if (cConstr === 'Array') obj = [];
  666. else if (cConstr === 'String' && leaf.textContent === '') obj = '';
  667. attr = leaf.attributes;
  668. i = 0;
  669. il = attr.length;
  670. for (; i<il; i++) {
  671. a = attr.item(i);
  672. if (a.nodeName.match(/\:d|d\:/g) !== null) continue;
  673. cConstr = leaf.getAttribute('d:'+ a.nodeName);
  674. if (cConstr && cConstr !== 'undefined') {
  675. if (a.nodeValue === 'null') cval = null;
  676. else cval = win[ cConstr ]( (a.nodeValue === 'false') ? '' : a.nodeValue );
  677. } else {
  678. cval = a.nodeValue;
  679. }
  680. obj['@'+ a.nodeName] = cval;
  681. }
  682. break;
  683. case 3:
  684. type = leaf.parentNode.getAttribute('d:type');
  685. cval = (type) ? win[ type ]( leaf.nodeValue === 'false' ? '' : leaf.nodeValue ) : leaf.nodeValue;
  686. obj = cval;
  687. break;
  688. }
  689. if (leaf.hasChildNodes()) {
  690. i = 0;
  691. il = leaf.childNodes.length;
  692. for(; i<il; i++) {
  693. item = leaf.childNodes.item(i);
  694. cname = item.nodeName;
  695. attr = leaf.attributes;
  696. if (cname === 'd:name') {
  697. cname = item.getAttribute('d:name');
  698. }
  699. if (cname === '#text') {
  700. cConstr = leaf.getAttribute('d:constr');
  701. if (cConstr === 'undefined') cConstr = undefined;
  702. text = item.textContent || item.text;
  703. cval = cConstr === 'Boolean' && text === 'false' ? '' : text;
  704. if (!cConstr && !attr.length) obj = cval;
  705. else if (cConstr && il === 1) {
  706. obj = win[cConstr](cval);
  707. } else if (!leaf.hasChildNodes()) {
  708. obj[cname] = (cConstr)? win[cConstr](cval) : cval;
  709. } else {
  710. if (attr.length < 3) obj = (cConstr)? win[cConstr](cval) : cval;
  711. else obj[cname] = (cConstr)? win[cConstr](cval) : cval;
  712. }
  713. } else {
  714. if (obj[cname]) {
  715. if (obj[cname].push) obj[cname].push( interpret(item) );
  716. else obj[cname] = [obj[cname], interpret(item)];
  717. continue;
  718. }
  719. cConstr = item.getAttribute('d:constr');
  720. switch (cConstr) {
  721. case 'null':
  722. if (obj.push) obj.push(null);
  723. else obj[cname] = null;
  724. break;
  725. case 'Array':
  726. //console.log( Defiant.node.prettyPrint(item) );
  727. if (item.parentNode.firstChild === item && cConstr === 'Array' && cname !== 'd:item') {
  728. if (cname === 'd:item' || cConstr === 'Array') {
  729. cval = interpret(item);
  730. obj[cname] = cval.length ? [cval] : cval;
  731. } else {
  732. obj[cname] = interpret(item);
  733. }
  734. }
  735. else if (obj.push) obj.push( interpret(item) );
  736. else obj[cname] = interpret(item);
  737. break;
  738. case 'String':
  739. case 'Number':
  740. case 'Boolean':
  741. text = item.textContent || item.text;
  742. cval = cConstr === 'Boolean' && text === 'false' ? '' : text;
  743. if (obj.push) obj.push( win[cConstr](cval) );
  744. else obj[cname] = interpret(item);
  745. break;
  746. default:
  747. if (obj.push) obj.push( interpret( item ) );
  748. else obj[cname] = interpret( item );
  749. }
  750. }
  751. }
  752. }
  753. if (leaf.nodeType === 1 && leaf.getAttribute('d:type') === 'ArrayItem') {
  754. obj = [obj];
  755. }
  756. return obj;
  757. },
  758. node = (xnode.nodeType === 9) ? xnode.documentElement : xnode,
  759. ret = interpret(node),
  760. rn = ret[node.nodeName];
  761. // exclude root, if "this" is root node
  762. if (node === node.ownerDocument.documentElement && rn && rn.constructor === Array) {
  763. ret = rn;
  764. }
  765. if (stringify && stringify.toString() === 'true') stringify = '\t';
  766. return stringify ? JSON.stringify(ret, null, stringify) : ret;
  767. };
  768. // check if jQuery is present
  769. if (typeof(jQuery) !== 'undefined') {
  770. (function ( $ ) {
  771. 'use strict';
  772. $.fn.defiant = function(template, xpath) {
  773. this.html( Defiant.render(template, xpath) );
  774. return this;
  775. };
  776. }(jQuery));
  777. }