Code tự động chèn width và height vào thuộc tính của ảnh trong Ckeditor

Code tự động chèn width và height vào thuộc tính của ảnh trong Ckeditor



Code tự động chèn width và height vào thuộc tính của ảnh trong Ckeditor cho các bạn nào cần tham khảo nhé mình dùng dc cho bản 3.6.2 
Tự động chèn link vào ảnh trong CKEditor

CKEDITOR.on('instanceReady', function (ev) {

// Ends self closing tags the HTML4 way, like
.
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;
            }
        }
    });
});

Ngày đăng: 3819 lượt đọc


Bài viết liên quan

Web 3.0 là gì và nó có thể giúp cho thế giới web như thế nào?
Những điều bạn nên biết về giao thức HTTP/2
Link tải 157 fonts tiếng Hàn, Korean fonts
Tại sao trang web phải thân thiện với người dùng di động?
5 sai lầm SEO thường gặp khi thiết kế lại trang web của bạn
15 thuật ngữ trong thiết kế mà có thể bạn đã từng dùng sai