View Full Version : How to move boundaries of a selection?

12-27-2010, 04:05 PM
Hi all,

I'm trying to do something with a WYSIWYG editor. Below is a piece of code that takes selected text and wraps various BBCodes around it. The code almost works... but I want a few changes and I hope someone has an idea for me.

Note: the var range = ed.selection.getRng(true); line gets a W3C compatible range containing the current selection - even under MSIE. The editor takes care of this.
Note: The var elem = ed.dom.create('span', false, ' '); line creates a SPAN element with the initial content " " and returns it's element in "elem".

var ed = (tinymce.activeEditor || opener.tinymce.activeEditor);
var sel = ed.selection.getContent();
var range = ed.selection.getRng(true);
var elem = ed.dom.create('span', false, ' ');
elem.innerHTML = bbopen + sel + bbclose;
This code works, but not exactly as I want. For example, if I type "This is a test" and wrap code tags around it, I get this:


But what I WANT is this:


Also, if I select no text and hit the BBCode button, I get the open tag and the close tag, all highlighted.

What I want in this case is the open tag, the close tag and the cursor IN BETWEEN them ready for text insertion.

I'm thinking that I can take the range start and end offsets and move them to get what I want, but so far I've failed.

One thing I DON'T want to have to do is create THREE spans... one for the open, one for the selection and one for the close. Trying to avoid that if possible.

Any help will be greatly appreciated!


-- Roger