PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/assets/javascripts/jasmine-fixture.js

https://gitlab.com/jk/gitlab-ce
JavaScript | 433 lines | 427 code | 2 blank | 4 comment | 98 complexity | 2c66e985d5bbfd7692e0f7bb3f974117 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. /* jasmine-fixture - 1.3.1
  2. * Makes injecting HTML snippets into the DOM easy & clean!
  3. * https://github.com/searls/jasmine-fixture
  4. */
  5. (function() {
  6. var createHTMLBlock,
  7. __slice = [].slice;
  8. (function($) {
  9. var ewwSideEffects, jasmineFixture, originalAffix, originalJasmineDotFixture, originalJasmineFixture, root, _, _ref;
  10. root = (1, eval)('this');
  11. originalJasmineFixture = root.jasmineFixture;
  12. originalJasmineDotFixture = (_ref = root.jasmine) != null ? _ref.fixture : void 0;
  13. originalAffix = root.affix;
  14. _ = function(list) {
  15. return {
  16. inject: function(iterator, memo) {
  17. var item, _i, _len, _results;
  18. _results = [];
  19. for (_i = 0, _len = list.length; _i < _len; _i++) {
  20. item = list[_i];
  21. _results.push(memo = iterator(memo, item));
  22. }
  23. return _results;
  24. }
  25. };
  26. };
  27. root.jasmineFixture = function($) {
  28. var $whatsTheRootOf, affix, create, jasmineFixture, noConflict;
  29. affix = function(selectorOptions) {
  30. return create.call(this, selectorOptions, true);
  31. };
  32. create = function(selectorOptions, attach) {
  33. var $top;
  34. $top = null;
  35. _(selectorOptions.split(/[ ](?![^\{]*\})(?=[^\]]*?(?:\[|$))/)).inject(function($parent, elementSelector) {
  36. var $el;
  37. if (elementSelector === ">") {
  38. return $parent;
  39. }
  40. $el = createHTMLBlock($, elementSelector);
  41. if (attach || $top) {
  42. $el.appendTo($parent);
  43. }
  44. $top || ($top = $el);
  45. return $el;
  46. }, $whatsTheRootOf(this));
  47. return $top;
  48. };
  49. noConflict = function() {
  50. var currentJasmineFixture, _ref1;
  51. currentJasmineFixture = jasmine.fixture;
  52. root.jasmineFixture = originalJasmineFixture;
  53. if ((_ref1 = root.jasmine) != null) {
  54. _ref1.fixture = originalJasmineDotFixture;
  55. }
  56. root.affix = originalAffix;
  57. return currentJasmineFixture;
  58. };
  59. $whatsTheRootOf = function(that) {
  60. if (that.jquery != null) {
  61. return that;
  62. } else if ($('#jasmine_content').length > 0) {
  63. return $('#jasmine_content');
  64. } else {
  65. return $('<div id="jasmine_content"></div>').appendTo('body');
  66. }
  67. };
  68. jasmineFixture = {
  69. affix: affix,
  70. create: create,
  71. noConflict: noConflict
  72. };
  73. ewwSideEffects(jasmineFixture);
  74. return jasmineFixture;
  75. };
  76. ewwSideEffects = function(jasmineFixture) {
  77. var _ref1;
  78. if ((_ref1 = root.jasmine) != null) {
  79. _ref1.fixture = jasmineFixture;
  80. }
  81. $.fn.affix = root.affix = jasmineFixture.affix;
  82. return afterEach(function() {
  83. return $('#jasmine_content').remove();
  84. });
  85. };
  86. if ($) {
  87. return jasmineFixture = root.jasmineFixture($);
  88. } else {
  89. return root.affix = function() {
  90. var nowJQueryExists;
  91. nowJQueryExists = window.jQuery || window.$;
  92. if (nowJQueryExists != null) {
  93. jasmineFixture = root.jasmineFixture(nowJQueryExists);
  94. return affix.call.apply(affix, [this].concat(__slice.call(arguments)));
  95. } else {
  96. throw new Error("jasmine-fixture requires jQuery to be defined at window.jQuery or window.$");
  97. }
  98. };
  99. }
  100. })(window.jQuery || window.$);
  101. createHTMLBlock = (function() {
  102. var bindData, bindEvents, parseAttributes, parseClasses, parseContents, parseEnclosure, parseReferences, parseVariableScope, regAttr, regAttrDfn, regAttrs, regCBrace, regClass, regClasses, regData, regDatas, regEvent, regEvents, regExclamation, regId, regReference, regTag, regTagNotContent, regZenTagDfn;
  103. createHTMLBlock = function($, ZenObject, data, functions, indexes) {
  104. var ZenCode, arr, block, blockAttrs, blockClasses, blockHTML, blockId, blockTag, blocks, el, el2, els, forScope, indexName, inner, len, obj, origZenCode, paren, result, ret, zc, zo;
  105. if ($.isPlainObject(ZenObject)) {
  106. ZenCode = ZenObject.main;
  107. } else {
  108. ZenCode = ZenObject;
  109. ZenObject = {
  110. main: ZenCode
  111. };
  112. }
  113. origZenCode = ZenCode;
  114. if (indexes === undefined) {
  115. indexes = {};
  116. }
  117. if (ZenCode.charAt(0) === "!" || $.isArray(data)) {
  118. if ($.isArray(data)) {
  119. forScope = ZenCode;
  120. } else {
  121. obj = parseEnclosure(ZenCode, "!");
  122. obj = obj.substring(obj.indexOf(":") + 1, obj.length - 1);
  123. forScope = parseVariableScope(ZenCode);
  124. }
  125. while (forScope.charAt(0) === "@") {
  126. forScope = parseVariableScope("!for:!" + parseReferences(forScope, ZenObject));
  127. }
  128. zo = ZenObject;
  129. zo.main = forScope;
  130. el = $();
  131. if (ZenCode.substring(0, 5) === "!for:" || $.isArray(data)) {
  132. if (!$.isArray(data) && obj.indexOf(":") > 0) {
  133. indexName = obj.substring(0, obj.indexOf(":"));
  134. obj = obj.substr(obj.indexOf(":") + 1);
  135. }
  136. arr = ($.isArray(data) ? data : data[obj]);
  137. zc = zo.main;
  138. if ($.isArray(arr) || $.isPlainObject(arr)) {
  139. $.map(arr, function(value, index) {
  140. var next;
  141. zo.main = zc;
  142. if (indexName !== undefined) {
  143. indexes[indexName] = index;
  144. }
  145. if (!$.isPlainObject(value)) {
  146. value = {
  147. value: value
  148. };
  149. }
  150. next = createHTMLBlock($, zo, value, functions, indexes);
  151. if (el.length !== 0) {
  152. return $.each(next, function(index, value) {
  153. return el.push(value);
  154. });
  155. }
  156. });
  157. }
  158. if (!$.isArray(data)) {
  159. ZenCode = ZenCode.substr(obj.length + 6 + forScope.length);
  160. } else {
  161. ZenCode = "";
  162. }
  163. } else if (ZenCode.substring(0, 4) === "!if:") {
  164. result = parseContents("!" + obj + "!", data, indexes);
  165. if (result !== "undefined" || result !== "false" || result !== "") {
  166. el = createHTMLBlock($, zo, data, functions, indexes);
  167. }
  168. ZenCode = ZenCode.substr(obj.length + 5 + forScope.length);
  169. }
  170. ZenObject.main = ZenCode;
  171. } else if (ZenCode.charAt(0) === "(") {
  172. paren = parseEnclosure(ZenCode, "(", ")");
  173. inner = paren.substring(1, paren.length - 1);
  174. ZenCode = ZenCode.substr(paren.length);
  175. zo = ZenObject;
  176. zo.main = inner;
  177. el = createHTMLBlock($, zo, data, functions, indexes);
  178. } else {
  179. blocks = ZenCode.match(regZenTagDfn);
  180. block = blocks[0];
  181. if (block.length === 0) {
  182. return "";
  183. }
  184. if (block.indexOf("@") >= 0) {
  185. ZenCode = parseReferences(ZenCode, ZenObject);
  186. zo = ZenObject;
  187. zo.main = ZenCode;
  188. return createHTMLBlock($, zo, data, functions, indexes);
  189. }
  190. block = parseContents(block, data, indexes);
  191. blockClasses = parseClasses($, block);
  192. if (regId.test(block)) {
  193. blockId = regId.exec(block)[1];
  194. }
  195. blockAttrs = parseAttributes(block, data);
  196. blockTag = (block.charAt(0) === "{" ? "span" : "div");
  197. if (ZenCode.charAt(0) !== "#" && ZenCode.charAt(0) !== "." && ZenCode.charAt(0) !== "{") {
  198. blockTag = regTag.exec(block)[1];
  199. }
  200. if (block.search(regCBrace) !== -1) {
  201. blockHTML = block.match(regCBrace)[1];
  202. }
  203. blockAttrs = $.extend(blockAttrs, {
  204. id: blockId,
  205. "class": blockClasses,
  206. html: blockHTML
  207. });
  208. el = $("<" + blockTag + ">", blockAttrs);
  209. el.attr(blockAttrs);
  210. el = bindEvents(block, el, functions);
  211. el = bindData(block, el, data);
  212. ZenCode = ZenCode.substr(blocks[0].length);
  213. ZenObject.main = ZenCode;
  214. }
  215. if (ZenCode.length > 0) {
  216. if (ZenCode.charAt(0) === ">") {
  217. if (ZenCode.charAt(1) === "(") {
  218. zc = parseEnclosure(ZenCode.substr(1), "(", ")");
  219. ZenCode = ZenCode.substr(zc.length + 1);
  220. } else if (ZenCode.charAt(1) === "!") {
  221. obj = parseEnclosure(ZenCode.substr(1), "!");
  222. forScope = parseVariableScope(ZenCode.substr(1));
  223. zc = obj + forScope;
  224. ZenCode = ZenCode.substr(zc.length + 1);
  225. } else {
  226. len = Math.max(ZenCode.indexOf("+"), ZenCode.length);
  227. zc = ZenCode.substring(1, len);
  228. ZenCode = ZenCode.substr(len);
  229. }
  230. zo = ZenObject;
  231. zo.main = zc;
  232. els = $(createHTMLBlock($, zo, data, functions, indexes));
  233. els.appendTo(el);
  234. }
  235. if (ZenCode.charAt(0) === "+") {
  236. zo = ZenObject;
  237. zo.main = ZenCode.substr(1);
  238. el2 = createHTMLBlock($, zo, data, functions, indexes);
  239. $.each(el2, function(index, value) {
  240. return el.push(value);
  241. });
  242. }
  243. }
  244. ret = el;
  245. return ret;
  246. };
  247. bindData = function(ZenCode, el, data) {
  248. var datas, i, split;
  249. if (ZenCode.search(regDatas) === 0) {
  250. return el;
  251. }
  252. datas = ZenCode.match(regDatas);
  253. if (datas === null) {
  254. return el;
  255. }
  256. i = 0;
  257. while (i < datas.length) {
  258. split = regData.exec(datas[i]);
  259. if (split[3] === undefined) {
  260. $(el).data(split[1], data[split[1]]);
  261. } else {
  262. $(el).data(split[1], data[split[3]]);
  263. }
  264. i++;
  265. }
  266. return el;
  267. };
  268. bindEvents = function(ZenCode, el, functions) {
  269. var bindings, fn, i, split;
  270. if (ZenCode.search(regEvents) === 0) {
  271. return el;
  272. }
  273. bindings = ZenCode.match(regEvents);
  274. if (bindings === null) {
  275. return el;
  276. }
  277. i = 0;
  278. while (i < bindings.length) {
  279. split = regEvent.exec(bindings[i]);
  280. if (split[2] === undefined) {
  281. fn = functions[split[1]];
  282. } else {
  283. fn = functions[split[2]];
  284. }
  285. $(el).bind(split[1], fn);
  286. i++;
  287. }
  288. return el;
  289. };
  290. parseAttributes = function(ZenBlock, data) {
  291. var attrStrs, attrs, i, parts;
  292. if (ZenBlock.search(regAttrDfn) === -1) {
  293. return undefined;
  294. }
  295. attrStrs = ZenBlock.match(regAttrDfn);
  296. attrs = {};
  297. i = 0;
  298. while (i < attrStrs.length) {
  299. parts = regAttr.exec(attrStrs[i]);
  300. attrs[parts[1]] = "";
  301. if (parts[3] !== undefined) {
  302. attrs[parts[1]] = parseContents(parts[3], data);
  303. }
  304. i++;
  305. }
  306. return attrs;
  307. };
  308. parseClasses = function($, ZenBlock) {
  309. var classes, clsString, i;
  310. ZenBlock = ZenBlock.match(regTagNotContent)[0];
  311. if (ZenBlock.search(regClasses) === -1) {
  312. return undefined;
  313. }
  314. classes = ZenBlock.match(regClasses);
  315. clsString = "";
  316. i = 0;
  317. while (i < classes.length) {
  318. clsString += " " + regClass.exec(classes[i])[1];
  319. i++;
  320. }
  321. return $.trim(clsString);
  322. };
  323. parseContents = function(ZenBlock, data, indexes) {
  324. var html;
  325. if (indexes === undefined) {
  326. indexes = {};
  327. }
  328. html = ZenBlock;
  329. if (data === undefined) {
  330. return html;
  331. }
  332. while (regExclamation.test(html)) {
  333. html = html.replace(regExclamation, function(str, str2) {
  334. var begChar, fn, val;
  335. begChar = "";
  336. if (str.indexOf("!for:") > 0 || str.indexOf("!if:") > 0) {
  337. return str;
  338. }
  339. if (str.charAt(0) !== "!") {
  340. begChar = str.charAt(0);
  341. str = str.substring(2, str.length - 1);
  342. }
  343. fn = new Function("data", "indexes", "var r=undefined;" + "with(data){try{r=" + str + ";}catch(e){}}" + "with(indexes){try{if(r===undefined)r=" + str + ";}catch(e){}}" + "return r;");
  344. val = unescape(fn(data, indexes));
  345. return begChar + val;
  346. });
  347. }
  348. html = html.replace(/\\./g, function(str) {
  349. return str.charAt(1);
  350. });
  351. return unescape(html);
  352. };
  353. parseEnclosure = function(ZenCode, open, close, count) {
  354. var index, ret;
  355. if (close === undefined) {
  356. close = open;
  357. }
  358. index = 1;
  359. if (count === undefined) {
  360. count = (ZenCode.charAt(0) === open ? 1 : 0);
  361. }
  362. if (count === 0) {
  363. return;
  364. }
  365. while (count > 0 && index < ZenCode.length) {
  366. if (ZenCode.charAt(index) === close && ZenCode.charAt(index - 1) !== "\\") {
  367. count--;
  368. } else {
  369. if (ZenCode.charAt(index) === open && ZenCode.charAt(index - 1) !== "\\") {
  370. count++;
  371. }
  372. }
  373. index++;
  374. }
  375. ret = ZenCode.substring(0, index);
  376. return ret;
  377. };
  378. parseReferences = function(ZenCode, ZenObject) {
  379. ZenCode = ZenCode.replace(regReference, function(str) {
  380. var fn;
  381. str = str.substr(1);
  382. fn = new Function("objs", "var r=\"\";" + "with(objs){try{" + "r=" + str + ";" + "}catch(e){}}" + "return r;");
  383. return fn(ZenObject, parseReferences);
  384. });
  385. return ZenCode;
  386. };
  387. parseVariableScope = function(ZenCode) {
  388. var forCode, rest, tag;
  389. if (ZenCode.substring(0, 5) !== "!for:" && ZenCode.substring(0, 4) !== "!if:") {
  390. return undefined;
  391. }
  392. forCode = parseEnclosure(ZenCode, "!");
  393. ZenCode = ZenCode.substr(forCode.length);
  394. if (ZenCode.charAt(0) === "(") {
  395. return parseEnclosure(ZenCode, "(", ")");
  396. }
  397. tag = ZenCode.match(regZenTagDfn)[0];
  398. ZenCode = ZenCode.substr(tag.length);
  399. if (ZenCode.length === 0 || ZenCode.charAt(0) === "+") {
  400. return tag;
  401. } else if (ZenCode.charAt(0) === ">") {
  402. rest = "";
  403. rest = parseEnclosure(ZenCode.substr(1), "(", ")", 1);
  404. return tag + ">" + rest;
  405. }
  406. return undefined;
  407. };
  408. regZenTagDfn = /([#\.\@]?[\w-]+|\[([\w-!?=:"']+(="([^"]|\\")+")? {0,})+\]|\~[\w$]+=[\w$]+|&[\w$]+(=[\w$]+)?|[#\.\@]?!([^!]|\\!)+!){0,}(\{([^\}]|\\\})+\})?/i;
  409. regTag = /(\w+)/i;
  410. regId = /(?:^|\b)#([\w-!]+)/i;
  411. regTagNotContent = /((([#\.]?[\w-]+)?(\[([\w!]+(="([^"]|\\")+")? {0,})+\])?)+)/i;
  412. /*
  413. See lookahead syntax (?!) at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
  414. */
  415. regClasses = /(\.[\w-]+)(?!["\w])/g;
  416. regClass = /\.([\w-]+)/i;
  417. regReference = /(@[\w$_][\w$_\d]+)/i;
  418. regAttrDfn = /(\[([\w-!]+(="?([^"]|\\")+"?)? {0,})+\])/ig;
  419. regAttrs = /([\w-!]+(="([^"]|\\")+")?)/g;
  420. regAttr = /([\w-!]+)(="?((([\w]+(\[.*?\])+)|[^"\]]|\\")+)"?)?/i;
  421. regCBrace = /\{(([^\}]|\\\})+)\}/i;
  422. regExclamation = /(?:([^\\]|^))!([^!]|\\!)+!/g;
  423. regEvents = /\~[\w$]+(=[\w$]+)?/g;
  424. regEvent = /\~([\w$]+)=([\w$]+)/i;
  425. regDatas = /&[\w$]+(=[\w$]+)?/g;
  426. regData = /&([\w$]+)(=([\w$]+))?/i;
  427. return createHTMLBlock;
  428. })();
  429. }).call(this);