X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;ds=sidebyside;f=build%2Fruntest%2Fenv.js;h=e15a2263184475b42f0f6d3b3d4ee23807a6d938;hb=c10f87120f6fb2c6c269e766d6988e9be946dd03;hp=30cd8f1b447f41612b235d56c7c64f212ceb10eb;hpb=e155a6ae51fad3649e77ebc6f784e4fc6b957ad1;p=jquery.git
diff --git a/build/runtest/env.js b/build/runtest/env.js
index 30cd8f1..e15a226 100644
--- a/build/runtest/env.js
+++ b/build/runtest/env.js
@@ -1,3 +1,9 @@
+/*
+ * Simulated browser environment for Rhino
+ * By John Resig
+ * Copyright 2007 John Resig, under the MIT License
+ */
+
// The window Object
var window = this;
@@ -11,14 +17,32 @@ var window = this;
}
};
+ var curLocation = (new java.io.File("./")).toURL();
+
window.__defineSetter__("location", function(url){
- window.document = new DOMDocument(url);
+ var xhr = new XMLHttpRequest();
+ xhr.open("GET", url);
+ xhr.onreadystatechange = function(){
+ curLocation = new java.net.URL( curLocation, url );
+ window.document = xhr.responseXML;
+
+ var event = document.createEvent();
+ event.initEvent("load");
+ window.dispatchEvent( event );
+ };
+ xhr.send();
});
window.__defineGetter__("location", function(url){
return {
get protocol(){
- return "file:";
+ return curLocation.getProtocol() + ":";
+ },
+ get href(){
+ return curLocation.toString();
+ },
+ toString: function(){
+ return this.href;
}
};
});
@@ -60,9 +84,51 @@ var window = this;
};
// Window Events
+
+ var events = [{}];
- window.addEventListener = function(){};
- window.removeEventListener = function(){};
+ window.addEventListener = function(type, fn){
+ if ( !this.uuid || this == window ) {
+ this.uuid = events.length;
+ events[this.uuid] = {};
+ }
+
+ if ( !events[this.uuid][type] )
+ events[this.uuid][type] = [];
+
+ if ( events[this.uuid][type].indexOf( fn ) < 0 )
+ events[this.uuid][type].push( fn );
+ };
+
+ window.removeEventListener = function(type, fn){
+ if ( !this.uuid || this == window ) {
+ this.uuid = events.length;
+ events[this.uuid] = {};
+ }
+
+ if ( !events[this.uuid][type] )
+ events[this.uuid][type] = [];
+
+ events[this.uuid][type] =
+ events[this.uuid][type].filter(function(f){
+ return f != fn;
+ });
+ };
+
+ window.dispatchEvent = function(event){
+ if ( event.type ) {
+ if ( this.uuid && events[this.uuid][event.type] ) {
+ var self = this;
+
+ events[this.uuid][event.type].forEach(function(fn){
+ fn.call( self, event );
+ });
+ }
+
+ if ( this["on" + event.type] )
+ this["on" + event.type].call( self, event );
+ }
+ };
// DOM Document
@@ -71,17 +137,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, "&").replace(//g, ">")) );
},
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,19 +168,15 @@ 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(){},
+ addEventListener: window.addEventListener,
+ removeEventListener: window.removeEventListener,
+ dispatchEvent: window.dispatchEvent,
get nodeName() {
return "#document";
},
@@ -120,20 +187,44 @@ var window = this;
return "Document" + (typeof this._file == "string" ?
": " + this._file : "");
},
+ get innerHTML(){
+ return this.documentElement.outerHTML;
+ },
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;
}
- }
+ };
+ }
+ };
+ },
+
+ createEvent: function(){
+ return {
+ type: "",
+ initEvent: function(type){
+ this.type = type;
}
};
}
};
+ function getDocument(node){
+ return obj_nodes.get(node);
+ }
+
// DOM NodeList
window.DOMNodeList = function(list){
@@ -151,9 +242,9 @@ var window = this;
return "[ " +
Array.prototype.join.call( this, ", " ) + " ]";
},
- valueOf: function(){
+ get outerHTML(){
return Array.prototype.map.call(
- this, function(node){return node.valueOf();}).join('');
+ this, function(node){return node.outerHTML;}).join('');
}
};
@@ -177,10 +268,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() );
@@ -194,7 +285,7 @@ var window = this;
toString: function(){
return '"' + this.nodeValue + '"';
},
- valueOf: function(){
+ get outerHTML(){
return this.nodeValue;
}
};
@@ -203,6 +294,19 @@ var window = this;
window.DOMElement = function(elem){
this._dom = elem;
+ this.style = {
+ get opacity(){ return this._opacity; },
+ set opacity(val){ this._opacity = val + ""; }
+ };
+
+ // Load CSS info
+ var styles = (this.getAttribute("style") || "").split(/\s*;\s*/);
+
+ for ( var i = 0; i < styles.length; i++ ) {
+ var style = styles[i].split(/\s*:\s*/);
+ if ( style.length == 2 )
+ this.style[ style[0] ] = style[1];
+ }
};
DOMElement.prototype = extend( new DOMNode(), {
@@ -215,14 +319,14 @@ var window = this;
toString: function(){
return "<" + this.tagName + (this.id ? "#" + this.id : "" ) + ">";
},
- valueOf: function(){
+ get outerHTML(){
var ret = "<" + this.tagName, attr = this.attributes;
for ( var i in attr )
ret += " " + i + "='" + attr[i] + "'";
if ( this.childNodes.length || this.nodeName == "SCRIPT" )
- ret += ">" + this.childNodes.valueOf() +
+ ret += ">" + this.childNodes.outerHTML +
"" + this.tagName + ">";
else
ret += "/>";
@@ -240,13 +344,17 @@ var window = this;
},
get innerHTML(){
- return this.childNodes.valueOf();
+ return this.childNodes.outerHTML;
},
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("" + html + ""))
- .getBytes())).documentElement, true).childNodes;
+ .getBytes("UTF8"))).documentElement, true).childNodes;
while (this.firstChild)
this.removeChild( this.firstChild );
@@ -271,8 +379,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: {},
@@ -281,17 +388,50 @@ var window = this;
offsetHeight: 0,
offsetWidth: 0,
- get disabled() { return !!this.getAttribute("disabled"); },
+ get disabled() {
+ var val = this.getAttribute("disabled");
+ return val != "false" && !!val;
+ },
set disabled(val) { return this.setAttribute("disabled",val); },
- get checked() { return !!this.getAttribute("checked"); },
+ get checked() {
+ var val = this.getAttribute("checked");
+ return val != "false" && !!val;
+ },
set checked(val) { return this.setAttribute("checked",val); },
- get selected() { return !!this.getAttribute("selected"); },
+ 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;
+ },
set selected(val) { return this.setAttribute("selected",val); },
get className() { return this.getAttribute("class") || ""; },
- set className(val) { return this.setAttribute("class",val); },
+ set className(val) {
+ return this.setAttribute("class",
+ val.replace(/(^\s*|\s*$)/g,""));
+ },
get type() { return this.getAttribute("type") || ""; },
set type(val) { return this.setAttribute("type",val); },
@@ -307,7 +447,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){
@@ -337,13 +477,50 @@ var window = this;
},
getElementsByTagName: DOMDocument.prototype.getElementsByTagName,
- addEventListener: function(){},
- removeEventListener: function(){},
- click: function(){},
- submit: function(){},
- focus: function(){},
- blur: function(){},
- elements: []
+
+ addEventListener: window.addEventListener,
+ removeEventListener: window.removeEventListener,
+ dispatchEvent: window.dispatchEvent,
+
+ click: function(){
+ var event = document.createEvent();
+ event.initEvent("click");
+ this.dispatchEvent(event);
+ },
+ submit: function(){
+ var event = document.createEvent();
+ event.initEvent("submit");
+ this.dispatchEvent(event);
+ },
+ focus: function(){
+ var event = document.createEvent();
+ event.initEvent("focus");
+ this.dispatchEvent(event);
+ },
+ blur: function(){
+ var event = document.createEvent();
+ event.initEvent("blur");
+ this.dispatchEvent(event);
+ },
+ 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(
+ "
"))
+ .getBytes("UTF8")));
+ return this._doc;
+ } else
+ return null;
+ }
});
// Helper method for extending one object with another
@@ -381,16 +558,138 @@ var window = this;
}
// XMLHttpRequest
+ // Originally implemented by Yehuda Katz
- window.XMLHttpRequest = function(){ };
+ window.XMLHttpRequest = function(){
+ this.headers = {};
+ this.responseHeaders = {};
+ };
XMLHttpRequest.prototype = {
- open: function(){ },
- setRequestHeader: function(){ },
- getResponseHeader: function(){ },
+ open: function(method, url, async, user, password){
+ this.readyState = 1;
+ if (async)
+ this.async = true;
+ this.method = method || "GET";
+ this.url = url;
+ this.onreadystatechange();
+ },
+ setRequestHeader: function(header, value){
+ this.headers[header] = value;
+ },
+ getResponseHeader: function(header){ },
+ send: function(data){
+ var self = this;
+
+ function makeRequest(){
+ var url = new java.net.URL(curLocation, self.url);
+
+ if ( url.getProtocol() == "file" ) {
+ if ( self.method == "PUT" ) {
+ var out = new java.io.FileWriter(
+ new java.io.File( new java.net.URI( url.toString() ) ) ),
+ text = new java.lang.String( data || "" );
+
+ out.write( text, 0, text.length() );
+ out.flush();
+ out.close();
+ } else if ( self.method == "DELETE" ) {
+ var file = new java.io.File( new java.net.URI( url.toString() ) );
+ file["delete"]();
+ } else {
+ var connection = url.openConnection();
+ connection.connect();
+ handleResponse();
+ }
+ } else {
+ var connection = url.openConnection();
+
+ connection.setRequestMethod( self.method );
+
+ // Add headers to Java connection
+ for (var header in self.headers)
+ connection.addRequestProperty(header, self.headers[header]);
+
+ connection.connect();
+
+ // Stick the response headers into responseHeaders
+ for (var i = 0; ; i++) {
+ var headerName = connection.getHeaderFieldKey(i);
+ var headerValue = connection.getHeaderField(i);
+ if (!headerName && !headerValue) break;
+ if (headerName)
+ self.responseHeaders[headerName] = headerValue;
+ }
+
+ handleResponse();
+ }
+
+ function handleResponse(){
+ self.readyState = 4;
+ self.status = parseInt(connection.responseCode) || undefined;
+ self.statusText = connection.responseMessage || "";
+
+ var stream = new java.io.InputStreamReader(connection.getInputStream()),
+ buffer = new java.io.BufferedReader(stream), line;
+
+ while ((line = buffer.readLine()) != null)
+ self.responseText += line;
+
+ self.responseXML = null;
+
+ if ( self.responseText.match(/^\s*) ) {
+ try {
+ self.responseXML = new DOMDocument(
+ new java.io.ByteArrayInputStream(
+ (new java.lang.String(
+ self.responseText)).getBytes("UTF8")));
+ } catch(e) {}
+ }
+ }
+
+ self.onreadystatechange();
+ }
+
+ if (this.async)
+ (new java.lang.Thread(new java.lang.Runnable({
+ run: makeRequest
+ }))).start();
+ else
+ makeRequest();
+ },
+ abort: function(){},
+ onreadystatechange: function(){},
+ getResponseHeader: function(header){
+ if (this.readyState < 3)
+ throw new Error("INVALID_STATE_ERR");
+ else {
+ var returnedHeaders = [];
+ for (var rHeader in this.responseHeaders) {
+ if (rHeader.match(new Regexp(header, "i")))
+ returnedHeaders.push(this.responseHeaders[rHeader]);
+ }
+
+ if (returnedHeaders.length)
+ return returnedHeaders.join(", ");
+ }
+
+ return null;
+ },
+ getAllResponseHeaders: function(header){
+ if (this.readyState < 3)
+ throw new Error("INVALID_STATE_ERR");
+ else {
+ var returnedHeaders = [];
+
+ for (var header in this.responseHeaders)
+ returnedHeaders.push( header + ": " + this.responseHeaders[header] );
+
+ return returnedHeaders.join("\r\n");
+ }
+ },
+ async: true,
readyState: 0,
responseText: "",
- responseXML: {},
status: 0
};
})();