From 403ce9d3059c078aedfb84c93d544a22e3a552f9 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Fri, 23 Jan 2009 14:25:24 -0800 Subject: [PATCH] Fixed cursor positioning at the end of an iframe's text to work in more edge cases. --- static/js/Editor.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/static/js/Editor.js b/static/js/Editor.js index 552792a..a8bac5d 100644 --- a/static/js/Editor.js +++ b/static/js/Editor.js @@ -388,12 +388,17 @@ Editor.prototype.position_cursor = function ( click_position, div_range ) { var selection = this.iframe.contentWindow.getSelection(); var last_node = this.document.body.lastChild; - /// FIXME: sometimes positioning the cursor at the end puts it in a place where you can't type any characters - if ( ( last_node.nodeValue == "\n" || last_node.tagName == "BR" ) && last_node.previousSibling ) + if ( selection.rangeCount > 0 ) + var range = selection.getRangeAt( 0 ); + else + var range = this.document.createRange(); + + while ( ( last_node.nodeValue == "\n" || last_node.tagName == "BR" ) && last_node.previousSibling ) last_node = last_node.previousSibling; - selection.selectAllChildren( last_node ); - selection.collapseToEnd(); + range.selectNodeContents( last_node ); + range.collapse( false ); + selection.addRange( range ); } else if ( this.document.selection ) { // browsers such as IE var range = this.document.selection.createRange(); range.move( "textedit" );