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