首页 > 解决方案 > 防止 TinyMCE 替换为常规空间

问题描述

TinyMCE 编辑器 在开始输入时将所有空格替换为常规空格。我想阻止 html 实体转换。它在每次按键时转换实体。

我已经试过了,

 entity_encoding: "named",
 entities: '160,nbsp,38,amp,60,lt,62,gt'

 entity_encoding: "named",
 entities: ' '

还有什么可做的吗?这是我的 TinyMCE 初始化,

tinymce.init({
        selector: selector,
        apply_source_formatting:true,
        entity_encoding: "named",
        entities: '160,nbsp,38,amp,60,lt,62,gt',
        force_p_newlines: force_p,
        forced_root_block: forced_root,
        valid_elements: "*[*]",
        invalid_elements: invalid_elms,
        valid_children: "+span[div],+th[input]",
        extended_valid_elements: "sc[id],dd[*],dt[*],monospace[*],input[*]", 
        custom_elements: "~sc,~monospace,~input",
        table_toolbar: "",
        menubar: false,
        save_enablewhendirty: false,
        toolbar1: toolbar_icons,
        plugins: [plugins_str],
        inline: true,
        formats: format_array_new,
        resize: true,
        toolbar_sticky: true,
        contextmenu: false,
        remove_script_host: false,

        skin_url: this._global.tinymceUrl + "/skins/ui/oxide",
        autofocus: true,
        fixed_toolbar_container: "#editor-toolbar",
        draggable_modal: true,
        paste_auto_cleanup_on_paste: true,
        paste_remove_spans: true,
        paste_remove_styles: true,
        paste_text_sticky: true,
        paste_strip_class_attributes: "all",
        paste_retain_style_properties: "",
        paste_as_text: true,
});

标签: javascriptjquerytinymcehtml-entitiestinymce-plugins

解决方案


 当 TinyMCE 处理包含任何类型的多个空格字符串的内容时, TinyMCE 将(不管配置设置)混合使用硬 ( ) 和常规空格。

例如,如果您键入一个字母,空格键 5 次,然后另一个字母 TinyMCE 会以如下形式结束:

a     b

这样做有几个原因:

  1. 它允许人们以与在文字处理器中可能执行的操作一致的方式将空格插入到内容中
  2. 交替使用硬空格和常规空格可以让内容包裹在其包含的元素中。一长串硬空格通常会导致内容脱离其包含的元素,并且典型的内容作者不会理解该句子或如何解决该问题。

当 TinyMCE 序列化程序运行时,这个空间管理是进程的一部分,而不是你可以禁用的东西。

您可以做的是包装您不想修改的硬空间。如果您加载 Nonbreak Space TinyMCE 插件,则此功能有一个配置选项:

https://www.tiny.cloud/docs/plugins/nonbreak/#nonbreak_wrap

当您加载不间断空间插件时,默认情况下这是“开启”的,您会看到,如果您使用菜单插入/不间断空间,您会得到如下内容:

<span class="mce-nbsp-wrap" contenteditable="false">&nbsp;</span>

除了具有适当类的跨度之外,没有“魔法”。如果您以这种方式包装您想要维护的任何硬空间,TinyMCE 不会在空间管理过程中修改该空间。


推荐阅读