Added a number of fixes: Tag name case-sensitivity, text escaping, opacity setting...
[jquery.git] / build / runtest / env.js
index f4c54bb..f3afb19 100644 (file)
@@ -1,3 +1,9 @@
+/*
+ * Simulated browser environment for Rhino
+ *   By John Resig <http://ejohn.org/>
+ * Copyright 2007 John Resig, under the MIT License
+ */
+
 // The window Object
 var window = this;
 
@@ -12,7 +18,9 @@ var window = this;
        };
        
        window.__defineSetter__("location", function(url){
-               window.document = new DOMDocument(url);
+               window.document = new DOMDocument(
+                       new Packages.org.xml.sax.InputSource(\r                  new java.io.InputStreamReader(
+                               new java.io.FileInputStream(url))));
        });
        
        window.__defineGetter__("location", function(url){
@@ -71,17 +79,22 @@ var window = this;
                this._dom = Packages.javax.xml.parsers.
                        DocumentBuilderFactory.newInstance()
                                .newDocumentBuilder().parse(file);
+               
+               if ( !obj_nodes.containsKey( this._dom ) )
+                       obj_nodes.put( this._dom, this );
        };
        
        DOMDocument.prototype = {
                createTextNode: function(text){
-                       return makeNode( this._dom.createTextNode(text) );
+                       return makeNode( this._dom.createTextNode(
+                               text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) );
                },
                createElement: function(name){
-                       return makeNode( this._dom.createElement(name) );
+                       return makeNode( this._dom.createElement(name.toLowerCase()) );
                },
                getElementsByTagName: function(name){
-                       return new DOMNodeList( this._dom.getElementsByTagName(name) );
+                       return new DOMNodeList( this._dom.getElementsByTagName(
+                               name.toLowerCase()) );
                },
                getElementById: function(id){
                        var elems = this._dom.getElementsByTagName("*");
@@ -97,16 +110,11 @@ var window = this;
                get body(){
                        return this.getElementsByTagName("body")[0];
                },
-               defaultView: {
-                       getComputedStyle: {
-                               getPropertyValue: function(){ }
-                       }
-               },
                get documentElement(){
                        return makeNode( this._dom.getDocumentElement() );
                },
                get ownerDocument(){
-                       return this;
+                       return null;
                },
                addEventListener: function(){},
                removeEventListener: function(){},
@@ -123,17 +131,29 @@ var window = this;
                
                get defaultView(){
                        return {
-                               getComputedStyle: function(){
+                               getComputedStyle: function(elem){
                                        return {
-                                               getPropertyValue: function(){
-                                                       return "";
+                                               getPropertyValue: function(prop){
+                                                       prop = prop.replace(/\-(\w)/g,function(m,c){
+                                                               return c.toUpperCase();
+                                                       });
+                                                       var val = elem.style[prop];
+                                                       
+                                                       if ( prop == "opacity" && val == "" )
+                                                               val = "1";
+                                                               
+                                                       return val;
                                                }
-                                       }
+                                       };
                                }
                        };
                }
        };
        
+       function getDocument(node){
+               return obj_nodes.get(node);
+       }
+       
        // DOM NodeList
        
        window.DOMNodeList = function(list){
@@ -177,10 +197,10 @@ var window = this;
                        return makeNode( this._dom.cloneNode(deep) );
                },
                get ownerDocument(){
-                       return document;
+                       return getDocument( this._dom.ownerDocument );
                },
                get documentElement(){
-                       return document.documentElement;
+                       return makeNode( this._dom.documentElement );
                },
                get parentNode() {
                        return makeNode( this._dom.getParentNode() );
@@ -203,11 +223,13 @@ var window = this;
 
        window.DOMElement = function(elem){
                this._dom = elem;
-               this.style = {};
+               this.style = {
+                       get opacity(){ return this._opacity; },
+                       set opacity(val){ this._opacity = val + ""; }
+               };
                
                // Load CSS info
-               var styles = (new String(this.getAttribute("style") || ""))
-                       .split(/\s*;\s*/);
+               var styles = (this.getAttribute("style") || "").split(/\s*;\s*/);
                
                for ( var i = 0; i < styles.length; i++ ) {
                        var style = styles[i].split(/\s*:\s*/);
@@ -254,10 +276,14 @@ var window = this;
                        return this.childNodes.valueOf();       
                },
                set innerHTML(html){
+                       html = html.replace(/<\/?([A-Z]+)/g, function(m){
+                               return m.toLowerCase();
+                       });
+                       
                        var nodes = this.ownerDocument.importNode(
                                new DOMDocument( new java.io.ByteArrayInputStream(
                                        (new java.lang.String("<wrap>" + html + "</wrap>"))
-                                               .getBytes())).documentElement, true).childNodes;
+                                               .getBytes("UTF8"))).documentElement, true).childNodes;
                                
                        while (this.firstChild)
                                this.removeChild( this.firstChild );
@@ -282,8 +308,7 @@ var window = this;
                set textContent(text){
                        while (this.firstChild)
                                this.removeChild( this.firstChild );
-                       this.appendChild( document.createTextNode(text) );
-                       this.innerHTML = document.createTextNode(text).nodeValue;
+                       this.appendChild( this.ownerDocument.createTextNode(text));
                },
                
                style: {},
@@ -305,6 +330,27 @@ var window = this;
                set checked(val) { return this.setAttribute("checked",val); },
                
                get selected() {
+                       if ( !this._selectDone ) {
+                               this._selectDone = true;
+                               
+                               if ( this.nodeName == "OPTION" && !this.parentNode.getAttribute("multiple") ) {
+                                       var opt = this.parentNode.getElementsByTagName("option");
+                                       
+                                       if ( this == opt[0] ) {
+                                               var select = true;
+                                               
+                                               for ( var i = 1; i < opt.length; i++ )
+                                                       if ( opt[i].selected ) {
+                                                               select = false;
+                                                               break;
+                                                       }
+                                                       
+                                               if ( select )
+                                                       this.selected = true;
+                                       }
+                               }
+                       }
+                       
                        var val = this.getAttribute("selected");
                        return val != "false" && !!val;
                },
@@ -327,7 +373,7 @@ var window = this;
                
                getAttribute: function(name){
                        return this._dom.hasAttribute(name) ?
-                               this._dom.getAttribute(name) :
+                               new String( this._dom.getAttribute(name) ) :
                                null;
                },
                setAttribute: function(name,value){
@@ -365,6 +411,22 @@ var window = this;
                blur: function(){},
                get elements(){
                        return this.getElementsByTagName("*");
+               },
+               get contentWindow(){
+                       return this.nodeName == "IFRAME" ? {
+                               document: this.contentDocument
+                       } : null;
+               },
+               get contentDocument(){
+                       if ( this.nodeName == "IFRAME" ) {
+                               if ( !this._doc )
+                                       this._doc = new DOMDocument(
+                                               new java.io.ByteArrayInputStream((new java.lang.String(
+                                               "<html><head><title></title></head><body></body></html>"))
+                                               .getBytes("UTF8")));
+                               return this._doc;
+                       } else
+                               return null;
                }
        });