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