有没有一种方法可以从FCKEditor中删除所有不必要的MS Word格式?

7

我安装了fckeditor,但是从MS Word中粘贴文本时会添加很多不必要的格式。我想保留某些内容,如加粗、斜体、项目符号等。我在网上搜索并找到了一些解决方案,但它们会将所有内容都去掉,甚至包括我想保留的加粗和斜体等内容。有没有办法只去除不必要的Word格式?


任何曾经维护过CMS的人都知道你所说的邪恶。祝你好运找到答案。我们只是让他们从Word中粘贴,然后我有一个程序从数据库中删除不可显示的字符。 - Steve
6个回答

11

如果有人想要C#版本的被接受答案,请参考以下代码:

public string CleanHtml(string html)
    {
        //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)
        {
            // Make all tags lowercase
            html = Regex.Replace(html, "<[^>]+>", delegate(Match m){
                return m.ToString().ToLower();
            });
            // Filter out anything except allowed tags
            // Problem: this strips attributes, including href from a
            // https://dev59.com/o3VC5IYBdhLWcg3wZwPj
            string AcceptableTags = "i|b|u|sup|sub|ol|ul|li|br|h2|h3|h4|h5|span|div|p|a|img|blockquote";
            string WhiteListPattern = "</?(?(?=" + 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);
        }


         // No CRs
         html = html.Replace("\r", "");
         // Convert remaining LFs to line breaks
         html = html.Replace("\n", "<br />");
         // Trim BRs at the end of any string, and spaces on either side
         return Regex.Replace(html, "(<br />)+$", "", RegexOptions.Compiled).Trim();
    }

8

以下是我用来清理富文本编辑器中传入的HTML的解决方案...它是用VB.NET编写的,我没有时间转换为C#,但它非常简单明了:

 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
         '' https://dev59.com/o3VC5IYBdhLWcg3wZwPj
         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

在你的情况下,你需要修改“AcceptableTags”中的“已批准”的HTML标签列表--代码仍将剥离所有无用的属性(不幸的是,有用的如HREF和SRC也会被剥离,希望这些对你不重要)。
当然,这需要服务器的支持。如果你不想这样,你需要在工具栏上添加某种“清理”按钮,调用JavaScript来处理编辑器当前的文本。不幸的是,“粘贴”不能被捕获以自动清理标记,而每次OnChange后都进行清理会导致编辑器无法使用(因为更改标记会改变文本光标位置)。

哇,这太棒了。但我确实需要链接和基本的HTML标签。 - user161433

5
尝试了已接受的解决方案,但它没有清除生成的单词标签。
但是,这段代码对我有用。

static string CleanWordHtml(string html) {

StringCollection sc = new StringCollection();
// get rid of unnecessary tag spans (comments and title)
sc.Add(@"<!--(\w|\W)+?-->");
sc.Add(@"<title>(\w|\W)+?</title>");
// Get rid of classes and styles
sc.Add(@"\s?class=\w+");
sc.Add(@"\s+style='[^']+'");
// Get rid of unnecessary tags
sc.Add(
@"<(meta|link|/?o:|/?style|/?div|/?st\d|/?head|/?html|body|/?body|/?span|!\[)[^>]*?>");
// Get rid of empty paragraph tags
sc.Add(@"(<[^>]+>)+&nbsp;(</\w+>)+");
// remove bizarre v: element attached to <img> tag
sc.Add(@"\s+v:\w+=""[^""]+""");
// remove extra lines
sc.Add(@"(\n\r){2,}");
foreach (string s in sc)
{
    html = Regex.Replace(html, s, "", RegexOptions.IgnoreCase);
}
return html; 
}

2
我非常了解这个问题。当从MS-Word(或任何文字处理或富文本编辑感知文本区域)复制后,粘贴到FCKEditor(TinyMCE也有同样的问题),原始标记将包含在剪贴板中并且被处理。该标记并不总是与嵌入到粘贴操作目标中的标记相容。
我不知道解决方案,除非成为FCKEditor的贡献者并学习代码并进行修改。我的常规做法是指导用户执行两阶段剪贴板操作: - 从MS-Word复制 - 粘贴到记事本 - 全选 - 从记事本复制 - 粘贴到FCKEDitor

0
对于我的解决方案,我最终采用了C#版本的CleanHtml函数和清除MS Office标记的部分组合。基本上是Glenn's过程的代码版本。我会看看当我将它们全部推到一个巨大的Excel电子表格时会发生什么。

0

但是,正如名称和网站所示,fckeditor是一个文本编辑器。对我来说,这意味着它只会显示文件中的字符。

如果没有一些额外的字符,您无法使用粗体和斜体格式。

编辑:啊,我明白了。仔细查看Fckeditor网站后,它是一个HTML编辑器,而不是我习惯的简单文本编辑器之一。

其中一个功能是自动检测Word清理剪贴板


pavium,fckeditor是一个富文本编辑器,它抽象了使用可编辑DIV的所有麻烦,并添加了漂亮的工具栏。在幕后,它存储在HTML中,这意味着当有人从Word粘贴时,Word会传递各种HTML邪恶。 - richardtallent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接