| 1 |
/* |
| 2 |
|
| 3 |
* FCKeditor - The text editor for Internet - http://www.fckeditor.net |
| 4 |
|
| 5 |
* Copyright (C) 2003-2009 Frederico Caldeira Knabben |
| 6 |
|
| 7 |
* |
| 8 |
|
| 9 |
* == BEGIN LICENSE == |
| 10 |
|
| 11 |
* |
| 12 |
|
| 13 |
* Licensed under the terms of any of the following licenses at your |
| 14 |
|
| 15 |
* choice: |
| 16 |
|
| 17 |
* |
| 18 |
|
| 19 |
* - GNU General Public License Version 2 or later (the "GPL") |
| 20 |
|
| 21 |
* http://www.gnu.org/licenses/gpl.html |
| 22 |
|
| 23 |
* |
| 24 |
|
| 25 |
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL") |
| 26 |
|
| 27 |
* http://www.gnu.org/licenses/lgpl.html |
| 28 |
|
| 29 |
* |
| 30 |
|
| 31 |
* - Mozilla Public License Version 1.1 or later (the "MPL") |
| 32 |
|
| 33 |
* http://www.mozilla.org/MPL/MPL-1.1.html |
| 34 |
|
| 35 |
* |
| 36 |
|
| 37 |
* == END LICENSE == |
| 38 |
|
| 39 |
* |
| 40 |
|
| 41 |
* Format the HTML. |
| 42 |
|
| 43 |
*/ |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
var FCKCodeFormatter = new Object() ; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
FCKCodeFormatter.Init = function() |
| 52 |
|
| 53 |
{ |
| 54 |
|
| 55 |
var oRegex = this.Regex = new Object() ; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
// Regex for line breaks. |
| 60 |
|
| 61 |
oRegex.BlocksOpener = /\<(P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|OL|UL|LI|DL|DT|DD|TITLE|META|LINK|BASE|SCRIPT|LINK|TD|TH|AREA|OPTION)[^\>]*\>/gi ; |
| 62 |
|
| 63 |
oRegex.BlocksCloser = /\<\/(P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|OL|UL|LI|DL|DT|DD|TITLE|META|LINK|BASE|SCRIPT|LINK|TD|TH|AREA|OPTION)[^\>]*\>/gi ; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
oRegex.NewLineTags = /\<(BR|HR)[^\>]*\>/gi ; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
oRegex.MainTags = /\<\/?(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR)[^\>]*\>/gi ; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
oRegex.LineSplitter = /\s*\n+\s*/g ; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
// Regex for indentation. |
| 80 |
|
| 81 |
oRegex.IncreaseIndent = /^\<(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL|DL)[ \/\>]/i ; |
| 82 |
|
| 83 |
oRegex.DecreaseIndent = /^\<\/(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL|DL)[ \>]/i ; |
| 84 |
|
| 85 |
oRegex.FormatIndentatorRemove = new RegExp( '^' + FCKConfig.FormatIndentator ) ; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
oRegex.ProtectedTags = /(<PRE[^>]*>)([\s\S]*?)(<\/PRE>)/gi ; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
FCKCodeFormatter._ProtectData = function( outer, opener, data, closer ) |
| 96 |
|
| 97 |
{ |
| 98 |
|
| 99 |
return opener + '___FCKpd___' + ( FCKCodeFormatter.ProtectedData.push( data ) - 1 ) + closer ; |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
FCKCodeFormatter.Format = function( html ) |
| 106 |
|
| 107 |
{ |
| 108 |
|
| 109 |
if ( !this.Regex ) |
| 110 |
|
| 111 |
this.Init() ; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
// Protected content that remain untouched during the |
| 116 |
|
| 117 |
// process go in the following array. |
| 118 |
|
| 119 |
FCKCodeFormatter.ProtectedData = new Array() ; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
var sFormatted = html.replace( this.Regex.ProtectedTags, FCKCodeFormatter._ProtectData ) ; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
// Line breaks. |
| 128 |
|
| 129 |
sFormatted = sFormatted.replace( this.Regex.BlocksOpener, '\n$&' ) ; |
| 130 |
|
| 131 |
sFormatted = sFormatted.replace( this.Regex.BlocksCloser, '$&\n' ) ; |
| 132 |
|
| 133 |
sFormatted = sFormatted.replace( this.Regex.NewLineTags, '$&\n' ) ; |
| 134 |
|
| 135 |
sFormatted = sFormatted.replace( this.Regex.MainTags, '\n$&\n' ) ; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
// Indentation. |
| 140 |
|
| 141 |
var sIndentation = '' ; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
var asLines = sFormatted.split( this.Regex.LineSplitter ) ; |
| 146 |
|
| 147 |
sFormatted = '' ; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
for ( var i = 0 ; i < asLines.length ; i++ ) |
| 152 |
|
| 153 |
{ |
| 154 |
|
| 155 |
var sLine = asLines[i] ; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if ( sLine.length == 0 ) |
| 160 |
|
| 161 |
continue ; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if ( this.Regex.DecreaseIndent.test( sLine ) ) |
| 166 |
|
| 167 |
sIndentation = sIndentation.replace( this.Regex.FormatIndentatorRemove, '' ) ; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
sFormatted += sIndentation + sLine + '\n' ; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if ( this.Regex.IncreaseIndent.test( sLine ) ) |
| 176 |
|
| 177 |
sIndentation += FCKConfig.FormatIndentator ; |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
// Now we put back the protected data. |
| 184 |
|
| 185 |
for ( var j = 0 ; j < FCKCodeFormatter.ProtectedData.length ; j++ ) |
| 186 |
|
| 187 |
{ |
| 188 |
|
| 189 |
var oRegex = new RegExp( '___FCKpd___' + j ) ; |
| 190 |
|
| 191 |
sFormatted = sFormatted.replace( oRegex, FCKCodeFormatter.ProtectedData[j].replace( /\$/g, '$$$$' ) ) ; |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
return sFormatted.Trim() ; |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
|