jbarnette-johnson 1.0.0.20090326122910 → 1.0.0.20090326154650

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,705 +0,0 @@
1
- /*
2
- * Simulated browser environment for Rhino
3
- * By John Resig <http://ejohn.org/>
4
- * Copyright 2007 John Resig, under the MIT License
5
- */
6
-
7
- // The window Object
8
- var window = this;
9
-
10
- Ruby.require("uri");
11
- Ruby.require("taka");
12
-
13
- print = function(txt) { Ruby.puts(txt); };
14
-
15
- (function(){
16
-
17
- // Browser Navigator
18
-
19
- window.navigator = {
20
- get userAgent(){
21
- return "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";
22
- }
23
- };
24
-
25
- var fileToUrl = function(file) {
26
- return Ruby.URI.parse("file://" + Ruby.File.expand_path(file));
27
- };
28
-
29
- var curLocation = fileToUrl(".");
30
-
31
- window.__defineSetter__("location", function(url){
32
- var xhr = new XMLHttpRequest();
33
- xhr.open("GET", url);
34
- xhr.onreadystatechange = function(){
35
- curLocation = curLocation.merge(url);
36
- window.document = xhr.responseXML;
37
-
38
- if(window.document) {
39
- var event = document.createEvent();
40
- event.initEvent("load");
41
- window.dispatchEvent( event );
42
- }
43
- };
44
- xhr.send();
45
- });
46
-
47
- window.__defineGetter__("location", function(url){
48
- return {
49
- get protocol(){
50
- return curLocation.scheme() + ":";
51
- },
52
- get href(){
53
- return curLocation.toString();
54
- },
55
- toString: function(){
56
- return this.href.toString();
57
- }
58
- };
59
- });
60
-
61
- // Timers
62
-
63
- var timers = [];
64
-
65
- window.setTimeout = function(fn, time){
66
- var num;
67
- return num = setInterval(function(){
68
- fn();
69
- clearInterval(num);
70
- }, time);
71
- };
72
-
73
- window.setInterval = function(fn, time){
74
- var num = timers.length;
75
-
76
- timers[num] = new Ruby.Thread(function() {
77
- while(true) {
78
- Ruby.sleep(time);
79
- fn();
80
- }
81
- });
82
-
83
- return num;
84
- };
85
-
86
- window.clearInterval = function(num){
87
- if ( timers[num] ) {
88
- timers[num].kill();
89
- delete timers[num];
90
- }
91
- };
92
-
93
- // Window Events
94
-
95
- var events = [{}];
96
-
97
- window.addEventListener = function(type, fn){
98
- if ( !this.uuid || this == window ) {
99
- this.uuid = events.length;
100
- events[this.uuid] = {};
101
- }
102
-
103
- if ( !events[this.uuid][type] )
104
- events[this.uuid][type] = [];
105
-
106
- if ( events[this.uuid][type].indexOf( fn ) < 0 )
107
- events[this.uuid][type].push( fn );
108
- };
109
-
110
- window.removeEventListener = function(type, fn){
111
- if ( !this.uuid || this == window ) {
112
- this.uuid = events.length;
113
- events[this.uuid] = {};
114
- }
115
-
116
- if ( !events[this.uuid][type] )
117
- events[this.uuid][type] = [];
118
-
119
- events[this.uuid][type] =
120
- events[this.uuid][type].filter(function(f){
121
- return f != fn;
122
- });
123
- };
124
-
125
- window.dispatchEvent = function(event){
126
- if ( event.type ) {
127
- if ( this.uuid && events[this.uuid][event.type] ) {
128
- var self = this;
129
-
130
- events[this.uuid][event.type].forEach(function(fn){
131
- fn.call( self, event );
132
- });
133
- }
134
-
135
- if ( this["on" + event.type] )
136
- this["on" + event.type].call( self, event );
137
- }
138
- };
139
-
140
- // DOM Document
141
-
142
- var parse = function(text) {
143
- try {
144
- // Annoying. We have to do send because HTML isn't recognized
145
- // as a function.
146
- return Ruby.Taka.DOM.send("HTML", text);
147
- } catch(e) {
148
- Ruby.puts("FAIL\n" + text);
149
- Ruby.raise(e)
150
- Ruby.exit()
151
- }
152
- }
153
-
154
- window.DOMDocument = function(file){
155
- this._file = file;
156
- this._dom = parse(file)
157
-
158
- if ( !obj_nodes["key?"]( this._dom ) )
159
- obj_nodes[this._dom] = this;
160
- };
161
-
162
- DOMDocument.prototype = {
163
- nodeType: 1,
164
- createTextNode: function(text){
165
- return makeNode( this._dom.createTextNode(
166
- text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) );
167
- },
168
- createElement: function(name){
169
- return makeNode( this._dom.createElement(name.toLowerCase()) );
170
- },
171
- getElementsByTagName: function(name){
172
- return new DOMNodeList( this._dom.getElementsByTagName(
173
- name.toLowerCase()) );
174
- },
175
- _cacheIds: function() {
176
-
177
- },
178
- getElementById: function(id){
179
- return this._dom.getElementById(id);
180
- },
181
- get body(){
182
- return this.getElementsByTagName("body")[0];
183
- },
184
- get documentElement(){
185
- return makeNode( this._dom.documentElement() );
186
- },
187
- get ownerDocument(){
188
- return null;
189
- },
190
- addEventListener: window.addEventListener,
191
- removeEventListener: window.removeEventListener,
192
- dispatchEvent: window.dispatchEvent,
193
- get nodeName() {
194
- return "#document";
195
- },
196
- importNode: function(node, deep){
197
- return makeNode( this._dom.importNode(node._dom, deep) );
198
- },
199
- toString: function(){
200
- return "Document" + (typeof this._file == "string" ?
201
- ": " + this._file : "");
202
- },
203
- get innerHTML(){
204
- return this.documentElement.outerHTML;
205
- },
206
-
207
- get defaultView(){
208
- return {
209
- getComputedStyle: function(elem){
210
- return {
211
- getPropertyValue: function(prop){
212
- prop = prop.replace(/\-(\w)/g,function(m,c){
213
- return c.toUpperCase();
214
- });
215
- var val = elem.style[prop];
216
-
217
- if ( prop === "opacity" && val === "" )
218
- val = "1";
219
-
220
- return val;
221
- }
222
- };
223
- }
224
- };
225
- },
226
-
227
- createEvent: function(){
228
- return {
229
- type: "",
230
- initEvent: function(type){
231
- this.type = type;
232
- }
233
- };
234
- }
235
- };
236
-
237
- function getDocument(node){
238
- return obj_nodes[node];
239
- }
240
-
241
- // DOM NodeList
242
-
243
- window.DOMNodeList = function(list){
244
- this._dom = list;
245
- this.length = list.length();
246
-
247
- for ( var i = 0; i < this.length; i++ ) {
248
- var node = list.item(i);
249
- this[i] = makeNode( node );
250
- }
251
- };
252
-
253
- DOMNodeList.prototype = {
254
- toString: function(){
255
- return "[ " +
256
- Array.prototype.join.call( this, ", " ) + " ]";
257
- },
258
- get outerHTML(){
259
- return Array.prototype.map.call(
260
- this, function(node){return node.outerHTML;}).join('');
261
- }
262
- };
263
-
264
- // DOM Node
265
-
266
- window.DOMNode = function(node){
267
- this._dom = node;
268
- };
269
-
270
- DOMNode.prototype = {
271
- get nodeType(){
272
- return this._dom.nodeType();
273
- },
274
- get nodeValue(){
275
- return this._dom.nodeValue();
276
- },
277
- get nodeName() {
278
- return this._dom.nodeName();
279
- },
280
- cloneNode: function(deep){
281
- return makeNode( this._dom.cloneNode(deep) );
282
- },
283
- get ownerDocument(){
284
- return getDocument( this._dom.ownerDocument );
285
- // return getDocument( this._dom.ownerDocument() );
286
- },
287
- get documentElement(){
288
- return makeNode( this._dom.documentElement() );
289
- },
290
- get parentNode() {
291
- return makeNode( this._dom.parentNode() );
292
- },
293
- get nextSibling() {
294
- return makeNode( this._dom.nextSibling() );
295
- },
296
- get previousSibling() {
297
- return makeNode( this._dom.previousSibling() );
298
- },
299
- toString: function(){
300
- return '"' + this.nodeValue + '"';
301
- },
302
- get outerHTML(){
303
- return this.nodeValue;
304
- }
305
- };
306
-
307
- // DOM Element
308
-
309
- window.DOMElement = function(elem){
310
- this._dom = elem;
311
- this.style = {
312
- get opacity(){ return this._opacity; },
313
- set opacity(val){ this._opacity = val + ""; }
314
- };
315
-
316
- // Load CSS info
317
- var styles = (this.getAttribute("style") || "").split(/\s*;\s*/);
318
-
319
- for ( var i = 0; i < styles.length; i++ ) {
320
- var style = styles[i].split(/\s*:\s*/);
321
- if ( style.length == 2 )
322
- this.style[ style[0] ] = style[1];
323
- }
324
- };
325
-
326
- DOMElement.prototype = extend( new DOMNode(), {
327
- get nodeName(){
328
- return this.tagName.toUpperCase();
329
- },
330
- get tagName(){
331
- return this._dom.tagName().toUpperCase();
332
- },
333
- toString: function(){
334
- return "<" + this.tagName + (this.id ? "#" + this.id : "" ) + ">";
335
- },
336
- get outerHTML(){
337
- var ret = "<" + this.tagName, attrs = this._dom.attributes();
338
-
339
- attrs.each(function(attr) {
340
- ret += " " + attr.nodeName() + "='" + attr.nodeValue() + "'";
341
- });
342
-
343
- if ( this.childNodes.length || this.nodeName == "SCRIPT" )
344
- ret += ">" + this.childNodes.outerHTML +
345
- "</" + this.tagName + ">";
346
- else
347
- ret += "/>";
348
-
349
- return ret;
350
- },
351
-
352
- get attributes(){
353
- var attr = {}, attrs = this._dom.attributes();
354
-
355
- for ( var i = 0; i < attrs.length(); i++ )
356
- attr[ attrs.item(i).nodeName ] = attrs.item(i).nodeValue;
357
-
358
- return attr;
359
- },
360
-
361
- get innerHTML(){
362
- return this.childNodes.outerHTML;
363
- },
364
- set innerHTML(html){
365
- html = html.replace(/<\/?([A-Z]+)/g, function(m){
366
- return m.toLowerCase();
367
- });
368
-
369
- // Ruby.p(this._dom.ownerDocument);
370
- var frag = parse("<doc>" + html + "</doc>");
371
-
372
- var nodes = new DOMNodeList(frag.getElementsByTagName("doc")[0].childNodes());
373
- // new DOMDocument( html ).documentElement.childNodes
374
-
375
- while (this.firstChild)
376
- this.removeChild( this.firstChild );
377
-
378
- for ( var i = 0; i < nodes.length; i++ ) {
379
- this.appendChild( nodes[i] );
380
- }
381
- },
382
-
383
- get textContent(){
384
- function nav(nodes){
385
- var str = "";
386
- for ( var i = 0; i < nodes.length; i++ ) {
387
- if ( nodes[i].nodeType == 3 )
388
- str += nodes[i].nodeValue;
389
- else if ( nodes[i].nodeType == 1 )
390
- str += nav(nodes[i].childNodes);
391
- }
392
- return str;
393
- }
394
-
395
- return nav(this.childNodes);
396
- },
397
- set textContent(text){
398
- while (this.firstChild)
399
- this.removeChild( this.firstChild );
400
- this.appendChild( this.ownerDocument.createTextNode(text) );
401
- },
402
-
403
- style: {},
404
- clientHeight: 0,
405
- clientWidth: 0,
406
- offsetHeight: 0,
407
- offsetWidth: 0,
408
-
409
- get disabled() {
410
- var val = this.getAttribute("disabled");
411
- return val != "false" && !!val;
412
- },
413
- set disabled(val) { return this.setAttribute("disabled",val); },
414
-
415
- get checked() {
416
- var val = this.getAttribute("checked");
417
- return val != "false" && !!val;
418
- },
419
- set checked(val) { return this.setAttribute("checked",val.toString()); },
420
-
421
- get options() {
422
- return this.getElementsByTagName("options");
423
- },
424
-
425
- get selected() {
426
- if ( !this._selectDone ) {
427
- this._selectDone = true;
428
-
429
- if ( this.nodeName == "OPTION" && !this.parentNode.getAttribute("multiple") ) {
430
- var opt = this.parentNode.getElementsByTagName("option");
431
-
432
- if ( this == opt[0] ) {
433
- var select = true;
434
-
435
- for ( var i = 1; i < opt.length; i++ ) {
436
- if ( opt[i].selected ) {
437
- select = false;
438
- break;
439
- }
440
- }
441
-
442
- if ( select )
443
- this.selected = true;
444
- }
445
- }
446
- }
447
-
448
- var val = this.getAttribute("selected");
449
- return val != "false" && !!val;
450
- },
451
- set selected(val) { return this.setAttribute("selected",val); },
452
-
453
- get className() { return this.getAttribute("class") || ""; },
454
- set className(val) {
455
- return this.setAttribute("class",
456
- val.replace(/(^\s*|\s*$)/g,""));
457
- },
458
-
459
- get type() { return this.getAttribute("type") || ""; },
460
- set type(val) { return this.setAttribute("type",val); },
461
-
462
- get value() { return this.getAttribute("value") || ""; },
463
- set value(val) { return this.setAttribute("value",val); },
464
-
465
- get src() { return this.getAttribute("src") || ""; },
466
- set src(val) { return this.setAttribute("src",val); },
467
-
468
- get id() { return this.getAttribute("id") || ""; },
469
- set id(val) { return this.setAttribute("id",val); },
470
-
471
- getAttribute: function(name){
472
- return this._dom.getAttribute(name);
473
- },
474
- setAttribute: function(name,value){
475
- this._dom.setAttribute(name,value);
476
- },
477
- removeAttribute: function(name){
478
- this._dom.removeAttribute(name);
479
- },
480
-
481
- get childNodes(){
482
- return new DOMNodeList( this._dom.childNodes() );
483
- },
484
- get firstChild(){
485
- return makeNode( this._dom.firstChild() );
486
- },
487
- get lastChild(){
488
- return makeNode( this._dom.lastChild() );
489
- },
490
- appendChild: function(node){
491
- this._dom.appendChild( node._dom );
492
- },
493
- insertBefore: function(node,before){
494
- if(!before) return;
495
- this._dom.insertBefore( node._dom, before._dom );
496
- },
497
- removeChild: function(node){
498
- this._dom.removeChild( node._dom );
499
- },
500
-
501
- getElementsByTagName: DOMDocument.prototype.getElementsByTagName,
502
-
503
- addEventListener: window.addEventListener,
504
- removeEventListener: window.removeEventListener,
505
- dispatchEvent: window.dispatchEvent,
506
-
507
- click: function(){
508
- var event = document.createEvent();
509
- event.initEvent("click");
510
- this.dispatchEvent(event);
511
- },
512
- submit: function(){
513
- var event = document.createEvent();
514
- event.initEvent("submit");
515
- this.dispatchEvent(event);
516
- },
517
- focus: function(){
518
- var event = document.createEvent();
519
- event.initEvent("focus");
520
- this.dispatchEvent(event);
521
- },
522
- blur: function(){
523
- var event = document.createEvent();
524
- event.initEvent("blur");
525
- this.dispatchEvent(event);
526
- },
527
- get elements(){
528
- return this.getElementsByTagName("*");
529
- },
530
- get contentWindow(){
531
- return this.nodeName == "IFRAME" ? {
532
- document: this.contentDocument
533
- } : null;
534
- },
535
- get contentDocument(){
536
- if ( this.nodeName == "IFRAME" ) {
537
- if ( !this._doc )
538
- this._doc = new DOMDocument(
539
- "<html><head><title></title></head><body></body></html>"
540
- );
541
- return this._doc;
542
- } else
543
- return null;
544
- }
545
- });
546
-
547
- // Helper method for extending one object with another
548
-
549
- function extend(a,b) {
550
- for ( var i in b ) {
551
- var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
552
-
553
- if ( g || s ) {
554
- if ( g )
555
- a.__defineGetter__(i, g);
556
- if ( s )
557
- a.__defineSetter__(i, s);
558
- } else
559
- a[i] = b[i];
560
- }
561
- return a;
562
- }
563
-
564
- // Helper method for generating the right
565
- // DOM objects based upon the type
566
-
567
- var obj_nodes = new Ruby.Hash;
568
-
569
- function makeNode(node){
570
- if ( node ) {
571
- if ( !obj_nodes['key?']( node.object_id() ) ) {
572
- obj_nodes[node.object_id()] = node.nodeType() == 1 ?
573
- new DOMElement( node ) : new DOMNode( node );
574
- }
575
-
576
- return obj_nodes[node.object_id()];
577
- } else
578
- return null;
579
- }
580
-
581
- // XMLHttpRequest
582
- // Originally implemented by Yehuda Katz
583
-
584
- window.XMLHttpRequest = function(){
585
- this.headers = {};
586
- this.responseHeaders = {};
587
- };
588
-
589
- XMLHttpRequest.prototype = {
590
- open: function(method, url, async, user, password){
591
- this.readyState = 1;
592
- if (async)
593
- this.async = true;
594
- this.method = method || "GET";
595
- this.url = url;
596
- this.onreadystatechange();
597
- },
598
- setRequestHeader: function(header, value){
599
- this.headers[header] = value;
600
- },
601
- getResponseHeader: function(header){ },
602
- send: function(data){
603
- var self = this;
604
-
605
- function makeRequest(){
606
- var url = curLocation.merge(self.url);
607
- var connection;
608
-
609
- if ( url.scheme == "file" ) {
610
- if ( self.method == "PUT" ) {
611
- var out = new Ruby.File(url.path);
612
- var text = data || "";
613
-
614
- out.puts( text );
615
- out.flush();
616
- out.close();
617
- } else if ( self.method == "DELETE" ) {
618
- var file = new Ruby.File(url.path());
619
- file["delete"]();
620
- } else if ( self.method == "GET" ) {
621
- var file = Ruby.File.read(url.path);
622
- connection = {
623
- code: "200",
624
- message: "Ok",
625
- body: file,
626
- }
627
- handleResponse();
628
- } else {
629
- connection = Ruby.Net.HTTP.start(url.host, url.port, function(http) {
630
- http.get(url.path);
631
- });
632
- handleResponse();
633
- }
634
- } else {
635
- var http = Ruby.Net.HTTP.new(url.host, url.port);
636
- var request = new Ruby.Net.HTTP.Get(url.path);
637
- for (var header in self.headers)
638
- request.add_field(header, self.headers[header]);
639
-
640
- var connection = http.request(request);
641
- connection.each_header(function(k,v) {
642
- self.responseHeaders[k] = v;
643
- });
644
-
645
- handleResponse();
646
- }
647
-
648
- function handleResponse(){
649
- self.readyState = 4;
650
- self.status = parseInt(connection.code) || undefined;
651
- self.statusText = connection.message || "";
652
-
653
- self.responseText = connection.body;
654
-
655
- self.responseXML = null;
656
-
657
- if ( self.responseText.match(/^\s*</) ) {
658
- self.responseXML = new DOMDocument( self.responseText );
659
- }
660
- }
661
-
662
- self.onreadystatechange();
663
- }
664
-
665
- if (this.async)
666
- new Ruby.Thread(function() { makeRequest(); });
667
- else
668
- makeRequest();
669
- },
670
- abort: function(){},
671
- onreadystatechange: function(){},
672
- getResponseHeader: function(header){
673
- if (this.readyState < 3)
674
- throw new Error("INVALID_STATE_ERR");
675
- else {
676
- var returnedHeaders = [];
677
- for (var rHeader in this.responseHeaders) {
678
- if (rHeader.match(new Regexp(header, "i")))
679
- returnedHeaders.push(this.responseHeaders[rHeader]);
680
- }
681
-
682
- if (returnedHeaders.length)
683
- return returnedHeaders.join(", ");
684
- }
685
-
686
- return null;
687
- },
688
- getAllResponseHeaders: function(header){
689
- if (this.readyState < 3)
690
- throw new Error("INVALID_STATE_ERR");
691
- else {
692
- var returnedHeaders = [];
693
-
694
- for (var aHeader in this.responseHeaders)
695
- returnedHeaders.push( aHeader + ": " + this.responseHeaders[aHeader] );
696
-
697
- return returnedHeaders.join("\r\n");
698
- }
699
- },
700
- async: true,
701
- readyState: 0,
702
- responseText: "",
703
- status: 0
704
- };
705
- })();