forked from cloudlinux/kuberdock-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser-shim.js
165 lines (154 loc) · 4.99 KB
/
browser-shim.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
This file is a part of:
https://github.com/nathanboktae/mocha-phantomjs-core
It's licensed under MIT, see
https://github.com/nathanboktae/mocha-phantomjs-core/blob/master/MIT-LICENSE
and local copy:
mocha-phantomjs-core_LICENSE.txt
*/
(function(){
if (typeof Function.prototype.bind !== 'function') {
Function.prototype.bind = function bind(obj) {
var args = Array.prototype.slice.call(arguments, 1),
self = this,
nop = function() {},
bound = function() {
return self.apply(
this instanceof nop ? this : (obj || {}), args.concat(
Array.prototype.slice.call(arguments)
)
)
}
nop.prototype = this.prototype || {}
bound.prototype = new nop()
return bound
}
}
function isFileReady(readyState) {
// Check to see if any of the ways a file can be ready are available as properties on the file's element
return (!readyState || readyState == 'loaded' || readyState == 'complete' || readyState == 'uninitialized')
}
function shimMochaProcess(M) {
// Mocha needs a process.stdout.write in order to change the cursor position.
M.process = M.process || {}
M.process.stdout = M.process.stdout || process.stdout
M.process.stdout.write = function(s) { window.callPhantom({ stdout: s }) }
window.callPhantom({ getColWith: true })
}
function shimMochaInstance(m) {
var origRun = m.run, origUi = m.ui
m.ui = function() {
var retval = origUi.apply(mocha, arguments)
window.callPhantom({ configureMocha: true })
m.reporter = function() {}
return retval
}
m.run = function() {
window.callPhantom({ testRunStarted: m.suite.suites.length })
m.runner = origRun.apply(mocha, arguments)
if (m.runner.stats && m.runner.stats.end) {
window.callPhantom({ testRunEnded: m.runner })
} else {
m.runner.on('end', function() {
window.callPhantom({ testRunEnded: m.runner })
})
}
return m.runner
}
}
Object.defineProperty(window, 'checkForMocha', {
value: function() {
var scriptTags = document.querySelectorAll('script'),
mochaScript = Array.prototype.filter.call(scriptTags, function(s) {
var src = s.getAttribute('src')
return src && src.match(/mocha\.js$/)
})[0]
if (mochaScript) {
mochaScript.onreadystatechange = mochaScript.onload = function () {
if (isFileReady(mochaScript.readyState)) {
initMochaPhantomJS()
}
}
}
}
})
if ('mozInnerScreenX' in window) {
// in slimerjs, we can stub out a setter to shim Mocha. phantomjs 2 fails
// to allow the property to be reconfigured...
Object.defineProperty(window, 'mocha', {
get: function() { return undefined },
set: function(m) {
shimMochaInstance(m)
delete window.mocha
window.mocha = m
},
configurable: true
})
Object.defineProperty(window, 'Mocha', {
get: function() { return undefined },
set: function(m) {
delete window.Mocha
window.Mocha = m
shimMochaProcess(m)
},
configurable: true
})
} else {
Object.defineProperty(window, 'initMochaPhantomJS', {
value: function () {
shimMochaProcess(Mocha)
shimMochaInstance(mocha)
delete window.initMochaPhantomJS
},
configurable: true
})
}
// Mocha needs the formating feature of console.log so copy node's format function and
// monkey-patch it into place. This code is copied from node's, links copyright applies.
// https://github.com/joyent/node/blob/master/lib/util.js
if (!console.format) {
console.format = function(f) {
if (typeof f !== 'string') {
return Array.prototype.map.call(arguments, function(arg) {
try {
return JSON.stringify(arg)
}
catch (_) {
return '[Circular]'
}
}).join(' ')
}
var i = 1;
var args = arguments;
var len = args.length;
var str = String(f).replace(/%[sdj%]/g, function(x) {
if (x === '%%') return '%';
if (i >= len) return x;
switch (x) {
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j':
try {
return JSON.stringify(args[i++]);
} catch (_) {
return '[Circular]';
}
default:
return x;
}
});
for (var x = args[i]; i < len; x = args[++i]) {
if (x === null || typeof x !== 'object') {
str += ' ' + x;
} else {
str += ' ' + JSON.stringify(x);
}
}
return str;
};
var origError = console.error;
console.error = function(){ origError.call(console, console.format.apply(console, arguments)); };
var origLog = console.log;
console.log = function(){ origLog.call(console, console.format.apply(console, arguments)); };
}
})();