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 051117
2
 
3
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
4
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5
//A copy of the GNU General Public License can be obtained at: http://www.gnu.org/licenses/gpl.html
6
 
7
var nlTag='|div|p|table|tbody|tr|td|th|title|head|body|script|comment|li|meta|h1|h2|h3|h4|h5|h6|hr|ul|ol|option|select|';
8
var tagNl='|html|head|body|p|th|style|';
9
var regCmt=new RegExp();
10
regCmt.compile("^<!--(.*)-->$");
11
var regHyph=new RegExp();
12
regHyph.compile("-$");
13
function get_xhtml(node,lang,encoding,ndNl,inPre){
14
 var i;
15
 var tx='';
16
 var kids=node.childNodes;
17
 var kidsL=kids.length;
18
 var tagNm;
19
 var doNl=ndNl?true:false;
20
 var pagMode=true;
21
 for(i=0; i<kidsL; i++){
22
  var kid=kids[i];
23
  switch(kid.nodeType){
24
   case 1:{
25
    var tagNm=String(kid.tagName).toLowerCase();
26
    if(tagNm=='')break;
27
    if(tagNm=='meta'){
28
     var metaNm=String(kid.name).toLowerCase();
29
     if(metaNm=='generator')break;
30
    }
31
    if(!ndNl&&tagNm=='body'){ pagMode=false; }
32
    if(tagNm=='!'){
33
     var bits=regCmt.exec(kid.tx);
34
     if(bits){
35
      var innerTx=bits[1];
36
      tx+=tidyCmt(innerTx);
37
     }
38
    }else{
39
     if(tagNm=='html'){
40
      tx='<?xml version="1.0" encoding="'+encoding+'"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n';
41
     }
42
     if(nlTag.indexOf('|'+tagNm+'|')!=-1){
43
      if((doNl||tx!='')&&!inPre)tx+='\n';
44
      else doNl=true;
45
     }
46
     tx+='<'+tagNm;
47
     var attr=kid.attributes;
48
     var atLn=attr.length;
49
     var atVal;
50
     var atLang=false;
51
     var atXml=false;
52
     var atXmlns=false;
53
     var isAlt=false;
54
     for(j=0; j<atLn; j++){
55
      var atNm=attr[j].nodeName.toLowerCase();
56
      if(!attr[j].specified&&(atNm!='selected'||!kid.selected)
57
         && (atNm!='style'||kid.style.cssText=='')
58
         && atNm!='value')
59
       continue;
60
      if(atNm=='_moz_dirty'||atNm=='_moz_resizing'||tagNm=='br'&&atNm=='type'&&kid.getAttribute('type')=='_moz')
61
       continue;
62
      var valid_attr=true;
63
      switch(atNm){
64
       case "style":atVal=kid.style.cssText.toLowerCase();
65
        break;
66
       case "class":atVal=kid.className;
67
        break;
68
       case "http-equiv":atVal=kid.httpEquiv;
69
        break;
70
       case "noshade":
71
       case "checked":
72
       case "selected":
73
       case "multiple":
74
       case "nowrap":
75
       case "disabled":atVal=atNm;
76
        break;
77
       default:try{
78
        atVal=kid.getAttribute(atNm,2);
79
       }catch(e){ valid_attr=false; }
80
      }
81
      if(atNm=='lang'){
82
       atLang=true;
83
       atVal=lang;
84
      }
85
      if(atNm=='xml:lang'){
86
       atXml=true;
87
       atVal=lang;
88
      }
89
      if(atNm=='xmlns') atXmlns=true;
90
      if(valid_attr){
91
       if(!(tagNm=='li'&&atNm=='value')){
92
        tx+=' '+atNm+'="'+tidyAt(atVal)+'"';
93
       }
94
      }
95
      if(atNm=='alt')isAlt=true;
96
     }
97
     if(tagNm=='img'&&!isAlt) tx+=' alt=""';
98
     if(tagNm=='html'){
99
      if(!atLang)tx+=' lang="'+lang+'"';
100
      if(!atXml)tx+=' xml:lang="'+lang+'"';
101
      if(!atXmlns)tx+=' xmlns="http://www.w3.org/1999/xhtml"';
102
     }
103
    if(kid.canHaveChildren||kid.hasChildNodes()){
104
     tx+='>';
105
     if(tagNl.indexOf('|'+tagNm+'|')!=-1){}
106
     tx+=get_xhtml(kid,lang,encoding,true,inPre||tagNm=='pre'?true:false);
107
     tx+='</'+tagNm+'>';
108
    }else{
109
     if(tagNm=='style'||tagNm=='title'||tagNm=='script'){
110
      tx+='>';
111
      var innerTx;
112
      if(tagNm=='script'){
113
       innerTx=kid.tx;
114
      }else innerTx=kid.innerHTML;
115
      if(tagNm=='style'){
116
       innerTx=String(innerTx).replace(/[\n]+/g,'\n');
117
      }
118
      tx+=innerTx+'</'+tagNm+'>';
119
     }else{ tx+=' />'; }
120
    }
121
   }
122
   break;
123
  }
124
  case 3:{
125
   if(!inPre){
126
    if(kid.nodeValue!='\n'){ tx+=tidyTxt(kid.nodeValue); }
127
   }
128
   else tx+=kid.nodeValue;
129
   break;
130
  }
131
  case 8:{
132
   tx+=tidyCmt(kid.nodeValue);
133
   break;
134
  }
135
  default: break;
136
  }
137
 }
138
 if(!ndNl&&!pagMode){
139
  tx=tx.replace(/<\/?head>[\n]*/gi,"");
140
  tx=tx.replace(/<head \/>[\n]*/gi,"");
141
  tx=tx.replace(/ <\/?body>[\n]*/gi,"");
142
 }
143
 return tx;
144
}
145
function tidyCmt(tx){
146
 tx=tx.replace(/--/g,"__");
147
 if(regHyph.exec(tx)){
148
  tx+=" ";
149
 }return "<!--"+tx+"-->";
150
}
151
function tidyTxt(tx){
152
 return String(tx).replace(/\n{2,}/g,"\n").replace(/\&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\u00A0/g,"&nbsp;");
153
}
154
function tidyAt(tx){
155
 return String(tx).replace(/\&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
156
}