Added basic support for IFrames, disabled a test which we don't take into account.
[jquery.git] / build / runtest / env.js
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 (function(){
11
12         // Browser Navigator
13
14         window.navigator = {
15                 get userAgent(){
16                         return "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";
17                 }
18         };
19         
20         window.__defineSetter__("location", function(url){
21                 window.document = new DOMDocument(
22                         new Packages.org.xml.sax.InputSource(\r                  new java.io.InputStreamReader(
23                                 new java.io.FileInputStream(url))));
24         });
25         
26         window.__defineGetter__("location", function(url){
27                 return {
28                         get protocol(){
29                                 return "file:";
30                         }
31                 };
32         });
33         
34         // Timers
35
36         var timers = [];
37         
38         window.setTimeout = function(fn, time){
39                 var num;
40                 return num = setInterval(function(){
41                         fn();
42                         clearInterval(num);
43                 }, time);
44         };
45         
46         window.setInterval = function(fn, time){
47                 var num = timers.length;
48                 
49                 timers[num] = new java.lang.Thread(new java.lang.Runnable({
50                         run: function(){
51                                 while (true){
52                                         java.lang.Thread.currentThread().sleep(time);
53                                         fn();
54                                 }
55                         }
56                 }));
57                 
58                 timers[num].start();
59         
60                 return num;
61         };
62         
63         window.clearInterval = function(num){
64                 if ( timers[num] ) {
65                         timers[num].stop();
66                         delete timers[num];
67                 }
68         };
69         
70         // Window Events
71
72         window.addEventListener = function(){};
73         window.removeEventListener = function(){};
74         
75         // DOM Document
76         
77         window.DOMDocument = function(file){
78                 this._file = file;
79                 this._dom = Packages.javax.xml.parsers.
80                         DocumentBuilderFactory.newInstance()
81                                 .newDocumentBuilder().parse(file);
82                 
83                 if ( !obj_nodes.containsKey( this._dom ) )
84                         obj_nodes.put( this._dom, this );
85         };
86         
87         DOMDocument.prototype = {
88                 createTextNode: function(text){
89                         return makeNode( this._dom.createTextNode(text) );
90                 },
91                 createElement: function(name){
92                         return makeNode( this._dom.createElement(name) );
93                 },
94                 getElementsByTagName: function(name){
95                         return new DOMNodeList( this._dom.getElementsByTagName(name) );
96                 },
97                 getElementById: function(id){
98                         var elems = this._dom.getElementsByTagName("*");
99                         
100                         for ( var i = 0; i < elems.length; i++ ) {
101                                 var elem = elems.item(i);
102                                 if ( elem.getAttribute("id") == id )
103                                         return makeNode(elem);
104                         }
105                         
106                         return null;
107                 },
108                 get body(){
109                         return this.getElementsByTagName("body")[0];
110                 },
111                 get documentElement(){
112                         return makeNode( this._dom.getDocumentElement() );
113                 },
114                 get ownerDocument(){
115                         return null;
116                 },
117                 addEventListener: function(){},
118                 removeEventListener: function(){},
119                 get nodeName() {
120                         return "#document";
121                 },
122                 importNode: function(node, deep){
123                         return makeNode( this._dom.importNode(node._dom, deep) );
124                 },
125                 toString: function(){
126                         return "Document" + (typeof this._file == "string" ?
127                                 ": " + this._file : "");
128                 },
129                 
130                 get defaultView(){
131                         return {
132                                 getComputedStyle: function(elem){
133                                         return {
134                                                 getPropertyValue: function(prop){
135                                                         prop = prop.replace(/\-(\w)/g,function(m,c){
136                                                                 return c.toUpperCase();
137                                                         });
138                                                         var val = elem.style[prop];
139                                                         
140                                                         if ( prop == "opacity" && val == "" )
141                                                                 val = "1";
142                                                                 
143                                                         return val;
144                                                 }
145                                         };
146                                 }
147                         };
148                 }
149         };
150         
151         function getDocument(node){
152                 return obj_nodes.get(node);
153         }
154         
155         // DOM NodeList
156         
157         window.DOMNodeList = function(list){
158                 this._dom = list;
159                 this.length = list.getLength();
160                 
161                 for ( var i = 0; i < this.length; i++ ) {
162                         var node = list.item(i);
163                         this[i] = makeNode( node );
164                 }
165         };
166         
167         DOMNodeList.prototype = {
168                 toString: function(){
169                         return "[ " +
170                                 Array.prototype.join.call( this, ", " ) + " ]";
171                 },
172                 valueOf: function(){
173                         return Array.prototype.map.call(
174                                 this, function(node){return node.valueOf();}).join('');
175                 }
176         };
177         
178         // DOM Node
179         
180         window.DOMNode = function(node){
181                 this._dom = node;
182         };
183         
184         DOMNode.prototype = {
185                 get nodeType(){
186                         return this._dom.getNodeType();
187                 },
188                 get nodeValue(){
189                         return this._dom.getNodeValue();
190                 },
191                 get nodeName() {
192                         return this._dom.getNodeName();
193                 },
194                 cloneNode: function(deep){
195                         return makeNode( this._dom.cloneNode(deep) );
196                 },
197                 get ownerDocument(){
198                         return getDocument( this._dom.ownerDocument );
199                 },
200                 get documentElement(){
201                         return makeNode( this._dom.documentElement );
202                 },
203                 get parentNode() {
204                         return makeNode( this._dom.getParentNode() );
205                 },
206                 get nextSibling() {
207                         return makeNode( this._dom.getNextSibling() );
208                 },
209                 get previousSibling() {
210                         return makeNode( this._dom.getPreviousSibling() );
211                 },
212                 toString: function(){
213                         return '"' + this.nodeValue + '"';
214                 },
215                 valueOf: function(){
216                         return this.nodeValue;
217                 }
218         };
219
220         // DOM Element
221
222         window.DOMElement = function(elem){
223                 this._dom = elem;
224                 this.style = {};
225                 
226                 // Load CSS info
227                 var styles = (this.getAttribute("style") || "").split(/\s*;\s*/);
228                 
229                 for ( var i = 0; i < styles.length; i++ ) {
230                         var style = styles[i].split(/\s*:\s*/);
231                         if ( style.length == 2 )
232                                 this.style[ style[0] ] = style[1];
233                 }
234         };
235         
236         DOMElement.prototype = extend( new DOMNode(), {
237                 get nodeName(){
238                         return this.tagName.toUpperCase();
239                 },
240                 get tagName(){
241                         return this._dom.getTagName();
242                 },
243                 toString: function(){
244                         return "<" + this.tagName + (this.id ? "#" + this.id : "" ) + ">";
245                 },
246                 valueOf: function(){
247                         var ret = "<" + this.tagName, attr = this.attributes;
248                         
249                         for ( var i in attr )
250                                 ret += " " + i + "='" + attr[i] + "'";
251                                 
252                         if ( this.childNodes.length || this.nodeName == "SCRIPT" )
253                                 ret += ">" + this.childNodes.valueOf() + 
254                                         "</" + this.tagName + ">";
255                         else
256                                 ret += "/>";
257                         
258                         return ret;
259                 },
260                 
261                 get attributes(){
262                         var attr = {}, attrs = this._dom.getAttributes();
263                         
264                         for ( var i = 0; i < attrs.getLength(); i++ )
265                                 attr[ attrs.item(i).nodeName ] = attrs.item(i).nodeValue;
266                                 
267                         return attr;
268                 },
269                 
270                 get innerHTML(){
271                         return this.childNodes.valueOf();       
272                 },
273                 set innerHTML(html){
274                         var nodes = this.ownerDocument.importNode(
275                                 new DOMDocument( new java.io.ByteArrayInputStream(
276                                         (new java.lang.String("<wrap>" + html + "</wrap>"))
277                                                 .getBytes("UTF8"))).documentElement, true).childNodes;
278                                 
279                         while (this.firstChild)
280                                 this.removeChild( this.firstChild );
281                         
282                         for ( var i = 0; i < nodes.length; i++ )
283                                 this.appendChild( nodes[i] );
284                 },
285                 
286                 get textContent(){
287                         return nav(this.childNodes);
288                         
289                         function nav(nodes){
290                                 var str = "";
291                                 for ( var i = 0; i < nodes.length; i++ )
292                                         if ( nodes[i].nodeType == 3 )
293                                                 str += nodes[i].nodeValue;
294                                         else if ( nodes[i].nodeType == 1 )
295                                                 str += nav(nodes[i].childNodes);
296                                 return str;
297                         }
298                 },
299                 set textContent(text){
300                         while (this.firstChild)
301                                 this.removeChild( this.firstChild );
302                         this.appendChild( document.createTextNode(text) );
303                         this.innerHTML = document.createTextNode(text).nodeValue;
304                 },
305                 
306                 style: {},
307                 clientHeight: 0,
308                 clientWidth: 0,
309                 offsetHeight: 0,
310                 offsetWidth: 0,
311                 
312                 get disabled() {
313                         var val = this.getAttribute("disabled");
314                         return val != "false" && !!val;
315                 },
316                 set disabled(val) { return this.setAttribute("disabled",val); },
317                 
318                 get checked() {
319                         var val = this.getAttribute("checked");
320                         return val != "false" && !!val;
321                 },
322                 set checked(val) { return this.setAttribute("checked",val); },
323                 
324                 get selected() {
325                         if ( !this._selectDone ) {
326                                 this._selectDone = true;
327                                 
328                                 if ( this.nodeName == "OPTION" && !this.parentNode.getAttribute("multiple") ) {
329                                         var opt = this.parentNode.getElementsByTagName("option");
330                                         
331                                         if ( this == opt[0] ) {
332                                                 var select = true;
333                                                 
334                                                 for ( var i = 1; i < opt.length; i++ )
335                                                         if ( opt[i].selected ) {
336                                                                 select = false;
337                                                                 break;
338                                                         }
339                                                         
340                                                 if ( select )
341                                                         this.selected = true;
342                                         }
343                                 }
344                         }
345                         
346                         var val = this.getAttribute("selected");
347                         return val != "false" && !!val;
348                 },
349                 set selected(val) { return this.setAttribute("selected",val); },
350
351                 get className() { return this.getAttribute("class") || ""; },
352                 set className(val) { return this.setAttribute("class",val); },
353                 
354                 get type() { return this.getAttribute("type") || ""; },
355                 set type(val) { return this.setAttribute("type",val); },
356                 
357                 get value() { return this.getAttribute("value") || ""; },
358                 set value(val) { return this.setAttribute("value",val); },
359                 
360                 get src() { return this.getAttribute("src") || ""; },
361                 set src(val) { return this.setAttribute("src",val); },
362                 
363                 get id() { return this.getAttribute("id") || ""; },
364                 set id(val) { return this.setAttribute("id",val); },
365                 
366                 getAttribute: function(name){
367                         return this._dom.hasAttribute(name) ?
368                                 new String( this._dom.getAttribute(name) ) :
369                                 null;
370                 },
371                 setAttribute: function(name,value){
372                         this._dom.setAttribute(name,value);
373                 },
374                 removeAttribute: function(name){
375                         this._dom.removeAttribute(name);
376                 },
377                 
378                 get childNodes(){
379                         return new DOMNodeList( this._dom.getChildNodes() );
380                 },
381                 get firstChild(){
382                         return makeNode( this._dom.getFirstChild() );
383                 },
384                 get lastChild(){
385                         return makeNode( this._dom.getLastChild() );
386                 },
387                 appendChild: function(node){
388                         this._dom.appendChild( node._dom );
389                 },
390                 insertBefore: function(node,before){
391                         this._dom.insertBefore( node._dom, before ? before._dom : before );
392                 },
393                 removeChild: function(node){
394                         this._dom.removeChild( node._dom );
395                 },
396
397                 getElementsByTagName: DOMDocument.prototype.getElementsByTagName,
398                 addEventListener: function(){},
399                 removeEventListener: function(){},
400                 click: function(){},
401                 submit: function(){},
402                 focus: function(){},
403                 blur: function(){},
404                 get elements(){
405                         return this.getElementsByTagName("*");
406                 },
407                 get contentWindow(){
408                         return this.nodeName == "IFRAME" ? {
409                                 document: this.contentDocument
410                         } : null;
411                 },
412                 get contentDocument(){
413                         if ( this.nodeName == "IFRAME" ) {
414                                 if ( !this._doc )
415                                         this._doc = new DOMDocument(
416                                                 new java.io.ByteArrayInputStream((new java.lang.String(
417                                                 "<html><head><title></title></head><body></body></html>"))
418                                                 .getBytes("UTF8")));
419                                 return this._doc;
420                         } else
421                                 return null;
422                 }
423         });
424         
425         // Helper method for extending one object with another
426         
427         function extend(a,b) {
428                 for ( var i in b ) {
429                         var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
430                         
431                         if ( g || s ) {
432                                 if ( g )
433                                         a.__defineGetter__(i, g);
434                                 if ( s )
435                                         a.__defineSetter__(i, s);
436                         } else
437                                 a[i] = b[i];
438                 }
439                 return a;
440         }
441         
442         // Helper method for generating the right
443         // DOM objects based upon the type
444         
445         var obj_nodes = new java.util.HashMap();
446         
447         function makeNode(node){
448                 if ( node ) {
449                         if ( !obj_nodes.containsKey( node ) )
450                                 obj_nodes.put( node, node.getNodeType() == 
451                                         Packages.org.w3c.dom.Node.ELEMENT_NODE ?
452                                                 new DOMElement( node ) : new DOMNode( node ) );
453                         
454                         return obj_nodes.get(node);
455                 } else
456                         return null;
457         }
458         
459         // XMLHttpRequest
460
461         window.XMLHttpRequest = function(){ };
462         
463         XMLHttpRequest.prototype = {
464                 open: function(){ },
465                 setRequestHeader: function(){ },
466                 getResponseHeader: function(){ },
467                 readyState: 0,
468                 responseText: "",
469                 responseXML: {},
470                 status: 0
471         };
472 })();