UNPKG

4.82 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.9.2
2(function() {
3 var Fixture,
4 slice = [].slice;
5
6 Fixture = (function() {
7 function Fixture(base, id) {
8 this.base = base != null ? base : 'spec/fixtures';
9 this.id = id != null ? id : 'fixture_container';
10 this.json = [];
11 this.scriptTypes = {
12 'application/ecmascript': 1,
13 'application/javascript': 1,
14 'application/x-ecmascript': 1,
15 'application/x-javascript': 1,
16 'text/ecmascript': 1,
17 'text/javascript': 1,
18 'text/javascript1.0': 1,
19 'text/javascript1.1': 1,
20 'text/javascript1.2': 1,
21 'text/javascript1.3': 1,
22 'text/javascript1.4': 1,
23 'text/javascript1.5': 1,
24 'text/jscript': 1,
25 'text/livescript': 1,
26 'text/x-ecmascript': 1,
27 'text/x-javascript': 1
28 };
29 this.el = window[this.id] || ((function(_this) {
30 return function() {
31 var container;
32 container = document.createElement('div');
33 container.setAttribute('id', _this.id);
34 return document.body.appendChild(container);
35 };
36 })(this))();
37 }
38
39 Fixture.prototype.load = function() {
40 var __html__, __json__, append, filename, filenames, fixture_path, ignore, j, json, k, len, results;
41 filenames = 2 <= arguments.length ? slice.call(arguments, 0, j = arguments.length - 1) : (j = 0, []), append = arguments[j++];
42 if (append == null) {
43 append = false;
44 }
45 __html__ = window.__html__ || {};
46 __json__ = window.__json__ || {};
47 if (typeof append !== 'boolean') {
48 filenames.push(append);
49 append = false;
50 }
51 if (append === false) {
52 this.cleanup();
53 }
54 results = [];
55 for (k = 0, len = filenames.length; k < len; k++) {
56 filename = filenames[k];
57 if (filename[0] === '/') {
58 fixture_path = filename.substr(1);
59 } else {
60 fixture_path = this.base + "/" + filename;
61 }
62 if (filename.indexOf('.json') !== -1) {
63 json = __json__[filename.replace('.json', '')] || __json__[filename] || __json__[fixture_path] || __json__[this.base + "/" + (filename.replace('.json', ''))];
64 if (json == null) {
65 this._throwNoFixture(fixture_path);
66 }
67 try {
68 json = JSON.parse(JSON.stringify(json));
69 } catch (_error) {
70 ignore = _error;
71 }
72 this.json.push(json);
73 results.push(json);
74 } else if (__html__[fixture_path]) {
75 results.push(this._appendFixture(__html__[fixture_path]));
76 } else {
77 if (typeof string === "undefined" || string === null) {
78 this._throwNoFixture(fixture_path);
79 }
80 }
81 }
82 if (results.length === 1) {
83 results = results[0];
84 }
85 return results;
86 };
87
88 Fixture.prototype.set = function() {
89 var append, j, k, len, results, string, strings;
90 strings = 2 <= arguments.length ? slice.call(arguments, 0, j = arguments.length - 1) : (j = 0, []), append = arguments[j++];
91 if (append == null) {
92 append = false;
93 }
94 if (typeof append !== 'boolean') {
95 strings.push(append);
96 append = false;
97 }
98 if (append === false) {
99 this.cleanup();
100 }
101 results = [];
102 for (k = 0, len = strings.length; k < len; k++) {
103 string = strings[k];
104 results.push(this._appendFixture(string));
105 }
106 if (results.length === 1) {
107 results = results[0];
108 }
109 return results;
110 };
111
112 Fixture.prototype.cleanup = function() {
113 this.json = [];
114 return this.el.innerHTML = '';
115 };
116
117 Fixture.prototype.setBase = function(base) {
118 this.base = base;
119 };
120
121 Fixture.prototype._appendFixture = function(html_string) {
122 var i, results, temp_div;
123 temp_div = document.createElement('div');
124 temp_div.innerHTML = html_string;
125 results = [];
126 while (i = temp_div.firstChild) {
127 if (i.nodeType !== 1) {
128 temp_div.removeChild(i);
129 } else {
130 this.el.appendChild(i);
131 results.push(i);
132 if (i.nodeName === 'SCRIPT' && (!i.type || this.scriptTypes[i.type])) {
133 eval(i.innerText || i.textContent);
134 }
135 }
136 }
137 return results;
138 };
139
140 Fixture.prototype._throwNoFixture = function(fixture_path) {
141 throw new ReferenceError("Cannot find fixture '" + fixture_path + "'");
142 };
143
144 return Fixture;
145
146 })();
147
148 if (typeof exports === 'object') {
149 module.exports = Fixture;
150 } else if (typeof define === 'function' && define.amd) {
151 define('fixture', [], function() {
152 return Fixture;
153 });
154 } else {
155 this['Fixture'] = Fixture;
156 }
157
158}).call(this);