how to select a text range in CKEDITOR programatically? how to select a text range in CKEDITOR programatically? selenium selenium

how to select a text range in CKEDITOR programatically?


Get current selection

var editor = CKEDITOR.instances["id_corpo"];var sel = editor.getSelection();

Change the selection to the current element

var element = sel.getStartElement();sel.selectElement(element);

Move the range to the text you would like to select

var findString = 'foobar';var ranges = editor.getSelection().getRanges();var startIndex = element.getHtml().indexOf(findString);if (startIndex != -1) {    ranges[0].setStart(element.getFirst(), startIndex);    ranges[0].setEnd(element.getFirst(), startIndex + findString.length);    sel.selectRanges([ranges[0]]);}


You can also do the following:

get the current selection

var selection = editor.getSelection();var selectedElement = selection.getSelectedElement();

if nothing is selected then create a new paragraph element

if (!selectedElement)    selectedElement = new CKEDITOR.dom.element('p');

Insert your content into the element

selectedElement.setHtml(someHtml);

If needed, insert your element into the DOM (it will be inserted into the current position)

editor.insertElement(selectedElement);

and then just select it

selection.selectElement(selectedElement);