最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CKEditor - remove script tag with data processor - Stack Overflow

programmeradmin6浏览0评论

I am quite new with CKEditor (starting to use it 2 days ago) and I am still fighting with some configuration like removing the tag from editor.

So for example, if a user type in source mode the following:

<script type="text/javascript">alert('hello');</script>

I would like to remove it.

Looking the documentation, I found that this can be done using an HTML filter. I so defined it but it does not work.

var editor = ev.editor;
var dataProcessor = editor.dataProcessor;
var htmlFilter = dataProcessor && dataProcessor.htmlFilter;
htmlFilter.addRules(
    {
        elements :
          {
             script : function(element)
                {
                   alert('Found script :' + element.name);
                   element.remove();
                },
             img : function( element )
                {
                   alert('Found script :' + element.name);
                   if ( !element.attributes.alt )
                       element.attributes.alt = 'Cookingfactory';
                   }
                 }
             });

The img part is working well but not the script one. I guess I missed something. It even does not display the alert message for script.

Any help would be more than wele :o)

I am quite new with CKEditor (starting to use it 2 days ago) and I am still fighting with some configuration like removing the tag from editor.

So for example, if a user type in source mode the following:

<script type="text/javascript">alert('hello');</script>

I would like to remove it.

Looking the documentation, I found that this can be done using an HTML filter. I so defined it but it does not work.

var editor = ev.editor;
var dataProcessor = editor.dataProcessor;
var htmlFilter = dataProcessor && dataProcessor.htmlFilter;
htmlFilter.addRules(
    {
        elements :
          {
             script : function(element)
                {
                   alert('Found script :' + element.name);
                   element.remove();
                },
             img : function( element )
                {
                   alert('Found script :' + element.name);
                   if ( !element.attributes.alt )
                       element.attributes.alt = 'Cookingfactory';
                   }
                 }
             });

The img part is working well but not the script one. I guess I missed something. It even does not display the alert message for script.

Any help would be more than wele :o)

Share Improve this question asked Oct 25, 2012 at 9:38 VéniziaVénizia 5815 silver badges14 bronze badges 8
  • Take a look at this: stackoverflow./questions/3391288/… – Andrea Ligios Commented Oct 25, 2012 at 9:48
  • Andrea,Thx for trying to help. Not sure how the link is supposed to help (maybe I do not understant all) but do you have any idea why the solution I tried is not working? – Vénizia Commented Oct 25, 2012 at 13:31
  • no, don't know htmlFilter. But what you want to achieve is "stripping html tags from source in CKEditor"; the link i posted is about "strip bogus HTML from CKEditor". The solution accepted suggests to use the integrated cleanUp function of CKEditor, no need to build one by yourself... have you tried that solution ? – Andrea Ligios Commented Oct 25, 2012 at 13:35
  • I just tried and it does not look like to work. Also it looks it's doing more than what I want. Advantages of the htmlfilter solution is that you do define what to do with specific tag. Not sure of what does the pastefromword cleaning tool. – Vénizia Commented Oct 25, 2012 at 13:54
  • Ah it's working when I use the save button. But still, I do not know what does pletely this process. I would prefer to understand why the htmlfilter is not working (it seems to be the right method to do such a thing). – Vénizia Commented Oct 25, 2012 at 14:02
 |  Show 3 more ments

3 Answers 3

Reset to default 3

You can use this :

CKEDITOR.replace('editor1', {
   on: {
      pluginsLoaded: function(event) {
         event.editor.dataProcessor.dataFilter.addRules({
            elements: {
               script: function(element) {
                  return false;
               }
            }
         });
      }
   }
});

If you are using CKEditor 4.1 or above, you may use Advanced Content Filter to allow the content you want.

If you are using CKEditor 4.4 or above, there is an easier way. You can use Disallowed Content to filter content you don't like .

 config.disallowedContent = 'script';

As I'm having CKEditor 4, I did the next

CKEDITOR.instances.editor1.config.protectedSource.push( /{.*\".*}/g );

It will ignore quotes in smarty curly brackets

发布评论

评论列表(0)

  1. 暂无评论