envjs 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. data/.jslintrbrc +29 -0
  2. data/.project +17 -0
  3. data/CHANGELOG.rdoc +3 -0
  4. data/DTD/xhtml-lat1.ent +196 -0
  5. data/DTD/xhtml-special.ent +80 -0
  6. data/DTD/xhtml-symbol.ent +237 -0
  7. data/DTD/xhtml.soc +14 -0
  8. data/DTD/xhtml1-frameset.dtd +1235 -0
  9. data/DTD/xhtml1-strict.dtd +978 -0
  10. data/DTD/xhtml1-transitional.dtd +1201 -0
  11. data/DTD/xhtml1.dcl +192 -0
  12. data/Makefile +7 -0
  13. data/Manifest.txt +287 -0
  14. data/{README → README.rdoc} +24 -3
  15. data/Rakefile +196 -0
  16. data/Wakefile +24 -0
  17. data/build.properties +9 -0
  18. data/build.xml +247 -0
  19. data/gm/jquery.js +6002 -0
  20. data/gm/mainx.js +2648 -0
  21. data/gm/sensx.js +135 -0
  22. data/gm/t.js +6 -0
  23. data/gm/x.html +76 -0
  24. data/htmlparser/BrowserTreeBuilder.java +456 -0
  25. data/htmlparser/README +34 -0
  26. data/htmlparser/build.sh +38 -0
  27. data/jsl/jsl +0 -0
  28. data/jsl/jsl.default.conf +129 -0
  29. data/jsl/jsl.exe +0 -0
  30. data/lib/envjs.rb +2 -0
  31. data/lib/envjs/env.js +22 -3
  32. data/lib/envjs/event_loop.js +2 -0
  33. data/lib/envjs/static.js +155 -21
  34. data/licenses/GPL-LICENSE.txt +278 -0
  35. data/licenses/MIT-LICENSE.txt +20 -0
  36. data/src/base64.js +80 -0
  37. data/src/build.js +6 -0
  38. data/src/cruft/bad.html +24 -0
  39. data/src/cruft/dom.js +606 -0
  40. data/src/cruft/element.js +297 -0
  41. data/src/cruft/good.html +30 -0
  42. data/src/cruft/good.js +32 -0
  43. data/src/cruft/internal.js +81 -0
  44. data/src/cruft/parser.js +458 -0
  45. data/src/css/properties.js +293 -0
  46. data/src/css/rule.js +22 -0
  47. data/src/css/sizzle.js +717 -0
  48. data/src/css/stylesheet.js +52 -0
  49. data/src/dom/attr.js +55 -0
  50. data/src/dom/cdatasection.js +31 -0
  51. data/src/dom/characterdata.js +119 -0
  52. data/src/dom/comment.js +30 -0
  53. data/src/dom/doctype.js +9 -0
  54. data/src/dom/document.js +553 -0
  55. data/src/dom/dom.js +134 -0
  56. data/src/dom/element.js +217 -0
  57. data/src/dom/entities.js +273 -0
  58. data/src/dom/exception.js +28 -0
  59. data/src/dom/fragment.js +37 -0
  60. data/src/dom/implementation.js +602 -0
  61. data/src/dom/instruction.js +51 -0
  62. data/src/dom/namednodemap.js +374 -0
  63. data/src/dom/namespace.js +50 -0
  64. data/src/dom/node.js +618 -0
  65. data/src/dom/nodelist.js +195 -0
  66. data/src/dom/parser.js +1207 -0
  67. data/src/dom/text.js +73 -0
  68. data/src/event/event.js +39 -0
  69. data/src/event/mouseevent.js +4 -0
  70. data/src/event/uievent.js +8 -0
  71. data/src/html/a.js +110 -0
  72. data/src/html/anchor.js +80 -0
  73. data/src/html/area.js +57 -0
  74. data/src/html/base.js +26 -0
  75. data/src/html/blockquote-q.js +19 -0
  76. data/src/html/body.js +19 -0
  77. data/src/html/button.js +21 -0
  78. data/src/html/canvas.js +14 -0
  79. data/src/html/col-colgroup.js +49 -0
  80. data/src/html/collection.js +72 -0
  81. data/src/html/cookie.js +151 -0
  82. data/src/html/del-ins.js +25 -0
  83. data/src/html/div.js +28 -0
  84. data/src/html/document.js +359 -0
  85. data/src/html/element.js +380 -0
  86. data/src/html/fieldset.js +19 -0
  87. data/src/html/form.js +484 -0
  88. data/src/html/frame.js +89 -0
  89. data/src/html/frameset.js +25 -0
  90. data/src/html/head.js +44 -0
  91. data/src/html/html.js +0 -0
  92. data/src/html/htmlparser.js +340 -0
  93. data/src/html/iframe.js +26 -0
  94. data/src/html/image.js +0 -0
  95. data/src/html/img.js +62 -0
  96. data/src/html/input-elements.js +307 -0
  97. data/src/html/input.js +65 -0
  98. data/src/html/label.js +26 -0
  99. data/src/html/legend.js +19 -0
  100. data/src/html/link.js +82 -0
  101. data/src/html/map.js +22 -0
  102. data/src/html/meta.js +37 -0
  103. data/src/html/object.js +89 -0
  104. data/src/html/optgroup.js +25 -0
  105. data/src/html/option.js +97 -0
  106. data/src/html/param.js +38 -0
  107. data/src/html/script.js +122 -0
  108. data/src/html/select.js +129 -0
  109. data/src/html/style.js +31 -0
  110. data/src/html/table.js +199 -0
  111. data/src/html/tbody-thead-tfoot.js +91 -0
  112. data/src/html/td-th.js +18 -0
  113. data/src/html/textarea.js +25 -0
  114. data/src/html/title.js +20 -0
  115. data/src/html/tr.js +114 -0
  116. data/src/intro.js +141 -0
  117. data/src/outro.js +70 -0
  118. data/src/parser/html5.detailed.js +10762 -0
  119. data/src/parser/html5.min.js +503 -0
  120. data/src/parser/html5.pretty.js +10815 -0
  121. data/src/parser/intro.js +42 -0
  122. data/src/parser/outro.js +9 -0
  123. data/src/platform/core.js +323 -0
  124. data/src/platform/johnson.js +479 -0
  125. data/src/platform/rhino.js +327 -0
  126. data/src/platform/static/intro.js +41 -0
  127. data/src/platform/static/outro.js +30 -0
  128. data/src/profile/aop.js +238 -0
  129. data/src/profile/profile.js +402 -0
  130. data/src/serializer/xml.js +21 -0
  131. data/src/svg/animatedstring.js +25 -0
  132. data/src/svg/document.js +25 -0
  133. data/src/svg/element.js +22 -0
  134. data/src/svg/locatable.js +17 -0
  135. data/src/svg/rect.js +18 -0
  136. data/src/svg/rectelement.js +24 -0
  137. data/src/svg/stylable.js +49 -0
  138. data/src/svg/svgelement.js +22 -0
  139. data/src/svg/transformable.js +15 -0
  140. data/src/window/css.js +15 -0
  141. data/src/window/dialog.js +16 -0
  142. data/src/window/document.js +28 -0
  143. data/src/window/event.js +262 -0
  144. data/src/window/history.js +62 -0
  145. data/src/window/location.js +138 -0
  146. data/src/window/navigator.js +48 -0
  147. data/src/window/screen.js +53 -0
  148. data/src/window/timer.js +21 -0
  149. data/src/window/window.js +284 -0
  150. data/src/window/xhr.js +127 -0
  151. data/src/xpath/expression.js +49 -0
  152. data/src/xpath/implementation.js +2482 -0
  153. data/src/xpath/result.js +67 -0
  154. data/src/xpath/util.js +551 -0
  155. data/src/xpath/xmltoken.js +149 -0
  156. data/src/xslt/COPYING +34 -0
  157. data/src/xslt/ajaxslt-0.8.1/AUTHORS +1 -0
  158. data/src/xslt/ajaxslt-0.8.1/ChangeLog +136 -0
  159. data/src/xslt/ajaxslt-0.8.1/Makefile +49 -0
  160. data/src/xslt/ajaxslt-0.8.1/README +102 -0
  161. data/src/xslt/ajaxslt-0.8.1/TODO +15 -0
  162. data/src/xslt/ajaxslt-0.8.1/dom.js +566 -0
  163. data/src/xslt/ajaxslt-0.8.1/dom_unittest.html +24 -0
  164. data/src/xslt/ajaxslt-0.8.1/dom_unittest.js +131 -0
  165. data/src/xslt/ajaxslt-0.8.1/simplelog.js +79 -0
  166. data/src/xslt/ajaxslt-0.8.1/test/xpath.html +18 -0
  167. data/src/xslt/ajaxslt-0.8.1/test/xpath_script.js +45 -0
  168. data/src/xslt/ajaxslt-0.8.1/test/xslt.html +58 -0
  169. data/src/xslt/ajaxslt-0.8.1/test/xslt_script.js +33 -0
  170. data/src/xslt/ajaxslt-0.8.1/unittestsuite.html +26 -0
  171. data/src/xslt/ajaxslt-0.8.1/xmltoken.js +149 -0
  172. data/src/xslt/ajaxslt-0.8.1/xmltoken_unittest.html +18 -0
  173. data/src/xslt/ajaxslt-0.8.1/xmltoken_unittest.js +811 -0
  174. data/src/xslt/ajaxslt-0.8.1/xpath_unittest.html +39 -0
  175. data/src/xslt/ajaxslt-0.8.1/xpath_unittest.js +557 -0
  176. data/src/xslt/ajaxslt-0.8.1/xpathdebug.js +234 -0
  177. data/src/xslt/ajaxslt-0.8.1/xslt_unittest.html +138 -0
  178. data/src/xslt/ajaxslt-0.8.1/xslt_unittest.js +68 -0
  179. data/src/xslt/implementation.js +625 -0
  180. data/src/xslt/processor.js +37 -0
  181. data/src/xslt/util.js +449 -0
  182. data/test/foo.html +8 -0
  183. data/test/foo.js +40 -0
  184. data/test/jquery.js +6002 -0
  185. data/test/x.js +1 -0
  186. data/test/y.js +1 -0
  187. metadata +245 -14
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @author thatcher
3
+ */
4
+
5
+ (function(/*window,document*/){
6
+
7
+ var Html5Parser;
8
+
9
+ var psettimeout;
10
+
11
+ var sync = function(window,parser){
12
+ parser.ptimeouts = [];
13
+ parser.pschedule = function($schedule,timer,t) {
14
+ var old = psettimeout;
15
+ psettimeout = function(fn){
16
+ parser.ptimeouts.push(fn);
17
+ };
18
+ $schedule(window,timer,t);
19
+ psettimeout = old;
20
+ };
21
+ parser.pwait = function() {
22
+ var fn;
23
+ while ((fn = parser.ptimeouts.pop())) {
24
+ fn();
25
+ };
26
+ };
27
+ };
28
+
29
+ var async = function(window,parser) {
30
+ delete parser.ptimeouts;
31
+ parser.pschedule = function($schedule,timer,t) {
32
+ var old = psettimeout;
33
+ psettimeout = window.setTimeout;
34
+ $schedule(window,timer,t);
35
+ psettimeout = old;
36
+ };
37
+ parser.pwait = function(){
38
+ // $master.print("wait called");
39
+ window.$envx.wait(-1);
40
+ // $master.print("after wait");
41
+ };
42
+ };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @author thatcher
3
+ */
4
+
5
+ envjs_init_5 = function() {
6
+ Html5Parser();
7
+ };
8
+
9
+ })();
@@ -0,0 +1,323 @@
1
+
2
+ /**
3
+ * @author thatcher
4
+ */
5
+ var Envjs = function(){
6
+ if(arguments.length === 2){
7
+ for ( var i in arguments[1] ) {
8
+ var g = arguments[1].__lookupGetter__(i),
9
+ s = arguments[1].__lookupSetter__(i);
10
+ if ( g || s ) {
11
+ if ( g ) { Envjs.__defineGetter__(i, g); }
12
+ if ( s ) { Envjs.__defineSetter__(i, s); }
13
+ } else {
14
+ Envjs[i] = arguments[1][i];
15
+ }
16
+ }
17
+ }
18
+
19
+ if (arguments[0] !== null && arguments[0] !== "") {
20
+ window.location = arguments[0];
21
+ }
22
+ };
23
+
24
+ /*
25
+ * core.js
26
+ */
27
+ (function($env){
28
+
29
+ //You can emulate different user agents by overriding these after loading env
30
+ $env.appCodeName = "Envjs";//eg "Mozilla"
31
+ $env.appName = "Resig/20070309 BirdDog/0.0.0.1";//eg "Gecko/20070309 Firefox/2.0.0.3"
32
+
33
+ //set this to true and see profile/profile.js to select which methods
34
+ //to profile
35
+ $env.profile = false;
36
+
37
+ $env.log = $env.log || function(msg, level){};
38
+
39
+ $env.DEBUG = 4;
40
+ $env.INFO = 3;
41
+ $env.WARN = 2;
42
+ $env.ERROR = 1;
43
+ $env.NONE = 0;
44
+
45
+ //set this if you want to get some internal log statements
46
+ $env.logLevel = $env.INFO;
47
+ $env.logLevel = $env.ERROR;
48
+ $env.logLevel = $env.DEBUG;
49
+ $env.logLevel = $env.WARN;
50
+
51
+ $env.debug = function(msg){
52
+ if($env.logLevel >= $env.DEBUG) {
53
+ $env.log(msg,"DEBUG");
54
+ }
55
+ };
56
+ $env.info = function(msg){
57
+ if($env.logLevel >= $env.INFO) {
58
+ $env.log(msg,"INFO");
59
+ }
60
+ };
61
+ $env.warn = function(msg){
62
+ if($env.logLevel >= $env.WARN) {
63
+ $env.log(msg,"WARNIING");
64
+ }
65
+ };
66
+ $env.error = function(msg, e){
67
+ if ($env.logLevel >= $env.ERROR) {
68
+ var line = $env.lineSource(e);
69
+ if ( line !== "" ) { line = " Line: "+ line ;}
70
+ $env.log(msg + line, 'ERROR');
71
+ if(e) {
72
+ $env.log(e || "", 'ERROR');
73
+ }
74
+ }
75
+ };
76
+
77
+ $env.debug("Initializing Core Platform Env");
78
+
79
+
80
+ // if we're running in an environment without env.js' custom extensions
81
+ // for manipulating the JavaScript scope chain, put in trivial emulations
82
+ $env.debug("performing check for custom Java methods in env-js.jar");
83
+ var countOfMissing = 0, dontCare;
84
+ try { dontCare = getFreshScopeObj; }
85
+ catch (ex){ getFreshScopeObj = function(){ return {}; };
86
+ countOfMissing++; }
87
+ try { dontCare = getProxyFor; }
88
+ catch (ex){ getProxyFor = function(obj){ return obj; };
89
+ countOfMissing++; }
90
+ try { dontCare = getScope; }
91
+ catch (ex){ getScope = function(){}; countOfMissing++; }
92
+ try { dontCare = setScope; }
93
+ catch (ex){ setScope = function(){}; countOfMissing++; }
94
+ try { dontCare = configureScope; }
95
+ catch (ex){ configureScope = function(){}; countOfMissing++; }
96
+ try { dontCare = restoreScope; }
97
+ catch (ex){ restoreScope = function(){}; countOfMissing++; }
98
+ try { $env.loadIntoFnsScope = loadIntoFnsScope; }
99
+ catch (ex){ $env.loadIntoFnsScope = load; countOfMissing++; }
100
+ if (countOfMissing != 0 && countOfMissing != 7)
101
+ $env.warn("Some but not all of scope-manipulation functions were " +
102
+ "not present in environment. JavaScript execution may " +
103
+ "not occur correctly.");
104
+
105
+ $env.lineSource = $env.lineSource || function(e){};
106
+
107
+ //resolves location relative to base or window location
108
+ $env.location = $env.location || function(path, base){};
109
+
110
+ $env.sync = $env.sync || function(fn){
111
+ return function(){ return fn.apply(this,arguments); };
112
+ };
113
+
114
+ $env.spawn = $env.spawn || function(fn) {
115
+ setTimeout(fn,0);
116
+ };
117
+
118
+ $env.sleep = $env.sleep || function(){};
119
+
120
+ $env.javaEnabled = false;
121
+
122
+ //Used in the XMLHttpRquest implementation to run a
123
+ // request in a seperate thread
124
+ $env.runAsync = $env.runAsync || function(fn){};
125
+
126
+ //Used to write to a local file
127
+ $env.writeToFile = $env.writeToFile || function(text, url){};
128
+
129
+ //Used to write to a local file
130
+ $env.writeToTempFile = $env.writeToTempFile || function(text, suffix){};
131
+
132
+ //Used to delete a local file
133
+ $env.deleteFile = $env.deleteFile || function(url){};
134
+
135
+ $env.connection = $env.connection || function(xhr, responseHandler, data){};
136
+
137
+ $env.parseHTML = function(htmlstring){};
138
+ $env.parseXML = function(xmlstring){};
139
+ $env.xpath = function(expression, doc){};
140
+
141
+ $env.tmpdir = '';
142
+ $env.os_name = '';
143
+ $env.os_arch = '';
144
+ $env.os_version = '';
145
+ $env.lang = '';
146
+ $env.platform = "";
147
+
148
+ $env.scriptTypes = {
149
+ "text/javascript" :true,
150
+ "text/envjs" :true
151
+ };
152
+
153
+ $env.onScriptLoadError = $env.onScriptLoadError || function(){};
154
+ $env.loadLocalScript = function(script, parser){
155
+ $env.debug("loading script ");
156
+ // print("loadloacl");
157
+ // try{throw new Error}catch(e){print(e.stack);}
158
+ var types, type, src, i, base,
159
+ docWrites = [],
160
+ write = document.write,
161
+ writeln = document.writeln,
162
+ okay = true;
163
+ // SMP: see also the note in html/document.js about script.type
164
+ var script_type = script.type === null ? "text/javascript" : script.type;
165
+ try{
166
+ if(script_type){
167
+ types = script_type?script_type.split(";"):[];
168
+ for(i=0;i<types.length;i++){
169
+ if($env.scriptTypes[types[i]]){
170
+ if(script.src){
171
+ $env.info("loading allowed external script: " + script.src);
172
+ //lets you register a function to execute
173
+ //before the script is loaded
174
+ if($env.beforeScriptLoad){
175
+ for(src in $env.beforeScriptLoad){
176
+ if(script.src.match(src)){
177
+ $env.beforeScriptLoad[src]();
178
+ }
179
+ }
180
+ }
181
+ base = "" + window.location;
182
+ var filename = $env.location(script.src, base );
183
+ try {
184
+ load(filename);
185
+ } catch(e) {
186
+ $env.warn("could not load script "+ filename +": "+e );
187
+ okay = false;
188
+ }
189
+ //lets you register a function to execute
190
+ //after the script is loaded
191
+ if($env.afterScriptLoad){
192
+ for(src in $env.afterScriptLoad){
193
+ if(script.src.match(src)){
194
+ $env.afterScriptLoad[src]();
195
+ }
196
+ }
197
+ }
198
+ }else{
199
+ $env.loadInlineScript(script);
200
+ }
201
+ }else{
202
+ if(!script.src && script_type == "text/javascript"){
203
+ $env.loadInlineScript(script);
204
+ } else {
205
+ // load prohbited ...
206
+ okay = false;
207
+ }
208
+ }
209
+ }
210
+ }else{
211
+ // SMP this branch is probably dead ...
212
+ //anonymous type and anonymous src means inline
213
+ if(!script.src){
214
+ $env.loadInlineScript(script);
215
+ }
216
+ }
217
+ }catch(e){
218
+ okay = false;
219
+ $env.error("Error loading script.", e);
220
+ $env.onScriptLoadError(script);
221
+ }finally{
222
+ /*if(parser){
223
+ parser.appendFragment(docWrites.join(''));
224
+ }
225
+ //return document.write to it's non-script loading form
226
+ document.write = write;
227
+ document.writeln = writeln;*/
228
+ }
229
+ return okay;
230
+ };
231
+ $env.loadInlineScript = $env.loadInlineScript || function(script){};
232
+
233
+ $env.loadFrame = function(frameElement, url){
234
+ try {
235
+ if (frameElement._content){
236
+ $env.unload(frameElement._content);
237
+ $env.reload(frameElement._content, url);
238
+ }
239
+ else {
240
+ var v = $env.newwindow($w,
241
+ frameElement.ownerDocument.parentWindow, url);
242
+ frameElement._content = v;
243
+ }
244
+ } catch(e){
245
+ $env.error("failed to load frame content: from " + url, e);
246
+ }
247
+ };
248
+
249
+ $env.reload = $env.reload || function(oldWindowProxy, url){
250
+ var newWindowProxy = $env.newwindow(
251
+ oldWindowProxy.opener,
252
+ oldWindowProxy.parent,
253
+ url);
254
+ var newWindow = newWindowProxy.__proto__;
255
+
256
+ oldWindowProxy.__proto__ = newWindow;
257
+ newWindow.$thisWindowsProxyObject = oldWindowProxy;
258
+ newWindow.document._parentWindow = oldWindowProxy;
259
+ };
260
+
261
+ $env.newwindow = $env.newwindow || function(openingWindow, parentArg, url){
262
+ var newWindow = $env.getFreshScopeObj();
263
+ var newProxy = $env.getProxyFor(newWindow);
264
+ newWindow.$thisWindowsProxyObject = newProxy;
265
+
266
+ var local__window__ = $env.window,
267
+ local_env = $env,
268
+ local_opener = openingWindow,
269
+ local_parent = parentArg ? parentArg : newWindow;
270
+
271
+ var inNewContext = function(){
272
+ local__window__(newWindow, // object to "window-ify"
273
+ local_env, // our scope for globals
274
+ local_parent, // win's "parent"
275
+ local_opener, // win's "opener"
276
+ local_parent.top, // win's "top"
277
+ false // this win isn't the original
278
+ );
279
+ print("QQ");
280
+ if (url)
281
+ // newWindow.__loadAWindowsDocument__(url);
282
+ $env.load(url);
283
+ };
284
+
285
+ var scopes = recordScopesOfKeyObjects(inNewContext);
286
+ setScopesOfKeyObjects(inNewContext, newWindow);
287
+ print("ZZ");
288
+ inNewContext(); // invoke local fn to window-ify new scope object
289
+ print("TT");
290
+ restoreScopesOfKeyObjects(inNewContext, scopes);
291
+ return newProxy;
292
+ };
293
+
294
+ function recordScopesOfKeyObjects(fnToExecInOtherContext){
295
+ return { // getScope()/setScope() from Window.java
296
+ frame : getScope(fnToExecInOtherContext),
297
+ window : getScope($env.window),
298
+ global_load : getScope($env.loadIntoFnsScope),
299
+ local_load : getScope($env.loadLocalScript)
300
+ };
301
+ }
302
+
303
+ function setScopesOfKeyObjects(fnToExecInOtherContext, windowObj){
304
+ setScope(fnToExecInOtherContext, windowObj);
305
+ setScope($env.window, windowObj);
306
+ setScope($env.loadIntoFnsScope, windowObj);
307
+ setScope($env.loadLocalScript, windowObj);
308
+ }
309
+
310
+ function restoreScopesOfKeyObjects(fnToExecInOtherContext, scopes){
311
+ setScope(fnToExecInOtherContext, scopes.frame);
312
+ setScope($env.window, scopes.window);
313
+ setScope($env.loadIntoFnsScope, scopes.global_load);
314
+ setScope($env.loadLocalScript, scopes.local_load);
315
+ }
316
+
317
+ })($env);
318
+
319
+ // Local Variables:
320
+ // espresso-indent-level:4
321
+ // c-basic-offset:4
322
+ // tab-width:4
323
+ // End:
@@ -0,0 +1,479 @@
1
+ $env.log = function(msg, level){
2
+ debug(' '+ (level?level:'LOG') + ':\t['+ new Date()+"] {ENVJS} "+msg);
3
+ };
4
+
5
+ $env.location = function(path, base){
6
+ var debug = false;
7
+ debug && print("loc",path,base);
8
+ if ( path == "about:blank" ) {
9
+ return path;
10
+ }
11
+ var protocol = new RegExp('(^file\:|^http\:|^https\:|data:)');
12
+ var m = protocol.exec(path);
13
+ if(m&&m.length>1){
14
+ var url = Ruby.URI.parse(path);
15
+ var s = url.toString();
16
+ if ( s.substring(0,6) == "file:/" && s[6] != "/" ) {
17
+ s = "file://" + s.substring(5,s.length);
18
+ }
19
+ debug && print("YY",s);
20
+ return s;
21
+ }else if(base){
22
+ base = Ruby.URI.parse(base);
23
+ path = Ruby.URI.parse(path);
24
+ var b = Ruby.eval("lambda { |a,b| a+b; }");
25
+ base = b(base,path);
26
+ base = base + "";
27
+ var result = base;
28
+ // print("ZZ",result);
29
+ // ? This path only used for files?
30
+ if ( result.substring(0,6) == "file:/" && result[6] != "/" ) {
31
+ result = "file://" + result.substring(5,result.length);
32
+ }
33
+ if ( result.substring(0,7) == "file://" ) {
34
+ result = result.substring(7,result.length);
35
+ }
36
+ debug && print("ZZ",result);
37
+ return result;
38
+ }else{
39
+ //return an absolute url from a url relative to the window location
40
+ // print("hi", $master.first_script_window, $master.first_script_window && $master.first_script_window.location );
41
+ // if( ( base = window.location ) &&
42
+ if( ( base = ( ( $master.first_script_window && $master.first_script_window.location ) || window.location ) ) &&
43
+ ( base != "about:blank" ) &&
44
+ base.href &&
45
+ (base.href.length > 0) ) {
46
+
47
+ base_uri = Ruby.URI.parse(base.href);
48
+ new_uri = Ruby.URI.parse(path);
49
+ result = Ruby.eval("lambda { |a,b| a+b; }")(base_uri,new_uri)+"";
50
+ debug && print("IIII",result);
51
+ return result;
52
+
53
+ base = base.href.substring(0, base.href.lastIndexOf('/'));
54
+ var result;
55
+ // print("XXXXX",base);
56
+ if ( base[base.length-1] == "/" || path[0] == "/" ) {
57
+ result = base + path;
58
+ } else {
59
+ result = base + '/' + path;
60
+ }
61
+ if ( result.substring(0,6) == "file:/" && result[6] != "/" ) {
62
+ result = "file://" + result.substring(5,result.length);
63
+ }
64
+ debug && print("****",result);
65
+ return result;
66
+ } else {
67
+ // print("RRR",result);
68
+ return "file://"+Ruby.File.expand_path(path);
69
+ }
70
+ }
71
+ };
72
+
73
+ $env.connection = $master.connection || function(xhr, responseHandler, data){
74
+ var url = Ruby.URI.parse(xhr.url);
75
+ var connection;
76
+ var resp;
77
+ // print("xhr",xhr.url);
78
+ // print("xhr",url);
79
+ if ( /^file\:/.test(url) ) {
80
+ // experimental hack
81
+ try {
82
+ Ruby.eval("require 'envjs/net/cgi'");
83
+ resp = connection = new Ruby.Envjs.Net.CGI( xhr, data );
84
+ } catch(e) {
85
+ try{
86
+ if ( xhr.method == "PUT" ) {
87
+ var text = data || "" ;
88
+ $env.writeToFile(text, url);
89
+ } else if ( xhr.method == "DELETE" ) {
90
+ $env.deleteFile(url);
91
+ } else {
92
+ Ruby.eval("require 'envjs/net/file'");
93
+ var request = new Ruby.Envjs.Net.File.Get( url.path );
94
+ connection = Ruby.Envjs.Net.File.start( url.host, url.port );
95
+ resp = connection.request( request );
96
+ //try to add some canned headers that make sense
97
+
98
+ try{
99
+ if(xhr.url.match(/html$/)){
100
+ xhr.responseHeaders["Content-Type"] = 'text/html';
101
+ }else if(xhr.url.match(/.xml$/)){
102
+ xhr.responseHeaders["Content-Type"] = 'text/xml';
103
+ }else if(xhr.url.match(/.js$/)){
104
+ xhr.responseHeaders["Content-Type"] = 'text/javascript';
105
+ }else if(xhr.url.match(/.json$/)){
106
+ xhr.responseHeaders["Content-Type"] = 'application/json';
107
+ }else{
108
+ xhr.responseHeaders["Content-Type"] = 'text/plain';
109
+ }
110
+ //xhr.responseHeaders['Last-Modified'] = connection.getLastModified();
111
+ //xhr.responseHeaders['Content-Length'] = headerValue+'';
112
+ //xhr.responseHeaders['Date'] = new Date()+'';*/
113
+ }catch(e){
114
+ $env.warn('failed to load response headers',e);
115
+ }
116
+
117
+ }
118
+ } catch (e) {
119
+ connection = null;
120
+ xhr.readyState = 4;
121
+ if(e.toString().match(/Errno::ENOENT/)) {
122
+ xhr.status = "404";
123
+ xhr.statusText = "Not Found";
124
+ xhr.responseText = undefined;
125
+ } else {
126
+ xhr.status = "500";
127
+ xhr.statusText = "Local File Protocol Error";
128
+ xhr.responseText = "<html><head/><body><p>"+ e+ "</p></body></html>";
129
+ }
130
+ }
131
+ }
132
+ } else {
133
+ Ruby.eval("require 'net/http'");
134
+
135
+ var req;
136
+ var path;
137
+ try {
138
+ path = url.request_uri();
139
+ } catch(e) {
140
+ path = url.path;
141
+ }
142
+ if ( xhr.method == "GET" ) {
143
+ req = new Ruby.Net.HTTP.Get( path );
144
+ } else if ( xhr.method == "POST" ) {
145
+ req = new Ruby.Net.HTTP.Post( path );
146
+ } else if ( xhr.method == "PUT" ) {
147
+ req = new Ruby.Net.HTTP.Put( path );
148
+ }
149
+
150
+ for (var header in xhr.headers){
151
+ $master.add_req_field( req, header, xhr.headers[header] );
152
+ }
153
+
154
+ //write data to output stream if required
155
+ if(data&&data.length&&data.length>0){
156
+ if ( xhr.method == "PUT" || xhr.method == "POST" ) {
157
+ Ruby.eval("lambda { |req,data| req.body = data}").call(req,data);
158
+ // req.body = data;
159
+ }
160
+ }
161
+
162
+ try {
163
+ connection = Ruby.Net.HTTP.start( url.host, url.port );
164
+ resp = connection.request(req);
165
+ } catch(e) {
166
+ $env.warn("XHR net request failed: "+e);
167
+ // FIX: do the on error stuff ...
168
+ throw e;
169
+ }
170
+
171
+ }
172
+ if(connection){
173
+ try{
174
+ if (false) {
175
+ var respheadlength = connection.getHeaderFields().size();
176
+ // Stick the response headers into responseHeaders
177
+ for (var i = 0; i < respheadlength; i++) {
178
+ var headerName = connection.getHeaderFieldKey(i);
179
+ var headerValue = connection.getHeaderField(i);
180
+ if (headerName)
181
+ xhr.responseHeaders[headerName+''] = headerValue+'';
182
+ }
183
+ }
184
+ resp.each(function(k,v){
185
+ xhr.responseHeaders[k] = v;
186
+ });
187
+ }catch(e){
188
+ $env.error('failed to load response headers: '+e);
189
+ throw e;
190
+ }
191
+
192
+ xhr.readyState = 4;
193
+ xhr.status = parseInt(resp.code,10) || 0;
194
+ xhr.statusText = connection.responseMessage || "";
195
+
196
+ var contentEncoding = resp["Content-Encoding"] || "utf-8",
197
+ baos = new Ruby.StringIO,
198
+ length,
199
+ stream = null,
200
+ responseXML = null;
201
+
202
+ try{
203
+ var lower = contentEncoding.toLowerCase();
204
+ stream = ( lower == "gzip" || lower == "decompress" ) ?
205
+ ( Ruby.raise("java") && new java.util.zip.GZIPInputStream(resp.getInputStream()) ) : resp;
206
+ }catch(e){
207
+ if (resp.code == "404")
208
+ $env.info('failed to open connection stream \n' +
209
+ e.toString(), e);
210
+ else
211
+ $env.error('failed to open connection stream \n' +
212
+ e.toString(), e);
213
+ stream = resp;
214
+ }
215
+
216
+ baos.write(resp.body);
217
+
218
+ baos.close();
219
+ connection.finish();
220
+
221
+ xhr.responseText = baos.string();
222
+ }
223
+ if(responseHandler){
224
+ $env.debug('calling ajax response handler');
225
+ responseHandler();
226
+ }
227
+ };
228
+
229
+ var extract_line =
230
+ Ruby.eval(
231
+ "lambda { |e| \
232
+ begin; \
233
+ e.stack.to_s.split(%(\n))[1].match(/:([^:]*)$/)[1]; \
234
+ rescue; %(unknown); end; \
235
+ }");
236
+
237
+ var get_exception = window.get_exception =
238
+ Ruby.eval(" \
239
+ lambda { |e| \
240
+ estr = e.to_s; \
241
+ estr.gsub!(/(<br \\/>)+/, %( )); \
242
+ ss = ''; \
243
+ ss = ss + %(Exception: ) + estr + %(\n); \
244
+ begin; \
245
+ e.stack.to_s.split(%(\n)).each do |line| \
246
+ m = line.match(/(.*)@([^@]*)$/); \
247
+ m[2] == %(:0) && next; \
248
+ s = m[1]; \
249
+ s.gsub!(/(<br \\/>)+/, %( )); \
250
+ limit = 100; \
251
+ if ( s.length > limit ); \
252
+ s = s[0,limit] + %(...); \
253
+ end; \
254
+ ss = ss + m[2] + %( ) + s + %(\n); \
255
+ end; \
256
+ rescue; end; \
257
+ ss; \
258
+ } \
259
+ ");
260
+
261
+ var get_exception_trace = window.get_exception_trace =
262
+ Ruby.eval(" \
263
+ lambda { |e| \
264
+ estr = e.to_s; \
265
+ estr.gsub!(/(<br \\/>)+/, %( )); \
266
+ begin; \
267
+ ss = ''; \
268
+ e.stack.to_s.split(%(\n)).each do |line| \
269
+ m = line.match(/(.*)@([^@]*)$/); \
270
+ m[2] == %(:0) && next; \
271
+ s = m[1]; \
272
+ s.gsub!(/(<br \\/>)+/, %( )); \
273
+ limit = 100; \
274
+ if ( s.length > limit ); \
275
+ s = s[0,limit] + %(...); \
276
+ end; \
277
+ ss = ss + m[2] + %( ) + s +%(\n); \
278
+ end; \
279
+ rescue; end; \
280
+ ss; \
281
+ } \
282
+ ");
283
+
284
+ var print_exception = window.print_exception =
285
+ Ruby.eval(" \
286
+ lambda { |e| \
287
+ estr = e.to_s; \
288
+ estr.gsub!(/(<br \\/>)+/, %( )); \
289
+ $stderr.print(%(Exception: ),estr,%(\n)); \
290
+ begin; \
291
+ e.stack.to_s.split(%(\n)).each do |line| \
292
+ m = line.match(/(.*)@([^@]*)$/); \
293
+ m[2] == %(:0) && next; \
294
+ s = m[1]; \
295
+ s.gsub!(/(<br \\/>)+/, %( )); \
296
+ limit = 100; \
297
+ if ( s.length > limit ); \
298
+ s = s[0,limit] + %(...); \
299
+ end; \
300
+ $stderr.print(m[2],%( ),s,%(\n)); \
301
+ end; \
302
+ rescue; end; \
303
+ } \
304
+ ");
305
+
306
+ var print_exception_trace = window.print_exception_trace =
307
+ Ruby.eval(" \
308
+ lambda { |e| \
309
+ estr = e.to_s; \
310
+ estr.gsub!(/(<br \\/>)+/, %( )); \
311
+ begin; \
312
+ e.stack.to_s.split(%(\n)).each do |line| \
313
+ m = line.match(/(.*)@([^@]*)$/); \
314
+ m[2] == %(:0) && next; \
315
+ s = m[1]; \
316
+ s.gsub!(/(<br \\/>)+/, %( )); \
317
+ limit = 100; \
318
+ if ( s.length > limit ); \
319
+ s = s[0,limit] + %(...); \
320
+ end; \
321
+ $stderr.print(m[2],%( ),s,%(\n)); \
322
+ end; \
323
+ rescue; end; \
324
+ } \
325
+ ");
326
+
327
+ $env.lineSource = function(e){
328
+ if(e){
329
+ print_exception.call(e);
330
+ return extract_line.call(e);
331
+ } else {
332
+ return "";
333
+ }
334
+ };
335
+
336
+ $env.loadInlineScript = function(script){
337
+ var undef;
338
+ if (script.text === undef ||
339
+ script.text === null ||
340
+ script.text.match(/^\s*$/)) {
341
+ return;
342
+ }
343
+ var original_script_window = $master.first_script_window;
344
+ // debug("lis",original_script_window,original_script_window.isInner);
345
+ // debug("XX",window,window.isInner);
346
+ if ( !$master.first_script_window ) {
347
+ // $master.first_script_window = window;
348
+ }
349
+ // debug("lix",$master.first_script_window,$master.first_script_window.isInner,$w,$w.isInner);
350
+ try {
351
+ $master.evaluate(script.text,$inner);
352
+ } catch(e) {
353
+ $env.error("error evaluating script: "+script.text);
354
+ $env.error(e);
355
+ // try { throw new Error("here") } catch(b) { $env.error(b.stack); }
356
+ // throw e;
357
+ }
358
+ // $master.first_script_window = original_script_window;
359
+ // debug("lis",original_script_window,original_script_window.isInner);
360
+ };
361
+
362
+ $env.writeToTempFile = function(text, suffix){
363
+ $env.debug("writing text to temp url : " + suffix);
364
+ // print(text);
365
+ // Create temp file.
366
+ Ruby.eval("require 'envjs/tempfile'");
367
+ var temp = new Ruby.Envjs.TempFile( "envjs-tmp", suffix );
368
+
369
+ // Write to temp file
370
+ temp.write(text);
371
+ temp.close();
372
+ return temp.getAbsolutePath().toString()+'';
373
+ };
374
+
375
+ $env.writeToFile = function(text, url){
376
+ // print("writing text to url : " + url);
377
+ $env.debug("writing text to url : " + url);
378
+ if ( url.substring(0,7) == "file://" ) {
379
+ url = url.substring(7,url.length);
380
+ }
381
+ var file = Ruby.open( url, "w" );
382
+ // Write to temp file
383
+ file.write(text);
384
+ file.close();
385
+ };
386
+
387
+ $env.deleteFile = function(url){
388
+ Ruby.File.unlink(url);
389
+ };
390
+
391
+ $env.__eval__ = function(script,scope){
392
+ if (script == "")
393
+ return undefined;
394
+ try {
395
+ var scope = node;
396
+ var __scopes__ = [];
397
+ var original = script;
398
+ if(scope) {
399
+ // script = "(function(){return eval(original)}).call(__scopes__[0])";
400
+ script = "return (function(){"+original+"}).call(__scopes__[0])";
401
+ while(scope) {
402
+ __scopes__.push(scope);
403
+ scope = scope.parentNode;
404
+ script = "with(__scopes__["+(__scopes__.length-1)+"] ){"+script+"};";
405
+ }
406
+ }
407
+ script = "function(original,__scopes__){"+script+"}";
408
+ // print("scripta",script);
409
+ // print("scriptb",original);
410
+ var original_script_window = $master.first_script_window;
411
+ if ( !$master.first_script_window ) {
412
+ // $master.first_script_window = window;
413
+ }
414
+ // FIX!!!
415
+ var $inner = node.ownerDocument._parentWindow["$inner"];
416
+ var result = $master.evaluate(script,$inner)(original,__scopes__);
417
+ // $master.first_script_window = original_script_window;
418
+ return result;
419
+ }catch(e){
420
+ $warn("Exception during on* event eval: "+e);
421
+ throw e;
422
+ }
423
+ };
424
+
425
+ $env.newwindow = function(openingWindow, parentArg, url, outer,xhr_options){
426
+ // print(location);
427
+ // print("url",url,window.location,openingWindow);
428
+ // print("parent",parentArg);
429
+ var options = {
430
+ opener: openingWindow,
431
+ parent: parentArg,
432
+ url: $env.location(url),
433
+ xhr: xhr_options
434
+ };
435
+
436
+ // print("$w",$w);
437
+ // print("$ww",$w.window);
438
+ // print("$ww",$w === $w.window);
439
+ var pair = $env.new_window(outer);
440
+ var proxy = pair[0];
441
+ var new_window = pair[1];
442
+ options.proxy = proxy;
443
+ new_window.$options = options;
444
+ // print("$w",$w);
445
+ $master.load($master.Ruby.Envjs.ENVJS, new_window);
446
+ return proxy;
447
+ };
448
+
449
+ $env.reload = function(oldWindowProxy, url,options){
450
+ // print("reload",window,oldWindowProxy,url);
451
+ $env.newwindow( oldWindowProxy.opener,
452
+ oldWindowProxy.parent,
453
+ url,
454
+ oldWindowProxy,
455
+ options );
456
+ };
457
+
458
+ $env.sleep = function(n){Ruby.sleep(n/1000.);};
459
+
460
+ $env.loadIntoFnsScope = function(file) {
461
+ return load(file);
462
+ }
463
+
464
+ $env.runAsync = function(fn){
465
+ $env.debug("running async");
466
+
467
+ var run = $env.sync( function(){ fn(); } );
468
+
469
+ try{
470
+ $env.spawn(run);
471
+ }catch(e){
472
+ $env.error("error while running async", e);
473
+ }
474
+ };
475
+
476
+ // Local Variables:
477
+ // espresso-indent-level:4
478
+ // c-basic-offset:4
479
+ // End: