【问题标题】:javascript html editor copy/paste problemjavascript html编辑器复制/粘贴问题
【发布时间】:2010-08-09 16:07:19
【问题描述】:

我们运行一些大型目录,用户经常将 Word 文档等内容复制/粘贴到我们的 TinyMCE html 编辑器中。

这样的问题通常是例如以下文本被隐藏在我们的网页上:

<!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; mso-layout-grid-align:none; punctuation-wrap:simple; text-autospace:none; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} p {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

是否有 TinyMCE 插件或其他一些跨浏览器 html 编辑器可以自动删除它?

或者另一种解决方案是一些 php 正则表达式命令或可以去除这些注释声明的东西。

【问题讨论】:

  • 祝你好运。我看了又看,没有找到任何东西,但我像鹰一样看这篇文章,希望有人有比手动使用 Dreamweaver 清理 Word 的烂摊子更好的解决方案。
  • ckeditor.com尝试ckeditor
  • TinyMCE 似乎具有“从 Word 粘贴”功能,就像其他 WYSIWYG 编辑器一样。查看副本。
  • 添加到 Pekka 的评论中,我还看到 TinyMCE 将 Paste from word 设置为在 Drupal 中使用时的默认粘贴行为。似乎这是 Drupal 公开的 TinyMCE 功能,而不是 Drupal 的额外功能。

标签: php javascript tinymce


【解决方案1】:

多年来我一直在努力优化它。

到目前为止,我最好的解决方案是这样的:

  • 不要使用根块,布局会实现根布局
  • 不要指望用户理解 &lt;p&gt;&lt;br /&gt; 之间的区别,因此将所有内容都视为简单的中断,因为它不那么混乱,更像 ms-word
  • 只允许预期的元素

这将是初始化代码

remove_linebreaks : false,
force_br_newlines : true, <?php /* maybe we can behave more like gmail */ ?>
force_p_newlines : false,   <?php /* and preserve all message line breaks */ ?> 
convert_newlines_to_brs : false, <?php /* even so i would not count with it */ ?>
forced_root_block : false

<?php /* explicitly define what will be allowed */ ?>
valid_elements: "h1,h2,h3,br,b,a,i,u,strong/b,em/i,u/span,strike/span,span,span[style],"+
                "sub,sup,a[href|name|anchor|target|title],ul,ol,li,p,object[classid|width|height|codebase|*],"+
                "param[name|value|_value],embed[type|width|height|src|*],"+
                "img[style|longdesc|usemap|src|border|alt=|title|hspace|vspace|width|height|align]",

然后我有以下后处理功能来删除所有&lt;p&gt;并将所有&lt;/p&gt;转换为&lt;br /&gt;&lt;br /&gt;这是我一直以来最稳定的复制粘贴解决方案能够开发。

这是后处理功能

setup : function(ed) {
    ed.onPostProcess.add(function(ed, o) {
        // Remove all paragraphs and replace with BR
        o.content = o.content.replace(/<p [^>]+>|<p>/g, '');
        o.content = o.content.replace(/<\/p>/g, '<br />');
    });
},

请注意,所有这些都只是 Javascript 过滤,用户将能够迅速将所有不需要的代码传递到服务器。尽管此设置可能是为最终管理员设置而设计的,但在服务器端也使用strip_tags,因为某个地方的某人可能会绕过它。

希望对你有帮助!

【讨论】:

    【解决方案2】:

    我个人会使用 PHP regex 命令。

    $str = preg_replace('/<!--.*?--\>/','',$str);
    

    【讨论】:

    • 完美,试一试
    猜你喜欢
    • 2015-10-25
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    相关资源
    最近更新 更多