Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CKEditor - remove script tag with data processor

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 welcome :o)

like image 271
Vénizia Avatar asked Nov 16 '25 16:11

Vénizia


1 Answers

You can use this :

CKEDITOR.replace('editor1', {
   on: {
      pluginsLoaded: function(event) {
         event.editor.dataProcessor.dataFilter.addRules({
            elements: {
               script: function(element) {
                  return false;
               }
            }
         });
      }
   }
});
like image 132
EpokK Avatar answered Nov 18 '25 06:11

EpokK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!