source: [view]
var template = this._template,
isEnter = args ? args.enter : false,
ed = this.editor,
selection = ed.window.getSelection();
if(selection){
var node = isEnter ? this._findLastEditingNode(selection.anchorNode) :
(this._saved || selection.anchorNode),
bm = this._saved = selection.anchorNode,
bmOff = selection.anchorOffset;
if(node.nodeType == 3 && !this._inLink(node)){
var linked = false, result = this._findUrls(node, bm, bmOff),
range = ed.document.createRange(),
item, cost = 0, isSameNode = (bm == node);
item = result.shift();
while(item){
// Covert a URL to a link.
range.setStart(node, item.start);
range.setEnd(node, item.end);
selection.removeAllRanges();
selection.addRange(range);
ed.execCommand("insertHTML", dojo.string.substitute(template, {url: range.toString()}));
cost += item.end;
item = result.shift();
linked = true;
}
// If bm and node are the some dom node, caculate the actual bookmark offset
// If the position of the cursor is modified (turned into a link, etc.), no
// need to recover the cursor position
if(isSameNode && (bmOff = bmOff - cost) <= 0){ return; }
// We didn't update anything, so don't collapse selections.
if(!linked) { return ; }
try{
// Try to recover the cursor position
range.setStart(bm, 0);
range.setEnd(bm, bmOff);
selection.removeAllRanges();
selection.addRange(range);
dojo.withGlobal(ed.window, "collapse", dijit._editor.selection, []);
}catch(e){}
}
}