From: Bartosz DziewoƄski Date: Mon, 5 Feb 2018 22:44:05 +0000 (+0100) Subject: jquery.textSelection: Implement 'encapsulateSelection' in terms of the other commands X-Git-Tag: 1.31.0-rc.0~658^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=ce6be3292e7d44d06f0aba8b98fbbbab80c5630c jquery.textSelection: Implement 'encapsulateSelection' in terms of the other commands Any users of jquery.textSelection that add an alternative API using 'register' no longer have to override 'encapsulateSelection' (which is a big beast), as long as the other commands are implemented. Bug: T185917 Change-Id: I39bbbce20336a1286a07bf8032acdfb8a7e0cdb6 --- diff --git a/resources/src/jquery/jquery.textSelection.js b/resources/src/jquery/jquery.textSelection.js index 27d5fc790c..2bb37d76fb 100644 --- a/resources/src/jquery/jquery.textSelection.js +++ b/resources/src/jquery/jquery.textSelection.js @@ -113,7 +113,7 @@ */ encapsulateSelection: function ( options ) { return this.each( function () { - var selText, scrollTop, insertText, + var selText, allText, currSelection, scrollTop, insertText, isSample, startPos, endPos, pre = options.pre, post = options.post; @@ -173,8 +173,10 @@ } selText = $( this ).textSelection( 'getSelection' ); - startPos = this.selectionStart; - endPos = this.selectionEnd; + allText = $( this ).textSelection( 'getContents' ); + currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } ); + startPos = currSelection[ 0 ]; + endPos = currSelection[ 1 ]; scrollTop = this.scrollTop; checkSelectedText(); if ( @@ -182,7 +184,7 @@ endPos - startPos !== options.selectionEnd - options.selectionStart ) { // This means there is a difference in the selection range returned by browser and what we passed. - // This happens for Chrome in the case of composite characters. Ref bug #30130 + // This happens for Chrome in the case of composite characters. Ref T32130 // Set the startPos to the correct position. startPos = options.selectionStart; } @@ -192,25 +194,28 @@ insertText = doSplitLines( selText, pre, post ); } if ( options.ownline ) { - if ( startPos !== 0 && this.value.charAt( startPos - 1 ) !== '\n' && this.value.charAt( startPos - 1 ) !== '\r' ) { + if ( startPos !== 0 && allText.charAt( startPos - 1 ) !== '\n' && allText.charAt( startPos - 1 ) !== '\r' ) { insertText = '\n' + insertText; pre += '\n'; } - if ( this.value.charAt( endPos ) !== '\n' && this.value.charAt( endPos ) !== '\r' ) { + if ( allText.charAt( endPos ) !== '\n' && allText.charAt( endPos ) !== '\r' ) { insertText += '\n'; post += '\n'; } } - this.value = this.value.slice( 0, startPos ) + insertText + - this.value.slice( endPos ); - // Setting this.value scrolls the textarea to the top, restore the scroll position + $( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + insertText + + allText.slice( endPos ) ); + // Setting this.value (via setContents) may scroll the textarea, restore the scroll position this.scrollTop = scrollTop; if ( isSample && options.selectPeri && ( !options.splitlines || ( options.splitlines && selText.indexOf( '\n' ) === -1 ) ) ) { - this.selectionStart = startPos + pre.length; - this.selectionEnd = startPos + pre.length + selText.length; + $( this ).textSelection( 'setSelection', { + start: startPos + pre.length, + end: startPos + pre.length + selText.length + } ); } else { - this.selectionStart = startPos + insertText.length; - this.selectionEnd = this.selectionStart; + $( this ).textSelection( 'setSelection', { + start: startPos + insertText.length + } ); } $( this ).trigger( 'encapsulateSelection', [ options.pre, options.peri, options.post, options.ownline, options.replace, options.splitlines ] );