1075 |
ddelon |
1 |
/*
|
|
|
2 |
* FCKeditor - The text editor for internet
|
|
|
3 |
* Copyright (C) 2003-2006 Frederico Caldeira Knabben
|
|
|
4 |
*
|
|
|
5 |
* Licensed under the terms of the GNU Lesser General Public License:
|
|
|
6 |
* http://www.opensource.org/licenses/lgpl-license.php
|
|
|
7 |
*
|
|
|
8 |
* For further information visit:
|
|
|
9 |
* http://www.fckeditor.net/
|
|
|
10 |
*
|
|
|
11 |
* "Support Open Source software. What about a donation today?"
|
|
|
12 |
*
|
|
|
13 |
* File Name: fckcodeformatter.js
|
|
|
14 |
* Format the HTML.
|
|
|
15 |
*
|
|
|
16 |
* File Authors:
|
|
|
17 |
* Frederico Caldeira Knabben (fredck@fckeditor.net)
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
var FCKCodeFormatter = new Object() ;
|
|
|
21 |
|
|
|
22 |
FCKCodeFormatter.Init = function()
|
|
|
23 |
{
|
|
|
24 |
var oRegex = this.Regex = new Object() ;
|
|
|
25 |
|
|
|
26 |
// Regex for line breaks.
|
|
|
27 |
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 ;
|
|
|
28 |
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 ;
|
|
|
29 |
|
|
|
30 |
oRegex.NewLineTags = /\<(BR|HR)[^\>]*\>/gi ;
|
|
|
31 |
|
|
|
32 |
oRegex.MainTags = /\<\/?(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR)[^\>]*\>/gi ;
|
|
|
33 |
|
|
|
34 |
oRegex.LineSplitter = /\s*\n+\s*/g ;
|
|
|
35 |
|
|
|
36 |
// Regex for indentation.
|
|
|
37 |
oRegex.IncreaseIndent = /^\<(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ \/\>]/i ;
|
|
|
38 |
oRegex.DecreaseIndent = /^\<\/(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ \>]/i ;
|
|
|
39 |
oRegex.FormatIndentatorRemove = new RegExp( '^' + FCKConfig.FormatIndentator ) ;
|
|
|
40 |
|
|
|
41 |
oRegex.ProtectedTags = /(<PRE[^>]*>)([\s\S]*?)(<\/PRE>)/gi ;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
FCKCodeFormatter._ProtectData = function( outer, opener, data, closer )
|
|
|
45 |
{
|
|
|
46 |
return opener + '___FCKpd___' + FCKCodeFormatter.ProtectedData.AddItem( data ) + closer ;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
FCKCodeFormatter.Format = function( html )
|
|
|
50 |
{
|
|
|
51 |
if ( !this.Regex )
|
|
|
52 |
this.Init() ;
|
|
|
53 |
|
|
|
54 |
// Protected content that remain untouched during the
|
|
|
55 |
// process go in the following array.
|
|
|
56 |
FCKCodeFormatter.ProtectedData = new Array() ;
|
|
|
57 |
|
|
|
58 |
var sFormatted = html.replace( this.Regex.ProtectedTags, FCKCodeFormatter._ProtectData ) ;
|
|
|
59 |
|
|
|
60 |
// Line breaks.
|
|
|
61 |
sFormatted = sFormatted.replace( this.Regex.BlocksOpener, '\n$&' ) ; ;
|
|
|
62 |
sFormatted = sFormatted.replace( this.Regex.BlocksCloser, '$&\n' ) ;
|
|
|
63 |
sFormatted = sFormatted.replace( this.Regex.NewLineTags, '$&\n' ) ;
|
|
|
64 |
sFormatted = sFormatted.replace( this.Regex.MainTags, '\n$&\n' ) ;
|
|
|
65 |
|
|
|
66 |
// Indentation.
|
|
|
67 |
var sIndentation = '' ;
|
|
|
68 |
|
|
|
69 |
var asLines = sFormatted.split( this.Regex.LineSplitter ) ;
|
|
|
70 |
sFormatted = '' ;
|
|
|
71 |
|
|
|
72 |
for ( var i = 0 ; i < asLines.length ; i++ )
|
|
|
73 |
{
|
|
|
74 |
var sLine = asLines[i] ;
|
|
|
75 |
|
|
|
76 |
if ( sLine.length == 0 )
|
|
|
77 |
continue ;
|
|
|
78 |
|
|
|
79 |
if ( this.Regex.DecreaseIndent.test( sLine ) )
|
|
|
80 |
sIndentation = sIndentation.replace( this.Regex.FormatIndentatorRemove, '' ) ;
|
|
|
81 |
|
|
|
82 |
sFormatted += sIndentation + sLine + '\n' ;
|
|
|
83 |
|
|
|
84 |
if ( this.Regex.IncreaseIndent.test( sLine ) )
|
|
|
85 |
sIndentation += FCKConfig.FormatIndentator ;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// Now we put back the protected data.
|
|
|
89 |
for ( var i = 0 ; i < FCKCodeFormatter.ProtectedData.length ; i++ )
|
|
|
90 |
{
|
|
|
91 |
var oRegex = new RegExp( '___FCKpd___' + i ) ;
|
|
|
92 |
sFormatted = sFormatted.replace( oRegex, FCKCodeFormatter.ProtectedData[i].replace( /\$/g, '$$$$' ) ) ;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
return sFormatted.trim() ;
|
|
|
96 |
}
|