r56723 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56722‎ | r56723 | r56724 >
Date:18:05, 21 September 2009
Author:catrope
Status:ok (Comments)
Tags:
Comment:
EditToolbar: (bug 20715) Search&Replace messes up in Opera

This was caused by Opera treating newlines as one character in $textarea.val().indexOf() , but as two characters in $textarea.setSelection(). Worked around by an ugly temporary hack that detects whether we're on a broken browser and replacing \n with \r\n in $textarea.val() . Will redo properly after discussing with Trevor.
Modified paths:
  • /trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.php (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.js
@@ -1179,6 +1179,40 @@
11801180 dialog: {
11811181 buttons: {
11821182 'edittoolbar-tool-replace-button': function() {
 1183+ function fixOperaBrokenness( s ) {
 1184+ // This function works around Opera's
 1185+ // broken newline handling in textareas.
 1186+ // .val() has \n while selection functions
 1187+ // treat newlines as \r\n
 1188+
 1189+ if ( typeof $j.isOperaBroken == 'undefined' ) {
 1190+ // Create a textarea inside a div
 1191+ // with zero area, to hide it properly
 1192+ var div = $j( '<div />' )
 1193+ .height( 0 )
 1194+ .width( 0 )
 1195+ .insertBefore( $textarea );
 1196+ var textarea = $j( '<textarea></textarea' )
 1197+ .height( 0 )
 1198+ .appendTo( div )
 1199+ .val( "foo\r\nbar" );
 1200+
 1201+ // Try to search&replace bar --> BAR
 1202+ var index = textarea.val().indexOf( 'bar' );
 1203+ textarea.select();
 1204+ textarea.setSelection( index, index + 3 );
 1205+ textarea.encapsulateSelection( '', 'BAR', '', false, true );
 1206+ if ( textarea.val().substr( -1 ) == 'R' )
 1207+ $j.isOperaBroken = false;
 1208+ else
 1209+ $j.isOperaBroken = true;
 1210+ div.remove();
 1211+ }
 1212+ if ( $j.isOperaBroken )
 1213+ s = s.replace( /\n/g, "\r\n" );
 1214+ return s;
 1215+ }
 1216+
11831217 var searchStr = $j( '#edittoolbar-replace-search' ).val();
11841218 var replaceStr = $j( '#edittoolbar-replace-replace' ).val();
11851219 var flags = '';
@@ -1193,13 +1227,14 @@
11941228 }
11951229 var regex = new RegExp( searchStr, flags );
11961230 var $textarea = $j(this).data( 'context' ).$textarea;
1197 - var matches = $textarea.val().match( regex );
 1231+ var text = fixOperaBrokenness( $textarea.val() );
 1232+ var matches = text.match( regex );
11981233 if ( !matches ) {
11991234 alert( gM( 'edittoolbar-tool-replace-nomatch' ) );
12001235 } else {
12011236 var start, end;
12021237 for ( var i = 0; i < matches.length; i++ ) {
1203 - start = $textarea.val().indexOf( matches[i] );
 1238+ start = text.indexOf( matches[i] );
12041239 end = start + matches[i].length;
12051240 $textarea.setSelection( start, end );
12061241 $textarea.encapsulateSelection( '', replaceStr, '', false, true );
Index: trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.php
@@ -19,7 +19,7 @@
2020 /* Configuration */
2121
2222 // Bump the version number every time you change any of the .css/.js files
23 -$wgEditToolbarStyleVersion = 35;
 23+$wgEditToolbarStyleVersion = 36;
2424
2525 // Set this to true to simply override the stock toolbar for everyone
2626 $wgEditToolbarGlobalEnable = false;

Follow-up revisions

RevisionCommit summaryAuthorDate
r56775Fix up r56723: unbreak "replace all" modecatrope17:23, 22 September 2009

Comments

#Comment by Tim Starling (talk | contribs)   02:24, 2 December 2009

Please report this to the Opera bug tracker if you haven't done so already. http://www.opera.com/support/bugs/

#Comment by Catrope (talk | contribs)   10:13, 2 December 2009

Looks like this bug was fixed at some point; doesn't appear in Opera 10.10

Status & tagging log