* (bug 10526) Fix toolbar/insertTags behavior for IE 6/7 and Opera (8+)
authorBrion Vibber <brion@users.mediawiki.org>
Thu, 12 Jul 2007 17:39:15 +0000 (17:39 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Thu, 12 Jul 2007 17:39:15 +0000 (17:39 +0000)
  Now matches the selection behavior on Mozilla / Safari.
  Patch by Alex Smotrov.
* Don't show non-functional toolbar buttons on Opera 7 anymore

Tested and confirmed expected behavior:
* Firefox 2.0 / Mac
* Opera 9.20 / Mac
* Opera 8.0 / Mac
* Opera 7.5 / Mac (no toolbar)
* Opera 6 / Mac (no toolbar)
* Safari 2.0 / Mac
* Safari 3.0.2 / Win XP
* IE 7.0 / Win XP
* IE 6.0 / Win XP

Tested with oddities:
* iCab 3 / Mac (toolbar displays but nonfunctional; seems to have HTML parsing errors unrelated to the JS work)

RELEASE-NOTES
includes/DefaultSettings.php
skins/common/wikibits.js

index df48151..e8da053 100644 (file)
@@ -281,7 +281,10 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 10552) Suppress rollback link in history for single-revision pages
 * (bug 10538) Gracefully handle invalid input on move success page
 * Fix for Esperanto double-x-encoding in move success page
-
+* (bug 10526) Fix toolbar/insertTags behavior for IE 6/7 and Opera (8+)
+  Now matches the selection behavior on Mozilla / Safari.
+  Patch by Alex Smotrov.
+* Don't show non-functional toolbar buttons on Opera 7 anymore
 
 == API changes since 1.10 ==
 
index 542a726..7a9c4f9 100644 (file)
@@ -1201,7 +1201,7 @@ $wgCacheEpoch = '20030516000000';
  * to ensure that client-side caches don't keep obsolete copies of global
  * styles.
  */
-$wgStyleVersion = '81';
+$wgStyleVersion = '82';
 
 
 # Server-side caching:
index d7b19c4..58d2fe6 100644 (file)
@@ -373,7 +373,8 @@ function mwSetupToolbar() {
 
        // Don't generate buttons for browsers which don't fully
        // support it.
-       if (!document.selection && textbox.selectionStart === null) {
+       if (!(document.selection && document.selection.createRange)
+               && textbox.selectionStart === null) {
                return false;
        }
 
@@ -408,7 +409,6 @@ function escapeQuotesHTML(text) {
 
 // apply tagOpen/tagClose to selection in textarea,
 // use sampleText instead of selection if there is none
-// copied and adapted from phpBB
 function insertTags(tagOpen, tagClose, sampleText) {
        var txtarea;
        if (document.editform) {
@@ -418,62 +418,72 @@ function insertTags(tagOpen, tagClose, sampleText) {
                var areas = document.getElementsByTagName('textarea');
                txtarea = areas[0];
        }
+       var selText, isSample = false;
 
-       // IE
-       if (document.selection  && !is_gecko) {
-               var theSelection = document.selection.createRange().text;
-               if (!theSelection) {
-                       theSelection=sampleText;
-               }
+       if (document.selection  && document.selection.createRange) { // IE/Opera
+
+               //save window scroll position
+               if (document.documentElement && document.documentElement.scrollTop)
+                       var winScroll = document.documentElement.scrollTop
+               else if (document.body)
+                       var winScroll = document.body.scrollTop;
+               //get current selection  
                txtarea.focus();
-               if (theSelection.charAt(theSelection.length - 1) == " ") { // exclude ending space char, if any
-                       theSelection = theSelection.substring(0, theSelection.length - 1);
-                       document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
-               } else {
-                       document.selection.createRange().text = tagOpen + theSelection + tagClose;
+               var range = document.selection.createRange();
+               selText = range.text;
+               //insert tags
+               checkSelectedText();
+               range.text = tagOpen + selText + tagClose;
+               //mark sample text as selected
+               if (isSample && range.moveStart) {
+                       if (window.opera)
+                               tagClose = tagClose.replace(/\n/g,'');
+                       range.moveStart('character', - tagClose.length - selText.length); 
+                       range.moveEnd('character', - tagClose.length); 
                }
-
-       // Mozilla
-       } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
-               var replaced = false;
+               range.select();   
+               //restore window scroll position
+               if (document.documentElement && document.documentElement.scrollTop)
+                       document.documentElement.scrollTop = winScroll
+               else if (document.body)
+                       document.body.scrollTop = winScroll;
+
+       } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
+
+               //save textarea scroll position
+               var textScroll = txtarea.scrollTop;
+               //get current selection
+               txtarea.focus();
                var startPos = txtarea.selectionStart;
                var endPos = txtarea.selectionEnd;
-               if (endPos-startPos) {
-                       replaced = true;
-               }
-               var scrollTop = txtarea.scrollTop;
-               var myText = (txtarea.value).substring(startPos, endPos);
-               if (!myText) {
-                       myText=sampleText;
-               }
-               var subst;
-               if (myText.charAt(myText.length - 1) == " ") { // exclude ending space char, if any
-                       subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
-               } else {
-                       subst = tagOpen + myText + tagClose;
-               }
-               txtarea.value = txtarea.value.substring(0, startPos) + subst +
-                       txtarea.value.substring(endPos, txtarea.value.length);
-               txtarea.focus();
+               selText = txtarea.value.substring(startPos, endPos);
+               //insert tags
+               checkSelectedText();
+               txtarea.value = txtarea.value.substring(0, startPos)
+                       + tagOpen + selText + tagClose
+                       + txtarea.value.substring(endPos, txtarea.value.length);
                //set new selection
-               if (replaced) {
-                       var cPos = startPos+(tagOpen.length+myText.length+tagClose.length);
-                       txtarea.selectionStart = cPos;
-                       txtarea.selectionEnd = cPos;
+               if (isSample) {
+                       txtarea.selectionStart = startPos + tagOpen.length;
+                       txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
                } else {
-                       txtarea.selectionStart = startPos+tagOpen.length;
-                       txtarea.selectionEnd = startPos+tagOpen.length+myText.length;
+                       txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
+                       txtarea.selectionEnd = txtarea.selectionStart;
                }
-               txtarea.scrollTop = scrollTop;
+               //restore textarea scroll position
+               txtarea.scrollTop = textScroll;
+       } 
 
-       // All other browsers get no toolbar.
-       // There was previously support for a crippled "help"
-       // bar, but that caused more problems than it solved.
-       }
-       // reposition cursor if possible
-       if (txtarea.createTextRange) {
-               txtarea.caretPos = document.selection.createRange().duplicate();
+       function checkSelectedText(){
+               if (!selText) {
+                       selText = sampleText;
+                       isSample = true;
+               } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
+                       selText = selText.substring(0, selText.length - 1);
+                       tagClose += ' '
+               } 
        }
+
 }