Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code> 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 &lt;br /&gt; '' Acceptable HTML includes HTML-encoded entities. html = html.Replace("&amp;" &amp; "nbsp;", " ").Trim() ' concat here due to SO formatting '' Does this have HTML tags? If html.IndexOf("&lt;") &gt;= 0 Then '' Make all tags lowercase html = RegEx.Replace(html, "&lt;[^&gt;]+&gt;", 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 = "&lt;/?(?(?=" &amp; AcceptableTags &amp; ")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:([""']?).*?\1?)?)*\s*/?&gt;" 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*&lt;br[^&gt;]*&gt;\s*", "&lt;br /&gt;", RegExOptions.Compiled) End If '' No CRs html = html.Replace(controlChars.CR, "") '' Convert remaining LFs to line breaks html = html.Replace(controlChars.LF, "&lt;br /&gt;") '' Trim BRs at the end of any string, and spaces on either side Return RegEx.Replace(html, "(&lt;br /&gt;)+$", "", RegExOptions.Compiled).Trim() End Function Public Shared Function LowerTag(m As Match) As String Return m.ToString().ToLower() End Function </code></pre> <p>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).</p> <p>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).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload