If I understand this correctly the data is from the custom xwiki-image-widget.
A sample looks like this:
data-cke-widget-data="{"hasCaption":false,"src":"/xwiki/bin/download/pasteTest/source/WebHome/1531915339545-818.png?width=288&height=288","alt":"1531915339545-818.png","width":"288","height":"288","lock":true,"align":"none","resourceReference":{"typed":false,"type":"attach","reference":"1531915339545-818.png","parameters":{}},"classes":null}"
Actually I’d argue that this data should be cleaned if it is from an external source which we can determine via evt.data.dataTransfer.getTransferType(), see here. Let’s assume you have an application where you copy data from that also uses ckeditor in a different configuration, then you should not “trust” and reuse the data from them.
I updated the script once more to include the test for the DataTransferType and to update links also:
require(['deferred!ckeditor'], function(ckeditorPromise) {
ckeditorPromise.done(function(ckeditor) {
ckeditor.on('instanceCreated', function(event) {
event.editor.on('paste', function(evt){
//Update references only if they are pasted from an external source
console.log("DataTransferType: " + evt.data.dataTransfer.getTransferType())
if(evt.data.dataTransfer.getTransferType()!==CKEDITOR.DATA_TRANSFER_EXTERNAL){
return;
}
var fragment = CKEDITOR.htmlParser.fragment.fromHtml(evt.data.dataValue);
count=0;
fragment.forEach(function(node){
if(node.name!=null && (node.name=="img" || node.name=="a")){
if(node.attributes["data-reference"] || node.attributes["data-cke-widget-data"]){
delete node.attributes["data-reference"];
delete node.attributes["data-cke-widget-data"];
src=node.attributes["src"]
node.attributes["src"]=window.location.origin + src
count++;
}
}
}, CKEDITOR.NODE_ELEMENT, true);
if(count>0){
if(confirm("Update image references?")){
var writer = new CKEDITOR.htmlParser.basicWriter();
fragment.writeHtml( writer );
var writtenHTML=writer.getHtml();
console.log(writtenHTML);
evt.data.dataValue = writtenHTML;
}
}
});
});
});
});