Listing du fichier arbresdom.js
00001// (gH) -_- domtrees.js ; TimeStamp (unix) : 04 Juin 2009 vers 14:50
00002
00003 // F. Permadi 2005.
00004 // (C) F. Permadi
00005 // Print DOM tree
00006
00007 // This function traverses the DOM tree of an element and prints the tree.
00008 // This function called recursively until the DOM tree is fully traversed.
00009 //
00010 // Parameters:
00011 // - targetDocument is where the tree will be printed into
00012 // - currentElement is the element that we want to print
00013 // - depth is the depth of the current element
00014 // (it should be 1 for the initial element)
00015
00016
00017 // ###############################################################
00018
00019 function sousArbreDom(currentElement, depth,attr) {
00020
00021 // ###############################################################
00022
00023 tdd = typeof(depth) ; if (tdd=="undefined") { depth = 1 }
00024 if (typeof(attr)=="undefined") { attr = 1 }
00025
00026 rt = "" // initialisation de la chaine à renvoyer
00027
00028 // ceci constitue le critère d'arret
00029
00030 if (currentElement) {
00031
00032 var tagName=currentElement.tagName;
00033
00034 // on écrit la balise comme <a>, <img>, etc
00035 if (tagName) {
00036 if (depth>=1) {
00037 rt += "<"+currentElement.tagName.toLowerCase()
00038 // il faut maintenant récupérer les attributs non nuls de la balise
00039 if (attr==1) {
00040 attrs = ""
00041 nbac = currentElement.attributes.length
00042 if (nbac>0) {
00043 for (ida=0; ida<currentElement.attributes.length;ida++ ) {
00044 curn = currentElement.attributes[ida].name
00045 curv = currentElement.attributes[ida].value
00046 ok = 1
00047 if (typeof(curv)=='undefined') { ok = 0 }
00048 if (curv=='null') { ok = 0 }
00049 if (!curv) { ok = 0 }
00050 if (curv==null) { ok = 0 }
00051 if (curv==0) { ok = 0 }
00052 if (curv=='') { ok = 0 }
00053 if (ok==1) {
00054 attrs += " " + curn + "='" + curv + "'";
00055 } // fin si
00056 } // fin pour
00057 } // fin si
00058 rt += attrs
00059 } // fin si
00060 } // fin si
00061 } else {
00062 cv = currentElement.nodeValue
00063 rt += cv
00064 } // fin de si
00065
00066 if (tagName) {
00067 if (depth>=1) {
00068 rt += ">"
00069 } // fin si
00070
00071 // traversée récursive de l'arbre
00072
00073 var indc=0;
00074 var enfant=0;
00075 var currentElementChild=currentElement.childNodes[indc];
00076 while (currentElementChild) {
00077 // traversée récursive du sous-arbre du noeud enfant
00078 rt += sousArbreDom(currentElementChild, depth+1,attr);
00079 indc++;
00080 currentElementChild=currentElement.childNodes[indc];
00081 } // fin de tant que
00082
00083 // fermeture éventuelle de la balise
00084
00085 if (tagName) {
00086 if (depth>=1) {
00087 rt += "</"+tagName.toLowerCase()+">";
00088 } // fin si
00089 } // fin si
00090 } // fin si
00091
00092 } // fin de si currentElement
00093
00094 return( rt )
00095
00096 } // fin de fonction sousArbreDom
00097
00098 // ###############################################################
00099
00100 function sousArbreDomGraphique(currentElement, depth,attr) {
00101
00102 // ###############################################################
00103
00104 tdd = typeof(depth)
00105 if (tdd=="undefined") { depth = 1 }
00106
00107 tda = typeof(attr)
00108 if (tda=="undefined") { attr = 1 }
00109
00110 if (currentElement) {
00111
00112 // -------------------------------------------------------------
00113
00114 var j;
00115 var tagName=currentElement.tagName;
00116
00117 if (tagName) {
00118
00119 ecrit_infos_sansrc("<"+currentElement.tagName.toLowerCase());
00120 if (attr==1) {
00121 // il faut maintenant récupérer les attributs non nuls de la balise
00122 attrs = ""
00123 nbac = currentElement.attributes.length
00124 if (nbac>0) {
00125 for (ida=0; ida<currentElement.attributes.length;ida++ ) {
00126 curn = currentElement.attributes[ida].name
00127 curv = currentElement.attributes[ida].value
00128 ok = 1
00129 if (typeof(curv)=='undefined') { ok = 0 }
00130 if (curv=='null') { ok = 0 }
00131 if (!curv) { ok = 0 }
00132 if (curv==null) { ok = 0 }
00133 if (curv==0) { ok = 0 }
00134 if (curv=='') { ok = 0 }
00135 if (ok==1) {
00136 attrs += " " + curn + "='" + curv + "'";
00137 } // fin si
00138 } // fin pour
00139 ecrit_infos_sansrc(attrs);
00140 } // fin si
00141 } // fin si
00142 ecrit_infos_sansrc(">");
00143
00144 } else {
00145 // ==> ecrit_infos_sansrc("[unknown tag]")
00146 nv = currentElement.nodeValue;
00147 // ne pas afficher retour-charriot (code 10 mais [rc])
00148 if (nv.charCodeAt(0)==10) {
00149 ecrit_infos_sansrc("[rc]")
00150 } else {
00151 ecrit_infos_sansrc(nv)
00152 } // fin si
00153 } ; // fin si
00154
00155 // ---------------- partie récursive -----------------------
00156
00157 var i=0;
00158 var currentElementChild=currentElement.childNodes[i];
00159 while (currentElementChild) {
00160 ecrit_infos_sansrc("\n");
00161 for (j=0; j<depth; j++) {
00162 ecrit_infos_sansrc(" |");
00163 } // fin pour
00164 if (tagName) {
00165 ecrit_infos_sansrc("--");
00166 } // fin si
00167 sousArbreDomGraphique(currentElementChild, depth+1,attr);
00168 i++;
00169 currentElementChild=currentElement.childNodes[i];
00170 } // fintant que
00171
00172 // -------------------------------------------------------------
00173
00174 if (tagName) {
00175 if (depth>=1) {
00176 ecrit_infos_sansrc("\n");
00177
00178 for (j=0; j<depth-1; j++) {
00179 ecrit_infos_sansrc(" |");
00180 } // fin pour
00181 if (depth>1) { ecrit_infos_sansrc(" "); }
00182 } // fin si
00183 ecrit_infos_sansrc("</"+tagName.toLowerCase()+">");
00184 } // fin si
00185
00186 currentElement=0;
00187
00188 } // fin si
00189
00190 } // fin de fonction sousArbreDomGraphique
00191
00192 // ###############################################################
00193
00194
Pour ne pas voir les numéros de ligne, ajoutez &nl=non à la suite du nom du fichier.
Retour à la page principale de (gH)