PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/testing/selenium-core/domviewer/selenium-domviewer.js

http://datanucleus-appengine.googlecode.com/
JavaScript | 205 lines | 174 code | 21 blank | 10 comment | 66 complexity | 0b49e62baaaecc0a937781fe37ca2dc9 MD5 | raw file
  1var HIDDEN="hidden";
  2var LEVEL = "level";
  3var PLUS_SRC="butplus.gif";
  4var MIN_SRC="butmin.gif";
  5var newRoot;
  6var maxColumns=1;
  7
  8function loadDomViewer() {
  9    // See if the rootDocument variable has been set on this window.
 10    var rootDocument = window.rootDocument;
 11
 12    // If not look to the opener for an explicity rootDocument variable, otherwise, use the opener document
 13    if (!rootDocument && window.opener) {
 14        rootDocument = window.opener.rootDocument || window.opener.document;
 15    }
 16
 17    if (rootDocument) {
 18        document.body.innerHTML = displayDOM(rootDocument);
 19    }
 20    else {
 21        document.body.innerHTML = "<b>Must specify rootDocument for window. This can be done by setting the rootDocument variable on this window, or on the opener window for a popup window.</b>";
 22    }
 23}
 24
 25
 26function displayDOM(root){
 27    var str = "";
 28    str+="<table>";
 29    str += treeTraversal(root,0);
 30    // to make table columns work well.
 31    str += "<tr>";
 32    for (var i=0; i < maxColumns; i++) {
 33        str+= "<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>";
 34    }
 35    str += "</tr>";
 36    str += "</table>";
 37    return str;
 38}
 39
 40function checkForChildren(element){
 41    if(!element.hasChildNodes())
 42        return false;
 43    
 44    var nodes = element.childNodes;
 45    var size = nodes.length;
 46    var count=0;
 47    
 48    for(var i=0; i< size; i++){
 49        var node = nodes.item(i);
 50        //if(node.toString()=="[object Text]"){
 51        //this is equalent to the above
 52        //but will work with more browsers
 53        if(node.nodeType!=1){
 54            count++;
 55        }
 56    }
 57    
 58    if(count == size)
 59        return false;
 60    else
 61        return true;
 62}
 63
 64function treeTraversal(root, level){
 65    var str = "";
 66    var nodes= null;
 67    var size = null;
 68    //it is supposed to show the last node, 
 69    //but the last node is always nodeText type
 70    //and we don't show it
 71    if(!root.hasChildNodes())
 72        return "";//displayNode(root,level,false);
 73    
 74    nodes = root.childNodes;
 75    size = nodes.length;
 76
 77    for(var i=0; i< size; i++){
 78        var element = nodes.item(i);
 79        //if the node is textNode, don't display
 80        if(element.nodeType==1){
 81            str+= displayNode(element,level,checkForChildren(element));
 82            str+=treeTraversal(element, level+1);	
 83        }
 84    }
 85    return str;
 86}
 87
 88function displayNode(element, level, isLink){
 89    nodeContent = getNodeContent(element);
 90    columns = Math.round((nodeContent.length / 12) + 0.5);
 91    if (columns + level > maxColumns) {
 92        maxColumns = columns + level;
 93    }
 94    var str ="<tr class='"+LEVEL+level+"'>";
 95    for (var i=0; i < level; i++)
 96        str+= "<td> </td>";
 97    str+="<td colspan='"+ columns +"' class='box"+" boxlevel"+level+"' >";
 98    if(isLink){
 99        str+='<a onclick="hide(this);return false;" href="javascript:void();">';
100        str+='<img src="'+MIN_SRC+'" />';
101    }
102    str += nodeContent;
103    if(isLink)
104        str+="</a></td></tr>";
105    return str;
106}
107
108function getNodeContent(element) {
109
110    str = "";
111    id ="";
112    if (element.id != null && element.id != "") {
113        id = " ID(" + element.id +")";
114    }
115    name ="";
116    if (element.name != null && element.name != "") {
117        name = " NAME(" + element.name + ")";
118    }
119    value ="";
120    if (element.value != null && element.value != "") {
121        value = " VALUE(" + element.value + ")";
122    }
123    href ="";
124    if (element.href != null && element.href != "") {
125        href = " HREF(" + element.href + ")";
126    }
127    clazz = "";
128    if (element.className != null && element.className != "") {
129        clazz = " CLASS(" + element.className + ")";
130    }
131    src = "";
132    if (element.src != null && element.src != "") {
133        src = " SRC(" + element.src + ")";
134    }
135    alt = "";
136    if (element.alt != null && element.alt != "") {
137        alt = " ALT(" + element.alt + ")";
138    }
139    type = "";
140    if (element.type != null && element.type != "") {
141        type = " TYPE(" + element.type + ")";
142    }
143    text ="";
144    if (element.text != null && element.text != "" && element.text != "undefined") {
145        text = " #TEXT(" + trim(element.text) +")";
146    }
147    str+=" <b>"+ element.nodeName + id + alt + type + clazz + name + value + href + src + text + "</b>";
148    return str;
149
150}
151
152function trim(val) {
153    val2 = val.substring(0,40) + "                   ";
154    var spaceChr = String.fromCharCode(32);
155    var length = val2.length;
156    var retVal = "";
157    var ix = length -1;
158
159    while(ix > -1){
160        if(val2.charAt(ix) == spaceChr) {
161        } else {
162            retVal = val2.substring(0, ix +1);
163            break;
164        }
165        ix = ix-1;
166    }
167    if (val.length > 40) {
168        retVal += "...";
169    }
170    return retVal;
171}
172
173function hide(hlink){
174    var isHidden = false;
175    var image = hlink.firstChild;
176    if(image.src.toString().indexOf(MIN_SRC)!=-1){
177        image.src=PLUS_SRC;
178        isHidden=true;
179    }else{
180        image.src=MIN_SRC;
181    }
182    var rowObj= hlink.parentNode.parentNode;
183    var rowLevel = parseInt(rowObj.className.substring(LEVEL.length));
184	
185    var sibling = rowObj.nextSibling;
186    var siblingLevel = sibling.className.substring(LEVEL.length);
187    if(siblingLevel.indexOf(HIDDEN)!=-1){
188        siblingLevel = siblingLevel.substring(0,siblingLevel.length - HIDDEN.length-1);
189    }
190    siblingLevel=parseInt(siblingLevel);
191    while(sibling!=null && rowLevel<siblingLevel){
192        if(isHidden){
193            sibling.className += " "+ HIDDEN;
194        }else if(!isHidden && sibling.className.indexOf(HIDDEN)!=-1){
195            var str = sibling.className;
196            sibling.className=str.substring(0, str.length - HIDDEN.length-1);
197        }
198        sibling = sibling.nextSibling;
199        siblingLevel = parseInt(sibling.className.substring(LEVEL.length));
200    }
201}
202
203function LOG(message) {
204    window.opener.LOG.warn(message);
205}