From 4d6ec1909af30744d8558d42411e0b8dd5fcd800 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 21 Aug 2021 00:52:12 -0400 Subject: [PATCH 01/11] tests: Fix `helper.linesDiv()` --- src/tests/frontend/helper/methods.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tests/frontend/helper/methods.js b/src/tests/frontend/helper/methods.js index 97ea6a643b4..57784daef93 100644 --- a/src/tests/frontend/helper/methods.js +++ b/src/tests/frontend/helper/methods.js @@ -49,7 +49,8 @@ helper.edit = async (message, line) => { * * @returns {Array.} array of divs */ -helper.linesDiv = () => helper.padInner$('.ace-line').map(function () { return $(this); }).get(); +helper.linesDiv = + () => helper.padInner$('.ace-line').map(function () { return helper.padInner$(this); }).get(); /** * The pad text as an array of lines From 75591c99460a7eaabe6142fed765ca6abb6d2f75 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 21 Aug 2021 00:53:14 -0400 Subject: [PATCH 02/11] tests: Fix off-by-one bug in `timeslider_follow.js` --- src/tests/frontend/specs/timeslider_follow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/frontend/specs/timeslider_follow.js b/src/tests/frontend/specs/timeslider_follow.js index e3d9b80678a..5b7ec492faf 100644 --- a/src/tests/frontend/specs/timeslider_follow.js +++ b/src/tests/frontend/specs/timeslider_follow.js @@ -13,7 +13,7 @@ describe('timeslider follow', function () { // send 6 revisions const revs = 6; const message = 'a\n\n\n\n\n\n\n\n\n\n'; - const newLines = message.split('\n').length; + const newLines = message.split('\n').length - 1; for (let i = 0; i < revs; i++) { await helper.edit(message, newLines * i + 1); } From 7147195ac65f59b7ae7a5c0c491018b8f8152ef1 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 20 Aug 2021 19:33:56 -0400 Subject: [PATCH 03/11] sendkeys: Undo formatting and unnecessary changes This partially reverts the following commits: * 04e9fc3a2f8fa5d2d370d90003f25b642635cf82 * 3d8452b1439fd24473fb68653cb2f7e3e0bbdb4b * 07182bb7166d62aa3013454039f7a0bb0c41512c * 68ed9b219fa39d80c5d1503a19568fabfc9f726d --- src/tests/frontend/lib/sendkeys.js | 613 ++++++++++++++--------------- 1 file changed, 306 insertions(+), 307 deletions(-) diff --git a/src/tests/frontend/lib/sendkeys.js b/src/tests/frontend/lib/sendkeys.js index 91433f96482..cb9792d7131 100644 --- a/src/tests/frontend/lib/sendkeys.js +++ b/src/tests/frontend/lib/sendkeys.js @@ -24,329 +24,329 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -(function($){ +(function(){ bililiteRange = function(el, debug){ - var ret; - if (debug){ - ret = new NothingRange(); // Easier to force it to use the no-selection type than to try to find an old browser - }else if (document.selection && !document.addEventListener){ - // Internet Explorer 8 and lower - ret = new IERange(); - }else if (window.getSelection && el.setSelectionRange){ - // Standards. Element is an input or textarea - ret = new InputRange(); - }else if (window.getSelection){ - // Standards, with any other kind of element - ret = new W3CRange() - }else{ - // doesn't support selection - ret = new NothingRange(); - } - ret._el = el; - ret._doc = el.ownerDocument; - ret._win = 'defaultView' in ret._doc ? ret._doc.defaultView : ret._doc.parentWindow; - ret._textProp = textProp(el); - ret._bounds = [0, ret.length()]; - return ret; + var ret; + if (debug){ + ret = new NothingRange(); // Easier to force it to use the no-selection type than to try to find an old browser + }else if (document.selection && !document.addEventListener){ + // Internet Explorer 8 and lower + ret = new IERange(); + }else if (window.getSelection && el.setSelectionRange){ + // Standards. Element is an input or textarea + ret = new InputRange(); + }else if (window.getSelection){ + // Standards, with any other kind of element + ret = new W3CRange() + }else{ + // doesn't support selection + ret = new NothingRange(); + } + ret._el = el; + ret._doc = el.ownerDocument; + ret._win = 'defaultView' in ret._doc ? ret._doc.defaultView : ret._doc.parentWindow; + ret._textProp = textProp(el); + ret._bounds = [0, ret.length()]; + return ret; } function textProp(el){ - // returns the property that contains the text of the element - if (typeof el.value != 'undefined') return 'value'; - if (typeof el.text != 'undefined') return 'text'; - if (typeof el.textContent != 'undefined') return 'textContent'; - return 'innerText'; + // returns the property that contains the text of the element + if (typeof el.value != 'undefined') return 'value'; + if (typeof el.text != 'undefined') return 'text'; + if (typeof el.textContent != 'undefined') return 'textContent'; + return 'innerText'; } // base class function Range(){} Range.prototype = { - length: function() { - return this._el[this._textProp].replace(/\r/g, '').length; // need to correct for IE's CrLf weirdness - }, - bounds: function(s){ - if (s === 'all'){ - this._bounds = [0, this.length()]; - }else if (s === 'start'){ - this._bounds = [0, 0]; - }else if (s === 'end'){ - this._bounds = [this.length(), this.length()]; - }else if (s === 'selection'){ - this.bounds ('all'); // first select the whole thing for constraining - this._bounds = this._nativeSelection(); - }else if (s){ - this._bounds = s; // don't error check now; the element may change at any moment, so constrain it when we need it. - }else{ - var b = [ - Math.max(0, Math.min (this.length(), this._bounds[0])), - Math.max(0, Math.min (this.length(), this._bounds[1])) - ]; - return b; // need to constrain it to fit - } - return this; // allow for chaining - }, - select: function(){ - this._nativeSelect(this._nativeRange(this.bounds())); - return this; // allow for chaining - }, - text: function(text, select){ - if (arguments.length){ - this._nativeSetText(text, this._nativeRange(this.bounds())); - if (select == 'start'){ - this.bounds ([this._bounds[0], this._bounds[0]]); - this.select(); - }else if (select == 'end'){ - this.bounds ([this._bounds[0]+text.length, this._bounds[0]+text.length]); - this.select(); - }else if (select == 'all'){ - this.bounds ([this._bounds[0], this._bounds[0]+text.length]); - this.select(); - } - return this; // allow for chaining - }else{ - return this._nativeGetText(this._nativeRange(this.bounds())); - } - }, - insertEOL: function (){ - this._nativeEOL(); - this._bounds = [this._bounds[0]+1, this._bounds[0]+1]; // move past the EOL marker - return this; - } + length: function() { + return this._el[this._textProp].replace(/\r/g, '').length; // need to correct for IE's CrLf weirdness + }, + bounds: function(s){ + if (s === 'all'){ + this._bounds = [0, this.length()]; + }else if (s === 'start'){ + this._bounds = [0, 0]; + }else if (s === 'end'){ + this._bounds = [this.length(), this.length()]; + }else if (s === 'selection'){ + this.bounds ('all'); // first select the whole thing for constraining + this._bounds = this._nativeSelection(); + }else if (s){ + this._bounds = s; // don't error check now; the element may change at any moment, so constrain it when we need it. + }else{ + var b = [ + Math.max(0, Math.min (this.length(), this._bounds[0])), + Math.max(0, Math.min (this.length(), this._bounds[1])) + ]; + return b; // need to constrain it to fit + } + return this; // allow for chaining + }, + select: function(){ + this._nativeSelect(this._nativeRange(this.bounds())); + return this; // allow for chaining + }, + text: function(text, select){ + if (arguments.length){ + this._nativeSetText(text, this._nativeRange(this.bounds())); + if (select == 'start'){ + this.bounds ([this._bounds[0], this._bounds[0]]); + this.select(); + }else if (select == 'end'){ + this.bounds ([this._bounds[0]+text.length, this._bounds[0]+text.length]); + this.select(); + }else if (select == 'all'){ + this.bounds ([this._bounds[0], this._bounds[0]+text.length]); + this.select(); + } + return this; // allow for chaining + }else{ + return this._nativeGetText(this._nativeRange(this.bounds())); + } + }, + insertEOL: function (){ + this._nativeEOL(); + this._bounds = [this._bounds[0]+1, this._bounds[0]+1]; // move past the EOL marker + return this; + } }; function IERange(){} IERange.prototype = new Range(); IERange.prototype._nativeRange = function (bounds){ - var rng; - if (this._el.tagName == 'INPUT'){ - // IE 8 is very inconsistent; textareas have createTextRange but it doesn't work - rng = this._el.createTextRange(); - }else{ - rng = this._doc.body.createTextRange (); - rng.moveToElementText(this._el); - } - if (bounds){ - if (bounds[1] < 0) bounds[1] = 0; // IE tends to run elements out of bounds - if (bounds[0] > this.length()) bounds[0] = this.length(); - if (bounds[1] < rng.text.replace(/\r/g, '').length){ // correct for IE's CrLf wierdness - // block-display elements have an invisible, uncounted end of element marker, so we move an extra one and use the current length of the range - rng.moveEnd ('character', -1); - rng.moveEnd ('character', bounds[1]-rng.text.replace(/\r/g, '').length); - } - if (bounds[0] > 0) rng.moveStart('character', bounds[0]); - } - return rng; + var rng; + if (this._el.tagName == 'INPUT'){ + // IE 8 is very inconsistent; textareas have createTextRange but it doesn't work + rng = this._el.createTextRange(); + }else{ + rng = this._doc.body.createTextRange (); + rng.moveToElementText(this._el); + } + if (bounds){ + if (bounds[1] < 0) bounds[1] = 0; // IE tends to run elements out of bounds + if (bounds[0] > this.length()) bounds[0] = this.length(); + if (bounds[1] < rng.text.replace(/\r/g, '').length){ // correct for IE's CrLf wierdness + // block-display elements have an invisible, uncounted end of element marker, so we move an extra one and use the current length of the range + rng.moveEnd ('character', -1); + rng.moveEnd ('character', bounds[1]-rng.text.replace(/\r/g, '').length); + } + if (bounds[0] > 0) rng.moveStart('character', bounds[0]); + } + return rng; }; IERange.prototype._nativeSelect = function (rng){ - rng.select(); + rng.select(); }; IERange.prototype._nativeSelection = function (){ - // returns [start, end] for the selection constrained to be in element - var rng = this._nativeRange(); // range of the element to constrain to - var len = this.length(); - if (this._doc.selection.type != 'Text') return [0,0]; // append to the end - var sel = this._doc.selection.createRange(); - try{ - return [ - iestart(sel, rng), - ieend (sel, rng) - ]; - }catch (e){ - // IE gets upset sometimes about comparing text to input elements, but the selections cannot overlap, so make a best guess - return (sel.parentElement().sourceIndex < this._el.sourceIndex) ? [0,0] : [len, len]; - } + // returns [start, end] for the selection constrained to be in element + var rng = this._nativeRange(); // range of the element to constrain to + var len = this.length(); + if (this._doc.selection.type != 'Text') return [len, len]; // append to the end + var sel = this._doc.selection.createRange(); + try{ + return [ + iestart(sel, rng), + ieend (sel, rng) + ]; + }catch (e){ + // IE gets upset sometimes about comparing text to input elements, but the selections cannot overlap, so make a best guess + return (sel.parentElement().sourceIndex < this._el.sourceIndex) ? [0,0] : [len, len]; + } }; IERange.prototype._nativeGetText = function (rng){ - return rng.text.replace(/\r/g, ''); // correct for IE's CrLf weirdness + return rng.text.replace(/\r/g, ''); // correct for IE's CrLf weirdness }; IERange.prototype._nativeSetText = function (text, rng){ - rng.text = text; + rng.text = text; }; IERange.prototype._nativeEOL = function(){ - if (typeof this._el.value != 'undefined'){ - this.text('\n'); // for input and textarea, insert it straight - }else{ - this._nativeRange(this.bounds()).pasteHTML('
'); - } + if (typeof this._el.value != 'undefined'){ + this.text('\n'); // for input and textarea, insert it straight + }else{ + this._nativeRange(this.bounds()).pasteHTML('
'); + } }; // IE internals function iestart(rng, constraint){ - // returns the position (in character) of the start of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after - var len = constraint.text.replace(/\r/g, '').length; // correct for IE's CrLf wierdness - if (rng.compareEndPoints ('StartToStart', constraint) <= 0) return 0; // at or before the beginning - if (rng.compareEndPoints ('StartToEnd', constraint) >= 0) return len; - for (var i = 0; rng.compareEndPoints ('StartToStart', constraint) > 0; ++i, rng.moveStart('character', -1)); - return i; + // returns the position (in character) of the start of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after + var len = constraint.text.replace(/\r/g, '').length; // correct for IE's CrLf wierdness + if (rng.compareEndPoints ('StartToStart', constraint) <= 0) return 0; // at or before the beginning + if (rng.compareEndPoints ('StartToEnd', constraint) >= 0) return len; + for (var i = 0; rng.compareEndPoints ('StartToStart', constraint) > 0; ++i, rng.moveStart('character', -1)); + return i; } function ieend (rng, constraint){ - // returns the position (in character) of the end of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after - var len = constraint.text.replace(/\r/g, '').length; // correct for IE's CrLf wierdness - if (rng.compareEndPoints ('EndToEnd', constraint) >= 0) return len; // at or after the end - if (rng.compareEndPoints ('EndToStart', constraint) <= 0) return 0; - for (var i = 0; rng.compareEndPoints ('EndToStart', constraint) > 0; ++i, rng.moveEnd('character', -1)); - return i; + // returns the position (in character) of the end of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after + var len = constraint.text.replace(/\r/g, '').length; // correct for IE's CrLf wierdness + if (rng.compareEndPoints ('EndToEnd', constraint) >= 0) return len; // at or after the end + if (rng.compareEndPoints ('EndToStart', constraint) <= 0) return 0; + for (var i = 0; rng.compareEndPoints ('EndToStart', constraint) > 0; ++i, rng.moveEnd('character', -1)); + return i; } // an input element in a standards document. "Native Range" is just the bounds array function InputRange(){} InputRange.prototype = new Range(); InputRange.prototype._nativeRange = function(bounds) { - return bounds || [0, this.length()]; + return bounds || [0, this.length()]; }; InputRange.prototype._nativeSelect = function (rng){ - this._el.setSelectionRange(rng[0], rng[1]); + this._el.setSelectionRange(rng[0], rng[1]); }; InputRange.prototype._nativeSelection = function(){ - return [this._el.selectionStart, this._el.selectionEnd]; + return [this._el.selectionStart, this._el.selectionEnd]; }; InputRange.prototype._nativeGetText = function(rng){ - return this._el.value.substring(rng[0], rng[1]); + return this._el.value.substring(rng[0], rng[1]); }; InputRange.prototype._nativeSetText = function(text, rng){ - var val = this._el.value; - this._el.value = val.substring(0, rng[0]) + text + val.substring(rng[1]); + var val = this._el.value; + this._el.value = val.substring(0, rng[0]) + text + val.substring(rng[1]); }; InputRange.prototype._nativeEOL = function(){ - this.text('\n'); + this.text('\n'); }; function W3CRange(){} W3CRange.prototype = new Range(); W3CRange.prototype._nativeRange = function (bounds){ - var rng = this._doc.createRange(); - rng.selectNodeContents(this._el); - if (bounds){ - w3cmoveBoundary (rng, bounds[0], true, this._el); - rng.collapse (true); - w3cmoveBoundary (rng, bounds[1]-bounds[0], false, this._el); - } - return rng; + var rng = this._doc.createRange(); + rng.selectNodeContents(this._el); + if (bounds){ + w3cmoveBoundary (rng, bounds[0], true, this._el); + rng.collapse (true); + w3cmoveBoundary (rng, bounds[1]-bounds[0], false, this._el); + } + return rng; }; W3CRange.prototype._nativeSelect = function (rng){ - this._win.getSelection().removeAllRanges(); - this._win.getSelection().addRange (rng); + this._win.getSelection().removeAllRanges(); + this._win.getSelection().addRange (rng); }; W3CRange.prototype._nativeSelection = function (){ - // returns [start, end] for the selection constrained to be in element - var rng = this._nativeRange(); // range of the element to constrain to - if (this._win.getSelection().rangeCount == 0) return [this.length(), this.length()]; // append to the end - var sel = this._win.getSelection().getRangeAt(0); - return [ - w3cstart(sel, rng), - w3cend (sel, rng) - ]; - } + // returns [start, end] for the selection constrained to be in element + var rng = this._nativeRange(); // range of the element to constrain to + if (this._win.getSelection().rangeCount == 0) return [this.length(), this.length()]; // append to the end + var sel = this._win.getSelection().getRangeAt(0); + return [ + w3cstart(sel, rng), + w3cend (sel, rng) + ]; + } W3CRange.prototype._nativeGetText = function (rng){ - return rng.toString(); + return rng.toString(); }; W3CRange.prototype._nativeSetText = function (text, rng){ - rng.deleteContents(); - rng.insertNode (this._doc.createTextNode(text)); - this._el.normalize(); // merge the text with the surrounding text + rng.deleteContents(); + rng.insertNode (this._doc.createTextNode(text)); + this._el.normalize(); // merge the text with the surrounding text }; W3CRange.prototype._nativeEOL = function(){ - var rng = this._nativeRange(this.bounds()); - rng.deleteContents(); - var br = this._doc.createElement('br'); - br.setAttribute ('_moz_dirty', ''); // for Firefox - rng.insertNode (br); - rng.insertNode (this._doc.createTextNode('\n')); - rng.collapse (false); + var rng = this._nativeRange(this.bounds()); + rng.deleteContents(); + var br = this._doc.createElement('br'); + br.setAttribute ('_moz_dirty', ''); // for Firefox + rng.insertNode (br); + rng.insertNode (this._doc.createTextNode('\n')); + rng.collapse (false); }; // W3C internals function nextnode (node, root){ - // in-order traversal - // we've already visited node, so get kids then siblings - if (node.firstChild) return node.firstChild; - if (node.nextSibling) return node.nextSibling; - if (node===root) return null; - while (node.parentNode){ - // get uncles - node = node.parentNode; - if (node == root) return null; - if (node.nextSibling) return node.nextSibling; - } - return null; + // in-order traversal + // we've already visited node, so get kids then siblings + if (node.firstChild) return node.firstChild; + if (node.nextSibling) return node.nextSibling; + if (node===root) return null; + while (node.parentNode){ + // get uncles + node = node.parentNode; + if (node == root) return null; + if (node.nextSibling) return node.nextSibling; + } + return null; } function w3cmoveBoundary (rng, n, bStart, el){ - // move the boundary (bStart == true ? start : end) n characters forward, up to the end of element el. Forward only! - // if the start is moved after the end, then an exception is raised - if (n <= 0) return; - var node = rng[bStart ? 'startContainer' : 'endContainer']; - if (node.nodeType == 3){ - // we may be starting somewhere into the text - n += rng[bStart ? 'startOffset' : 'endOffset']; - } - while (node){ - if (node.nodeType == 3){ - if (n <= node.nodeValue.length){ - rng[bStart ? 'setStart' : 'setEnd'](node, n); - // special case: if we end next to a
, include that node. - if (n == node.nodeValue.length){ - // skip past zero-length text nodes - for (var next = nextnode (node, el); next && next.nodeType==3 && next.nodeValue.length == 0; next = nextnode(next, el)){ - rng[bStart ? 'setStartAfter' : 'setEndAfter'](next); - } - if (next && next.nodeType == 1 && next.nodeName == "BR") rng[bStart ? 'setStartAfter' : 'setEndAfter'](next); - } - return; - }else{ - rng[bStart ? 'setStartAfter' : 'setEndAfter'](node); // skip past this one - n -= node.nodeValue.length; // and eat these characters - } - } - node = nextnode (node, el); - } + // move the boundary (bStart == true ? start : end) n characters forward, up to the end of element el. Forward only! + // if the start is moved after the end, then an exception is raised + if (n <= 0) return; + var node = rng[bStart ? 'startContainer' : 'endContainer']; + if (node.nodeType == 3){ + // we may be starting somewhere into the text + n += rng[bStart ? 'startOffset' : 'endOffset']; + } + while (node){ + if (node.nodeType == 3){ + if (n <= node.nodeValue.length){ + rng[bStart ? 'setStart' : 'setEnd'](node, n); + // special case: if we end next to a
, include that node. + if (n == node.nodeValue.length){ + // skip past zero-length text nodes + for (var next = nextnode (node, el); next && next.nodeType==3 && next.nodeValue.length == 0; next = nextnode(next, el)){ + rng[bStart ? 'setStartAfter' : 'setEndAfter'](next); + } + if (next && next.nodeType == 1 && next.nodeName == "BR") rng[bStart ? 'setStartAfter' : 'setEndAfter'](next); + } + return; + }else{ + rng[bStart ? 'setStartAfter' : 'setEndAfter'](node); // skip past this one + n -= node.nodeValue.length; // and eat these characters + } + } + node = nextnode (node, el); + } } var START_TO_START = 0; // from the w3c definitions var START_TO_END = 1; var END_TO_END = 2; var END_TO_START = 3; // from the Mozilla documentation, for range.compareBoundaryPoints(how, sourceRange) -// -1, 0, or 1, indicating whether the corresponding boundary-point of range is respectively before, equal to, or after the corresponding boundary-point of sourceRange. +// -1, 0, or 1, indicating whether the corresponding boundary-point of range is respectively before, equal to, or after the corresponding boundary-point of sourceRange. // * Range.END_TO_END compares the end boundary-point of sourceRange to the end boundary-point of range. // * Range.END_TO_START compares the end boundary-point of sourceRange to the start boundary-point of range. // * Range.START_TO_END compares the start boundary-point of sourceRange to the end boundary-point of range. - // * Range.START_TO_START compares the start boundary-point of sourceRange to the start boundary-point of range. + // * Range.START_TO_START compares the start boundary-point of sourceRange to the start boundary-point of range. function w3cstart(rng, constraint){ - if (rng.compareBoundaryPoints (START_TO_START, constraint) <= 0) return 0; // at or before the beginning - if (rng.compareBoundaryPoints (END_TO_START, constraint) >= 0) return constraint.toString().length; - rng = rng.cloneRange(); // don't change the original - rng.setEnd (constraint.endContainer, constraint.endOffset); // they now end at the same place - return constraint.toString().length - rng.toString().length; + if (rng.compareBoundaryPoints (START_TO_START, constraint) <= 0) return 0; // at or before the beginning + if (rng.compareBoundaryPoints (END_TO_START, constraint) >= 0) return constraint.toString().length; + rng = rng.cloneRange(); // don't change the original + rng.setEnd (constraint.endContainer, constraint.endOffset); // they now end at the same place + return constraint.toString().length - rng.toString().length; } function w3cend (rng, constraint){ - if (rng.compareBoundaryPoints (END_TO_END, constraint) >= 0) return constraint.toString().length; // at or after the end - if (rng.compareBoundaryPoints (START_TO_END, constraint) <= 0) return 0; - rng = rng.cloneRange(); // don't change the original - rng.setStart (constraint.startContainer, constraint.startOffset); // they now start at the same place - return rng.toString().length; + if (rng.compareBoundaryPoints (END_TO_END, constraint) >= 0) return constraint.toString().length; // at or after the end + if (rng.compareBoundaryPoints (START_TO_END, constraint) <= 0) return 0; + rng = rng.cloneRange(); // don't change the original + rng.setStart (constraint.startContainer, constraint.startOffset); // they now start at the same place + return rng.toString().length; } function NothingRange(){} NothingRange.prototype = new Range(); NothingRange.prototype._nativeRange = function(bounds) { - return bounds || [0,this.length()]; + return bounds || [0,this.length()]; }; NothingRange.prototype._nativeSelect = function (rng){ // do nothing }; NothingRange.prototype._nativeSelection = function(){ - return [0,0]; + return [0,0]; }; NothingRange.prototype._nativeGetText = function (rng){ - return this._el[this._textProp].substring(rng[0], rng[1]); + return this._el[this._textProp].substring(rng[0], rng[1]); }; NothingRange.prototype._nativeSetText = function (text, rng){ - var val = this._el[this._textProp]; - this._el[this._textProp] = val.substring(0, rng[0]) + text + val.substring(rng[1]); + var val = this._el[this._textProp]; + this._el[this._textProp] = val.substring(0, rng[0]) + text + val.substring(rng[1]); }; NothingRange.prototype._nativeEOL = function(){ - this.text('\n'); + this.text('\n'); }; -})(jQuery); +})(); // insert characters in a textarea or text input field // special characters are enclosed in {}; use {{} for the { character itself @@ -378,90 +378,89 @@ NothingRange.prototype._nativeEOL = function(){ (function($){ $.fn.sendkeys = function (x, opts){ - return this.each( function(){ - var localkeys = $.extend({}, opts, $(this).data('sendkeys')); // allow for element-specific key functions - // most elements to not keep track of their selection when they lose focus, so we have to do it for them - var rng = $.data (this, 'sendkeys.selection'); - if (!rng){ - rng = bililiteRange(this).bounds('selection'); - $.data(this, 'sendkeys.selection', rng); - $(this).bind('mouseup.sendkeys', function(){ - // we have to update the saved range. The routines here update the bounds with each press, but actual keypresses and mouseclicks do not - $.data(this, 'sendkeys.selection').bounds('selection'); - }).bind('keyup.sendkeys', function(evt){ - // restore the selection if we got here with a tab (a click should select what was clicked on) - if (evt.which == 9){ - // there's a flash of selection when we restore the focus, but I don't know how to avoid that - $.data(this, 'sendkeys.selection').select(); - }else{ - $.data(this, 'sendkeys.selection').bounds('selection'); - } - }); - } - this.focus(); - if (typeof x === 'undefined') return; // no string, so we just set up the event handlers - $.data(this, 'sendkeys.originalText', rng.text()); - x.replace(/\n/g, '{enter}'). // turn line feeds into explicit break insertions - replace(/{[^}]*}|[^{]+/g, function(s){ - (localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s); - }); - $(this).trigger({type: 'sendkeys', which: x}); - }); + return this.each( function(){ + var localkeys = $.extend({}, opts, $(this).data('sendkeys')); // allow for element-specific key functions + // most elements to not keep track of their selection when they lose focus, so we have to do it for them + var rng = $.data (this, 'sendkeys.selection'); + if (!rng){ + rng = bililiteRange(this).bounds('selection'); + $.data(this, 'sendkeys.selection', rng); + $(this).bind('mouseup.sendkeys', function(){ + // we have to update the saved range. The routines here update the bounds with each press, but actual keypresses and mouseclicks do not + $.data(this, 'sendkeys.selection').bounds('selection'); + }).bind('keyup.sendkeys', function(evt){ + // restore the selection if we got here with a tab (a click should select what was clicked on) + if (evt.which == 9){ + // there's a flash of selection when we restore the focus, but I don't know how to avoid that. + $.data(this, 'sendkeys.selection').select(); + }else{ + $.data(this, 'sendkeys.selection').bounds('selection'); + } + }); + } + this.focus(); + if (typeof x === 'undefined') return; // no string, so we just set up the event handlers + $.data(this, 'sendkeys.originalText', rng.text()); + x.replace(/\n/g, '{enter}'). // turn line feeds into explicit break insertions + replace(/{[^}]*}|[^{]+/g, function(s){ + (localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s); + }); + $(this).trigger({type: 'sendkeys', which: x}); + }); }; // sendkeys // add the functions publicly so they can be overridden $.fn.sendkeys.defaults = { - simplechar: function (rng, s){ - rng.text(s, 'end'); - for (var i =0; i < s.length; ++i){ - var x = s.charCodeAt(i); - // a bit of cheating: rng._el is the element associated with rng. - $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); - } - }, - '{{}': function (rng){ - $.fn.sendkeys.defaults.simplechar (rng, '{') - }, - '{enter}': function (rng){ - rng.insertEOL(); - rng.select(); - $(rng._el).trigger( - {type: 'keypress', keyCode: 13, which: 13, charCode: 13, code: 'Enter', key: 'Enter'}); - }, - '{backspace}': function (rng){ - var b = rng.bounds(); - if (b[0] == b[1]) rng.bounds([b[0]-1, b[0]]); // no characters selected; it's just an insertion point. Remove the previous character - rng.text('', 'end'); // delete the characters and update the selection - }, - '{del}': function (rng){ - var b = rng.bounds(); - if (b[0] == b[1]) rng.bounds([b[0], b[0]+1]); // no characters selected; it's just an insertion point. Remove the next character - rng.text('', 'end'); // delete the characters and update the selection - }, - '{rightarrow}': function (rng){ - var b = rng.bounds(); - if (b[0] == b[1]) ++b[1]; // no characters selected; it's just an insertion point. Move to the right - rng.bounds([b[1], b[1]]).select(); - }, - '{leftarrow}': function (rng){ - var b = rng.bounds(); - if (b[0] == b[1]) --b[0]; // no characters selected; it's just an insertion point. Move to the left - rng.bounds([b[0], b[0]]).select(); - }, - '{selectall}' : function (rng){ - rng.bounds('all').select(); - }, - '{selection}': function (rng){ - $.fn.sendkeys.defaults.simplechar(rng, $.data(rng._el, 'sendkeys.originalText')); - }, - '{mark}' : function (rng){ - var bounds = rng.bounds(); - $(rng._el).one('sendkeys', function(){ - // set up the event listener to change the selection after the sendkeys is done - rng.bounds(bounds).select(); - }); - } + simplechar: function (rng, s){ + rng.text(s, 'end'); + for (var i =0; i < s.length; ++i){ + var x = s.charCodeAt(i); + // a bit of cheating: rng._el is the element associated with rng. + $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); + } + }, + '{{}': function (rng){ + $.fn.sendkeys.defaults.simplechar (rng, '{') + }, + '{enter}': function (rng){ + rng.insertEOL(); + rng.select(); + $(rng._el).trigger({type: 'keypress', keyCode: 13, which: 13, charCode: 13, code: 'Enter', key: 'Enter'}); + }, + '{backspace}': function (rng){ + var b = rng.bounds(); + if (b[0] == b[1]) rng.bounds([b[0]-1, b[0]]); // no characters selected; it's just an insertion point. Remove the previous character + rng.text('', 'end'); // delete the characters and update the selection + }, + '{del}': function (rng){ + var b = rng.bounds(); + if (b[0] == b[1]) rng.bounds([b[0], b[0]+1]); // no characters selected; it's just an insertion point. Remove the next character + rng.text('', 'end'); // delete the characters and update the selection + }, + '{rightarrow}': function (rng){ + var b = rng.bounds(); + if (b[0] == b[1]) ++b[1]; // no characters selected; it's just an insertion point. Move to the right + rng.bounds([b[1], b[1]]).select(); + }, + '{leftarrow}': function (rng){ + var b = rng.bounds(); + if (b[0] == b[1]) --b[0]; // no characters selected; it's just an insertion point. Move to the left + rng.bounds([b[0], b[0]]).select(); + }, + '{selectall}' : function (rng){ + rng.bounds('all').select(); + }, + '{selection}': function (rng){ + $.fn.sendkeys.defaults.simplechar(rng, $.data(rng._el, 'sendkeys.originalText')); + }, + '{mark}' : function (rng){ + var bounds = rng.bounds(); + $(rng._el).one('sendkeys', function(){ + // set up the event listener to change the selection after the sendkeys is done + rng.bounds(bounds).select(); + }); + } }; })(jQuery) From ea556c4150845b81116e514ae95f0163480d95e3 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 20 Aug 2021 20:13:24 -0400 Subject: [PATCH 04/11] sendkeys: Upgrade to oldest version in upstream repo --- src/tests/frontend/lib/sendkeys.js | 78 +++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 11 deletions(-) diff --git a/src/tests/frontend/lib/sendkeys.js b/src/tests/frontend/lib/sendkeys.js index cb9792d7131..743e45b481c 100644 --- a/src/tests/frontend/lib/sendkeys.js +++ b/src/tests/frontend/lib/sendkeys.js @@ -1,6 +1,6 @@ // Cross-broswer implementation of text ranges and selections -// documentation: http://bililite.com/blog/2011/01/11/cross-browser-.and-selections/ -// Version: 1.1 +// documentation: http://bililite.com/blog/2011/01/17/cross-browser-text-ranges-and-selections/ +// Version: 1.5 // Copyright (c) 2010 Daniel Wachsstock // MIT license: // Permission is hereby granted, free of charge, to any person @@ -44,6 +44,7 @@ bililiteRange = function(el, debug){ ret = new NothingRange(); } ret._el = el; + // determine parent document, as implemented by John McLear ret._doc = el.ownerDocument; ret._win = 'defaultView' in ret._doc ? ret._doc.defaultView : ret._doc.parentWindow; ret._textProp = textProp(el); @@ -76,12 +77,13 @@ Range.prototype = { this.bounds ('all'); // first select the whole thing for constraining this._bounds = this._nativeSelection(); }else if (s){ - this._bounds = s; // don't error check now; the element may change at any moment, so constrain it when we need it. + this._bounds = s; // don't do error checking now; things may change at a moment's notice }else{ var b = [ Math.max(0, Math.min (this.length(), this._bounds[0])), Math.max(0, Math.min (this.length(), this._bounds[1])) ]; + b[1] = Math.max(b[0], b[1]); return b; // need to constrain it to fit } return this; // allow for chaining @@ -93,15 +95,15 @@ Range.prototype = { text: function(text, select){ if (arguments.length){ this._nativeSetText(text, this._nativeRange(this.bounds())); + try { // signal the text change (IE < 9 doesn't support this, so we live with it) + this._el.dispatchEvent(new CustomEvent('input', {detail: {text: text, bounds: this.bounds()}})); + }catch(e){ /* ignore */ } if (select == 'start'){ this.bounds ([this._bounds[0], this._bounds[0]]); - this.select(); }else if (select == 'end'){ this.bounds ([this._bounds[0]+text.length, this._bounds[0]+text.length]); - this.select(); }else if (select == 'all'){ this.bounds ([this._bounds[0], this._bounds[0]+text.length]); - this.select(); } return this; // allow for chaining }else{ @@ -112,9 +114,18 @@ Range.prototype = { this._nativeEOL(); this._bounds = [this._bounds[0]+1, this._bounds[0]+1]; // move past the EOL marker return this; + }, + scrollIntoView: function(){ + this._nativeScrollIntoView(this._nativeRange(this.bounds())); + return this; } }; +// allow extensions ala jQuery +bililiteRange.fn = Range.prototype; // to allow monkey patching +bililiteRange.extend = function(fns){ + for (fn in fns) Range.prototype[fn] = fns[fn]; +}; function IERange(){} IERange.prototype = new Range(); @@ -171,6 +182,9 @@ IERange.prototype._nativeEOL = function(){ this._nativeRange(this.bounds()).pasteHTML('
'); } }; +IERange.prototype._nativeScrollIntoView = function(rng){ + rng.scrollIntoView(); +} // IE internals function iestart(rng, constraint){ // returns the position (in character) of the start of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after @@ -211,6 +225,39 @@ InputRange.prototype._nativeSetText = function(text, rng){ InputRange.prototype._nativeEOL = function(){ this.text('\n'); }; +InputRange.prototype._nativeScrollIntoView = function(rng){ + // I can't remember where I found this clever hack to find the location of text in a text area + var style = getComputedStyle(this._el); + var oldheight = style.height; + var oldval = this._el.value; + var oldselection = this._nativeSelection(); + this._el.style.height = '1px'; + this._el.value = oldval.slice(0, rng[0]); + var top = this._el.scrollHeight; + // this gives the bottom of the text, so we have to subtract the height of a single line + this._el.value = 'X'; + top -= 2*this._el.scrollHeight; // show at least a line above + this._el.style.height = oldheight; + this._el.value = oldval; + this._nativeSelect(oldselection); + // scroll into position if necessary + if (this._el.scrollTop > top || this._el.scrollTop+this._el.clientHeight < top){ + this._el.scrollTop = top; + } + // now scroll the element into view; get its position as in jQuery.offset + var rect = this._el.getBoundingClientRect(); + rect.top += this._win.pageYOffset - this._doc.documentElement.clientTop; + rect.left += this._win.pageXOffset - this._doc.documentElement.clientLeft; + // create an element to scroll to + var div = this._doc.createElement('div'); + div.style.position = 'absolute'; + div.style.top = (rect.top+top-this._el.scrollTop)+'px'; // adjust for how far in the range is; it may not have scrolled all the way to the top + div.style.left = rect.left+'px'; + div.innerHTML = ' '; + this._doc.body.appendChild(div); + div.scrollIntoViewIfNeeded ? div.scrollIntoViewIfNeeded() : div.scrollIntoView(); + div.parentNode.removeChild(div); +} function W3CRange(){} W3CRange.prototype = new Range(); @@ -255,6 +302,13 @@ W3CRange.prototype._nativeEOL = function(){ rng.insertNode (this._doc.createTextNode('\n')); rng.collapse (false); }; +W3CRange.prototype._nativeScrollIntoView = function(rng){ + // can't scroll to a range; have to scroll to an element instead + var span = this._doc.createElement('span'); + rng.insertNode(span); + span.scrollIntoViewIfNeeded ? span.scrollIntoViewIfNeeded() : span.scrollIntoView(); + span.parentNode.removeChild(span); +} // W3C internals function nextnode (node, root){ // in-order traversal @@ -345,14 +399,17 @@ NothingRange.prototype._nativeSetText = function (text, rng){ NothingRange.prototype._nativeEOL = function(){ this.text('\n'); }; +NothingRange.prototype._nativeScrollIntoView = function(){ + this._el.scrollIntoView(); +}; })(); // insert characters in a textarea or text input field // special characters are enclosed in {}; use {{} for the { character itself // documentation: http://bililite.com/blog/2008/08/20/the-fnsendkeys-plugin/ -// Version: 2.0 -// Copyright (c) 2010 Daniel Wachsstock +// Version: 2.1 +// Copyright (c) 2013 Daniel Wachsstock // MIT license: // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -413,6 +470,8 @@ $.fn.sendkeys = function (x, opts){ // add the functions publicly so they can be overridden $.fn.sendkeys.defaults = { simplechar: function (rng, s){ + // deal with unknown {key}s + if (/^{.*}$/.test(s)) s = s.slice(1,-1); rng.text(s, 'end'); for (var i =0; i < s.length; ++i){ var x = s.charCodeAt(i); @@ -420,9 +479,6 @@ $.fn.sendkeys.defaults = { $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); } }, - '{{}': function (rng){ - $.fn.sendkeys.defaults.simplechar (rng, '{') - }, '{enter}': function (rng){ rng.insertEOL(); rng.select(); From 090035498037dc1eb7cddd18a9d3c5e2e7617462 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 20 Aug 2021 20:49:50 -0400 Subject: [PATCH 05/11] sendkeys: Split into separate files to match upstream --- src/tests/frontend/helper.js | 13 +- src/tests/frontend/index.html | 3 +- .../lib/{sendkeys.js => bililiteRange.js} | 116 ------------------ src/tests/frontend/lib/jquery.sendkeys.js | 115 +++++++++++++++++ 4 files changed, 126 insertions(+), 121 deletions(-) rename src/tests/frontend/lib/{sendkeys.js => bililiteRange.js} (77%) create mode 100644 src/tests/frontend/lib/jquery.sendkeys.js diff --git a/src/tests/frontend/helper.js b/src/tests/frontend/helper.js index 18981897e91..42162f8922c 100644 --- a/src/tests/frontend/helper.js +++ b/src/tests/frontend/helper.js @@ -36,10 +36,15 @@ const helper = {}; await p; }; - if (!win.$) await load('../../static/js/vendors/jquery.js'); - // sendkeys.js depends on jQuery, so it cannot be loaded until jQuery has finished loading. (In - // other words, do not load both jQuery and sendkeys inside a Promise.all() call.) - if (!win.bililiteRange && includeSendkeys) await load('../tests/frontend/lib/sendkeys.js'); + await Promise.all([ + !win.$ && load('../../static/js/vendors/jquery.js'), + !win.bililiteRange && includeSendkeys && load('../tests/frontend/lib/bililiteRange.js'), + ]); + // jquery.sendkeys.js depends on jQuery, so it cannot be loaded until jQuery has finished + // loading. (In other words, do not load sendkeys in the above Promise.all() call.) + if (!win.$.fn.sendkeys && includeSendkeys) { + await load('../tests/frontend/lib/jquery.sendkeys.js'); + } win.$.window = win; win.$.document = doc; diff --git a/src/tests/frontend/index.html b/src/tests/frontend/index.html index c3b7a4633f0..55c85c1cddd 100644 --- a/src/tests/frontend/index.html +++ b/src/tests/frontend/index.html @@ -16,7 +16,8 @@ - + + diff --git a/src/tests/frontend/lib/sendkeys.js b/src/tests/frontend/lib/bililiteRange.js similarity index 77% rename from src/tests/frontend/lib/sendkeys.js rename to src/tests/frontend/lib/bililiteRange.js index 743e45b481c..c56c35558b3 100644 --- a/src/tests/frontend/lib/sendkeys.js +++ b/src/tests/frontend/lib/bililiteRange.js @@ -404,119 +404,3 @@ NothingRange.prototype._nativeScrollIntoView = function(){ }; })(); - -// insert characters in a textarea or text input field -// special characters are enclosed in {}; use {{} for the { character itself -// documentation: http://bililite.com/blog/2008/08/20/the-fnsendkeys-plugin/ -// Version: 2.1 -// Copyright (c) 2013 Daniel Wachsstock -// MIT license: -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: - -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. - -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - -(function($){ - -$.fn.sendkeys = function (x, opts){ - return this.each( function(){ - var localkeys = $.extend({}, opts, $(this).data('sendkeys')); // allow for element-specific key functions - // most elements to not keep track of their selection when they lose focus, so we have to do it for them - var rng = $.data (this, 'sendkeys.selection'); - if (!rng){ - rng = bililiteRange(this).bounds('selection'); - $.data(this, 'sendkeys.selection', rng); - $(this).bind('mouseup.sendkeys', function(){ - // we have to update the saved range. The routines here update the bounds with each press, but actual keypresses and mouseclicks do not - $.data(this, 'sendkeys.selection').bounds('selection'); - }).bind('keyup.sendkeys', function(evt){ - // restore the selection if we got here with a tab (a click should select what was clicked on) - if (evt.which == 9){ - // there's a flash of selection when we restore the focus, but I don't know how to avoid that. - $.data(this, 'sendkeys.selection').select(); - }else{ - $.data(this, 'sendkeys.selection').bounds('selection'); - } - }); - } - this.focus(); - if (typeof x === 'undefined') return; // no string, so we just set up the event handlers - $.data(this, 'sendkeys.originalText', rng.text()); - x.replace(/\n/g, '{enter}'). // turn line feeds into explicit break insertions - replace(/{[^}]*}|[^{]+/g, function(s){ - (localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s); - }); - $(this).trigger({type: 'sendkeys', which: x}); - }); -}; // sendkeys - - -// add the functions publicly so they can be overridden -$.fn.sendkeys.defaults = { - simplechar: function (rng, s){ - // deal with unknown {key}s - if (/^{.*}$/.test(s)) s = s.slice(1,-1); - rng.text(s, 'end'); - for (var i =0; i < s.length; ++i){ - var x = s.charCodeAt(i); - // a bit of cheating: rng._el is the element associated with rng. - $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); - } - }, - '{enter}': function (rng){ - rng.insertEOL(); - rng.select(); - $(rng._el).trigger({type: 'keypress', keyCode: 13, which: 13, charCode: 13, code: 'Enter', key: 'Enter'}); - }, - '{backspace}': function (rng){ - var b = rng.bounds(); - if (b[0] == b[1]) rng.bounds([b[0]-1, b[0]]); // no characters selected; it's just an insertion point. Remove the previous character - rng.text('', 'end'); // delete the characters and update the selection - }, - '{del}': function (rng){ - var b = rng.bounds(); - if (b[0] == b[1]) rng.bounds([b[0], b[0]+1]); // no characters selected; it's just an insertion point. Remove the next character - rng.text('', 'end'); // delete the characters and update the selection - }, - '{rightarrow}': function (rng){ - var b = rng.bounds(); - if (b[0] == b[1]) ++b[1]; // no characters selected; it's just an insertion point. Move to the right - rng.bounds([b[1], b[1]]).select(); - }, - '{leftarrow}': function (rng){ - var b = rng.bounds(); - if (b[0] == b[1]) --b[0]; // no characters selected; it's just an insertion point. Move to the left - rng.bounds([b[0], b[0]]).select(); - }, - '{selectall}' : function (rng){ - rng.bounds('all').select(); - }, - '{selection}': function (rng){ - $.fn.sendkeys.defaults.simplechar(rng, $.data(rng._el, 'sendkeys.originalText')); - }, - '{mark}' : function (rng){ - var bounds = rng.bounds(); - $(rng._el).one('sendkeys', function(){ - // set up the event listener to change the selection after the sendkeys is done - rng.bounds(bounds).select(); - }); - } -}; - -})(jQuery) diff --git a/src/tests/frontend/lib/jquery.sendkeys.js b/src/tests/frontend/lib/jquery.sendkeys.js new file mode 100644 index 00000000000..e27f6b41ffe --- /dev/null +++ b/src/tests/frontend/lib/jquery.sendkeys.js @@ -0,0 +1,115 @@ +// insert characters in a textarea or text input field +// special characters are enclosed in {}; use {{} for the { character itself +// documentation: http://bililite.com/blog/2008/08/20/the-fnsendkeys-plugin/ +// Version: 2.1 +// Copyright (c) 2013 Daniel Wachsstock +// MIT license: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: + +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +(function($){ + +$.fn.sendkeys = function (x, opts){ + return this.each( function(){ + var localkeys = $.extend({}, opts, $(this).data('sendkeys')); // allow for element-specific key functions + // most elements to not keep track of their selection when they lose focus, so we have to do it for them + var rng = $.data (this, 'sendkeys.selection'); + if (!rng){ + rng = bililiteRange(this).bounds('selection'); + $.data(this, 'sendkeys.selection', rng); + $(this).bind('mouseup.sendkeys', function(){ + // we have to update the saved range. The routines here update the bounds with each press, but actual keypresses and mouseclicks do not + $.data(this, 'sendkeys.selection').bounds('selection'); + }).bind('keyup.sendkeys', function(evt){ + // restore the selection if we got here with a tab (a click should select what was clicked on) + if (evt.which == 9){ + // there's a flash of selection when we restore the focus, but I don't know how to avoid that. + $.data(this, 'sendkeys.selection').select(); + }else{ + $.data(this, 'sendkeys.selection').bounds('selection'); + } + }); + } + this.focus(); + if (typeof x === 'undefined') return; // no string, so we just set up the event handlers + $.data(this, 'sendkeys.originalText', rng.text()); + x.replace(/\n/g, '{enter}'). // turn line feeds into explicit break insertions + replace(/{[^}]*}|[^{]+/g, function(s){ + (localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s); + }); + $(this).trigger({type: 'sendkeys', which: x}); + }); +}; // sendkeys + + +// add the functions publicly so they can be overridden +$.fn.sendkeys.defaults = { + simplechar: function (rng, s){ + // deal with unknown {key}s + if (/^{.*}$/.test(s)) s = s.slice(1,-1); + rng.text(s, 'end'); + for (var i =0; i < s.length; ++i){ + var x = s.charCodeAt(i); + // a bit of cheating: rng._el is the element associated with rng. + $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); + } + }, + '{enter}': function (rng){ + rng.insertEOL(); + rng.select(); + $(rng._el).trigger({type: 'keypress', keyCode: 13, which: 13, charCode: 13, code: 'Enter', key: 'Enter'}); + }, + '{backspace}': function (rng){ + var b = rng.bounds(); + if (b[0] == b[1]) rng.bounds([b[0]-1, b[0]]); // no characters selected; it's just an insertion point. Remove the previous character + rng.text('', 'end'); // delete the characters and update the selection + }, + '{del}': function (rng){ + var b = rng.bounds(); + if (b[0] == b[1]) rng.bounds([b[0], b[0]+1]); // no characters selected; it's just an insertion point. Remove the next character + rng.text('', 'end'); // delete the characters and update the selection + }, + '{rightarrow}': function (rng){ + var b = rng.bounds(); + if (b[0] == b[1]) ++b[1]; // no characters selected; it's just an insertion point. Move to the right + rng.bounds([b[1], b[1]]).select(); + }, + '{leftarrow}': function (rng){ + var b = rng.bounds(); + if (b[0] == b[1]) --b[0]; // no characters selected; it's just an insertion point. Move to the left + rng.bounds([b[0], b[0]]).select(); + }, + '{selectall}' : function (rng){ + rng.bounds('all').select(); + }, + '{selection}': function (rng){ + $.fn.sendkeys.defaults.simplechar(rng, $.data(rng._el, 'sendkeys.originalText')); + }, + '{mark}' : function (rng){ + var bounds = rng.bounds(); + $(rng._el).one('sendkeys', function(){ + // set up the event listener to change the selection after the sendkeys is done + rng.bounds(bounds).select(); + }); + } +}; + +})(jQuery) From ed991419b80c9e22e45e62155916e1261ef69a6b Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 21 Aug 2021 00:19:55 -0400 Subject: [PATCH 06/11] sendkeys: Merge in latest upstream commit that mostly works https://github.com/dwachss/bililiteRange/commit/5c034c108cefc854bfb450ac2b606861832a02f2 seems to be the newest upstream commit that still works. After that, `{selectall}` doesn't seem to work. Starting with v3.0, sendkeys sends input events instead of keypress events, which `ace2_inner.js` doesn't listen for. --- src/tests/frontend/lib/bililiteRange.js | 160 ++++++++++++++++++---- src/tests/frontend/lib/jquery.sendkeys.js | 112 ++++++++++----- 2 files changed, 215 insertions(+), 57 deletions(-) diff --git a/src/tests/frontend/lib/bililiteRange.js b/src/tests/frontend/lib/bililiteRange.js index c56c35558b3..f305a465803 100644 --- a/src/tests/frontend/lib/bililiteRange.js +++ b/src/tests/frontend/lib/bililiteRange.js @@ -1,7 +1,7 @@ // Cross-broswer implementation of text ranges and selections // documentation: http://bililite.com/blog/2011/01/17/cross-browser-text-ranges-and-selections/ -// Version: 1.5 -// Copyright (c) 2010 Daniel Wachsstock +// Version: 2.0 +// Copyright (c) 2013 Daniel Wachsstock // MIT license: // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -49,11 +49,22 @@ bililiteRange = function(el, debug){ ret._win = 'defaultView' in ret._doc ? ret._doc.defaultView : ret._doc.parentWindow; ret._textProp = textProp(el); ret._bounds = [0, ret.length()]; + if (!('oninput' in el)){ + // give IE8 a chance + var inputhack = function() {ret.dispatch({type: 'input'}) }; + ret.listen('keyup', inputhack); + ret.listen('cut', inputhack); + ret.listen('paste', inputhack); + ret.listen('drop', inputhack); + el.oninput = 'patched'; + } return ret; } function textProp(el){ // returns the property that contains the text of the element + // note that for elements the text attribute represents the obsolete text color, not the textContent. + // we document that these routines do not work for elements so that should not be relevant if (typeof el.value != 'undefined') return 'value'; if (typeof el.text != 'undefined') return 'text'; if (typeof el.textContent != 'undefined') return 'textContent'; @@ -90,21 +101,24 @@ Range.prototype = { }, select: function(){ this._nativeSelect(this._nativeRange(this.bounds())); + this.dispatch({type: 'select'}); return this; // allow for chaining }, text: function(text, select){ if (arguments.length){ - this._nativeSetText(text, this._nativeRange(this.bounds())); - try { // signal the text change (IE < 9 doesn't support this, so we live with it) - this._el.dispatchEvent(new CustomEvent('input', {detail: {text: text, bounds: this.bounds()}})); - }catch(e){ /* ignore */ } + var bounds = this.bounds(), el = this._el; + // signal the input per DOM 3 input events, http://www.w3.org/TR/DOM-Level-3-Events/#h4_events-inputevents + // we add another field, bounds, which are the bounds of the original text before being changed. + this.dispatch({type: 'beforeinput', data: text, bounds: bounds}); + this._nativeSetText(text, this._nativeRange(bounds)); if (select == 'start'){ - this.bounds ([this._bounds[0], this._bounds[0]]); + this.bounds ([bounds[0], bounds[0]]); }else if (select == 'end'){ - this.bounds ([this._bounds[0]+text.length, this._bounds[0]+text.length]); + this.bounds ([bounds[0]+text.length, bounds[0]+text.length]); }else if (select == 'all'){ - this.bounds ([this._bounds[0], this._bounds[0]+text.length]); + this.bounds ([bounds[0], bounds[0]+text.length]); } + this.dispatch({type: 'input', data: text, bounds: bounds}); return this; // allow for chaining }else{ return this._nativeGetText(this._nativeRange(this.bounds())); @@ -118,6 +132,88 @@ Range.prototype = { scrollIntoView: function(){ this._nativeScrollIntoView(this._nativeRange(this.bounds())); return this; + }, + wrap: function (n){ + this._nativeWrap(n, this._nativeRange(this.bounds())); + return this; + }, + selection: function(text){ + if (arguments.length){ + return this.bounds('selection').text(text, 'end').select(); + }else{ + return this.bounds('selection').text(); + } + }, + clone: function(){ + return bililiteRange(this._el).bounds(this.bounds()); + }, + all: function(text){ + if (arguments.length){ + this.dispatch ({type: 'beforeinput', data: text}); + this._el[this._textProp] = text; + this.dispatch ({type: 'input', data: text}); + return this; + }else{ + return this._el[this._textProp].replace(/\r/g, ''); // need to correct for IE's CrLf weirdness; + } + }, + element: function() { return this._el }, + // includes a quickie polyfill for CustomEvent for IE that isn't perfect but works for me + // IE10 allows custom events but not "new CustomEvent"; have to do it the old-fashioned way + dispatch: function(opts){ + opts = opts || {}; + var event = document.createEvent ? document.createEvent('CustomEvent') : this._doc.createEventObject(); + event.initCustomEvent && event.initCustomEvent(opts.type, !!opts.bubbles, !!opts.cancelable, opts.detail); + + for (var key in opts) event[key] = opts[key]; + // dispatch event asynchronously (in the sense of on the next turn of the event loop; still should be fired in order of dispatch + var el = this._el; + setTimeout(function(){ + try { + el.dispatchEvent ? el.dispatchEvent(event) : el.fireEvent("on" + opts.type, document.createEventObject()); + }catch(e){ + // IE8 will not let me fire custom events at all. Call them directly + if (jQuery) { + jQuery(el).trigger(event); + }else{ + var listeners = el['listen'+opts.type]; + if (listeners) for (var i = 0; i < listeners.length; ++i){ + listeners[i].call(el, event); + } + } + } + }, 0); + return this; + }, + listen: function (type, func){ + var el = this._el; + if (el.addEventListener){ + el.addEventListener(type, func); + }else if (jQuery){ + jQuery(el).on(type, func); + }else{ + el.attachEvent("on" + type, func); + // IE8 can't even handle custom events created with createEventObject (though it permits attachEvent), so we have to make our own + var listeners = el['listen'+type] = el['listen'+type] || []; + listeners.push(func); + } + return this; + }, + dontlisten: function (type, func){ + var el = this._el; + if (el.removeEventListener){ + el.removeEventListener(type, func); + }else if (jQuery){ + jQuery(el).off(type, func); + }else try{ + el.detachEvent("on" + type, func); + }catch(e){ + var listeners = el['listen'+type]; + if (listeners) for (var i = 0; i < listeners.length; ++i){ + if (listeners[i] === func) listeners[i] = function(){}; // replace with a noop + } + } + return this; } }; @@ -155,9 +251,10 @@ IERange.prototype._nativeSelect = function (rng){ }; IERange.prototype._nativeSelection = function (){ // returns [start, end] for the selection constrained to be in element + // this fails for an empty selection! selection.createRange() if in a text area does not create a text selection, so I can't compare it. var rng = this._nativeRange(); // range of the element to constrain to var len = this.length(); - if (this._doc.selection.type != 'Text') return [len, len]; // append to the end + // this._el.focus(); This solves the problem of text areas not having a real selection , but sucks the focus from everything else, so I can't use it var sel = this._doc.selection.createRange(); try{ return [ @@ -176,15 +273,24 @@ IERange.prototype._nativeSetText = function (text, rng){ rng.text = text; }; IERange.prototype._nativeEOL = function(){ - if (typeof this._el.value != 'undefined'){ + if ('value' in this._el){ this.text('\n'); // for input and textarea, insert it straight }else{ - this._nativeRange(this.bounds()).pasteHTML('
'); + this._nativeRange(this.bounds()).pasteHTML('\n
'); } }; IERange.prototype._nativeScrollIntoView = function(rng){ rng.scrollIntoView(); } +IERange.prototype._nativeWrap = function(n, rng) { + // hacky to use string manipulation but I don't see another way to do it. + var div = document.createElement('div'); + div.appendChild(n); + // insert the existing range HTML after the first tag + var html = div.innerHTML.replace('><', '>'+rng.htmlText+'<'); + rng.pasteHTML(html); +}; + // IE internals function iestart(rng, constraint){ // returns the position (in character) of the start of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after @@ -227,19 +333,17 @@ InputRange.prototype._nativeEOL = function(){ }; InputRange.prototype._nativeScrollIntoView = function(rng){ // I can't remember where I found this clever hack to find the location of text in a text area - var style = getComputedStyle(this._el); - var oldheight = style.height; - var oldval = this._el.value; - var oldselection = this._nativeSelection(); - this._el.style.height = '1px'; - this._el.value = oldval.slice(0, rng[0]); - var top = this._el.scrollHeight; + var clone = this._el.cloneNode(true); + clone.style.visibility = 'hidden'; + clone.style.position = 'absolute'; + this._el.parentNode.insertBefore(clone, this._el); + clone.style.height = '1px'; + clone.value = this._el.value.slice(0, rng[0]); + var top = clone.scrollHeight; // this gives the bottom of the text, so we have to subtract the height of a single line - this._el.value = 'X'; - top -= 2*this._el.scrollHeight; // show at least a line above - this._el.style.height = oldheight; - this._el.value = oldval; - this._nativeSelect(oldselection); + clone.value = 'X'; + top -= 2*clone.scrollHeight; // show at least a line above + clone.parentNode.removeChild(clone); // scroll into position if necessary if (this._el.scrollTop > top || this._el.scrollTop+this._el.clientHeight < top){ this._el.scrollTop = top; @@ -248,7 +352,7 @@ InputRange.prototype._nativeScrollIntoView = function(rng){ var rect = this._el.getBoundingClientRect(); rect.top += this._win.pageYOffset - this._doc.documentElement.clientTop; rect.left += this._win.pageXOffset - this._doc.documentElement.clientLeft; - // create an element to scroll to + // create an element to scroll to (can't just use the clone above, since scrollIntoView wants a visible element) var div = this._doc.createElement('div'); div.style.position = 'absolute'; div.style.top = (rect.top+top-this._el.scrollTop)+'px'; // adjust for how far in the range is; it may not have scrolled all the way to the top @@ -258,6 +362,7 @@ InputRange.prototype._nativeScrollIntoView = function(rng){ div.scrollIntoViewIfNeeded ? div.scrollIntoViewIfNeeded() : div.scrollIntoView(); div.parentNode.removeChild(div); } +InputRange.prototype._nativeWrap = function() {throw new Error("Cannot wrap in a text element")}; function W3CRange(){} W3CRange.prototype = new Range(); @@ -309,6 +414,10 @@ W3CRange.prototype._nativeScrollIntoView = function(rng){ span.scrollIntoViewIfNeeded ? span.scrollIntoViewIfNeeded() : span.scrollIntoView(); span.parentNode.removeChild(span); } +W3CRange.prototype._nativeWrap = function(n, rng) { + rng.surroundContents(n); +}; + // W3C internals function nextnode (node, root){ // in-order traversal @@ -402,5 +511,6 @@ NothingRange.prototype._nativeEOL = function(){ NothingRange.prototype._nativeScrollIntoView = function(){ this._el.scrollIntoView(); }; +NothingRange.prototype._nativeWrap = function() {throw new Error("Wrapping not implemented")}; })(); diff --git a/src/tests/frontend/lib/jquery.sendkeys.js b/src/tests/frontend/lib/jquery.sendkeys.js index e27f6b41ffe..9e611985e66 100644 --- a/src/tests/frontend/lib/jquery.sendkeys.js +++ b/src/tests/frontend/lib/jquery.sendkeys.js @@ -1,7 +1,7 @@ // insert characters in a textarea or text input field // special characters are enclosed in {}; use {{} for the { character itself // documentation: http://bililite.com/blog/2008/08/20/the-fnsendkeys-plugin/ -// Version: 2.1 +// Version: 2.2 // Copyright (c) 2013 Daniel Wachsstock // MIT license: // Permission is hereby granted, free of charge, to any person @@ -31,51 +31,33 @@ $.fn.sendkeys = function (x, opts){ return this.each( function(){ var localkeys = $.extend({}, opts, $(this).data('sendkeys')); // allow for element-specific key functions // most elements to not keep track of their selection when they lose focus, so we have to do it for them - var rng = $.data (this, 'sendkeys.selection'); - if (!rng){ - rng = bililiteRange(this).bounds('selection'); - $.data(this, 'sendkeys.selection', rng); - $(this).bind('mouseup.sendkeys', function(){ - // we have to update the saved range. The routines here update the bounds with each press, but actual keypresses and mouseclicks do not - $.data(this, 'sendkeys.selection').bounds('selection'); - }).bind('keyup.sendkeys', function(evt){ - // restore the selection if we got here with a tab (a click should select what was clicked on) - if (evt.which == 9){ - // there's a flash of selection when we restore the focus, but I don't know how to avoid that. - $.data(this, 'sendkeys.selection').select(); - }else{ - $.data(this, 'sendkeys.selection').bounds('selection'); - } - }); - } + var rng = $(this).selectionTracker(); + $(this).trigger({type: 'beforesendkeys', which: x}); this.focus(); - if (typeof x === 'undefined') return; // no string, so we just set up the event handlers $.data(this, 'sendkeys.originalText', rng.text()); - x.replace(/\n/g, '{enter}'). // turn line feeds into explicit break insertions + x.replace(/([^{])\n/g, '$1{enter}'). // turn line feeds into explicit break insertions, but not if escaped replace(/{[^}]*}|[^{]+/g, function(s){ (localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s); + rng.select(); }); $(this).trigger({type: 'sendkeys', which: x}); }); }; // sendkeys - // add the functions publicly so they can be overridden $.fn.sendkeys.defaults = { simplechar: function (rng, s){ // deal with unknown {key}s - if (/^{.*}$/.test(s)) s = s.slice(1,-1); - rng.text(s, 'end'); + if (/^{[^}]*}$/.test(s)) s = s.slice(1,-1); for (var i =0; i < s.length; ++i){ var x = s.charCodeAt(i); - // a bit of cheating: rng._el is the element associated with rng. - $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); + $(rng.element()).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); } + rng.text(s, 'end'); }, '{enter}': function (rng){ - rng.insertEOL(); - rng.select(); $(rng._el).trigger({type: 'keypress', keyCode: 13, which: 13, charCode: 13, code: 'Enter', key: 'Enter'}); + rng.insertEOL(); }, '{backspace}': function (rng){ var b = rng.bounds(); @@ -90,26 +72,92 @@ $.fn.sendkeys.defaults = { '{rightarrow}': function (rng){ var b = rng.bounds(); if (b[0] == b[1]) ++b[1]; // no characters selected; it's just an insertion point. Move to the right - rng.bounds([b[1], b[1]]).select(); + rng.bounds([b[1], b[1]]); }, '{leftarrow}': function (rng){ var b = rng.bounds(); if (b[0] == b[1]) --b[0]; // no characters selected; it's just an insertion point. Move to the left - rng.bounds([b[0], b[0]]).select(); + rng.bounds([b[0], b[0]]); }, '{selectall}' : function (rng){ - rng.bounds('all').select(); + rng.bounds('all'); }, '{selection}': function (rng){ - $.fn.sendkeys.defaults.simplechar(rng, $.data(rng._el, 'sendkeys.originalText')); + // insert the characters without the sendkeys processing + var s = $.data(rng.element(), 'sendkeys.originalText'); + for (var i =0; i < s.length; ++i){ + var x = s.charCodeAt(i); + $(rng.element()).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); + } + rng.selection(s); }, '{mark}' : function (rng){ var bounds = rng.bounds(); - $(rng._el).one('sendkeys', function(){ + $(rng.element()).one('sendkeys', function(){ // set up the event listener to change the selection after the sendkeys is done rng.bounds(bounds).select(); }); } }; +// Most ranges do not keep track of what was selected when they lose focus. +// We have to do that for them +$.fn.selectionTracker = function(bounds){ + var rng = this.data('selectionTracker'); + if (!rng){ + rng = bililiteRange(this[0]).bounds('selection'); + this.data('selectionTracker', rng); + $(this).on('mouseup.selectionTracker', function(evt){ + // we have to update the saved range. + rng.bounds('selection'); + }).on('keyup.selectionTracker', function(evt){ + // restore the selection if we got here with a tab (a click should select what was clicked on) + if (evt.which == 9){ + // there's a flash of selection when we restore the focus, but I don't know how to avoid that. + rng.select(); + }else{ + rng.bounds('selection'); + } + }); + } + if (arguments.length > 0) rng.bounds(bounds); // change the saved selection without actually selecting + if (document.activeElement == this[0]) rng.select(); // explicitly select it if already active + return rng; +} + +// monkey patch bililiteRange to reflect the saved range +var oldselect = bililiteRange.fn.select; +bililiteRange.fn.select = function(){ + var $el = $(this.element()); + if ( + $el.data('selectionTracker') && + document.activeElement != $el[0] + ){ + $el.selectionTracker(this.bounds()); + } + return oldselect.apply(this, arguments); +}; +var oldbounds = bililiteRange.fn.bounds; +bililiteRange.fn.bounds = function(bounds){ + var $el = $(this.element()); + if ( + $el.data('selectionTracker') && // if we are tracking the selection + document.activeElement != $el[0] && // and the real selection isn't here + bounds == 'selection' // and we want the selection anyway + ){ + bounds = $el.selectionTracker().bounds(); // use the saved selection + } + return oldbounds.call(this, bounds); +} + +// monkey patch focus to actually focus the element, on the saved range +var focus = $.fn.focus; +$.fn.focus = function(){ + if (this.length > 0){ + this[0].focus(); + this.selectionTracker(); + } + focus.apply(this, arguments); +} + })(jQuery) From cff205dbd7b69ba18b2d9f13e09019506e5eb9a0 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 21 Aug 2021 04:42:47 -0400 Subject: [PATCH 07/11] sendkeys: Fix `\n` to `{enter}` conversion --- src/tests/frontend/lib/jquery.sendkeys.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tests/frontend/lib/jquery.sendkeys.js b/src/tests/frontend/lib/jquery.sendkeys.js index 9e611985e66..a5a67d5b05b 100644 --- a/src/tests/frontend/lib/jquery.sendkeys.js +++ b/src/tests/frontend/lib/jquery.sendkeys.js @@ -35,7 +35,8 @@ $.fn.sendkeys = function (x, opts){ $(this).trigger({type: 'beforesendkeys', which: x}); this.focus(); $.data(this, 'sendkeys.originalText', rng.text()); - x.replace(/([^{])\n/g, '$1{enter}'). // turn line feeds into explicit break insertions, but not if escaped + // turn line feeds into explicit break insertions, but not if escaped + x.replace(/{[^}]*}|[^{]+/g, (s) => s.startsWith('{') ? s : s.replace(/\n/g, '{enter}')). replace(/{[^}]*}|[^{]+/g, function(s){ (localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s); rng.select(); From 39506fcde1ebbb1c5e92c35ead8606ca1344dbc9 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 21 Aug 2021 04:44:23 -0400 Subject: [PATCH 08/11] sendkeys: Fix `jQuery.fn.focus()` return value --- src/tests/frontend/lib/jquery.sendkeys.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/frontend/lib/jquery.sendkeys.js b/src/tests/frontend/lib/jquery.sendkeys.js index a5a67d5b05b..ea8525433ab 100644 --- a/src/tests/frontend/lib/jquery.sendkeys.js +++ b/src/tests/frontend/lib/jquery.sendkeys.js @@ -158,7 +158,7 @@ $.fn.focus = function(){ this[0].focus(); this.selectionTracker(); } - focus.apply(this, arguments); + return focus.apply(this, arguments); } })(jQuery) From 9ff6e82c72f3a214969ae4a84a5d59003af4f4c5 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 21 Aug 2021 04:50:07 -0400 Subject: [PATCH 09/11] sendkeys: Fix Range adjustment logic --- src/tests/frontend/lib/bililiteRange.js | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/tests/frontend/lib/bililiteRange.js b/src/tests/frontend/lib/bililiteRange.js index f305a465803..f502b4f77fa 100644 --- a/src/tests/frontend/lib/bililiteRange.js +++ b/src/tests/frontend/lib/bililiteRange.js @@ -438,29 +438,19 @@ function w3cmoveBoundary (rng, n, bStart, el){ // if the start is moved after the end, then an exception is raised if (n <= 0) return; var node = rng[bStart ? 'startContainer' : 'endContainer']; - if (node.nodeType == 3){ + if (node.nodeType === Node.TEXT_NODE) { // we may be starting somewhere into the text n += rng[bStart ? 'startOffset' : 'endOffset']; } - while (node){ - if (node.nodeType == 3){ - if (n <= node.nodeValue.length){ + for (; node; node = nextnode(node, el)) { + if (node.nodeType === Node.TEXT_NODE) { + if (n < node.nodeValue.length) { rng[bStart ? 'setStart' : 'setEnd'](node, n); - // special case: if we end next to a
, include that node. - if (n == node.nodeValue.length){ - // skip past zero-length text nodes - for (var next = nextnode (node, el); next && next.nodeType==3 && next.nodeValue.length == 0; next = nextnode(next, el)){ - rng[bStart ? 'setStartAfter' : 'setEndAfter'](next); - } - if (next && next.nodeType == 1 && next.nodeName == "BR") rng[bStart ? 'setStartAfter' : 'setEndAfter'](next); - } return; - }else{ - rng[bStart ? 'setStartAfter' : 'setEndAfter'](node); // skip past this one - n -= node.nodeValue.length; // and eat these characters } + n -= node.nodeValue.length; } - node = nextnode (node, el); + if (!node.firstChild) rng[bStart ? 'setStartAfter' : 'setEndAfter'](node); } } var START_TO_START = 0; // from the w3c definitions From 216f5af6a92185d663fad254c22ec95967d1a02d Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 21 Aug 2021 04:51:54 -0400 Subject: [PATCH 10/11] tests: Fix `chat.js` tests to accommodate new sendkeys --- src/tests/frontend/specs/chat.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/tests/frontend/specs/chat.js b/src/tests/frontend/specs/chat.js index 82527f37257..c37889e074d 100644 --- a/src/tests/frontend/specs/chat.js +++ b/src/tests/frontend/specs/chat.js @@ -25,9 +25,7 @@ describe('Chat messages and UI', function () { const username = helper.chatTextParagraphs().children('b').text(); const time = helper.chatTextParagraphs().children('.time').text(); - // TODO: The '\n' is an artifact of $.sendkeys('{enter}'). Figure out how to get rid of it - // without breaking the other tests that use $.sendkeys(). - expect(helper.chatTextParagraphs().text()).to.be(`${username}${time} ${chatValue}\n`); + expect(helper.chatTextParagraphs().text()).to.be(`${username}${time} ${chatValue}`); await helper.hideChat(); }); @@ -48,9 +46,7 @@ describe('Chat messages and UI', function () { const username = chat.children('b').text(); const time = chat.children('.time').text(); - // TODO: Each '\n' is an artifact of $.sendkeys('{enter}'). Figure out how to get rid of them - // without breaking the other tests that use $.sendkeys(). - expect(chat.text()).to.be(`${username}${time} \n${chatValue}\n`); + expect(chat.text()).to.be(`${username}${time} \n${chatValue}`); }); it('makes chat stick to right side of the screen via settings, ' + From bd5fae97b553d5bbf78691af271810162c044d78 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 20 Aug 2021 23:51:14 -0400 Subject: [PATCH 11/11] ace2_inner: Get computed style of first Element, not first Node `window.getComputedStyle()` throws if passed a non-Element Node. --- src/static/js/ace2_inner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js index e64c8695d16..4b85d6a2b63 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.js @@ -3466,7 +3466,7 @@ function Ace2Inner(editorInfo, cssManagers) { // use that for displaying the side div line number inline with the first line // of content -- This is used in ep_headings, ep_font_size etc. where the line // height is increased. - const elementStyle = window.getComputedStyle(docLine.firstChild); + const elementStyle = window.getComputedStyle(docLine.firstElementChild); const lineHeight = parseInt(elementStyle.getPropertyValue('line-height')); const marginBottom = parseInt(elementStyle.getPropertyValue('margin-bottom')); lineHeights.push(lineHeight + marginBottom);