2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2009 Frederico Caldeira Knabben
7
* Licensed under the terms of any of the following licenses at your
10
* - GNU General Public License Version 2 or later (the "GPL")
11
* http://www.gnu.org/licenses/gpl.html
13
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
* http://www.gnu.org/licenses/lgpl.html
16
* - Mozilla Public License Version 1.1 or later (the "MPL")
17
* http://www.mozilla.org/MPL/MPL-1.1.html
24
var FCKCodeFormatter = new Object() ;
26
FCKCodeFormatter.Init = function()
28
var oRegex = this.Regex = new Object() ;
30
// Regex for line breaks.
31
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 ;
32
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 ;
34
oRegex.NewLineTags = /\<(BR|HR)[^\>]*\>/gi ;
36
oRegex.MainTags = /\<\/?(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR)[^\>]*\>/gi ;
38
oRegex.LineSplitter = /\s*\n+\s*/g ;
40
// Regex for indentation.
41
oRegex.IncreaseIndent = /^\<(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL|DL)[ \/\>]/i ;
42
oRegex.DecreaseIndent = /^\<\/(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL|DL)[ \>]/i ;
43
oRegex.FormatIndentatorRemove = new RegExp( '^' + FCKConfig.FormatIndentator ) ;
45
oRegex.ProtectedTags = /(<PRE[^>]*>)([\s\S]*?)(<\/PRE>)/gi ;
48
FCKCodeFormatter._ProtectData = function( outer, opener, data, closer )
50
return opener + '___FCKpd___' + ( FCKCodeFormatter.ProtectedData.push( data ) - 1 ) + closer ;
53
FCKCodeFormatter.Format = function( html )
58
// Protected content that remain untouched during the
59
// process go in the following array.
60
FCKCodeFormatter.ProtectedData = new Array() ;
62
var sFormatted = html.replace( this.Regex.ProtectedTags, FCKCodeFormatter._ProtectData ) ;
65
sFormatted = sFormatted.replace( this.Regex.BlocksOpener, '\n$&' ) ;
66
sFormatted = sFormatted.replace( this.Regex.BlocksCloser, '$&\n' ) ;
67
sFormatted = sFormatted.replace( this.Regex.NewLineTags, '$&\n' ) ;
68
sFormatted = sFormatted.replace( this.Regex.MainTags, '\n$&\n' ) ;
71
var sIndentation = '' ;
73
var asLines = sFormatted.split( this.Regex.LineSplitter ) ;
76
for ( var i = 0 ; i < asLines.length ; i++ )
78
var sLine = asLines[i] ;
80
if ( sLine.length == 0 )
83
if ( this.Regex.DecreaseIndent.test( sLine ) )
84
sIndentation = sIndentation.replace( this.Regex.FormatIndentatorRemove, '' ) ;
86
sFormatted += sIndentation + sLine + '\n' ;
88
if ( this.Regex.IncreaseIndent.test( sLine ) )
89
sIndentation += FCKConfig.FormatIndentator ;
92
// Now we put back the protected data.
93
for ( var j = 0 ; j < FCKCodeFormatter.ProtectedData.length ; j++ )
95
var oRegex = new RegExp( '___FCKpd___' + j ) ;
96
sFormatted = sFormatted.replace( oRegex, FCKCodeFormatter.ProtectedData[j].replace( /\$/g, '$$$$' ) ) ;
99
return sFormatted.Trim() ;