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 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))));
26 window.__defineGetter__("location", function(url){
38 window.setTimeout = function(fn, time){
40 return num = setInterval(function(){
46 window.setInterval = function(fn, time){
47 var num = timers.length;
49 timers[num] = new java.lang.Thread(new java.lang.Runnable({
52 java.lang.Thread.currentThread().sleep(time);
63 window.clearInterval = function(num){
72 window.addEventListener = function(){};
73 window.removeEventListener = function(){};
77 window.DOMDocument = function(file){
79 this._dom = Packages.javax.xml.parsers.
80 DocumentBuilderFactory.newInstance()
81 .newDocumentBuilder().parse(file);
83 if ( !obj_nodes.containsKey( this._dom ) )
84 obj_nodes.put( this._dom, this );
87 DOMDocument.prototype = {
88 createTextNode: function(text){
89 return makeNode( this._dom.createTextNode(text) );
91 createElement: function(name){
92 return makeNode( this._dom.createElement(name) );
94 getElementsByTagName: function(name){
95 return new DOMNodeList( this._dom.getElementsByTagName(name) );
97 getElementById: function(id){
98 var elems = this._dom.getElementsByTagName("*");
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);
109 return this.getElementsByTagName("body")[0];
111 get documentElement(){
112 return makeNode( this._dom.getDocumentElement() );
117 addEventListener: function(){},
118 removeEventListener: function(){},
122 importNode: function(node, deep){
123 return makeNode( this._dom.importNode(node._dom, deep) );
125 toString: function(){
126 return "Document" + (typeof this._file == "string" ?
127 ": " + this._file : "");
132 getComputedStyle: function(elem){
134 getPropertyValue: function(prop){
135 prop = prop.replace(/\-(\w)/g,function(m,c){
136 return c.toUpperCase();
138 var val = elem.style[prop];
140 if ( prop == "opacity" && val == "" )
151 function getDocument(node){
152 return obj_nodes.get(node);
157 window.DOMNodeList = function(list){
159 this.length = list.getLength();
161 for ( var i = 0; i < this.length; i++ ) {
162 var node = list.item(i);
163 this[i] = makeNode( node );
167 DOMNodeList.prototype = {
168 toString: function(){
170 Array.prototype.join.call( this, ", " ) + " ]";
173 return Array.prototype.map.call(
174 this, function(node){return node.valueOf();}).join('');
180 window.DOMNode = function(node){
184 DOMNode.prototype = {
186 return this._dom.getNodeType();
189 return this._dom.getNodeValue();
192 return this._dom.getNodeName();
194 cloneNode: function(deep){
195 return makeNode( this._dom.cloneNode(deep) );
198 return getDocument( this._dom.ownerDocument );
200 get documentElement(){
201 return makeNode( this._dom.documentElement );
204 return makeNode( this._dom.getParentNode() );
207 return makeNode( this._dom.getNextSibling() );
209 get previousSibling() {
210 return makeNode( this._dom.getPreviousSibling() );
212 toString: function(){
213 return '"' + this.nodeValue + '"';
216 return this.nodeValue;
222 window.DOMElement = function(elem){
227 var styles = (this.getAttribute("style") || "").split(/\s*;\s*/);
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];
236 DOMElement.prototype = extend( new DOMNode(), {
238 return this.tagName.toUpperCase();
241 return this._dom.getTagName();
243 toString: function(){
244 return "<" + this.tagName + (this.id ? "#" + this.id : "" ) + ">";
247 var ret = "<" + this.tagName, attr = this.attributes;
249 for ( var i in attr )
250 ret += " " + i + "='" + attr[i] + "'";
252 if ( this.childNodes.length || this.nodeName == "SCRIPT" )
253 ret += ">" + this.childNodes.valueOf() +
254 "</" + this.tagName + ">";
262 var attr = {}, attrs = this._dom.getAttributes();
264 for ( var i = 0; i < attrs.getLength(); i++ )
265 attr[ attrs.item(i).nodeName ] = attrs.item(i).nodeValue;
271 return this.childNodes.valueOf();
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;
279 while (this.firstChild)
280 this.removeChild( this.firstChild );
282 for ( var i = 0; i < nodes.length; i++ )
283 this.appendChild( nodes[i] );
287 return nav(this.childNodes);
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);
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;
313 var val = this.getAttribute("disabled");
314 return val != "false" && !!val;
316 set disabled(val) { return this.setAttribute("disabled",val); },
319 var val = this.getAttribute("checked");
320 return val != "false" && !!val;
322 set checked(val) { return this.setAttribute("checked",val); },
325 if ( !this._selectDone ) {
326 this._selectDone = true;
328 if ( this.nodeName == "OPTION" && !this.parentNode.getAttribute("multiple") ) {
329 var opt = this.parentNode.getElementsByTagName("option");
331 if ( this == opt[0] ) {
334 for ( var i = 1; i < opt.length; i++ )
335 if ( opt[i].selected ) {
341 this.selected = true;
346 var val = this.getAttribute("selected");
347 return val != "false" && !!val;
349 set selected(val) { return this.setAttribute("selected",val); },
351 get className() { return this.getAttribute("class") || ""; },
352 set className(val) { return this.setAttribute("class",val); },
354 get type() { return this.getAttribute("type") || ""; },
355 set type(val) { return this.setAttribute("type",val); },
357 get value() { return this.getAttribute("value") || ""; },
358 set value(val) { return this.setAttribute("value",val); },
360 get src() { return this.getAttribute("src") || ""; },
361 set src(val) { return this.setAttribute("src",val); },
363 get id() { return this.getAttribute("id") || ""; },
364 set id(val) { return this.setAttribute("id",val); },
366 getAttribute: function(name){
367 return this._dom.hasAttribute(name) ?
368 new String( this._dom.getAttribute(name) ) :
371 setAttribute: function(name,value){
372 this._dom.setAttribute(name,value);
374 removeAttribute: function(name){
375 this._dom.removeAttribute(name);
379 return new DOMNodeList( this._dom.getChildNodes() );
382 return makeNode( this._dom.getFirstChild() );
385 return makeNode( this._dom.getLastChild() );
387 appendChild: function(node){
388 this._dom.appendChild( node._dom );
390 insertBefore: function(node,before){
391 this._dom.insertBefore( node._dom, before ? before._dom : before );
393 removeChild: function(node){
394 this._dom.removeChild( node._dom );
397 getElementsByTagName: DOMDocument.prototype.getElementsByTagName,
398 addEventListener: function(){},
399 removeEventListener: function(){},
401 submit: function(){},
405 return this.getElementsByTagName("*");
408 return this.nodeName == "IFRAME" ? {
409 document: this.contentDocument
412 get contentDocument(){
413 if ( this.nodeName == "IFRAME" ) {
415 this._doc = new DOMDocument(
416 new java.io.ByteArrayInputStream((new java.lang.String(
417 "<html><head><title></title></head><body></body></html>"))
425 // Helper method for extending one object with another
427 function extend(a,b) {
429 var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
433 a.__defineGetter__(i, g);
435 a.__defineSetter__(i, s);
442 // Helper method for generating the right
443 // DOM objects based upon the type
445 var obj_nodes = new java.util.HashMap();
447 function makeNode(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 ) );
454 return obj_nodes.get(node);
461 window.XMLHttpRequest = function(){ };
463 XMLHttpRequest.prototype = {
465 setRequestHeader: function(){ },
466 getResponseHeader: function(){ },