我正在使用CKEditor 4.4,并且尝试使用ACF强制image2插件将widthheight设置为CSS属性(在style属性中),而不是使用相应的<img>标记属性。

换句话说,我现在使用editor.getData()方法得到的是这样的:

<img src="text.jpg" width="100" height="100" />


但我想要这种其他形式:

<img src="text.jpg" style="width:100px;height:100px" />


我尝试使用allowedContent文件中的disallowedContentconfig.js达到此结果。这是我尝试过的方法(请参阅this以获取参考):

//Allow everything
config.allowedContent = {
    $1: {
        // Use the ability to specify elements as an object.
        elements: CKEDITOR.dtd,
        attributes: true,
        styles: true,
        classes: true
    }
};

config.disallowedContent = "img[width,height]";


这样,结果就是不再设置widthheight(既不作为属性也不作为样式),无法调整图像的大小,并且“图像属性”对话框不再包括与图像尺寸有关的输入框。

我还尝试了扭转Marco Cortellinothis StackOverflow answer中提出的解决方案,但没有取得积极的结果。

有人能帮我吗?

最佳答案

我通过覆盖image2插件的downcastupcast方法(如Reinmar所建议的)解决了该问题。

调用editor.getData()方法时,此方法先处理图像元素。

因此,以下代码代表了一种可能的解决方案:

CKEDITOR.on("instanceCreated", function (ev) {
    ev.editor.on("widgetDefinition", function (evt) {
        var widgetData = evt.data;

        if (widgetData.name != "image" || widgetData.dialog != "image2") return;

        //Override of upcast
        if (!widgetData.stdUpcast) {
            widgetData.stdUpcast = widgetData.upcast;

            widgetData.upcast = function (el, data) {
                var el = widgetData.stdUpcast(el, data);

                if (!el) return el;

                var attrsHolder = el.name == 'a' ? el.getFirst() : el;
                var attrs = attrsHolder.attributes;

                if (el && el.name == "img") {
                    if (el.styles) {
                        attrs.width = (el.styles.width + "").replace('px', '');
                        attrs.height = (el.styles.height + "").replace('px', '');

                        delete el.styles.width;
                        delete el.styles.height;

                        attrs.style = CKEDITOR.tools.writeCssText(el.styles);
                    }
                }

                return el;
            }
        }

        //Override of downcast
        if (!widgetData.stdDowncast) {
            widgetData.stdDowncast = widgetData.downcast;

            widgetData.downcast = function (el) {

                el = this.stdDowncast(el);

                var attrsHolder = el.name == 'a' ? el.getFirst() : el;
                var attrs = attrsHolder.attributes;

                var realWidth, realHeight;

                var widgets = ev.editor.widgets.instances;
                for (widget in widgets) {

                    if (widgets[widget].name != "image" || widgets[widget].dialog != "image2") {
                        continue;
                    }

                    realWidth = $(widgets[widget].element.$).width();
                    realHeight = $(widgets[widget].element.$).height();
                }

                var style = CKEDITOR.tools.parseCssText(attrs.style)

                if (attrs.width) {
                    style.width = realWidth + "px";
                    delete attrs.width;
                }
                if (attrs.height) {
                    style.height = realHeight + "px";
                    delete attrs.height;
                }

                attrs.style = CKEDITOR.tools.writeCssText(style);

                return el;
            }
        }
    });
});

09-27 10:50