Index: trunk/extensions/Narayam/js/ext.narayam.core.js |
— | — | @@ -150,6 +150,27 @@ |
151 | 151 | } |
152 | 152 | |
153 | 153 | /** |
| 154 | + * Replace text part from startPos to endPos with peri |
| 155 | + * It function is specifically for webkit browsers, |
| 156 | + * because of bug: https://bugs.webkit.org/show_bug.cgi?id=66630 |
| 157 | + * TODO: remove when webkit bug is handled in jQuery.textSelection.js |
| 158 | + * |
| 159 | + * @param $element jQuery object to wich replacement to be taked place |
| 160 | + * @param startPos Starting position of text range to be replaced |
| 161 | + * @param endPos Ending position of text range to be replaced |
| 162 | + * @param peri String to be substituted |
| 163 | + */ |
| 164 | + function replaceString( $element, startPos, endPos, peri ) { |
| 165 | + // Take entire text of the element |
| 166 | + var text = $element.val(); |
| 167 | + var pre = text.substring( 0, startPos ); |
| 168 | + var post = text.substring( endPos, text.length ); |
| 169 | + |
| 170 | + // Then replace |
| 171 | + $element.val( pre + peri + post ); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
154 | 175 | * Keydown event handler. Handles shortcut key presses |
155 | 176 | * @param e Event object |
156 | 177 | */ |
— | — | @@ -221,16 +242,35 @@ |
222 | 243 | input = input.substring( divergingPos ); |
223 | 244 | replacement = replacement.substring( divergingPos ); |
224 | 245 | |
225 | | - // Select and replace the text |
226 | | - $this.textSelection( 'setSelection', { |
227 | | - 'start': startPos - input.length + 1, |
228 | | - 'end': endPos |
229 | | - } ); |
230 | | - $this.textSelection( 'encapsulateSelection', { |
231 | | - 'peri': replacement, |
232 | | - 'replace': true, |
233 | | - 'selectPeri': false |
234 | | - } ); |
| 246 | + // TODO: use better browser detection as $.browser may be moved out |
| 247 | + // from jQuery core |
| 248 | + if ( $.browser.webkit ) { |
| 249 | + // Webkit browser have a bug: |
| 250 | + // https://bugs.webkit.org/show_bug.cgi?id=66630 |
| 251 | + // TODO: remove when webkit bug is handled |
| 252 | + // in jQuery.textSelection.js |
| 253 | + |
| 254 | + replaceString($this, startPos - input.length + 1, endPos, replacement); |
| 255 | + // Calculate new position for caret to be set |
| 256 | + var newCaretPosition = startPos - input.length + 1 + replacement.length |
| 257 | + // Update caret postion |
| 258 | + $this.textSelection( 'setSelection', { |
| 259 | + 'start': newCaretPosition, |
| 260 | + 'end': newCaretPosition |
| 261 | + } ); |
| 262 | + } |
| 263 | + else { |
| 264 | + // Select and replace the text |
| 265 | + $this.textSelection( 'setSelection', { |
| 266 | + 'start': startPos - input.length + 1, |
| 267 | + 'end': endPos |
| 268 | + } ); |
| 269 | + $this.textSelection( 'encapsulateSelection', { |
| 270 | + 'peri': replacement, |
| 271 | + 'replace': true, |
| 272 | + 'selectPeri': false |
| 273 | + } ); |
| 274 | + } |
235 | 275 | |
236 | 276 | e.stopPropagation(); |
237 | 277 | return false; |