I wany to select several different words and apply single style to them. I am using code found at stackoverflow:
// Note: your instance name may differ.
var editor = CKEDITOR.instances.editor1,
selection = editor.getSelection(),
root = selection.root,
textNodes = [],
ranges = [],
range, text, index;
function getTextNodes( element ) {
var children = element.getChildren(),
child;
for ( var i = children.count(); i--; ) {
child = children.getItem( i );
if ( child.type == CKEDITOR.NODE_ELEMENT )
getTextNodes( child );
else if ( child.type == CKEDITOR.NODE_TEXT )
textNodes.push( child );
}
}
// Recursively search for text nodes starting from root.
// You may want to search a specific branch starting from other element.
getTextNodes( root );
// Iterate over found text nodes. If some contains
// phrase "the", create a range that selects this word.
for ( i = textNodes.length; i--; ) {
text = textNodes[ i ];
index = text.getText().indexOf( 'the' );
if ( index > -1 ) {
range = editor.createRange();
range.setStart( text, index );
// Note: 3 is fixed length of "the". You may want to change it.
range.setEnd( text, index + 3 );
ranges.push( range );
}
}
// Select all ranges "containing" phrase "the".
selection.selectRanges( ranges );
editor.execCommand('bold');
I want to have all the selected words in bold and I execute editor.execCommand('bold');, but I get an exception: Uncaught IndexSizeError: Index or size was negative, or greater than the allowed value.
How can I apply styles to selected items?
