Search code examples
c#asp.netjavascriptfckeditor

is there a Way to strip all Unnecessary MS Word Formatting from FCKEditor


I have installed fckeditor and when pasting from MS Word it adds alot of unnecessary formatting. I want to keep certain things like bold, italics, bulltes and so forth. I have searched the web and came up with solutions that strips everything away even the stuff that i wanted to keep like bold and italics. Is there a way to strip just the unnecessary word formatting?


Solution

  • Here's a solution I use to scrub incoming HTML from rich text editors... it's written in VB.NET and I don't have time to convert to C#, but it's pretty straightforward:

     Public Shared Function CleanHtml(ByVal html As String) As String
         '' Cleans all manner of evils from the rich text editors in IE, Firefox, Word, and Excel
         '' Only returns acceptable HTML, and converts line breaks to <br />
         '' Acceptable HTML includes HTML-encoded entities.
         html = html.Replace("&" & "nbsp;", " ").Trim() ' concat here due to SO formatting
         '' Does this have HTML tags?
         If html.IndexOf("<") >= 0 Then
             '' Make all tags lowercase
             html = RegEx.Replace(html, "<[^>]+>", AddressOf LowerTag)
             '' Filter out anything except allowed tags
             '' Problem: this strips attributes, including href from a
             '' http://stackoverflow.com/questions/307013/how-do-i-filter-all-html-tags-except-a-certain-whitelist
             Dim AcceptableTags      As String   = "i|b|u|sup|sub|ol|ul|li|br|h2|h3|h4|h5|span|div|p|a|img|blockquote"
             Dim WhiteListPattern    As String   = "</?(?(?=" & AcceptableTags & ")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:([""']?).*?\1?)?)*\s*/?>"
             html = Regex.Replace(html, WhiteListPattern, "", RegExOptions.Compiled)
             '' Make all BR/br tags look the same, and trim them of whitespace before/after
             html = RegEx.Replace(html, "\s*<br[^>]*>\s*", "<br />", RegExOptions.Compiled)
         End If
         '' No CRs
         html = html.Replace(controlChars.CR, "")
         '' Convert remaining LFs to line breaks
         html = html.Replace(controlChars.LF, "<br />")
         '' Trim BRs at the end of any string, and spaces on either side
         Return RegEx.Replace(html, "(<br />)+$", "", RegExOptions.Compiled).Trim()
     End Function
    
     Public Shared Function LowerTag(m As Match) As String
       Return m.ToString().ToLower()
     End Function
    

    In your case, you'll want to modify the list of "approved" HTML tags in "AcceptableTags"--the code will still strip all the useless attributes (and, unfortunately, the useful ones like HREF and SRC, hopefully those aren't important to you).

    Of course, this requires a trip to the server. If you don't want that, you'll need to add some sort of "clean up" button to the toolbar that calls JavaScript to mess with the editor's current text. Unfortunately, "pasting" is not an event that can be trapped to clean up the markup automatically, and cleaning after every OnChange would make for an unusable editor (since changing the markup changes the text cursor position).