forked from jeresig/env-js
/
core.js
238 lines (199 loc) · 7.87 KB
/
core.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* @author thatcher
*/
var Envjs = function(){
if(arguments.length === 2){
for ( var i in arguments[1] ) {
var g = arguments[1].__lookupGetter__(i),
s = arguments[1].__lookupSetter__(i);
if ( g || s ) {
if ( g ) Envjs.__defineGetter__(i, g);
if ( s ) Envjs.__defineSetter__(i, s);
} else
Envjs[i] = arguments[1][i];
}
}
window.location = arguments[0];
};
/*
* env.rhino.js
*/
(function($env){
//You can emulate different user agents by overriding these after loading env
$env.appCodeName = "Envjs";//eg "Mozilla"
$env.appName = "Resig/20070309 BirdDog/0.0.0.1";//eg "Gecko/20070309 Firefox/2.0.0.3"
//set this to true and see profile/profile.js to select which methods
//to profile
$env.profile = false;
$env.log = function(msg, level){};
$env.debug = function(){};
$env.info = function(){};
$env.warn = function(){};
$env.error = function(){};
//uncomment these if you want to get some internal log statementes
/*$env.debug = function(msg){
$env.log(msg,"DEBUG");
};*/
$env.info = function(msg){
$env.log(msg,"INFO");
};
$env.warn = function(msg){
$env.log(msg,"WARNIING");
};
$env.error = function(msg, e){
$env.log(msg+ " Line: "+ $env.lineSource(e),'ERROR');
$env.log(e||"",'ERROR');
};
$env.info("Initializing Core Platform Env");
$env.lineSource = function(e){};
$env.hashCode = function(obj){};
//resolves location relative to base or window location
$env.location = function(path, base){};
//For Java the window.timer is created using the java.lang.Thread in combination
//with the java.lang.Runnable
$env.timer = function(fn, time){};
$env.wait = function(wait){};
$env.javaEnabled = false;
//Used in the XMLHttpRquest implementation to run a
// request in a seperate thread
$env.runAsync = function(fn){};
//Used to write to a local file
$env.writeToFile = function(text, url){};
//Used to write to a local file
$env.writeToTempFile = function(text, suffix){};
//Used to delete a local file
$env.deleteFile = function(url){};
$env.connection = function(xhr, responseHandler, data){};
$env.parseHTML = function(htmlstring){};
$env.parseXML = function(xmlstring){};
$env.xpath = function(expression, doc){};
$env.tmpdir = '';
$env.os_name = '';
$env.os_arch = '';
$env.os_version = '';
$env.lang = '';
$env.platform = "Rhino ";//how do we get the version
$env.load = function(){};
$env.safeScript = function(){
//do nothing
};
$env.scriptTypes = {
"text/javascript" :false,
"text/envjs" :true
};
$env.loadLocalScript = function(script, parser){
$env.debug("loading script ");
var types, type, src, i, base,
docWrites = [],
write = document.write,
writeln = document.writeln;
//temporarily replace document write becuase the function
//has a different meaning during parsing
document.write = function(text){
docWrites.push(text);
};
try{
if(script.type){
types = script.type?script.type.split(";"):[];
for(i=0;i<types.length;i++){
if($env.scriptTypes[types[i]]){
if(script.src){
$env.info("loading allowed external script :" + script.src);
if($env.beforeload&&$env.beforeload[script.src]){
//lets you register a function to execute
//before the script is loaded
$env.beforeload[script.src]();
}
base = "" + window.location;
load($env.location(script.src.match(/([^\?#]*)/)[1], base ));
if($env.afterload&&$env.afterload[script.src]){
//lets you register a function to execute
//after the script is loaded
$env.afterload[script.src]();
}
}else{
$env.loadInlineScript(script);
}
}else{
if(!script.src && script.type == "text/javascript"){
$env.loadInlineScript(script);
}
}
}
}else{
//anonymous type and anonymous src means inline
if(!script.src){
$env.loadInlineScript(script);
}
}
}catch(e){
$env.error("Error loading script.", e);
}finally{
if(parser){
parser.appendFragment(docWrites.join(''));
}
//return document.write to it's non-script loading form
document.write = write;
document.writeln = writeln;
}
};
$env.loadInlineScript = function(script){};
$env.globalize = function(){};
$env.getScope = function(){};
$env.setScope = function(){};
$env.configureScope = function(){};
$env.restoreScope = function(){};
$env.loadFrame = function(frame, url){
try {
var frameWindow,
makingNewWinFlag = !(frame._content);
if (makingNewWinFlag)
// a blank object, inherits from original global
// see org.mozilla.javascript.tools.envjs.Window.java
frameWindow = $env.globalize();
else
frameWindow = frame._content;
// define local variables with content of things that are
// in current global/window, because when the following
// function executes we'll have a new/blank
// global/window and won't be able to get at them....
var local__window__ = $env.window,
local_env = $env,
local_window = frame.ownerDocument.parentWindow;
// a local function gives us something whose scope
// is easy to change
var __frame__ = function(){
if (makingNewWinFlag){
local__window__(frameWindow,
local_env,
local_window,
local_window.top);
}
frameWindow.location = url;
}
// change scope of window object creation
// functions, so that functions/code they create
// will be scoped to new window object
// getScope()/setScope() from Window.java
var scopes = {
frame : $env.getScope(__frame__),
window : $env.getScope(local__window__),
global_load: $env.getScope(load),
local_load: $env.getScope($env.loadLocalScript)
};
$env.setScope(__frame__, frameWindow);
$env.setScope(local__window__, frameWindow);
$env.setScope($env.load, frameWindow);
$env.setScope($env.loadLocalScript, frameWindow);
__frame__();
frame._content = frameWindow;
// now restore the scope
$env.setScope(__frame__, scopes.frame);
$env.setScope(local__window__, scopes.window);
$env.setScope($env.load, scopes.global_load);
$env.setScope($env.loadLocalScript, scopes.local_load);
} catch(e){
$env.error("failed to load frame content: from " + url, e);
}
};
})(Envjs);