From dd02d077b6b0efddefc594005d0fdb6f7731b9a6 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Sun, 20 Mar 2011 14:48:20 +0000 Subject: [PATCH] In $.textSelection, treat elements differently from other elements (such as textareas and iframes) in IE, seems to be needed for some reason (oh IE, how I hate you). This fixes a bug causing Narayam's keyboard mappings to not work in elements in IE. --- resources/jquery/jquery.textSelection.js | 25 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/resources/jquery/jquery.textSelection.js b/resources/jquery/jquery.textSelection.js index db8c0953e3..22ee09298c 100644 --- a/resources/jquery/jquery.textSelection.js +++ b/resources/jquery/jquery.textSelection.js @@ -3,6 +3,20 @@ */ ( function( $ ) { $.fn.textSelection = function( command, options ) { + +/** + * Helper function to get an IE TextRange object for an element + */ +function rangeForElementIE( e ) { + if ( e.nodeName.toLowerCase() == 'input' ) { + return e.createTextRange(); + } else { + var sel = document.body.createTextRange(); + sel.moveToElementText( e ); + return sel; + } +} + var fn = { /** * Get the contents of the textarea @@ -152,15 +166,11 @@ encapsulateSelection: function( options ) { // Create range containing text in the selection var periRange = document.selection.createRange().duplicate(); // Create range containing text before the selection - var preRange = document.body.createTextRange(); - // Select all the text - preRange.moveToElementText(e); + var preRange = rangeForElementIE( e ); // Move the end where we need it preRange.setEndPoint("EndToStart", periRange); // Create range containing text after the selection - var postRange = document.body.createTextRange(); - // Select all the text - postRange.moveToElementText(e); + var postRange = rangeForElementIE( e ); // Move the start where we need it postRange.setEndPoint("StartToEnd", periRange); // Load the text values we need to compare @@ -240,8 +250,7 @@ setSelection: function( options ) { this.selectionEnd = options.end; } } else if ( document.body.createTextRange ) { - var selection = document.body.createTextRange(); - selection.moveToElementText( this ); + var selection = rangeForElementIE( this ); var length = this.value.length; // IE doesn't count \n when computing the offset, so we won't either var newLines = this.value.match( /\n/g ); -- 2.20.1