如何防止ckeditor无法添加& nbsp;在空白的html标记 [英] How to prevent ckeditor to not add   in blank html tag

查看:219
本文介绍了如何防止ckeditor无法添加& nbsp;在空白的html标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 8.1操作系统中安装了Visual Studio 2012 Express,并按照需求在我的项目中使用了CKEditor。



我是CKEditor的新用户,并且正在使用它但问题是通过在CKEditor的源代码中定义它自动替换的HTML

 < div>< i class = 类名 >< / I>< / DIV> 

with

 < DIV>&安培; NBSP;< / DIV>或< div>< / div> 

那么如何防止CKEditor取代它并保存它?
我有一些解决方案,但仍然有一点点错误,我正在取代

 < i class =classname> ;< I> /; 

with

 < div class =classname>< / div> 

但是在标签之间它会自动添加& nbsp。

如何防止它不添加& nbsp?



下面的图片是CKEditor是开放的,你可以在圆形区域看到它自动添加一些空间或标签在我的代码。



如何阻止?



解决方案

请看这篇文章:
CKEditor unwanted& nbsp;字符


经过一些研究,我可能会对这个问题有所了解 -
不幸的是,在CKEditor中有四种方式可以产生不间断的空间(任何人都知道更多?):

/ p>


  1. 自动填充空白块。这可以在配置中禁用:

      config.fillEmptyBlocks = false; 


  2. 按TAB键时自动插入。这可以在配置中打开:

      config.tabSpaces = 0; 


  3. 将双空格转换为SPACE + NBSP。 这是浏览器行为,因此CKEditor团队不会修复。它可以固定
    serverside或通过客户端JavaScript onunload。也许这个PHP是一个
    开始:

    pre $ prec_replace('/ \ s& nbsp; \s ','',$ text);


  4. 通过复制并粘贴。如果您粘贴 UTF-8不间断空间或双空格CKEditor会自动转换它。我在这里看到的唯一
    解决方案就是像上面那样做一个正则表达式。
    config.forcePasteAsPlainText = true;
    不起作用。


摘要:为了摆脱所有的不间断空格,您需要编写一个
的附加函数来清除用户输入。



非常感谢意见和进一步的建议! (我正在使用ckeditor
3.6.4)


编辑#1



看看这个。

  CKEDITOR.dtd。 $ removeEmpty.i = 0; 

您也可以在span和其他标签中使用它。



此文档。


停止删除CKEditor中的任何空标记

如果
为空(即,请参阅dtd.js和$ removeEmpty或从控制台运行CKEDITOR.dtd。$ removeEmpty
),将会删除一个定义的标记列表。




  • 来自HTmL



不被移除,添加属性
'data-cke-survive':

 < span data-cke-survive =true>< / span> 




  • 来自配置



或者你可以配置特定标签不被删除:

  if(window .CKEDITOR){
CKEDITOR.on('instanceCreated',function(ev){
CKEDITOR.dtd。$ removeEmpty ['span'] = 0;
CKEDITOR.dtd。$ removeEmpty [ 'TAG-NAME'] = 0;
}
}

通过设置CKEDITOR.dtd。中的一个元素为$ 0 removeEmpty,它
防止空标记被CKEditor删除。


http://margotskapacs.com/


I have Visual Studio 2012 Express installed in Windows 8.1 OS and using CKEditor in my project as per requirement.

I am new for CKEditor and using it in a proper way as well but the problem is by defining the html in source in CKEditor it replaces automatically

<div><i class="classname"></i></div>

with

<div>&nbsp;</div> or <div></div>

So How to prevent CKEditor not to replace it and save as it is? I have got some solution but still little bit error I am replacing

<i class="classname"></i>

with

<div class="classname"></div>

but in between the tag it automatically add &nbsp.

How to prevent it to not add &nbsp?

Here in below image is CKEditor is open and you can see in rounded area it automatically adds some space or tab in my code.

How to stop that?

解决方案

Have a look at this Post: CKEditor unwanted &nbsp; characters

After some research I might shed some light on this issue - unfortunately there is no out-of-the-box solution.

In the CKEditor there are four ways a no-break space can occur (anybody knows more?):

  1. Automatic filling of empty blocks. This can be disabled in the config:

    config.fillEmptyBlocks = false;
    

  2. Automatic insertion when pressing TAB-key. This can be diabled in the config:

    config.tabSpaces = 0;
    

  3. Converting double spaces to SPACE+NBSP. This is a browser behavior and will thus not be fixed by the CKEditor team. It could be fixed serverside or by a clientside javascript onunload. Maybe this php is a start:

    preg_replace('/\s&nbsp;\s/i', ' ', $text);
    

  4. By copy&paste. If you paste a UTF-8 no-break space or double-spaces CKEditor will convert it automatically. The only solution I see here is doing a regex as above. config.forcePasteAsPlainText = true; doesn't help.

Summary: To get rid of all no-break spaces you need to write an additional function that cleans user input.

Comments and further suggestions are greatly appreciated! (I'm using ckeditor 3.6.4)

EDIT #1

Have a look at this.

CKEDITOR.dtd.$removeEmpty.i= 0;

You can also can use this with span and other tags.

The documentation to this.

Stop Removing ANY Empty Tag in CKEditor

There is a defined list of tags that is going to be removed if empty(see dtd.js and $removeEmpty or run CKEDITOR.dtd.$removeEmpty from console).

  • From HTmL

To ensure the certain empty tag are not being removed, add attribute ‘data-cke-survive’:

<span data-cke-survive="true" ></span>

  • From Configurations

Or you can configure the particular tag from not be removed:

if(window.CKEDITOR){
            CKEDITOR.on('instanceCreated', function (ev) {
                CKEDITOR.dtd.$removeEmpty['span'] = 0;
                CKEDITOR.dtd.$removeEmpty['TAG-NAME'] = 0;
           }
}

By setting an element to 0 in the CKEDITOR.dtd.$removeEmpty, it prevents the empty tags from being removed by CKEditor.

http://margotskapacs.com/

这篇关于如何防止ckeditor无法添加&amp; nbsp;在空白的html标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆