X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=build%2Fruntest%2Fenv.js;h=e15a2263184475b42f0f6d3b3d4ee23807a6d938;hb=d38ee78db9b8a8a89b985928c1d23543cb2ff1cd;hp=23ffaedddbedbd06e4406623b7f5c09dad545ac8;hpb=de71a046e619884292b122ddcf1401ad9932a5c7;p=jquery.git diff --git a/build/runtest/env.js b/build/runtest/env.js index 23ffaed..e15a226 100644 --- a/build/runtest/env.js +++ b/build/runtest/env.js @@ -17,17 +17,32 @@ var window = this; } }; + var curLocation = (new java.io.File("./")).toURL(); + window.__defineSetter__("location", function(url){ - window.document = new DOMDocument( - new Packages.org.xml.sax.InputSource( - new java.io.InputStreamReader( - new java.io.FileInputStream(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; } }; }); @@ -69,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 @@ -117,8 +174,9 @@ var window = this; get ownerDocument(){ return null; }, - addEventListener: function(){}, - removeEventListener: function(){}, + addEventListener: window.addEventListener, + removeEventListener: window.removeEventListener, + dispatchEvent: window.dispatchEvent, get nodeName() { return "#document"; }, @@ -129,6 +187,9 @@ var window = this; return "Document" + (typeof this._file == "string" ? ": " + this._file : ""); }, + get innerHTML(){ + return this.documentElement.outerHTML; + }, get defaultView(){ return { @@ -148,6 +209,15 @@ var window = this; }; } }; + }, + + createEvent: function(){ + return { + type: "", + initEvent: function(type){ + this.type = type; + } + }; } }; @@ -172,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(''); } }; @@ -215,7 +285,7 @@ var window = this; toString: function(){ return '"' + this.nodeValue + '"'; }, - valueOf: function(){ + get outerHTML(){ return this.nodeValue; } }; @@ -249,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 + ""; else ret += "/>"; @@ -274,7 +344,7 @@ var window = this; }, get innerHTML(){ - return this.childNodes.valueOf(); + return this.childNodes.outerHTML; }, set innerHTML(html){ html = html.replace(/<\/?([A-Z]+)/g, function(m){ @@ -407,12 +477,31 @@ var window = this; }, getElementsByTagName: DOMDocument.prototype.getElementsByTagName, - addEventListener: function(){}, - removeEventListener: function(){}, - click: function(){}, - submit: function(){}, - focus: function(){}, - blur: function(){}, + + 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("*"); }, @@ -490,44 +579,75 @@ var window = this; }, getResponseHeader: function(header){ }, send: function(data){ + var self = this; + function makeRequest(){ - var url = new java.net.URL(this.url), - connection = url.openConnection(); + var url = new java.net.URL(curLocation, self.url); - // Add headers to Java connection - for (var header in this.headers) - connection.addRequestProperty(header, this.headers[header]); - - connection.connect(); + 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]); - // Stick the response headers into responseHeaders - for (i=0; ; i++) { - var headerName = connection.getHeaderFieldKey(i); - var headerValue = connection.getHeaderField(i); - if (!headerName && !headerValue) break; - if (headerName) - this.responseHeaders[headerName] = headerValue; + 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(); } - this.readyState = 4; - this.status = parseInt(connection.responseCode); - this.statusText = connection.responseMessage; - - var stream = new java.io.InputStreamReader( - connection.getInputStream()), - buffer = new java.io.BufferedReader(stream), - line; - - while ((line = buffer.readLine()) != null) - this.responseText += line; - - try { - this.responseXML = new DOMDocument(this.responseText); - } catch(e) { - this.responseXML = null; + 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*