fckcodeformatter.js
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:3k
源码类别:

OA系统

开发平台:

C#

  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. var FCKCodeFormatter = new Object() ;
  20. FCKCodeFormatter.Init = function()
  21. {
  22. var oRegex = this.Regex = new Object() ;
  23. // Regex for line breaks.
  24. 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 ;
  25. 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 ;
  26. oRegex.NewLineTags = /<(BR|HR)[^>]*>/gi ;
  27. oRegex.MainTags = /</?(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR)[^>]*>/gi ;
  28. oRegex.LineSplitter = /s*n+s*/g ;
  29. // Regex for indentation.
  30. oRegex.IncreaseIndent = /^<(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ />]/i ;
  31. oRegex.DecreaseIndent = /^</(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ >]/i ;
  32. oRegex.FormatIndentatorRemove = new RegExp( '^' + FCKConfig.FormatIndentator ) ;
  33. oRegex.ProtectedTags = /(<PRE[^>]*>)([sS]*?)(</PRE>)/gi ;
  34. }
  35. FCKCodeFormatter._ProtectData = function( outer, opener, data, closer )
  36. {
  37. return opener + '___FCKpd___' + FCKCodeFormatter.ProtectedData.AddItem( data ) + closer ;
  38. }
  39. FCKCodeFormatter.Format = function( html )
  40. {
  41. if ( !this.Regex )
  42. this.Init() ;
  43. // Protected content that remain untouched during the
  44. // process go in the following array.
  45. FCKCodeFormatter.ProtectedData = new Array() ;
  46. var sFormatted = html.replace( this.Regex.ProtectedTags, FCKCodeFormatter._ProtectData ) ;
  47. // Line breaks.
  48. sFormatted = sFormatted.replace( this.Regex.BlocksOpener, 'n$&' ) ; ;
  49. sFormatted = sFormatted.replace( this.Regex.BlocksCloser, '$&n' ) ;
  50. sFormatted = sFormatted.replace( this.Regex.NewLineTags, '$&n' ) ;
  51. sFormatted = sFormatted.replace( this.Regex.MainTags, 'n$&n' ) ;
  52. // Indentation.
  53. var sIndentation = '' ;
  54. var asLines = sFormatted.split( this.Regex.LineSplitter ) ;
  55. sFormatted = '' ;
  56. for ( var i = 0 ; i < asLines.length ; i++ )
  57. {
  58. var sLine = asLines[i] ;
  59. if ( sLine.length == 0 )
  60. continue ;
  61. if ( this.Regex.DecreaseIndent.test( sLine ) )
  62. sIndentation = sIndentation.replace( this.Regex.FormatIndentatorRemove, '' ) ;
  63. sFormatted += sIndentation + sLine + 'n' ;
  64. if ( this.Regex.IncreaseIndent.test( sLine ) )
  65. sIndentation += FCKConfig.FormatIndentator ;
  66. }
  67. // Now we put back the protected data.
  68. for ( var i = 0 ; i < FCKCodeFormatter.ProtectedData.length ; i++ )
  69. {
  70. var oRegex = new RegExp( '___FCKpd___' + i ) ;
  71. sFormatted = sFormatted.replace( oRegex, FCKCodeFormatter.ProtectedData[i].replace( /$/g, '$$$$' ) ) ;
  72. }
  73. return sFormatted.trim() ;
  74. }