Disable sorting of element attributes

2019-02-26 21:54发布

Is there a way to disable the sorting of element attributes so that checkDirty() will work correctly when allowedContent is set to true?

Example of the sorting of attributes here

<div zattribute='z' attribute='a'>simple</div

gets changed to

<div attribute="a" zattribute="z">simple</div>

causing the checkDirty() call to always return true even though the user didn't actually change anything within the ckeditor user interface.

3条回答
孤傲高冷的网名
2楼-- · 2019-02-26 22:18

Your code does strange things like calling editor.resetDirty() on editor#contentDom (what for?). Additionally, CKEditor does not sort attributes, because the only thing that getSnapshot() does is taking the editable's innerHTML. So if anything sorts attributes it's browsers and if they do this (I remember that some do this randomly), then there's nothing you can do.

You need to start from scratch. Define first what you want to achieve and do this with least code possible. I would also advise you to not use editor.checkDirty() because it's a legacy method that won't work in specific cases (yes, this is missing in the docs). Use editor#change to get live notifications about changes, or simply compare editor.getData() from time to time.

查看更多
贪生不怕死
3楼-- · 2019-02-26 22:19
CKEDITOR.on( 'instanceReady', function( ev ) {
    ev.editor.dataProcessor.writer.sortAttributes = 0;
});

will disable attribute sorting for all editor instances on the page. This is not covered anywhere in CKEditor docs and was found by reviewing editor instance object.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-02-26 22:25

This is what I ended up using based on Reinmar's suggestions.

jsfiddle

var isdirty = function(ckeditor) {
        return ckeditor.initialdata !== ckeditor.getData();

    };
CKEDITOR.on('instanceReady', function (event) {
        event.editor.initialdata = event.editor.getData();
    });
查看更多
登录 后发表回答