1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 *  - GNU General Public License Version 2 or later (the "GPL")
11 *    http://www.gnu.org/licenses/gpl.html
12 *
13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 *    http://www.gnu.org/licenses/lgpl.html
15 *
16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
17 *    http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
20 *
21 * Format the HTML.
22 */
23
24var FCKCodeFormatter = new Object() ;
25
26FCKCodeFormatter.Init = function()
27{
28	var oRegex = this.Regex = new Object() ;
29
30	// Regex for line breaks.
31	oRegex.BlocksOpener = /\<(P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|OL|UL|LI|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|TITLE|META|LINK|BASE|SCRIPT|LINK|TD|TH|AREA|OPTION)[^\>]*\>/gi ;
33
34	oRegex.NewLineTags	= /\<(BR|HR)[^\>]*\>/gi ;
35
36	oRegex.MainTags = /\<\/?(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR)[^\>]*\>/gi ;
37
38	oRegex.LineSplitter = /\s*\n+\s*/g ;
39
40	// Regex for indentation.
41	oRegex.IncreaseIndent = /^\<(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ \/\>]/i ;
42	oRegex.DecreaseIndent = /^\<\/(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ \>]/i ;
43	oRegex.FormatIndentatorRemove = new RegExp( '^' + FCKConfig.FormatIndentator ) ;
44
45	oRegex.ProtectedTags = /(<PRE[^>]*>)([\s\S]*?)(<\/PRE>)/gi ;
46}
47
48FCKCodeFormatter._ProtectData = function( outer, opener, data, closer )
49{
50	return opener + '___FCKpd___' + FCKCodeFormatter.ProtectedData.AddItem( data ) + closer ;
51}
52
53FCKCodeFormatter.Format = function( html )
54{
55	if ( !this.Regex )
56		this.Init() ;
57
58	// Protected content that remain untouched during the
59	// process go in the following array.
60	FCKCodeFormatter.ProtectedData = new Array() ;
61
62	var sFormatted = html.replace( this.Regex.ProtectedTags, FCKCodeFormatter._ProtectData ) ;
63
64	// Line breaks.
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' ) ;
69
70	// Indentation.
71	var sIndentation = '' ;
72
73	var asLines = sFormatted.split( this.Regex.LineSplitter ) ;
74	sFormatted = '' ;
75
76	for ( var i = 0 ; i < asLines.length ; i++ )
77	{
78		var sLine = asLines[i] ;
79
80		if ( sLine.length == 0 )
81			continue ;
82
83		if ( this.Regex.DecreaseIndent.test( sLine ) )
84			sIndentation = sIndentation.replace( this.Regex.FormatIndentatorRemove, '' ) ;
85
86		sFormatted += sIndentation + sLine + '\n' ;
87
88		if ( this.Regex.IncreaseIndent.test( sLine ) )
89			sIndentation += FCKConfig.FormatIndentator ;
90	}
91
92	// Now we put back the protected data.
93	for ( var j = 0 ; j < FCKCodeFormatter.ProtectedData.length ; j++ )
94	{
95		var oRegex = new RegExp( '___FCKpd___' + j ) ;
96		sFormatted = sFormatted.replace( oRegex, FCKCodeFormatter.ProtectedData[j].replace( /\$/g, '$$$$' ) ) ;
97	}
98
99	return sFormatted.Trim() ;
100}