remove special IE support from textselection scripts #1535

The opera work around is still needed though! O_o
This commit is contained in:
Andreas Gohr 2016-04-19 19:32:12 +02:00
parent 28b1b49a14
commit 8a0c870e6b
1 changed files with 7 additions and 86 deletions

View File

@ -13,7 +13,6 @@ function selection_class(){
this.start = 0;
this.end = 0;
this.obj = null;
this.rangeCopy = null;
this.scroll = 0;
this.fix = 0;
@ -37,82 +36,11 @@ function selection_class(){
function DWgetSelection(textArea) {
var sel = new selection_class();
sel.obj = textArea;
sel.start = textArea.value.length;
sel.end = textArea.value.length;
textArea.focus();
if(document.getSelection) { // Mozilla et al.
sel.start = textArea.selectionStart;
sel.end = textArea.selectionEnd;
sel.scroll = textArea.scrollTop;
} else if(document.selection) { // MSIE
/*
* This huge lump of code is neccessary to work around two MSIE bugs:
*
* 1. Selections trim newlines at the end of the code
* 2. Selections count newlines as two characters
*/
// The current selection
sel.rangeCopy = document.selection.createRange().duplicate();
if (textArea.tagName === 'INPUT') {
var before_range = textArea.createTextRange();
before_range.expand('textedit'); // Selects all the text
} else {
var before_range = document.body.createTextRange();
before_range.moveToElementText(textArea); // Selects all the text
}
before_range.setEndPoint("EndToStart", sel.rangeCopy); // Moves the end where we need it
var before_finished = false, selection_finished = false;
var before_text, selection_text;
// Load the text values we need to compare
before_text = before_range.text;
selection_text = sel.rangeCopy.text;
sel.start = before_text.length;
sel.end = sel.start + selection_text.length;
// Check each range for trimmed newlines by shrinking the range by 1 character and seeing
// if the text property has changed. If it has not changed then we know that IE has trimmed
// a \r\n from the end.
do {
if (!before_finished) {
if (before_range.compareEndPoints("StartToEnd", before_range) == 0) {
before_finished = true;
} else {
before_range.moveEnd("character", -1);
if (before_range.text == before_text) {
sel.start += 2;
sel.end += 2;
} else {
before_finished = true;
}
}
}
if (!selection_finished) {
if (sel.rangeCopy.compareEndPoints("StartToEnd", sel.rangeCopy) == 0) {
selection_finished = true;
} else {
sel.rangeCopy.moveEnd("character", -1);
if (sel.rangeCopy.text == selection_text) {
sel.end += 2;
} else {
selection_finished = true;
}
}
}
} while ((!before_finished || !selection_finished));
// count number of newlines in str to work around stupid IE selection bug
var countNL = function(str) {
var m = str.split("\r\n");
if (!m || !m.length) return 0;
return m.length-1;
};
sel.fix = countNL(sel.obj.value.substring(0,sel.start));
}
sel.obj = textArea;
sel.start = textArea.selectionStart;
sel.end = textArea.selectionEnd;
sel.scroll = textArea.scrollTop;
return sel;
}
@ -127,16 +55,8 @@ function DWgetSelection(textArea) {
* @param {selection_class} selection a selection object as returned by DWgetSelection()
*/
function DWsetSelection(selection){
if(document.getSelection){ // FF
// what a pleasure in FF ;)
selection.obj.setSelectionRange(selection.start,selection.end);
if(selection.scroll) selection.obj.scrollTop = selection.scroll;
} else if(document.selection) { // IE
selection.rangeCopy.collapse(true);
selection.rangeCopy.moveStart('character',selection.start - selection.fix);
selection.rangeCopy.moveEnd('character',selection.end - selection.start);
selection.rangeCopy.select();
}
selection.obj.setSelectionRange(selection.start, selection.end);
if(selection.scroll) selection.obj.scrollTop = selection.scroll;
}
/**
@ -166,6 +86,7 @@ function pasteText(selection,text,opts){
selection.end = selection.start + text.length;
}
// modify the new selection if wanted
if(opts.startofs) selection.start += opts.startofs;
if(opts.endofs) selection.end -= opts.endofs;