Problem with getting selected text when using sprinter button and selection.createRange () in Internet Explorer

I am working on embedding sprint buttons in the Stackoverflow editor with my favorite WMD, and I came across an odd error. In all versions of IE, the selected text is lost when the button is pressed, so, for example, highlighting a block of text and pressing a code button acts as if you placed the cursor at the end of the selection and pressed the button.

eg. emphasizing this:

This
Is
Code

and by clicking the code button, you:

This
Is
Code`enter code here`

What is really strange is that I left the original line without scrolling, and that works fine. In fact, the ALL buttons and the shortcut code use the same function doClick(button)!

  • Old non-sprint buttons: OK
  • Hotkeys: OK
  • , IE: OK
  • IE: WTF

selection.createRange(), "". () doClick(), . , , , . - , - IE?

onclick :

button.onmouseout = function(){
  this.style.backgroundPosition = this.XShift + " " + normalYShift;
};

button.onclick = function() {
  if (this.onmouseout) {
    this.onmouseout();
  }
  doClick(this);
}

onmouseout doClick , , .

EDIT:

, , , . <li> . , ?

/EDIT

wmd git button-cleanup.

0d6d1b32bb42a6bd1d4ac4e409a19fdfe8f1ffcc, . - . . setInputAreaSelectionStartEnd() TextareaState.

, , , Javascript, , jQuery, .

!

+3
2

, .

HTML CSS, . CSS, (, ). CSS.

IE : IE "button". , ( document.selection.createRange()) .

- . IE mousedown. - , document.selection.createRange().

:

wmd.ieCachedRange = null;
wmd.ieRetardedClick = false;

if(global.isIE) {
  button.onmousedown =  function() { 
    wmd.ieRetardedClick = true;
    wmd.ieCachedRange = document.selection.createRange(); 
  };
}


var range;
if(wmd.ieRetardedClick && wmd.ieCachedRange) {
  range = wmd.ieCachedRange;
  wmd.ieRetardedClick = false;
}
else {
  range = doc.selection.createRange();
}

DOM .

, . , .

+4

blur() , IE - .

( ), ?

0

Source: https://habr.com/ru/post/1702250/


All Articles