Subversion-Projekte lars-tiefland.content-management

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
//Copyright © 2005, John Goodman - john.goodman(at)unverse.net  *date 060402 v2
2
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5
 
6
var nlTag='|div|p|table|tbody|tr|td|th|title|script|comment|li|h1|h2|h3|h4|h5|h6|hr|ul|ol|option|select|';
7
var tagNl='|p|th|style|';
8
var regCmt=new RegExp();
9
regCmt.compile("^<!--(.*)-->$");
10
var regHyph=new RegExp();
11
regHyph.compile("-$");
12
function get_xhtml(node,ndNl,inPre){
13
 var i;
14
 var tx='';
15
 var kids=node.childNodes;
16
 var kidsL=kids.length;
17
 var tagNm;
18
 var doNl=ndNl?true:false;
19
 var sz=["small","xx-small","x_small","small","medium","large","x-large","xx-large"];
20
 for(i=0; i<kidsL; i++){
21
  var kid=kids[i];
22
  switch(kid.nodeType){
23
   case 1:{
24
    var tagNm=String(kid.tagName).toLowerCase();
25
    if(tagNm=='')break;
26
    if(tagNm=='font') { tagNm='span'; }
27
    if(tagNm=='!'){
28
     var bits=regCmt.exec(kid.tx);
29
     if(bits){
30
      var innerTx=bits[1];
31
      tx+=tidyCmt(innerTx);
32
     }
33
    }else{
34
     if(nlTag.indexOf('|'+tagNm+'|')!=-1){
35
      if((doNl||tx!='')&&!inPre)tx+='\n';
36
      else doNl=true;
37
     }
38
     tx+='<'+tagNm;
39
     var attr=kid.attributes;
40
     var atLn=attr.length;
41
     var atVal;
42
     var atLang=false;
43
     var atXml=false;
44
     var atXmlns=false;
45
     var isAlt=false;
46
     for(j=0; j<atLn; j++){
47
      var atNm=attr[j].nodeName.toLowerCase();
48
      if(!attr[j].specified&&(atNm!='selected'||!kid.selected)
49
         && (atNm!='style'||kid.style.cssText=='')
50
         && atNm!='value')
51
       continue;
52
      if(atNm=='_moz_dirty'||atNm=='_moz_resizing'||tagNm=='br'&&atNm=='type'&&kid.getAttribute('type')=='_moz')
53
       continue;
54
      var valid_attr=true;
55
      switch(atNm){
56
       case "color":atNm="style";
57
        tVal="color:"+kid.color+";";
58
        if (window.atVal){atVal+=tVal ;}else{atVal=tVal;}
59
        break;
60
       case "style":atVal=kid.style.cssText.toLowerCase();
61
        break;
62
       case "class":atVal=kid.className;
63
        break;
64
       case "noshade":
65
       case "checked":
66
       case "selected":
67
       case "multiple":
68
       case "nowrap":
69
       case "disabled":atVal=atNm;
70
        break;
71
       default:try{
72
        atVal=kid.getAttribute(atNm,2);
73
       }catch(e){ valid_attr=false; }
74
      }
75
      if(valid_attr){
76
       if(!(tagNm=='li'&&atNm=='value')){
77
        tx+=' '+atNm+'="'+tidyAt(atVal)+'"';
78
       }
79
      }
80
      if(atNm=='alt')isAlt=true;
81
     }
82
     if(tagNm=='img'&&!isAlt) tx+=' alt=""';
83
    if(kid.canHaveChildren||kid.hasChildNodes()){
84
     tx+='>';
85
     if(tagNl.indexOf('|'+tagNm+'|')!=-1){}
86
     tx+=get_xhtml(kid,true,inPre||tagNm=='pre'?true:false);
87
     tx+='</'+tagNm+'>';
88
    }else{
89
     if(tagNm=='style'||tagNm=='title'||tagNm=='script'){
90
      tx+='>';
91
      var innerTx;
92
      if(tagNm=='script'){
93
       innerTx=kid.tx;
94
      }else innerTx=kid.innerHTML;
95
      if(tagNm=='style'){
96
       innerTx=String(innerTx).replace(/[\n]+/g,'\n');
97
      }
98
      tx+=innerTx+'</'+tagNm+'>';
99
     }else{ tx+=' />'; }
100
    }
101
   }
102
   break;
103
  }
104
  case 3:{
105
   if(!inPre){
106
    if(kid.nodeValue!='\n'){ tx+=tidyTxt(kid.nodeValue); }
107
   }
108
   else tx+=kid.nodeValue;
109
   break;
110
  }
111
  case 8:{
112
   tx+=tidyCmt(kid.nodeValue);
113
   break;
114
  }
115
  default: break;
116
  }
117
 }
118
 return tx;
119
}
120
function tidyCmt(tx){
121
 tx=tx.replace(/--/g,"__");
122
 if(regHyph.exec(tx)){
123
  tx+=" ";
124
 }return "<!--"+tx+"-->";
125
}
126
function tidyTxt(tx){
127
 return String(tx).replace(/\n{2,}/g,"\n").replace(/\&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\u00A0/g,"&nbsp;");
128
}
129
function tidyAt(tx){
130
 return String(tx).replace(/\&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
131
}