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(
90 text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")) );
92 createElement: function(name){
93 return makeNode( this._dom.createElement(name.toLowerCase()) );
95 getElementsByTagName: function(name){
96 return new DOMNodeList( this._dom.getElementsByTagName(
97 name.toLowerCase()) );
99 getElementById: function(id){
100 var elems = this._dom.getElementsByTagName("*");
102 for ( var i = 0; i < elems.length; i++ ) {
103 var elem = elems.item(i);
104 if ( elem.getAttribute("id") == id )
105 return makeNode(elem);
111 return this.getElementsByTagName("body")[0];
113 get documentElement(){
114 return makeNode( this._dom.getDocumentElement() );
119 addEventListener: function(){},
120 removeEventListener: function(){},
124 importNode: function(node, deep){
125 return makeNode( this._dom.importNode(node._dom, deep) );
127 toString: function(){
128 return "Document" + (typeof this._file == "string" ?
129 ": " + this._file : "");
134 getComputedStyle: function(elem){
136 getPropertyValue: function(prop){
137 prop = prop.replace(/\-(\w)/g,function(m,c){
138 return c.toUpperCase();
140 var val = elem.style[prop];
142 if ( prop == "opacity" && val == "" )
153 function getDocument(node){
154 return obj_nodes.get(node);
159 window.DOMNodeList = function(list){
161 this.length = list.getLength();
163 for ( var i = 0; i < this.length; i++ ) {
164 var node = list.item(i);
165 this[i] = makeNode( node );
169 DOMNodeList.prototype = {
170 toString: function(){
172 Array.prototype.join.call( this, ", " ) + " ]";
175 return Array.prototype.map.call(
176 this, function(node){return node.valueOf();}).join('');
182 window.DOMNode = function(node){
186 DOMNode.prototype = {
188 return this._dom.getNodeType();
191 return this._dom.getNodeValue();
194 return this._dom.getNodeName();
196 cloneNode: function(deep){
197 return makeNode( this._dom.cloneNode(deep) );
200 return getDocument( this._dom.ownerDocument );
202 get documentElement(){
203 return makeNode( this._dom.documentElement );
206 return makeNode( this._dom.getParentNode() );
209 return makeNode( this._dom.getNextSibling() );
211 get previousSibling() {
212 return makeNode( this._dom.getPreviousSibling() );
214 toString: function(){
215 return '"' + this.nodeValue + '"';
218 return this.nodeValue;
224 window.DOMElement = function(elem){
227 get opacity(){ return this._opacity; },
228 set opacity(val){ this._opacity = val + ""; }
232 var styles = (this.getAttribute("style") || "").split(/\s*;\s*/);
234 for ( var i = 0; i < styles.length; i++ ) {
235 var style = styles[i].split(/\s*:\s*/);
236 if ( style.length == 2 )
237 this.style[ style[0] ] = style[1];
241 DOMElement.prototype = extend( new DOMNode(), {
243 return this.tagName.toUpperCase();
246 return this._dom.getTagName();
248 toString: function(){
249 return "<" + this.tagName + (this.id ? "#" + this.id : "" ) + ">";
252 var ret = "<" + this.tagName, attr = this.attributes;
254 for ( var i in attr )
255 ret += " " + i + "='" + attr[i] + "'";
257 if ( this.childNodes.length || this.nodeName == "SCRIPT" )
258 ret += ">" + this.childNodes.valueOf() +
259 "</" + this.tagName + ">";
267 var attr = {}, attrs = this._dom.getAttributes();
269 for ( var i = 0; i < attrs.getLength(); i++ )
270 attr[ attrs.item(i).nodeName ] = attrs.item(i).nodeValue;
276 return this.childNodes.valueOf();
279 html = html.replace(/<\/?([A-Z]+)/g, function(m){
280 return m.toLowerCase();
283 var nodes = this.ownerDocument.importNode(
284 new DOMDocument( new java.io.ByteArrayInputStream(
285 (new java.lang.String("<wrap>" + html + "</wrap>"))
286 .getBytes("UTF8"))).documentElement, true).childNodes;
288 while (this.firstChild)
289 this.removeChild( this.firstChild );
291 for ( var i = 0; i < nodes.length; i++ )
292 this.appendChild( nodes[i] );
296 return nav(this.childNodes);
300 for ( var i = 0; i < nodes.length; i++ )
301 if ( nodes[i].nodeType == 3 )
302 str += nodes[i].nodeValue;
303 else if ( nodes[i].nodeType == 1 )
304 str += nav(nodes[i].childNodes);
308 set textContent(text){
309 while (this.firstChild)
310 this.removeChild( this.firstChild );
311 this.appendChild( this.ownerDocument.createTextNode(text));
321 var val = this.getAttribute("disabled");
322 return val != "false" && !!val;
324 set disabled(val) { return this.setAttribute("disabled",val); },
327 var val = this.getAttribute("checked");
328 return val != "false" && !!val;
330 set checked(val) { return this.setAttribute("checked",val); },
333 if ( !this._selectDone ) {
334 this._selectDone = true;
336 if ( this.nodeName == "OPTION" && !this.parentNode.getAttribute("multiple") ) {
337 var opt = this.parentNode.getElementsByTagName("option");
339 if ( this == opt[0] ) {
342 for ( var i = 1; i < opt.length; i++ )
343 if ( opt[i].selected ) {
349 this.selected = true;
354 var val = this.getAttribute("selected");
355 return val != "false" && !!val;
357 set selected(val) { return this.setAttribute("selected",val); },
359 get className() { return this.getAttribute("class") || ""; },
361 return this.setAttribute("class",
362 val.replace(/(^\s*|\s*$)/g,""));
365 get type() { return this.getAttribute("type") || ""; },
366 set type(val) { return this.setAttribute("type",val); },
368 get value() { return this.getAttribute("value") || ""; },
369 set value(val) { return this.setAttribute("value",val); },
371 get src() { return this.getAttribute("src") || ""; },
372 set src(val) { return this.setAttribute("src",val); },
374 get id() { return this.getAttribute("id") || ""; },
375 set id(val) { return this.setAttribute("id",val); },
377 getAttribute: function(name){
378 return this._dom.hasAttribute(name) ?
379 new String( this._dom.getAttribute(name) ) :
382 setAttribute: function(name,value){
383 this._dom.setAttribute(name,value);
385 removeAttribute: function(name){
386 this._dom.removeAttribute(name);
390 return new DOMNodeList( this._dom.getChildNodes() );
393 return makeNode( this._dom.getFirstChild() );
396 return makeNode( this._dom.getLastChild() );
398 appendChild: function(node){
399 this._dom.appendChild( node._dom );
401 insertBefore: function(node,before){
402 this._dom.insertBefore( node._dom, before ? before._dom : before );
404 removeChild: function(node){
405 this._dom.removeChild( node._dom );
408 getElementsByTagName: DOMDocument.prototype.getElementsByTagName,
409 addEventListener: function(){},
410 removeEventListener: function(){},
412 submit: function(){},
416 return this.getElementsByTagName("*");
419 return this.nodeName == "IFRAME" ? {
420 document: this.contentDocument
423 get contentDocument(){
424 if ( this.nodeName == "IFRAME" ) {
426 this._doc = new DOMDocument(
427 new java.io.ByteArrayInputStream((new java.lang.String(
428 "<html><head><title></title></head><body></body></html>"))
436 // Helper method for extending one object with another
438 function extend(a,b) {
440 var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
444 a.__defineGetter__(i, g);
446 a.__defineSetter__(i, s);
453 // Helper method for generating the right
454 // DOM objects based upon the type
456 var obj_nodes = new java.util.HashMap();
458 function makeNode(node){
460 if ( !obj_nodes.containsKey( node ) )
461 obj_nodes.put( node, node.getNodeType() ==
462 Packages.org.w3c.dom.Node.ELEMENT_NODE ?
463 new DOMElement( node ) : new DOMNode( node ) );
465 return obj_nodes.get(node);
472 window.XMLHttpRequest = function(){ };
474 XMLHttpRequest.prototype = {
476 setRequestHeader: function(){ },
477 getResponseHeader: function(){ },