From bcd1dcbcd9dd918153d2da873be1f716729a0502 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 23 Jul 2007 22:57:48 +0000 Subject: [PATCH] Increasingly brittle, but this fixes the following bug: * hit bold, type some text, hit space, start a link. this will not actually start a link and will just insert a space --- static/js/Editor.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/static/js/Editor.js b/static/js/Editor.js index f81a26b..dec9e72 100644 --- a/static/js/Editor.js +++ b/static/js/Editor.js @@ -323,7 +323,29 @@ Editor.prototype.start_link = function () { this.insert_html( " " ); var range = selection.getRangeAt( 0 ); var container = range.startContainer; - range.setStart( container, ( range.startOffset ? range.startOffset - 1 : 0 ) ); + + // if for some reason the inserted   isn't in the container we expect it to be in, use + // the previous sibling container instead + if ( range.startOffset == 0 ) { + var previous = null; + for ( i in container.parentNode.childNodes ) { + var child = container.parentNode.childNodes[ i ]; + + if ( child == container ) { + container = previous; + // descend into the sibling container until a text node is found + while ( container.firstChild && container.firstChild.noteType != 3 ) + container = container.firstChild; + break; + } + + previous = child; + } + // assume the   got subsumed into the end of the sibling container + range.setStart( container, container.nodeValue.length - 1 ); + } else { + range.setStart( container, range.startOffset - 1 ); + } this.exec_command( "createLink", "/notes/new" );