From 9f8ed93c585ef44233ad42f4e5403a31d4d88a5e Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 12 Jul 2007 17:39:15 +0000 Subject: [PATCH] * (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 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 | 5 +- includes/DefaultSettings.php | 2 +- skins/common/wikibits.js | 106 +++++++++++++++++++---------------- 3 files changed, 63 insertions(+), 50 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index df48151c9a..e8da0534db 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 == diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 542a726a5a..7a9c4f9727 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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: diff --git a/skins/common/wikibits.js b/skins/common/wikibits.js index d7b19c4cf3..58d2fe6148 100644 --- a/skins/common/wikibits.js +++ b/skins/common/wikibits.js @@ -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 += ' ' + } } + } -- 2.20.1