2 * Simulated browser environment for Rhino
3 * By John Resig <http://ejohn.org/>
4 * Copyright 2007 John Resig, under the MIT License
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";
20 var curLocation = (new java.io.File("./")).toURL();
22 window.__defineSetter__("location", function(url){
23 var xhr = new XMLHttpRequest();
25 xhr.onreadystatechange = function(){
26 curLocation = new java.net.URL( curLocation, url );
27 window.document = xhr.responseXML;
29 var event = document.createEvent();
30 event.initEvent("load");
31 window.dispatchEvent( event );
36 window.__defineGetter__("location", function(url){
39 return curLocation.getProtocol() + ":";
42 return curLocation.toString();
54 window.setTimeout = function(fn, time){
56 return num = setInterval(function(){
62 window.setInterval = function(fn, time){
63 var num = timers.length;
65 timers[num] = new java.lang.Thread(new java.lang.Runnable({
68 java.lang.Thread.currentThread().sleep(time);
79 window.clearInterval = function(num){
90 window.addEventListener = function(type, fn){
91 if ( !this.uuid || this == window ) {
92 this.uuid = events.length;
93 events[this.uuid] = {};
96 if ( !events[this.uuid][type] )
97 events[this.uuid][type] = [];
99 if ( events[this.uuid][type].indexOf( fn ) < 0 )
100 events[this.uuid][type].push( fn );
103 window.removeEventListener = function(type, fn){
104 if ( !this.uuid || this == window ) {
105 this.uuid = events.length;
106 events[this.uuid] = {};
109 if ( !events[this.uuid][type] )
110 events[this.uuid][type] = [];
112 events[this.uuid][type] =
113 events[this.uuid][type].filter(function(f){
118 window.dispatchEvent = function(event){
120 if ( this.uuid && events[this.uuid][event.type] ) {
123 events[this.uuid][event.type].forEach(function(fn){
124 fn.call( self, event );
128 if ( this["on" + event.type] )
129 this["on" + event.type].call( self, event );
135 window.DOMDocument = function(file){
137 this._dom = Packages.javax.xml.parsers.
138 DocumentBuilderFactory.newInstance()
139 .newDocumentBuilder().parse(file);
141 if ( !obj_nodes.containsKey( this._dom ) )
142 obj_nodes.put( this._dom, this );
145 DOMDocument.prototype = {
146 createTextNode: function(text){
147 return makeNode( this._dom.createTextNode(
148 text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")) );
150 createElement: function(name){
151 return makeNode( this._dom.createElement(name.toLowerCase()) );
153 getElementsByTagName: function(name){
154 return new DOMNodeList( this._dom.getElementsByTagName(
155 name.toLowerCase()) );
157 getElementById: function(id){
158 var elems = this._dom.getElementsByTagName("*");
160 for ( var i = 0; i < elems.length; i++ ) {
161 var elem = elems.item(i);
162 if ( elem.getAttribute("id") == id )
163 return makeNode(elem);
169 return this.getElementsByTagName("body")[0];
171 get documentElement(){
172 return makeNode( this._dom.getDocumentElement() );
177 addEventListener: window.addEventListener,
178 removeEventListener: window.removeEventListener,
179 dispatchEvent: window.dispatchEvent,
183 importNode: function(node, deep){
184 return makeNode( this._dom.importNode(node._dom, deep) );
186 toString: function(){
187 return "Document" + (typeof this._file == "string" ?
188 ": " + this._file : "");
191 return this.documentElement.outerHTML;
196 getComputedStyle: function(elem){
198 getPropertyValue: function(prop){
199 prop = prop.replace(/\-(\w)/g,function(m,c){
200 return c.toUpperCase();
202 var val = elem.style[prop];
204 if ( prop == "opacity" && val == "" )
214 createEvent: function(){
217 initEvent: function(type){
224 function getDocument(node){
225 return obj_nodes.get(node);
230 window.DOMNodeList = function(list){
232 this.length = list.getLength();
234 for ( var i = 0; i < this.length; i++ ) {
235 var node = list.item(i);
236 this[i] = makeNode( node );
240 DOMNodeList.prototype = {
241 toString: function(){
243 Array.prototype.join.call( this, ", " ) + " ]";
246 return Array.prototype.map.call(
247 this, function(node){return node.outerHTML;}).join('');
253 window.DOMNode = function(node){
257 DOMNode.prototype = {
259 return this._dom.getNodeType();
262 return this._dom.getNodeValue();
265 return this._dom.getNodeName();
267 cloneNode: function(deep){
268 return makeNode( this._dom.cloneNode(deep) );
271 return getDocument( this._dom.ownerDocument );
273 get documentElement(){
274 return makeNode( this._dom.documentElement );
277 return makeNode( this._dom.getParentNode() );
280 return makeNode( this._dom.getNextSibling() );
282 get previousSibling() {
283 return makeNode( this._dom.getPreviousSibling() );
285 toString: function(){
286 return '"' + this.nodeValue + '"';
289 return this.nodeValue;
295 window.DOMElement = function(elem){
298 get opacity(){ return this._opacity; },
299 set opacity(val){ this._opacity = val + ""; }
303 var styles = (this.getAttribute("style") || "").split(/\s*;\s*/);
305 for ( var i = 0; i < styles.length; i++ ) {
306 var style = styles[i].split(/\s*:\s*/);
307 if ( style.length == 2 )
308 this.style[ style[0] ] = style[1];
312 DOMElement.prototype = extend( new DOMNode(), {
314 return this.tagName.toUpperCase();
317 return this._dom.getTagName();
319 toString: function(){
320 return "<" + this.tagName + (this.id ? "#" + this.id : "" ) + ">";
323 var ret = "<" + this.tagName, attr = this.attributes;
325 for ( var i in attr )
326 ret += " " + i + "='" + attr[i] + "'";
328 if ( this.childNodes.length || this.nodeName == "SCRIPT" )
329 ret += ">" + this.childNodes.outerHTML +
330 "</" + this.tagName + ">";
338 var attr = {}, attrs = this._dom.getAttributes();
340 for ( var i = 0; i < attrs.getLength(); i++ )
341 attr[ attrs.item(i).nodeName ] = attrs.item(i).nodeValue;
347 return this.childNodes.outerHTML;
350 html = html.replace(/<\/?([A-Z]+)/g, function(m){
351 return m.toLowerCase();
354 var nodes = this.ownerDocument.importNode(
355 new DOMDocument( new java.io.ByteArrayInputStream(
356 (new java.lang.String("<wrap>" + html + "</wrap>"))
357 .getBytes("UTF8"))).documentElement, true).childNodes;
359 while (this.firstChild)
360 this.removeChild( this.firstChild );
362 for ( var i = 0; i < nodes.length; i++ )
363 this.appendChild( nodes[i] );
367 return nav(this.childNodes);
371 for ( var i = 0; i < nodes.length; i++ )
372 if ( nodes[i].nodeType == 3 )
373 str += nodes[i].nodeValue;
374 else if ( nodes[i].nodeType == 1 )
375 str += nav(nodes[i].childNodes);
379 set textContent(text){
380 while (this.firstChild)
381 this.removeChild( this.firstChild );
382 this.appendChild( this.ownerDocument.createTextNode(text));
392 var val = this.getAttribute("disabled");
393 return val != "false" && !!val;
395 set disabled(val) { return this.setAttribute("disabled",val); },
398 var val = this.getAttribute("checked");
399 return val != "false" && !!val;
401 set checked(val) { return this.setAttribute("checked",val); },
404 if ( !this._selectDone ) {
405 this._selectDone = true;
407 if ( this.nodeName == "OPTION" && !this.parentNode.getAttribute("multiple") ) {
408 var opt = this.parentNode.getElementsByTagName("option");
410 if ( this == opt[0] ) {
413 for ( var i = 1; i < opt.length; i++ )
414 if ( opt[i].selected ) {
420 this.selected = true;
425 var val = this.getAttribute("selected");
426 return val != "false" && !!val;
428 set selected(val) { return this.setAttribute("selected",val); },
430 get className() { return this.getAttribute("class") || ""; },
432 return this.setAttribute("class",
433 val.replace(/(^\s*|\s*$)/g,""));
436 get type() { return this.getAttribute("type") || ""; },
437 set type(val) { return this.setAttribute("type",val); },
439 get value() { return this.getAttribute("value") || ""; },
440 set value(val) { return this.setAttribute("value",val); },
442 get src() { return this.getAttribute("src") || ""; },
443 set src(val) { return this.setAttribute("src",val); },
445 get id() { return this.getAttribute("id") || ""; },
446 set id(val) { return this.setAttribute("id",val); },
448 getAttribute: function(name){
449 return this._dom.hasAttribute(name) ?
450 new String( this._dom.getAttribute(name) ) :
453 setAttribute: function(name,value){
454 this._dom.setAttribute(name,value);
456 removeAttribute: function(name){
457 this._dom.removeAttribute(name);
461 return new DOMNodeList( this._dom.getChildNodes() );
464 return makeNode( this._dom.getFirstChild() );
467 return makeNode( this._dom.getLastChild() );
469 appendChild: function(node){
470 this._dom.appendChild( node._dom );
472 insertBefore: function(node,before){
473 this._dom.insertBefore( node._dom, before ? before._dom : before );
475 removeChild: function(node){
476 this._dom.removeChild( node._dom );
479 getElementsByTagName: DOMDocument.prototype.getElementsByTagName,
481 addEventListener: window.addEventListener,
482 removeEventListener: window.removeEventListener,
483 dispatchEvent: window.dispatchEvent,
486 var event = document.createEvent();
487 event.initEvent("click");
488 this.dispatchEvent(event);
491 var event = document.createEvent();
492 event.initEvent("submit");
493 this.dispatchEvent(event);
496 var event = document.createEvent();
497 event.initEvent("focus");
498 this.dispatchEvent(event);
501 var event = document.createEvent();
502 event.initEvent("blur");
503 this.dispatchEvent(event);
506 return this.getElementsByTagName("*");
509 return this.nodeName == "IFRAME" ? {
510 document: this.contentDocument
513 get contentDocument(){
514 if ( this.nodeName == "IFRAME" ) {
516 this._doc = new DOMDocument(
517 new java.io.ByteArrayInputStream((new java.lang.String(
518 "<html><head><title></title></head><body></body></html>"))
526 // Helper method for extending one object with another
528 function extend(a,b) {
530 var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
534 a.__defineGetter__(i, g);
536 a.__defineSetter__(i, s);
543 // Helper method for generating the right
544 // DOM objects based upon the type
546 var obj_nodes = new java.util.HashMap();
548 function makeNode(node){
550 if ( !obj_nodes.containsKey( node ) )
551 obj_nodes.put( node, node.getNodeType() ==
552 Packages.org.w3c.dom.Node.ELEMENT_NODE ?
553 new DOMElement( node ) : new DOMNode( node ) );
555 return obj_nodes.get(node);
561 // Originally implemented by Yehuda Katz
563 window.XMLHttpRequest = function(){
565 this.responseHeaders = {};
568 XMLHttpRequest.prototype = {
569 open: function(method, url, async, user, password){
573 this.method = method || "GET";
575 this.onreadystatechange();
577 setRequestHeader: function(header, value){
578 this.headers[header] = value;
580 getResponseHeader: function(header){ },
581 send: function(data){
584 function makeRequest(){
585 var url = new java.net.URL(curLocation, self.url);
587 if ( url.getProtocol() == "file" ) {
588 if ( self.method == "PUT" ) {
589 var out = new java.io.FileWriter(
590 new java.io.File( new java.net.URI( url.toString() ) ) ),
591 text = new java.lang.String( data || "" );
593 out.write( text, 0, text.length() );
596 } else if ( self.method == "DELETE" ) {
597 var file = new java.io.File( new java.net.URI( url.toString() ) );
600 var connection = url.openConnection();
601 connection.connect();
605 var connection = url.openConnection();
607 connection.setRequestMethod( self.method );
609 // Add headers to Java connection
610 for (var header in self.headers)
611 connection.addRequestProperty(header, self.headers[header]);
613 connection.connect();
615 // Stick the response headers into responseHeaders
616 for (var i = 0; ; i++) {
617 var headerName = connection.getHeaderFieldKey(i);
618 var headerValue = connection.getHeaderField(i);
619 if (!headerName && !headerValue) break;
621 self.responseHeaders[headerName] = headerValue;
627 function handleResponse(){
629 self.status = parseInt(connection.responseCode) || undefined;
630 self.statusText = connection.responseMessage || "";
632 var stream = new java.io.InputStreamReader(connection.getInputStream()),
633 buffer = new java.io.BufferedReader(stream), line;
635 while ((line = buffer.readLine()) != null)
636 self.responseText += line;
638 self.responseXML = null;
640 if ( self.responseText.match(/^\s*</) ) {
642 self.responseXML = new DOMDocument(
643 new java.io.ByteArrayInputStream(
644 (new java.lang.String(
645 self.responseText)).getBytes("UTF8")));
650 self.onreadystatechange();
654 (new java.lang.Thread(new java.lang.Runnable({
661 onreadystatechange: function(){},
662 getResponseHeader: function(header){
663 if (this.readyState < 3)
664 throw new Error("INVALID_STATE_ERR");
666 var returnedHeaders = [];
667 for (var rHeader in this.responseHeaders) {
668 if (rHeader.match(new Regexp(header, "i")))
669 returnedHeaders.push(this.responseHeaders[rHeader]);
672 if (returnedHeaders.length)
673 return returnedHeaders.join(", ");
678 getAllResponseHeaders: function(header){
679 if (this.readyState < 3)
680 throw new Error("INVALID_STATE_ERR");
682 var returnedHeaders = [];
684 for (var header in this.responseHeaders)
685 returnedHeaders.push( header + ": " + this.responseHeaders[header] );
687 return returnedHeaders.join("\r\n");