CKEDITOR - prevent adding image dimensions as a css style

后端 未结 7 1455
独厮守ぢ
独厮守ぢ 2020-12-14 06:21

How to prevent CKEDITOR from adding image dimensions as a style?

Instead of this:


         


        
相关标签:
7条回答
  • 2020-12-14 06:34

    When you save your form, do this

    var CKEDITOR   = window.parent.CKEDITOR;   
            for ( var i in CKEDITOR.instances ){
               var currentInstance = i;
               break;
            }
            var oEditor = CKEDITOR.instances[currentInstance];
            oEditor.dataProcessor.htmlFilter.addRules({
                elements :{
                    img : function( element ){
                        if(!element.attributes.width){
                            if(element.attributes.style){
                                var styling = element.attributes.style
                                var findwidth = new RegExp("\[width: \]\s*(((?!\[width: \]|\[px\]).)+)\s*\[px\]")
                                var sWidth = findwidth.exec(styling)
                                sWidth = sWidth[1]
                                if(sWidth) element.attributes.width = sWidth;
                            }
                            // var reg=/width: \s*([0-9]+)\s*px/i;
                            // var res=styling.match(reg);
    
    
                        }
                       if(!element.attributes.height){
                            if(element.attributes.style){
                                var styling = element.attributes.style
                                var findheight = new RegExp("\[height: \]\s*(((?!\[height: \]|\[px\]).)+)\s*\[px\]")
                                var sHeight = findheight.exec(styling)
                                sHeight = sHeight[1]
                                if(sHeight) element.attributes.width = sHeight;
                            }
                        }
    
                    }
    
        }
    
    0 讨论(0)
  • 2020-12-14 06:35

    For ckeditor version 4.1 you can use the following:

    CKEDITOR.replace(textareaId,{
        allowedContent: true,
    });
    

    Be careful with this as it seems to remove all html validation.

    0 讨论(0)
  • 2020-12-14 06:40

    Hey, I figured it out! So I created an account here just to post this for you all. I'm not using it for an Outlook newsletter but it should still work for that because it adds on the height and width attributes to the img tags.

    If we ever happen to want to do this again here is exactly how I did it.

    First I found some fully formatted and commented source files:

    http://dev.fckeditor.net/browser/CKEditor/tags/3.2/_source/plugins/image/dialogs/image.js

    So I retrieved the source of plugins/image/dialogs/image.js:

    svn co http://svn.fckeditor.net/CKEditor/tags/3.2/_source/plugins/image/dialogs
    

    If you have line numbers on each line because you didn't download it and just copied it you can remove them by running this command (from Linux terminal):

    cut -c 5- image.js > image2.js
    

    Or just click the Original Format link near the bottom of the page at the 1st link above.

    Now we have a clean source file ready to edit.

    The original packed version was 19k in the one I had. The unpacked source code was 45k. But it should only load when someone is editing something anyway and shouldn't be a problem. If it is then just repack it.

    The version I have might be different from the one you have so I will show you which lines I am modifying and then what I did to them.

    Replace lines 636-641:

    if ( value )
        element.setStyle( 'width', CKEDITOR.tools.cssLength( value ) );
    else if ( !value && this.isChanged( ) )
        element.removeStyle( 'width' );
    
    !internalCommit && element.removeAttribute( 'width' );
    

    with:

    if ( value ) {
        element.setStyle( 'width', CKEDITOR.tools.cssLength( value ) );
        element.setAttribute( 'width', value );
    } else if ( !value && this.isChanged( ) ) {
        element.removeStyle( 'width' );
        element.removeAttribute( 'width' );
    }
    
    //!internalCommit && element.removeAttribute( 'width' );
    

    Replace line 653 (657 after above edits):

    element.setStyle( 'width', value + 'px');
    

    with:

    element.setStyle( 'width', value + 'px');
    element.setAttribute( 'width', value );
    

    Replace lines 686-692 (691-697 after above edits):

    if ( value )
        element.setStyle( 'height', CKEDITOR.tools.cssLength( value ) );
    else if ( !value && this.isChanged( ) )
        element.removeStyle( 'height' );
    
    if ( !internalCommit && type == IMAGE )
        element.removeAttribute( 'height' );
    

    with:

    if ( value ) {
        element.setStyle( 'height', CKEDITOR.tools.cssLength( value ) );
        element.setAttribute( 'height', value );
    } else if ( !value && this.isChanged( ) ) {
        element.removeStyle( 'height' );
        element.removeAttribute( 'height' );
    }
    
    //if ( !internalCommit && type == IMAGE )
    //  element.removeAttribute( 'height' );
    

    Replace line 704 (712 after above edits):

    element.setStyle( 'height', value + 'px' );
    

    with:

    element.setStyle( 'height', value + 'px' );
    element.setAttribute( 'height', value );
    

    The only catch is that it doesn't work when you drag the control points to resize it. I couldn't figure out that part. After dragging the control points to resize it just open the Image Properties and click OK and it will add the width and height attributes in again.

    0 讨论(0)
  • 2020-12-14 06:46

    You can resolve the issue by inserting this code in config.js of your CKEditor

    CKEDITOR.on('instanceReady', function (ev) {
    ev.editor.dataProcessor.htmlFilter.addRules(
        {
            elements:
            {
                $: function (element) {
                    // Output dimensions of images as width and height
                    if (element.name == 'img') {
                        var style = element.attributes.style;
    
                        if (style) {
                            // Get the width from the style.
                            var match = /(?:^|\s)width\s*:\s*(\d+)px/i.exec(style),
                                width = match && match[1];
    
                            // Get the height from the style.
                            match = /(?:^|\s)height\s*:\s*(\d+)px/i.exec(style);
                            var height = match && match[1];
    
                            if (width) {
                                element.attributes.style = element.attributes.style.replace(/(?:^|\s)width\s*:\s*(\d+)px;?/i, '');
                                element.attributes.width = width;
                            }
    
                            if (height) {
                                element.attributes.style = element.attributes.style.replace(/(?:^|\s)height\s*:\s*(\d+)px;?/i, '');
                                element.attributes.height = height;
                            }
                        }
                    }
    
    
    
                    if (!element.attributes.style)
                        delete element.attributes.style;
    
                    return element;
                }
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-14 06:46

    I do not believe you can do it without altering the image plugin file of the CKEDITOR..

    If you search their bug tracking site, you will see that they try to 'avoid XHTML deprecated attributes' in favor of styling. ( Avoid deprecated image attributes )

    The place to look if you want to do it yourself (by altering the source files) is this file : plugins_image_dialogs_image.js You will see in there that they specifically remove the attribute and add the style equivalent.

    0 讨论(0)
  • 2020-12-14 06:51

    Similar to Cedric Dugas' solution, there is a patch to a ticket for CKEditor here that helped me a lot solving this problem :

    http://dev.ckeditor.com/attachment/ticket/5024/5024_6.patch

    I used it but trimmed the code a little bit so that just images are processed by the filter. This solution works when the image gets inserted but also when it is resized with the handles in the editor.

    Hope it helps

    0 讨论(0)
提交回复
热议问题