而不是这样:
<img src="image.jpg" style="height:100px; width:100px;">
我想要这个
<img src="image.jpg" height="100px" width="100px">
<img src="image.jpg" style="height:100px; width:100px;">
我想要这个
<img src="image.jpg" height="100px" width="100px">
您可以通过将以下代码插入到CKEditor的config.js中来解决此问题
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;
}
}
});
});
sites/all/modules/ckeditor/ckeditor.config.js
文件。 - Vojtech Vitek - golang.cz有一种更简单的方法可以做到这一点,即使用在CKEditor 4.4.0中引入的禁用内容 (Disallowed Content):
CKEDITOR.replace( 'editor1', {
disallowedContent : 'img{width,height}'
} );
或在config.js中:
config.disallowedContent = 'img{width,height}';
此规则将禁止对img元素使用内联样式(宽度和高度),CKEditor将改用属性。
如果您注意到在图像对话框窗口中,宽度/高度输入元素现在消失了,请设置以下内容:
config.extraAllowedContent = 'img[width,height]';
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}';
链接 - Thomas Wood嘿,我搞定了!所以我创建了一个账户在这里发布给大家。虽然我没用它来发送Outlook新闻通讯,但它应该仍然可行,因为它将高度和宽度属性添加到img标签中。
如果我们将来想再次这样做,以下是我具体的步骤:
首先,我找到了一些完全格式化和注释的源文件:
http://dev.fckeditor.net/browser/CKEditor/tags/3.2/_source/plugins/image/dialogs/image.js
然后我提取了plugins/image/dialogs/image.js的源代码:
svn co http://svn.fckeditor.net/CKEditor/tags/3.2/_source/plugins/image/dialogs
cut -c 5- image.js > image2.js
或者只需点击上面第一个链接底部的“原始格式”链接。
现在我们有一个干净的源文件可以编辑。
我手头的压缩版本大小为19k。解压后的源代码大小为45k。但它应该只在有人编辑时才加载,不应该成为问题。如果是问题,只需重新打包即可。
我手头的版本可能与你手头的版本不同,所以我会告诉你我正在修改哪些行,然后告诉你我对它们做了什么。
替换第636-641行:
if ( value )
element.setStyle( 'width', CKEDITOR.tools.cssLength( value ) );
else if ( !value && this.isChanged( ) )
element.removeStyle( 'width' );
!internalCommit && element.removeAttribute( 'width' );
使用:
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' );
将653行替换为(在进行上述编辑后变成657行):
element.setStyle( 'width', value + 'px');
使用:
element.setStyle( 'width', value + 'px');
element.setAttribute( 'width', value );
if ( value )
element.setStyle( 'height', CKEDITOR.tools.cssLength( value ) );
else if ( !value && this.isChanged( ) )
element.removeStyle( 'height' );
if ( !internalCommit && type == IMAGE )
element.removeAttribute( 'height' );
使用:
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' );
element.setStyle( 'height', value + 'px' );
使用:
element.setStyle( 'height', value + 'px' );
element.setAttribute( 'height', value );
唯一的问题是,当您拖动控制点来调整大小时,它将无法工作。我无法弄清楚那部分。在拖动控制点以调整大小后,只需打开图像属性并单击“确定”,它将再次添加宽度和高度属性。
当您保存表单时,请执行此操作
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;
}
}
}
}
http://dev.ckeditor.com/attachment/ticket/5024/5024_6.patch
我使用了它,但稍微简化了代码,以便过滤器只处理图像。当图像被插入时,此解决方案有效,也适用于在编辑器中使用手柄调整大小时。
希望能有所帮助。
对于 CKEditor 版本 4.1,您可以使用以下内容:
CKEDITOR.replace(textareaId,{
allowedContent: true,
});
要小心使用这个,因为它似乎会删除所有的HTML验证。