I have written my own plugin for an auto-correct feature in CKEditor 4, it allows you to type @abc for instance and it will replace this with "as easy as one two three" plus a space character and put the cursor after the space.
This is the javascript code from the plugin that updates the string:
var s = editor.getSelection();
var bookmarks = s.createBookmarks(true);
var data = editor.getData();
var replaced_text = data.replace(typedchars, Suggestions + ' ');
editor.setData(replaced_text);
var range = s.getRanges()[0];
range.moveToBookmark(bookmarks[0]);
range.select();
It works perfectly on my local machine but as soon as I put this code into production I get an error on the line - range.moveToBookmark(bookmarks[0]); and the cursor goes to the start of the line.
The error is:
Uncaught TypeError: Cannot read property 'getParent' of null
and points to this line in the ckeditor.js file
setStartBefore: function(a) {
this.setStart(a.getParent(), a.getIndex())
},
From what i can see both versions of ckedtor.js are the same and no other files have changed.
There is nothing on Google to suggest a reason, so hoping someone here might have an idea.
Cheers
modified above to this and now it works perfectly
editor.focus();
var storeCursorLocation = function( editor ) {
bookmark = editor.getSelection().createBookmarks( true );
};
var restoreCursorLocation = function( editor ) {
editor.getSelection().selectBookmarks( bookmark );
};
storeCursorLocation( editor );
var data = editor.document.getBody().getHtml();
var replaced_text = data.replace(typedchars, Suggestions + ' ');
editor.document.getBody().setHtml( replaced_text );
typedchars = '';
restoreCursorLocation( editor );