Interface for the classic edit toolbar (mw.toolbar) does not correctly identify the focused element when CodeMirror is enabled in Chrome and IE (in FF it works correctly).
As a result the function mw.toolbar insertTags does not work correctly.
The function can insert tags, but cannot encapsulate selection.
Problem description:
[[ https://phabricator.wikimedia.org/source/mediawiki/browse/master/resources/src/mediawiki.toolbar/toolbar.js;005802b82b106605a928394c859248c2f47f93bb$151 | $currentFocused ]] is wrong in Chrome and IE when CodeMirror enabled
```lang=javascript
insertTags: function ( tagOpen, tagClose, sampleText ) {
if ( $currentFocused && $currentFocused.length ) {
$currentFocused.textSelection(
'encapsulateSelection', {
pre: tagOpen,
peri: sampleText,
post: tagClose
}
);
}
```
Code that set the [[ https://phabricator.wikimedia.org/source/mediawiki/browse/master/resources/src/mediawiki.toolbar/toolbar.js;005802b82b106605a928394c859248c2f47f93bb$203 | $currentFocused ]] variable:
```lang=javascript
$( document ).on( 'focus', 'textarea, input:text', function () {
$currentFocused = $( this );
} );
```
This works different in IE, Chrome and FF when CodeMirror is enabled.
And corresponded code in [[ https://phabricator.wikimedia.org/diffusion/ECMI/browse/master/resources/ext.CodeMirror.js;975f15358bcfbdb72613c77858c1f05cebd7c17b$20 | CodeMirror ]]:
```lang=javascript
function cmTextSelection( command, options ) {
if ( !codeMirror || codeMirror.getTextArea() !== this[ 0 ] ) {
return origTextSelection.call( this, command, options );
}
...
```
Only in FF when CodeMirror enabled and function mw.toolbar.insertTags called `this[ 0 ] === codeMirror.getTextArea()`
This bug was found when the CodeMirror and Charinsert extensions was used together.
Seems, it can be fixed in CodeMirror by:
```lang=javascript
function cmTextSelection( command, options ) {
if ( !codeMirror /*|| codeMirror.getTextArea() !== this[ 0 ]*/ ) {
return origTextSelection.call( this, command, options );
}
...
```
but I sure it should be fixed in MW core.
----
From the merged-in task:
Different results in Firefox 55 and Chromium 60 (linux)
To reproduce:
* Edit https://www.mediawiki.org/w/index.php?title=User:Quiddity_(WMF)/sandbox2&oldid=2544402 in classic wikitext mode (or [[https://meta.wikimedia.org/w/index.php?title=User:Quiddity_(WMF)/sandbox&oldid=17161790|same]] at metawiki)
* Select all (either manually or ctrl-A)
* Click one of the `<opening></closing>` links/buttons in MediaWiki:Edittools under the save button, e.g. `<translate></translate>` or `<code></code>`
* Result:
** Firefox: the entire block is replaced by, e.g. `<translate>-</translate>` (dash in middle)
** Chromium: nothing changes
Note: Removing //any// text from that example will result in it starting to work properly. **It is currently 1001 bytes** (possibly relevant?).