CKEDITOR 不会同时保存/解析属性和样式中的宽度/高度



这段代码不会在源模式下保存

<img src="/images/test.jpg" height="300" width="450" style="width: 350px; height: auto;" />

当我将其置于源模式并单击返回所见即所得模式时,CKEditor会去除高度/宽度属性。这就是我当时看到的

<img src="/images/test.jpg" style="width: 350px; height: auto;" />

但是我需要它来保存样式和属性(样式高度/宽度和属性高度/宽度)。

我尝试在下面使用此配置,但它不起作用

config.allowedContent = 'img{*}[*](*)'

我也试过

 config.allowedContent = true;

它确实有效,但我的config.disallowedContent当时不起作用。我相信编辑器在 HTML 代码解析时会忽略样式和/或属性(以先行者为准?

我终于找到了问题的答案。这是/ckeditor 中的代码.js其中我注释了一行

contentTransformations: [
    // ["img{width}: sizeToStyle", "img[width]: sizeToAttribute"],
    ["img{float}: alignmentToStyle", "img[align]: alignmentToAttribute"]
]

同样在另一个文件中,我添加了以下代码:

evt.editor.dataProcessor.htmlFilter.addRules( {
elements: {
    $: function( element ) {
        if (element.name == 'img') {
            if (element.attributes.style) {
                var match = /(?:^|s)widths*:s*((d+)(pt|in|px)|auto|initial|inherit)/i.exec(element.attributes.style),
                    width = match && (match[2] || match[1]);
                if(width && !element.attributes.width) {
                    element.attributes.width = width;
                    element.attributes.style = element.attributes.style.replace(/(widths*:s*(d+(pt|in|px)|auto|initial|inherit))/i, "");
                }
                match = /(?:^|s)heights*:s*((d+)(pt|in|px)|auto)/i.exec(element.attributes.style);
                var height = match && (match[2] || match[1]);
                if(height && !element.attributes.height) {
                    element.attributes.height = height;
                    element.attributes.style = element.attributes.style.replace(/(heights*:s*(d+(pt|in|px)|auto|initial|inherit))/i, "");
                }
            }
        }
        return element;
    }
}

});

最新更新