PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/dustjs-linkedin/0.4.0/dust-core.js

https://gitlab.com/alidz1982/cdnjs
JavaScript | 577 lines | 492 code | 72 blank | 13 comment | 84 complexity | a07f22f7292ac6926aff7b513045f817 MD5 | raw file
  1. //
  2. // Dust - Asynchronous Templating v0.4.0
  3. // http://akdubya.github.com/dustjs
  4. //
  5. // Copyright (c) 2010, Aleksander Williams
  6. // Released under the MIT License.
  7. //
  8. var dust = {};
  9. (function(dust) {
  10. dust.cache = {};
  11. dust.register = function(name, tmpl) {
  12. if (!name) return;
  13. dust.cache[name] = tmpl;
  14. };
  15. dust.render = function(name, context, callback) {
  16. var chunk = new Stub(callback).head;
  17. dust.load(name, chunk, Context.wrap(context)).end();
  18. };
  19. dust.stream = function(name, context) {
  20. var stream = new Stream();
  21. dust.nextTick(function() {
  22. dust.load(name, stream.head, Context.wrap(context)).end();
  23. });
  24. return stream;
  25. };
  26. dust.renderSource = function(source, context, callback) {
  27. return dust.compileFn(source)(context, callback);
  28. };
  29. dust.compileFn = function(source, name) {
  30. var tmpl = dust.loadSource(dust.compile(source, name));
  31. return function(context, callback) {
  32. var master = callback ? new Stub(callback) : new Stream();
  33. dust.nextTick(function() {
  34. tmpl(master.head, Context.wrap(context)).end();
  35. });
  36. return master;
  37. }
  38. };
  39. dust.load = function(name, chunk, context) {
  40. var tmpl = dust.cache[name];
  41. if (tmpl) {
  42. return tmpl(chunk, context);
  43. } else {
  44. if (dust.onLoad) {
  45. return chunk.map(function(chunk) {
  46. dust.onLoad(name, function(err, src) {
  47. if (err) return chunk.setError(err);
  48. if (!dust.cache[name]) dust.loadSource(dust.compile(src, name));
  49. dust.cache[name](chunk, context).end();
  50. });
  51. });
  52. }
  53. return chunk.setError(new Error("Template Not Found: " + name));
  54. }
  55. };
  56. dust.loadSource = function(source, path) {
  57. return eval(source);
  58. };
  59. if (Array.isArray) {
  60. dust.isArray = Array.isArray;
  61. } else {
  62. dust.isArray = function(arr) {
  63. return Object.prototype.toString.call(arr) == "[object Array]";
  64. };
  65. }
  66. dust.nextTick = function(callback) {
  67. setTimeout(callback, 0);
  68. }
  69. dust.isEmpty = function(value) {
  70. if (dust.isArray(value) && !value.length) return true;
  71. if (value === 0) return false;
  72. return (!value);
  73. };
  74. dust.filter = function(string, auto, filters) {
  75. if (filters) {
  76. for (var i=0, len=filters.length; i<len; i++) {
  77. var name = filters[i];
  78. if (name === "s") {
  79. auto = null;
  80. } else {
  81. string = dust.filters[name](string);
  82. }
  83. }
  84. }
  85. if (auto) {
  86. string = dust.filters[auto](string);
  87. }
  88. return string;
  89. };
  90. dust.filters = {
  91. h: function(value) { return dust.escapeHtml(value); },
  92. j: function(value) { return dust.escapeJs(value); },
  93. u: encodeURI,
  94. uc: encodeURIComponent
  95. }
  96. function Context(stack, global, blocks) {
  97. this.stack = stack;
  98. this.global = global;
  99. this.blocks = blocks;
  100. }
  101. dust.makeBase = function(global) {
  102. return new Context(new Stack(), global);
  103. }
  104. Context.wrap = function(context) {
  105. if (context instanceof Context) {
  106. return context;
  107. }
  108. return new Context(new Stack(context));
  109. }
  110. Context.prototype.get = function(key) {
  111. var ctx = this.stack, value;
  112. while(ctx) {
  113. if (ctx.isObject) {
  114. value = ctx.head[key];
  115. if (!(value === undefined)) {
  116. return value;
  117. }
  118. }
  119. ctx = ctx.tail;
  120. }
  121. return this.global ? this.global[key] : undefined;
  122. };
  123. Context.prototype.getPath = function(cur, down) {
  124. var ctx = this.stack,
  125. len = down.length;
  126. if (cur && len === 0) return ctx.head;
  127. if (!ctx.isObject) return undefined;
  128. ctx = ctx.head;
  129. var i = 0;
  130. while(ctx && i < len) {
  131. ctx = ctx[down[i]];
  132. i++;
  133. }
  134. return ctx;
  135. };
  136. Context.prototype.push = function(head, idx, len) {
  137. if( head ){
  138. // loop index for a block section
  139. head['$idx'] = idx;
  140. // loop size for a block section
  141. head['$len'] = len;
  142. }
  143. return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks);
  144. };
  145. Context.prototype.rebase = function(head) {
  146. return new Context(new Stack(head), this.global, this.blocks);
  147. };
  148. Context.prototype.current = function() {
  149. return this.stack.head;
  150. };
  151. Context.prototype.getBlock = function(key) {
  152. var blocks = this.blocks;
  153. if (!blocks) return;
  154. var len = blocks.length, fn;
  155. while (len--) {
  156. fn = blocks[len][key];
  157. if (fn) return fn;
  158. }
  159. }
  160. Context.prototype.shiftBlocks = function(locals) {
  161. var blocks = this.blocks;
  162. if (locals) {
  163. if (!blocks) {
  164. newBlocks = [locals];
  165. } else {
  166. newBlocks = blocks.concat([locals]);
  167. }
  168. return new Context(this.stack, this.global, newBlocks);
  169. }
  170. return this;
  171. }
  172. function Stack(head, tail, idx, len) {
  173. this.tail = tail;
  174. this.isObject = !dust.isArray(head) && head && typeof head === "object";
  175. this.head = head;
  176. this.index = idx;
  177. this.of = len;
  178. }
  179. function Stub(callback) {
  180. this.head = new Chunk(this);
  181. this.callback = callback;
  182. this.out = '';
  183. }
  184. Stub.prototype.flush = function() {
  185. var chunk = this.head;
  186. while (chunk) {
  187. if (chunk.flushable) {
  188. this.out += chunk.data;
  189. } else if (chunk.error) {
  190. this.callback(chunk.error);
  191. this.flush = function() {};
  192. return;
  193. } else {
  194. return;
  195. }
  196. chunk = chunk.next;
  197. this.head = chunk;
  198. }
  199. this.callback(null, this.out);
  200. }
  201. function Stream() {
  202. this.head = new Chunk(this);
  203. }
  204. Stream.prototype.flush = function() {
  205. var chunk = this.head;
  206. while(chunk) {
  207. if (chunk.flushable) {
  208. this.emit('data', chunk.data);
  209. } else if (chunk.error) {
  210. this.emit('error', chunk.error);
  211. this.flush = function() {};
  212. return;
  213. } else {
  214. return;
  215. }
  216. chunk = chunk.next;
  217. this.head = chunk;
  218. }
  219. this.emit('end');
  220. }
  221. Stream.prototype.emit = function(type, data) {
  222. var events = this.events;
  223. if (events && events[type]) {
  224. events[type](data);
  225. }
  226. }
  227. Stream.prototype.on = function(type, callback) {
  228. if (!this.events) {
  229. this.events = {};
  230. }
  231. this.events[type] = callback;
  232. return this;
  233. }
  234. function Chunk(root, next, taps) {
  235. this.root = root;
  236. this.next = next;
  237. this.data = '';
  238. this.flushable = false;
  239. this.taps = taps;
  240. }
  241. Chunk.prototype.write = function(data) {
  242. var taps = this.taps;
  243. if (taps) {
  244. data = taps.go(data);
  245. }
  246. this.data += data;
  247. return this;
  248. }
  249. Chunk.prototype.end = function(data) {
  250. if (data) {
  251. this.write(data);
  252. }
  253. this.flushable = true;
  254. this.root.flush();
  255. return this;
  256. }
  257. Chunk.prototype.map = function(callback) {
  258. var cursor = new Chunk(this.root, this.next, this.taps),
  259. branch = new Chunk(this.root, cursor, this.taps);
  260. this.next = branch;
  261. this.flushable = true;
  262. callback(branch);
  263. return cursor;
  264. }
  265. Chunk.prototype.tap = function(tap) {
  266. var taps = this.taps;
  267. if (taps) {
  268. this.taps = taps.push(tap);
  269. } else {
  270. this.taps = new Tap(tap);
  271. }
  272. return this;
  273. }
  274. Chunk.prototype.untap = function() {
  275. this.taps = this.taps.tail;
  276. return this;
  277. }
  278. Chunk.prototype.render = function(body, context) {
  279. return body(this, context);
  280. }
  281. Chunk.prototype.reference = function(elem, context, auto, filters) {
  282. if (typeof elem === "function") {
  283. elem = elem(this, context, null, {auto: auto, filters: filters});
  284. if (elem instanceof Chunk) {
  285. return elem;
  286. }
  287. }
  288. if (!dust.isEmpty(elem)) {
  289. return this.write(dust.filter(elem, auto, filters));
  290. } else {
  291. return this;
  292. }
  293. };
  294. Chunk.prototype.section = function(elem, context, bodies, params) {
  295. if (typeof elem === "function") {
  296. elem = elem(this, context, bodies, params);
  297. if (elem instanceof Chunk) {
  298. return elem;
  299. }
  300. }
  301. var body = bodies.block,
  302. skip = bodies['else'];
  303. if (params) {
  304. context = context.push(params);
  305. }
  306. if (dust.isArray(elem)) {
  307. if (body) {
  308. var len = elem.length, chunk = this;
  309. for (var i=0; i<len; i++) {
  310. chunk = body(chunk, context.push(elem[i], i, len));
  311. }
  312. return chunk;
  313. }
  314. } else if (elem === true) {
  315. if (body) return body(this, context);
  316. } else if (elem || elem === 0) {
  317. if (body) return body(this, context.push(elem));
  318. } else if (skip) {
  319. return skip(this, context);
  320. }
  321. return this;
  322. };
  323. Chunk.prototype.exists = function(elem, context, bodies) {
  324. var body = bodies.block,
  325. skip = bodies['else'];
  326. if (!dust.isEmpty(elem)) {
  327. if (body) return body(this, context);
  328. } else if (skip) {
  329. return skip(this, context);
  330. }
  331. return this;
  332. }
  333. Chunk.prototype.notexists = function(elem, context, bodies) {
  334. var body = bodies.block,
  335. skip = bodies['else'];
  336. if (dust.isEmpty(elem)) {
  337. if (body) return body(this, context);
  338. } else if (skip) {
  339. return skip(this, context);
  340. }
  341. return this;
  342. }
  343. Chunk.prototype.block = function(elem, context, bodies) {
  344. var body = bodies.block;
  345. if (elem) {
  346. body = elem;
  347. }
  348. if (body) {
  349. return body(this, context);
  350. }
  351. return this;
  352. };
  353. Chunk.prototype.partial = function(elem, context, params) {
  354. var ctx = context.stack, tempHead = ctx.head;
  355. if (params){
  356. //put the params context second to match what section does. {.} matches the current context without parameters
  357. //remove head
  358. context = context.rebase(ctx.tail);
  359. //put params on
  360. context = context.push(params);
  361. //reattach the head
  362. context = context.push(tempHead);
  363. }
  364. if (typeof elem === "function") {
  365. return this.capture(elem, context, function(name, chunk) {
  366. dust.load(name, chunk, context).end();
  367. });
  368. }
  369. return dust.load(elem, this, context);
  370. };
  371. Chunk.prototype.helper = function(name, context, bodies, params) {
  372. return dust.helpers[name](this, context, bodies, params);
  373. };
  374. Chunk.prototype.capture = function(body, context, callback) {
  375. return this.map(function(chunk) {
  376. var stub = new Stub(function(err, out) {
  377. if (err) {
  378. chunk.setError(err);
  379. } else {
  380. callback(out, chunk);
  381. }
  382. });
  383. body(stub.head, context).end();
  384. });
  385. };
  386. Chunk.prototype.setError = function(err) {
  387. this.error = err;
  388. this.root.flush();
  389. return this;
  390. };
  391. function Tap(head, tail) {
  392. this.head = head;
  393. this.tail = tail;
  394. }
  395. Tap.prototype.push = function(tap) {
  396. return new Tap(tap, this);
  397. };
  398. Tap.prototype.go = function(value) {
  399. var tap = this;
  400. while(tap) {
  401. value = tap.head(value);
  402. tap = tap.tail;
  403. }
  404. return value;
  405. };
  406. var HCHARS = new RegExp(/[&<>\"]/),
  407. AMP = /&/g,
  408. LT = /</g,
  409. GT = />/g,
  410. QUOT = /\"/g;
  411. dust.escapeHtml = function(s) {
  412. if (typeof s === "string") {
  413. if (!HCHARS.test(s)) {
  414. return s;
  415. }
  416. return s.replace(AMP,'&amp;').replace(LT,'&lt;').replace(GT,'&gt;').replace(QUOT,'&quot;');
  417. }
  418. return s;
  419. };
  420. var BS = /\\/g,
  421. CR = /\r/g,
  422. LS = /\u2028/g,
  423. PS = /\u2029/g,
  424. NL = /\n/g,
  425. LF = /\f/g,
  426. SQ = /'/g,
  427. DQ = /"/g,
  428. TB = /\t/g;
  429. dust.escapeJs = function(s) {
  430. if (typeof s === "string") {
  431. return s
  432. .replace(BS, '\\\\')
  433. .replace(DQ, '\\"')
  434. .replace(SQ, "\\'")
  435. .replace(CR, '\\r')
  436. .replace(LS, '\\u2028')
  437. .replace(PS, '\\u2029')
  438. .replace(NL, '\\n')
  439. .replace(LF, '\\f')
  440. .replace(TB, "\\t");
  441. }
  442. return s;
  443. };
  444. })(dust);
  445. if (typeof exports !== "undefined") {
  446. dust.helpers = require("./dust-helpers").helpers;
  447. if (typeof process !== "undefined" && typeof window === "undefined") {
  448. require('./server')(dust);
  449. }
  450. module.exports = dust;
  451. }
  452. (function(dust){
  453. var helpers = {
  454. sep: function(chunk, context, bodies) {
  455. if (context.stack.index === context.stack.of - 1) {
  456. return chunk;
  457. }
  458. return bodies.block(chunk, context);
  459. },
  460. idx: function(chunk, context, bodies) {
  461. return bodies.block(chunk, context.push(context.stack.index));
  462. },
  463. "if": function( chunk, context, bodies, params ){
  464. var cond = ( params.cond );
  465. if( params && params.cond ){
  466. // resolve dust references in the expression
  467. if( typeof cond === "function" ){
  468. cond = '';
  469. chunk.tap( function( data ){
  470. cond += data;
  471. return '';
  472. } ).render( params.cond, context ).untap();
  473. if( cond === '' ){
  474. cond = false;
  475. }
  476. }
  477. // eval expressions with no dust references
  478. if( eval( cond ) ){
  479. return chunk.render( bodies.block, context );
  480. }
  481. if( bodies['else'] ){
  482. return chunk.render( bodies['else'], context );
  483. }
  484. }
  485. // no condition
  486. else {
  487. if( window.console ){
  488. window.console.log( "No expression given!" );
  489. }
  490. }
  491. return chunk;
  492. }
  493. };
  494. dust.helpers = helpers;
  495. })(typeof exports !== 'undefined' ? exports : window.dust);