forked from velesin/jasmine-jquery
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfixtures.js
130 lines (121 loc) · 4.68 KB
/
fixtures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"use strict";
(function(fixtures){
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// NodeJS
module.exports = fixtures;
} else if (typeof define === 'function' && define.amd){
define(function(){
return fixtures;
});
} else{
var global = (false || eval)('this');
global.fixtures = fixtures;
}
}(new function(){
var fixturesCache = {};
var self = this;
self.containerId = 'js-fixtures';
self.path = 'spec/javascripts/fixtures';
self.window = function(){
var iframe = document.getElementById(self.containerId);
if (!iframe) return null;
return iframe.contentWindow || iframe.contentDocument;
};
self.body = function(){
if (!self.window()) return null;
var content = self.window().document.body.innerHTML;
return content;
};
self.load = function(html){
var cb = typeof arguments[arguments.length - 1] === 'function' ? arguments[arguments.length -1] : null;
addToContainer(self.read.apply(self, arguments), cb);
};
self.set = function(html){
addToContainer(html);
};
self.cache = function(){
self.read.apply(self, arguments);
};
self.sandbox = function(obj){
addToContainer(objToHTML(obj));
};
self.read = function(){
var htmlChunks = '';
Array.prototype.slice.call(arguments, 0).forEach(function(argument){
if (typeof argument === 'string') htmlChunks += getFixtureHtml(argument);
});
return htmlChunks;
};
self.clearCache = function(){
fixturesCache = {};
};
self.cleanUp = function(){
var iframe = document.getElementById(self.containerId);
if(!iframe) return null;
iframe.parentNode.removeChild(iframe);
};
var createContainer = function(html){
var cb = typeof arguments[arguments.length - 1] === 'function' ? arguments[arguments.length -1] : null;
var iframe = document.createElement('iframe');
iframe.setAttribute('id', self.containerId);
iframe.style.opacity = 0;
iframe.style.filter = 'alpha(0)';
document.body.appendChild(iframe);
var doc = iframe.contentWindow || iframe.contentDocument;
doc = doc.document ? doc.document : doc;
if (cb){
var iframeReady = setInterval(function(){
if (doc.readyState === 'complete'){
clearInterval(iframeReady);
cb();
}
}, 0);
}
doc.open();
doc.defaultView.onerror = captureErrors;
doc.write(html);
doc.close();
};
var addToContainer = function(html, cb){
var container = document.getElementById(self.containerId);
if (!container) createContainer.apply(self, arguments);
else self.window().document.body.innerHTML += html;
};
var captureErrors = function(){
if (window.onerror){
// Rewrite the message prefix to indicate that the error
// occurred in the fixture.
arguments[0] = arguments[0].replace(/^[^:]*/, "Uncaught fixture error");
window.onerror.apply(window, arguments);
}
return true;
};
var getFixtureHtml = function(url){
if (typeof fixturesCache[url] === 'undefined'){
loadFixtureIntoCache(url);
}
return fixturesCache[url];
};
var loadFixtureIntoCache = function(relativeUrl){
var url = makeFixtureUrl(relativeUrl);
var request = new XMLHttpRequest();
request.open('GET', url + '?' + new Date().getTime(), false);
request.send(null);
fixturesCache[relativeUrl] = request.responseText;
};
var makeFixtureUrl = function(relativeUrl){
return self.path.match('/$') ? self.path + relativeUrl : self.path + '/' + relativeUrl;
};
var objToHTML = function(obj){
var divElem = document.createElement('div');
for (var key in obj){
if (key === 'class'){ // IE < 9 compatibility
divElem.className = obj[key];
continue;
}
divElem.setAttribute(key, obj[key]);
}
return divElem.outerHTML;
};
}
));