smparkes-envjs 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/bin/envjsrb CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'rubygems'
4
+ require 'pathname'
4
5
 
5
6
  begin
6
7
  require 'johnson/tracemonkey'
@@ -23,9 +24,14 @@ RUNTIME.load Envjs::ENVJS
23
24
  if ARGV.length > 0
24
25
 
25
26
  begin
26
- ARGV.each do |file|
27
- if file =~ /\.x?html?$/
28
- RUNTIME.evaluate("window.location = '#{file}'", file, 1)
27
+ ARGV.each_with_index do |file,i|
28
+ if file =~ /\.x?html?$/ || file =~ %r(^https?://)
29
+ if i > 0
30
+ RUNTIME.wait
31
+ RUNTIME.evaluate("window.location = 'about:blank'", "(comand line)", 1)
32
+ end
33
+ RUNTIME.wait
34
+ RUNTIME.evaluate("window.location = 'file://#{Pathname.new(file).realpath}'", file, 1)
29
35
  else
30
36
  RUNTIME.load file
31
37
  end
data/lib/envjs/env.js CHANGED
@@ -48,11 +48,16 @@
48
48
  };
49
49
 
50
50
  $env.new_window = function(proxy){
51
+ var swap_script_window = ( $master.first_script_window.window === proxy );
51
52
  if(!proxy){
52
53
  proxy = $platform.new_split_global_outer();
54
+ // $master.print("np",proxy);
53
55
  }
54
56
  $master.proxy = proxy;
55
57
  new_window = $platform.new_split_global_inner(proxy,undefined);
58
+ if(swap_script_window) {
59
+ $master.first_script_window = new_window;
60
+ }
56
61
  new_window.$master = $master;
57
62
  for(var index in $master.symbols) {
58
63
  var symbol = $master.symbols[index];
@@ -78,26 +83,34 @@
78
83
  };
79
84
 
80
85
  $env.init_window = function(options){
86
+ options = options || {};
87
+
81
88
  $platform.init_window(this);
82
89
 
83
90
  var print = $master.print;
84
91
 
85
92
  // print("set",this);
86
93
  // print("set",this.window);
94
+ // print("set",options.proxy);
95
+ // print("set",this === options.proxy);
87
96
  if ( !this.window) {
88
97
  this.window = this;
89
98
  }
90
99
  // print("setx",this);
91
100
  // print("setx",this.window);
92
101
 
93
- options = options || {};
94
-
95
- var $w = this;$env.log = function(msg, level){
102
+ var $w = this;
103
+ // print("$$w",$w);
104
+ // print("$$w",$w === this);
105
+ $env.log = function(msg, level){
96
106
  print(' '+ (level?level:'LOG') + ':\t['+ new Date()+"] {ENVJS} "+msg);
97
107
  };
98
108
 
99
109
  $env.location = function(path, base){
100
110
  // print("loc",path,base);
111
+ if ( path == "about:blank" ) {
112
+ return path;
113
+ }
101
114
  var protocol = new RegExp('(^file\:|^http\:|^https\:)');
102
115
  var m = protocol.exec(path);
103
116
  if(m&&m.length>1){
@@ -123,14 +136,16 @@ $env.location = function(path, base){
123
136
  if ( result.substring(0,6) == "file:/" && result[6] != "/" ) {
124
137
  result = "file://" + result.substring(5,result.length);
125
138
  }
126
- if ( true && result.substring(0,7) == "file://" ) {
139
+ if ( result.substring(0,7) == "file://" ) {
127
140
  result = result.substring(7,result.length);
128
141
  }
129
142
  // print("ZZ",result);
130
143
  return result;
131
144
  }else{
132
145
  //return an absolute url from a url relative to the window location
133
- if( ( base = ( ( $master.first_script_window && $master.first_script_window.location ) || window.location) ) &&
146
+ // print("hi", $master.first_script_window, $master.first_script_window && $master.first_script_window.location );
147
+ if( ( base = ( ( $master.first_script_window && $master.first_script_window.location ) || window.location ) ) &&
148
+ ( base != "about:blank" ) &&
134
149
  base.href &&
135
150
  (base.href.length > 0) ) {
136
151
  base = base.href.substring(0, base.href.lastIndexOf('/'));
@@ -141,6 +156,7 @@ $env.location = function(path, base){
141
156
  // print("****",result);
142
157
  return result;
143
158
  } else {
159
+ // print("RRR",result);
144
160
  return "file://"+Ruby.File.expand_path(path);
145
161
  }
146
162
  }
@@ -186,23 +202,35 @@ $env.connection = function(xhr, responseHandler, data){
186
202
  }
187
203
 
188
204
  }
189
- }catch(e){
190
- $env.error('failed to open file '+ url, e);
205
+ } catch (e) {
191
206
  connection = null;
192
207
  xhr.readyState = 4;
193
- xhr.statusText = "Local File Protocol Error";
194
- xhr.responseText = "<html><head/><body><p>"+ e+ "</p></body></html>";
208
+ if(e.toString().match(/Errno::ENOENT/)) {
209
+ xhr.status = "404";
210
+ xhr.statusText = "Not Found";
211
+ xhr.responseText = undefined;
212
+ } else {
213
+ xhr.status = "500";
214
+ xhr.statusText = "Local File Protocol Error";
215
+ xhr.responseText = "<html><head/><body><p>"+ e+ "</p></body></html>";
216
+ }
195
217
  }
196
218
  } else {
197
219
  Ruby.require('net/http');
198
220
 
199
221
  var req;
222
+ var path;
223
+ try {
224
+ path = url.request_uri();
225
+ } catch(e) {
226
+ path = url.path;
227
+ }
200
228
  if ( xhr.method == "GET" ) {
201
- req = new Ruby.Net.HTTP.Get( url.request_uri() );
229
+ req = new Ruby.Net.HTTP.Get( path );
202
230
  } else if ( xhr.method == "POST" ) {
203
- req = new Ruby.Net.HTTP.Post( url.request_uri() );
231
+ req = new Ruby.Net.HTTP.Post( path );
204
232
  } else if ( xhr.method == "PUT" ) {
205
- req = new Ruby.Net.HTTP.Put( url.request_uri() );
233
+ req = new Ruby.Net.HTTP.Put( path );
206
234
  }
207
235
 
208
236
  for (var header in xhr.headers){
@@ -240,7 +268,7 @@ $env.connection = function(xhr, responseHandler, data){
240
268
  }
241
269
 
242
270
  xhr.readyState = 4;
243
- xhr.status = parseInt(connection.responseCode,10) || undefined;
271
+ xhr.status = parseInt(resp.code,10) || 0;
244
272
  xhr.statusText = connection.responseMessage || "";
245
273
 
246
274
  var contentEncoding = resp["Content-Encoding"] || "utf-8",
@@ -279,22 +307,51 @@ $env.connection = function(xhr, responseHandler, data){
279
307
  var extract_line =
280
308
  Ruby.eval(
281
309
  "lambda { |e| \
282
- e.stack.to_s.split(%(\n))[1].match(/:([^:]*)$/)[1]; \
310
+ begin; \
311
+ e.stack.to_s.split(%(\n))[1].match(/:([^:]*)$/)[1]; \
312
+ rescue; %(unknown); end; \
283
313
  }");
284
314
 
285
- var print_exception =
315
+ var print_exception = window.print_exception =
316
+ Ruby.eval(" \
317
+ lambda { |e| \
318
+ estr = e.to_s; \
319
+ estr.gsub!(/(<br \\/>)+/, %( )); \
320
+ print(%(Exception: ),estr,%(\n)); \
321
+ begin; \
322
+ e.stack.to_s.split(%(\n)).each do |line| \
323
+ m = line.match(/(.*)@([^@]*)$/); \
324
+ m[2] == %(:0) && next; \
325
+ s = m[1]; \
326
+ s.gsub!(/(<br \\/>)+/, %( )); \
327
+ limit = 100; \
328
+ if ( s.length > limit ); \
329
+ s = s[0,limit] + %(...); \
330
+ end; \
331
+ print(m[2],%( ),s,%(\n)); \
332
+ end; \
333
+ rescue; end; \
334
+ } \
335
+ ");
336
+
337
+ var print_exception_trace = window.print_exception_trace =
286
338
  Ruby.eval(" \
287
339
  lambda { |e| \
288
- print(%(Exception: ),e,%(\n)); \
340
+ estr = e.to_s; \
341
+ estr.gsub!(/(<br \\/>)+/, %( )); \
342
+ begin; \
289
343
  e.stack.to_s.split(%(\n)).each do |line| \
290
344
  m = line.match(/(.*)@([^@]*)$/); \
345
+ m[2] == %(:0) && next; \
291
346
  s = m[1]; \
292
- limit = 50; \
347
+ s.gsub!(/(<br \\/>)+/, %( )); \
348
+ limit = 100; \
293
349
  if ( s.length > limit ); \
294
350
  s = s[0,limit] + %(...); \
295
351
  end; \
296
352
  print(m[2],%( ),s,%(\n)); \
297
- end \
353
+ end; \
354
+ rescue; end; \
298
355
  } \
299
356
  ");
300
357
 
@@ -387,12 +444,16 @@ $env.makeNewWindowMaybeLoad = function(openingWindow, parentArg, url, outer){
387
444
  url: $env.location(url)
388
445
  };
389
446
 
447
+ // print("$w",$w);
448
+ // print("$ww",$w.window);
449
+ // print("$ww",$w === $w.window);
390
450
  var pair = $env.new_window(outer);
391
451
  var proxy = pair[0];
392
452
  var new_window = pair[1];
393
453
  options.proxy = proxy;
394
454
  new_window.$options = options;
395
- $master.load(Ruby.Envjs.ENVJS, new_window);
455
+ // print("$w",$w);
456
+ $master.load($master.Ruby.Envjs.ENVJS, new_window);
396
457
  return proxy;
397
458
  };
398
459
 
@@ -407,7 +468,6 @@ $env.reloadAWindowProxy = function(oldWindowProxy, url){
407
468
  $env.sleep = function(n){Ruby.sleep(n/1000.);};
408
469
 
409
470
  $env.loadIntoFnsScope = function(file) {
410
- // print("lifs",load);
411
471
  return load(file);
412
472
  }
413
473
 
@@ -470,8 +530,9 @@ var Envjs = function(){
470
530
  $env.NONE = 0;
471
531
 
472
532
  //set this if you want to get some internal log statements
473
- $env.logLevel = $env.DEBUG;
474
533
  $env.logLevel = $env.INFO;
534
+ $env.logLevel = $env.DEBUG;
535
+ $env.logLevel = $env.WARN;
475
536
 
476
537
  $env.debug = function(msg){
477
538
  if($env.logLevel >= $env.DEBUG)
@@ -487,12 +548,16 @@ var Envjs = function(){
487
548
  };
488
549
  $env.error = function(msg, e){
489
550
  if ($env.logLevel >= $env.ERROR) {
490
- $env.log(msg + " Line: " + $env.lineSource(e), 'ERROR');
491
- $env.log(e || "", 'ERROR');
551
+ var line = $env.lineSource(e);
552
+ line != "" && ( line = " Line: "+ line );
553
+ $env.log(msg + line, 'ERROR');
554
+ if(e) {
555
+ $env.log(e || "", 'ERROR');
556
+ }
492
557
  }
493
558
  };
494
559
 
495
- $env.info("Initializing Core Platform Env");
560
+ $env.debug("Initializing Core Platform Env");
496
561
 
497
562
 
498
563
  // if we're running in an environment without env.js' custom extensions
@@ -564,7 +629,7 @@ var Envjs = function(){
564
629
  $env.platform = "";
565
630
 
566
631
  $env.scriptTypes = {
567
- "text/javascript" :false,
632
+ "text/javascript" :true,
568
633
  "text/envjs" :true
569
634
  };
570
635
 
@@ -597,7 +662,13 @@ var Envjs = function(){
597
662
  }
598
663
  }
599
664
  base = "" + window.location;
600
- load($env.location(script.src.match(/([^\?#]*)/)[1], base ));
665
+ try {
666
+ load($env.location(script.src.match(/([^\?#]*)/)[1], base ));
667
+ } catch(e) {
668
+ $env.error("could not load script: " +
669
+ $env.location(script.src.match(/([^\?#]*)/)[1], base ) +
670
+ ": " + e + " thrown" );
671
+ }
601
672
  //lets you register a function to execute
602
673
  //after the script is loaded
603
674
  if($env.afterScriptLoad){
@@ -5024,22 +5095,45 @@ __extend__(DOMDocument.prototype, {
5024
5095
  load: function(url){
5025
5096
  $debug("Loading url into DOM Document: "+ url + " - (Asynch? "+$w.document.async+")");
5026
5097
  var scripts, _this = this;
5027
- var xhr = new XMLHttpRequest();
5098
+ var xhr;
5099
+ // print("KK",url,url =="about:blank");
5100
+ if (url == "about:blank"){
5101
+ xhr = ({
5102
+ open: function(){},
5103
+ send: function(){
5104
+ this.responseText = "<html><head><title></title></head><body></body></html>";
5105
+ this.onreadystatechange();
5106
+ },
5107
+ status: 200
5108
+ });
5109
+ } else {
5110
+ xhr = new XMLHttpRequest();
5111
+ }
5028
5112
  xhr.open("GET", url, $w.document.async);
5029
5113
  xhr.onreadystatechange = function(){
5030
- try{
5031
- _this.loadXML(xhr.responseText);
5032
- }catch(e){
5033
- $error("Error Parsing XML - ",e);
5114
+ if (xhr.status != 200) {
5115
+ $error("Could not retrieve XHR content from " + url + ": status code " + xhr.status);
5034
5116
  _this.loadXML(
5035
- "<html><head></head><body>"+
5036
- "<h1>Parse Error</h1>"+
5037
- "<p>"+e.toString()+"</p>"+
5038
- "</body></html>");
5117
+ "<html><head></head><body>"+
5118
+ "<h1>No File</h1>"+
5119
+ "</body></html>");
5120
+ } else {
5121
+ try{
5122
+ _this.loadXML(xhr.responseText);
5123
+ }catch(e){
5124
+ $error("Error Parsing XML - ",e);
5125
+ _this.loadXML(
5126
+ "<html><head></head><body>"+
5127
+ "<h1>Parse Error</h1>"+
5128
+ "<p>"+e.toString()+"</p>"+
5129
+ "</body></html>");
5130
+ }
5039
5131
  }
5040
5132
  _this._url = url;
5041
5133
 
5134
+ if ( url != "about:blank" ) {
5042
5135
  $info("Sucessfully loaded document at "+url);
5136
+ }
5043
5137
 
5044
5138
  // first fire body-onload event
5045
5139
  var event = document.createEvent();
@@ -5343,6 +5437,11 @@ var __isValidNamespace__ = function(doc, namespaceURI, qualifiedName, isAttribut
5343
5437
  };
5344
5438
 
5345
5439
  $w.Document = DOMDocument;
5440
+
5441
+ // Local Variables:
5442
+ // espresso-indent-level:4
5443
+ // c-basic-offset:4
5444
+ // End:
5346
5445
  /*
5347
5446
  * parser.js
5348
5447
  */
@@ -9055,14 +9154,22 @@ $w.__defineGetter__("location", function(url){
9055
9154
  // to modify the correct portion of the location object
9056
9155
  // when we navigate the history
9057
9156
  var setHistory = function( value, locationPart){
9058
- $info("adding value to history: " +value);
9157
+ if ( value == "about:blank" ) {
9158
+ return;
9159
+ }
9160
+ $debug("adding value to history: " +value);
9059
9161
  $currentHistoryIndex++;
9060
9162
  $history.push({
9061
9163
  location: locationPart||"href",
9062
9164
  value: value
9063
9165
  });
9064
9166
  };
9065
- /*
9167
+
9168
+ // Local Variables:
9169
+ // espresso-indent-level:4
9170
+ // c-basic-offset:4
9171
+ // End:
9172
+ /*
9066
9173
  * navigator.js
9067
9174
  * - requires env
9068
9175
  */
@@ -9214,7 +9321,7 @@ window.clearInterval = window.clearTimeout = function(num){
9214
9321
 
9215
9322
  // FIX: make a priority queue ...
9216
9323
 
9217
- window.$wait = $env.wait = $env.wait || function(wait) {
9324
+ window.$wait = $timers.wait = $env.wait = $env.wait || function(wait) {
9218
9325
  var start = Date.now();
9219
9326
  var old_loop_running = $event_loop_running;
9220
9327
  $event_loop_running = true;
@@ -10383,9 +10490,28 @@ try{
10383
10490
  // (function(){
10384
10491
  // function(){
10385
10492
 
10386
- if ( options.url ) {
10387
- $w.__loadAWindowsDocument__(options.url);
10388
- }
10493
+ // User accesible interface ...
10494
+ var Envjs = $w.Envjs = $env.Envjs = function(){
10495
+ if(arguments.length === 2){
10496
+ for ( var i in arguments[1] ) {
10497
+ var g = arguments[1].__lookupGetter__(i),
10498
+ s = arguments[1].__lookupSetter__(i);
10499
+ if ( g || s ) {
10500
+ if ( g ) $env.__defineGetter__(i, g);
10501
+ if ( s ) $env.__defineSetter__(i, s);
10502
+ } else
10503
+ $env[i] = arguments[1][i];
10504
+ }
10505
+ }
10506
+ if (arguments[0] != null && arguments[0] != "")
10507
+ window.location = arguments[0];
10508
+ };
10509
+ Envjs.$env = $env;
10510
+ Envjs.wait = $env.wait;
10511
+ Envjs.interpreter = window.whichInterpreter;
10512
+ Envjs.evaluate = $env.$master.evaluate;
10513
+
10514
+ $w.__loadAWindowsDocument__(options.url || "about:blank");
10389
10515
  };
10390
10516
 
10391
10517
  return $env;
@@ -10395,27 +10521,6 @@ try{
10395
10521
  // Initial window setup
10396
10522
  $env.init.call(this);
10397
10523
 
10398
- // User accesible interface ...
10399
- Envjs = $env.Envjs = function(){
10400
- if(arguments.length === 2){
10401
- for ( var i in arguments[1] ) {
10402
- var g = arguments[1].__lookupGetter__(i),
10403
- s = arguments[1].__lookupSetter__(i);
10404
- if ( g || s ) {
10405
- if ( g ) $env.__defineGetter__(i, g);
10406
- if ( s ) $env.__defineSetter__(i, s);
10407
- } else
10408
- $env[i] = arguments[1][i];
10409
- }
10410
- }
10411
- if (arguments[0] != null && arguments[0] != "")
10412
- window.location = arguments[0];
10413
- };
10414
- Envjs.$env = $env;
10415
- Envjs.wait = $env.wait;
10416
- Envjs.interpreter = window.whichInterpreter;
10417
- Envjs.evaluate = $env.$master.evaluate;
10418
-
10419
10524
  })();
10420
10525
 
10421
10526
  // Local Variables:
@@ -30,6 +30,7 @@ class Envjs::Net::File < Net::Protocol
30
30
  @path = path
31
31
  @file = File.new @path
32
32
  @body = @file.read
33
+ @code = @file.nil? ? "404" : "200";
33
34
  @file.close
34
35
  end
35
36
 
@@ -52,15 +53,12 @@ class Envjs::Net::File < Net::Protocol
52
53
  @file.nil? ? 404 : 200;
53
54
  end
54
55
 
55
- def code
56
- @file.nil? ? "404" : "200";
57
- end
58
56
 
59
57
  def getInputStream
60
58
  self
61
59
  end
62
60
 
63
- attr_reader :body
61
+ attr_reader :body, :code
64
62
 
65
63
  end
66
64
 
data/lib/envjs/runtime.rb CHANGED
@@ -86,6 +86,8 @@ EOJS
86
86
  master.first_script_window = window
87
87
 
88
88
  window["$master"] = master
89
+ window["$options"] = evaluate("new Object");
90
+ window["$options"].proxy = outer
89
91
 
90
92
  window.load = lambda { |*files|
91
93
  files.each do |f|
data/test/unit/events.js CHANGED
@@ -177,7 +177,7 @@ test("Check that events can be set with addEventListener(), and bubble",
177
177
  var elem = document.getElementById('eventsFrame').contentDocument.
178
178
  getElementById(id).addEventListener('click', function(event){
179
179
  try{
180
- ok( event.target === img && this === window,
180
+ ok( event.target === img && ( ( this === window ) || ( this.window === window ) ),
181
181
  "Scope: 'this' refers to the window '" + window + "'");
182
182
  }catch(e){print(e);}
183
183
  });
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smparkes-envjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Resig
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-11-02 00:00:00 -08:00
13
+ date: 2009-11-04 00:00:00 -08:00
14
14
  default_executable: envjsrb
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency