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);
84 DOMDocument.prototype = {
85 createTextNode: function(text){
86 return makeNode( this._dom.createTextNode(text) );
88 createElement: function(name){
89 return makeNode( this._dom.createElement(name) );
91 getElementsByTagName: function(name){
92 return new DOMNodeList( this._dom.getElementsByTagName(name) );
94 getElementById: function(id){
95 var elems = this._dom.getElementsByTagName("*");
97 for ( var i = 0; i < elems.length; i++ ) {
98 var elem = elems.item(i);
99 if ( elem.getAttribute("id") == id )
100 return makeNode(elem);
106 return this.getElementsByTagName("body")[0];
108 get documentElement(){
109 return makeNode( this._dom.getDocumentElement() );
114 addEventListener: function(){},
115 removeEventListener: function(){},
119 importNode: function(node, deep){
120 return makeNode( this._dom.importNode(node._dom, deep) );
122 toString: function(){
123 return "Document" + (typeof this._file == "string" ?
124 ": " + this._file : "");
129 getComputedStyle: function(elem){
131 getPropertyValue: function(prop){
132 prop = prop.replace(/\-(\w)/g,function(m,c){
133 return c.toUpperCase();
135 var val = elem.style[prop];
137 if ( prop == "opacity" && val == "" )
150 window.DOMNodeList = function(list){
152 this.length = list.getLength();
154 for ( var i = 0; i < this.length; i++ ) {
155 var node = list.item(i);
156 this[i] = makeNode( node );
160 DOMNodeList.prototype = {
161 toString: function(){
163 Array.prototype.join.call( this, ", " ) + " ]";
166 return Array.prototype.map.call(
167 this, function(node){return node.valueOf();}).join('');
173 window.DOMNode = function(node){
177 DOMNode.prototype = {
179 return this._dom.getNodeType();
182 return this._dom.getNodeValue();
185 return this._dom.getNodeName();
187 cloneNode: function(deep){
188 return makeNode( this._dom.cloneNode(deep) );
193 get documentElement(){
194 return document.documentElement;
197 return makeNode( this._dom.getParentNode() );
200 return makeNode( this._dom.getNextSibling() );
202 get previousSibling() {
203 return makeNode( this._dom.getPreviousSibling() );
205 toString: function(){
206 return '"' + this.nodeValue + '"';
209 return this.nodeValue;
215 window.DOMElement = function(elem){
220 var styles = (this.getAttribute("style") || "").split(/\s*;\s*/);
222 for ( var i = 0; i < styles.length; i++ ) {
223 var style = styles[i].split(/\s*:\s*/);
224 if ( style.length == 2 )
225 this.style[ style[0] ] = style[1];
229 DOMElement.prototype = extend( new DOMNode(), {
231 return this.tagName.toUpperCase();
234 return this._dom.getTagName();
236 toString: function(){
237 return "<" + this.tagName + (this.id ? "#" + this.id : "" ) + ">";
240 var ret = "<" + this.tagName, attr = this.attributes;
242 for ( var i in attr )
243 ret += " " + i + "='" + attr[i] + "'";
245 if ( this.childNodes.length || this.nodeName == "SCRIPT" )
246 ret += ">" + this.childNodes.valueOf() +
247 "</" + this.tagName + ">";
255 var attr = {}, attrs = this._dom.getAttributes();
257 for ( var i = 0; i < attrs.getLength(); i++ )
258 attr[ attrs.item(i).nodeName ] = attrs.item(i).nodeValue;
264 return this.childNodes.valueOf();
267 var nodes = this.ownerDocument.importNode(
268 new DOMDocument( new java.io.ByteArrayInputStream(
269 (new java.lang.String("<wrap>" + html + "</wrap>"))
270 .getBytes("UTF8"))).documentElement, true).childNodes;
272 while (this.firstChild)
273 this.removeChild( this.firstChild );
275 for ( var i = 0; i < nodes.length; i++ )
276 this.appendChild( nodes[i] );
280 return nav(this.childNodes);
284 for ( var i = 0; i < nodes.length; i++ )
285 if ( nodes[i].nodeType == 3 )
286 str += nodes[i].nodeValue;
287 else if ( nodes[i].nodeType == 1 )
288 str += nav(nodes[i].childNodes);
292 set textContent(text){
293 while (this.firstChild)
294 this.removeChild( this.firstChild );
295 this.appendChild( document.createTextNode(text) );
296 this.innerHTML = document.createTextNode(text).nodeValue;
306 var val = this.getAttribute("disabled");
307 return val != "false" && !!val;
309 set disabled(val) { return this.setAttribute("disabled",val); },
312 var val = this.getAttribute("checked");
313 return val != "false" && !!val;
315 set checked(val) { return this.setAttribute("checked",val); },
318 if ( !this._selectDone ) {
319 this._selectDone = true;
321 if ( this.nodeName == "OPTION" && !this.parentNode.getAttribute("multiple") ) {
322 var opt = this.parentNode.getElementsByTagName("option");
324 if ( this == opt[0] ) {
327 for ( var i = 1; i < opt.length; i++ )
328 if ( opt[i].selected ) {
334 this.selected = true;
339 var val = this.getAttribute("selected");
340 return val != "false" && !!val;
342 set selected(val) { return this.setAttribute("selected",val); },
344 get className() { return this.getAttribute("class") || ""; },
345 set className(val) { return this.setAttribute("class",val); },
347 get type() { return this.getAttribute("type") || ""; },
348 set type(val) { return this.setAttribute("type",val); },
350 get value() { return this.getAttribute("value") || ""; },
351 set value(val) { return this.setAttribute("value",val); },
353 get src() { return this.getAttribute("src") || ""; },
354 set src(val) { return this.setAttribute("src",val); },
356 get id() { return this.getAttribute("id") || ""; },
357 set id(val) { return this.setAttribute("id",val); },
359 getAttribute: function(name){
360 return this._dom.hasAttribute(name) ?
361 new String( this._dom.getAttribute(name) ) :
364 setAttribute: function(name,value){
365 this._dom.setAttribute(name,value);
367 removeAttribute: function(name){
368 this._dom.removeAttribute(name);
372 return new DOMNodeList( this._dom.getChildNodes() );
375 return makeNode( this._dom.getFirstChild() );
378 return makeNode( this._dom.getLastChild() );
380 appendChild: function(node){
381 this._dom.appendChild( node._dom );
383 insertBefore: function(node,before){
384 this._dom.insertBefore( node._dom, before ? before._dom : before );
386 removeChild: function(node){
387 this._dom.removeChild( node._dom );
390 getElementsByTagName: DOMDocument.prototype.getElementsByTagName,
391 addEventListener: function(){},
392 removeEventListener: function(){},
394 submit: function(){},
398 return this.getElementsByTagName("*");
402 // Helper method for extending one object with another
404 function extend(a,b) {
406 var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
410 a.__defineGetter__(i, g);
412 a.__defineSetter__(i, s);
419 // Helper method for generating the right
420 // DOM objects based upon the type
422 var obj_nodes = new java.util.HashMap();
424 function makeNode(node){
426 if ( !obj_nodes.containsKey( node ) )
427 obj_nodes.put( node, node.getNodeType() ==
428 Packages.org.w3c.dom.Node.ELEMENT_NODE ?
429 new DOMElement( node ) : new DOMNode( node ) );
431 return obj_nodes.get(node);
438 window.XMLHttpRequest = function(){ };
440 XMLHttpRequest.prototype = {
442 setRequestHeader: function(){ },
443 getResponseHeader: function(){ },