From 649a28b6c65904ebf876bd51f842005841286da4 Mon Sep 17 00:00:00 2001 From: johnyma22 Date: Tue, 2 Oct 2012 15:32:31 +0100 Subject: [PATCH 1/7] first user contributed test, note the two files that needed to be edited, this kinda sucks --- tests/frontend/index.html | 3 ++- tests/frontend/specs/button_italic.js | 33 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/frontend/specs/button_italic.js diff --git a/tests/frontend/index.html b/tests/frontend/index.html index b86d6f00f..8d5aa2df3 100644 --- a/tests/frontend/index.html +++ b/tests/frontend/index.html @@ -17,6 +17,7 @@ + - \ No newline at end of file + diff --git a/tests/frontend/specs/button_italic.js b/tests/frontend/specs/button_italic.js new file mode 100644 index 000000000..9633c0ea8 --- /dev/null +++ b/tests/frontend/specs/button_italic.js @@ -0,0 +1,33 @@ +describe("italic button", function(){ + //create a new pad before each test run + beforeEach(function(cb){ + testHelper.newPad(cb); + }); + + it("makes text italic", function() { + //get the inner iframe + var $inner = testHelper.$getPadInner(); + + //get the first text element out of the inner iframe + var firstTextElement = $inner.find("div").first(); + + //select this text element + testHelper.selectText(firstTextElement[0]); + + //get the bold button and click it + var $italicButton = testHelper.$getPadChrome().find(".buttonicon-italic"); + $italicButton.click(); + + //ace creates a new dom element when you press a button, so just get the first text element again + var newFirstTextElement = $inner.find("div").first(); + + // is there a element now? + var isItalic = newFirstTextElement.find("i").length === 1; + + //expect it to be bold + expect(isItalic).to.be(true); + + //make sure the text hasn't changed + expect(newFirstTextElement.text()).to.eql(firstTextElement.text()); + }); +}); From 54a77458d604b3225c4ac92dc8c1edcda8250c91 Mon Sep 17 00:00:00 2001 From: johnyma22 Date: Wed, 3 Oct 2012 20:56:52 +0100 Subject: [PATCH 2/7] clean up helper file --- tests/frontend/helper.js | 124 +++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 57 deletions(-) diff --git a/tests/frontend/helper.js b/tests/frontend/helper.js index 1e2f201b7..14ab5bfb6 100644 --- a/tests/frontend/helper.js +++ b/tests/frontend/helper.js @@ -1,80 +1,90 @@ var testHelper = {}; -(function(){ - var $iframeContainer = $("#iframe-container"), $iframe; +(function () { + var $iframeContainer = $("#iframe-container"), + $iframe; - testHelper.randomString = function randomString(len) - { - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var randomstring = ''; - for (var i = 0; i < len; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return randomstring; - } + testHelper.randomString = function randomString(len) { + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + var randomstring = ''; + for (var i = 0; i < len; i++) { + var rnum = Math.floor(Math.random() * chars.length); + randomstring += chars.substring(rnum, rnum + 1); + } + return randomstring; + } - testHelper.newPad = function(cb){ - var padName = "FRONTEND_TEST_" + testHelper.randomString(20); + testHelper.newPad = function (cb) { + var padName = "FRONTEND_TEST_" + testHelper.randomString(20); $iframe = $("") - $iframeContainer.empty().append($iframe); + $iframeContainer.empty() + .append($iframe); var checkInterval; - $iframe.load(function(){ - checkInterval = setInterval(function(){ - var loaded = false; + $iframe.load(function () { + checkInterval = setInterval(function () { + var loaded = false; - try { - //check if loading div is hidden - loaded = !testHelper.$getPadChrome().find("#editorloadingbox").is(":visible"); - } catch(e){} + try { + //check if loading div is hidden + loaded = !testHelper.$getPadChrome() + .find("#editorloadingbox") + .is(":visible"); + } + catch (e) {} - if(loaded){ - clearTimeout(timeout); - clearInterval(checkInterval); + if (loaded) { + clearTimeout(timeout); + clearInterval(checkInterval); - cb(null, {name: padName}); - } - }, 100); - }); + cb(null, { + name: padName + }); + } + }, 100); + }); - var timeout = setTimeout(function(){ - if(checkInterval) clearInterval(checkInterval); + var timeout = setTimeout(function () { + if (checkInterval) clearInterval(checkInterval); cb(new Error("Pad didn't load in 10 seconds")); }, 10000); - return padName; - } + return padName; + } - testHelper.$getPadChrome = function(){ - return $iframe.contents() - } + testHelper.$getPadChrome = function () { + return $iframe.contents() + } - testHelper.$getPadOuter = function(){ - return testHelper.$getPadChrome().find('iframe.[name="ace_outer"]').contents(); - } + testHelper.$getPadOuter = function () { + return testHelper.$getPadChrome() + .find('iframe.[name="ace_outer"]') + .contents(); + } - testHelper.$getPadInner = function(){ - return testHelper.$getPadOuter().find('iframe.[name="ace_inner"]').contents(); - } + testHelper.$getPadInner = function () { + return testHelper.$getPadOuter() + .find('iframe.[name="ace_inner"]') + .contents(); + } - // copied from http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse - testHelper.selectText = function(element){ - var doc = document, range, selection; + // copied from http: //stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse + testHelper.selectText = function (element) { + var doc = document, + range, selection; if (doc.body.createTextRange) { //ms - range = doc.body.createTextRange(); - range.moveToElementText(element); - range.select(); - } else if (window.getSelection) { //all others - selection = window.getSelection(); - range = doc.createRange(); - range.selectNodeContents(element); - selection.removeAllRanges(); - selection.addRange(range); + range = doc.body.createTextRange(); + range.moveToElementText(element); + range.select(); } - } + else if (window.getSelection) { //all others + selection = window.getSelection(); + range = doc.createRange(); + range.selectNodeContents(element); + selection.removeAllRanges(); + selection.addRange(range); + } + } })() - From 7820e3eb7c68971650909fcbd11336f1821fd0d9 Mon Sep 17 00:00:00 2001 From: johnyma22 Date: Wed, 3 Oct 2012 20:57:04 +0100 Subject: [PATCH 3/7] beginning of keystroke test --- tests/frontend/specs/keystroke_delete.js | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/frontend/specs/keystroke_delete.js diff --git a/tests/frontend/specs/keystroke_delete.js b/tests/frontend/specs/keystroke_delete.js new file mode 100644 index 000000000..bd0702535 --- /dev/null +++ b/tests/frontend/specs/keystroke_delete.js @@ -0,0 +1,48 @@ +describe("delete keystroke", function(){ + //create a new pad before each test run + beforeEach(function(cb){ + testHelper.newPad(cb); + }); + + it("makes text delete", function() { + //get the inner iframe + var $inner = testHelper.$getPadInner(); + + //get the first text element out of the inner iframe + var firstTextElement = $inner.find("div").first(); + + //select this text element + testHelper.selectText(firstTextElement[0]); + + // get the original length of this element + var elementLength = firstTextElement.html().length; + console.log(elementLength); + + //get the bold keystroke and click it + // var $deletekeystroke = testHelper.$getPadChrome().find(".keystrokeicon-delete"); + + //put the cursor in the pad + var press = $.Event("keypress"); + press.ctrlKey = false; + press.which = 46; // 46 is delete key + firstTextElement.trigger(press); // simulate a keypress of delete + press.which = 37; // 37 is left key taking user to first place in pad. + firstTextElement.trigger(press); // simulate a keypress of left key + + //ace creates a new dom element when you press a keystroke, so just get the first text element again + var newFirstTextElement = $inner.find("div").first(); + + // is there a element now? + // var isdelete = newFirstTextElement.find("i").length === 1; + + // get the new length of this element + var newElementLength = newFirstTextElement.html().length; + console.log(newElementLength); + + //expect it to be one char less + expect(newElementLength).to.be((elementLength-1)); + + //make sure the text hasn't changed + expect(newFirstTextElement.text()).to.eql(firstTextElement.text()); + }); +}); From aa41ebcd6d0188209ac50f7a5521cf4051abc701 Mon Sep 17 00:00:00 2001 From: johnyma22 Date: Wed, 3 Oct 2012 21:15:56 +0100 Subject: [PATCH 4/7] include a sendkeys library to emulate sending keypresses --- tests/frontend/sendkeys.js | 465 +++++++++++++++++++++++++++++++++++++ 1 file changed, 465 insertions(+) create mode 100644 tests/frontend/sendkeys.js diff --git a/tests/frontend/sendkeys.js b/tests/frontend/sendkeys.js new file mode 100644 index 000000000..b1cb50947 --- /dev/null +++ b/tests/frontend/sendkeys.js @@ -0,0 +1,465 @@ +// Cross-broswer implementation of text ranges and selections +// documentation: http://bililite.com/blog/2011/01/11/cross-browser-.and-selections/ +// Version: 1.1 +// Copyright (c) 2010 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(){ + +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){ + // Internet Explorer + 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._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'; +} + +// 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; + } +}; + + +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 = document.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(); +}; +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 (document.selection.type != 'Text') return [len, len]; // append to the end + var sel = document.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 +}; +IERange.prototype._nativeSetText = function (text, rng){ + 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('
'); + } +}; +// 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; +} +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; +} + +// 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()]; +}; +InputRange.prototype._nativeSelect = function (rng){ + this._el.setSelectionRange(rng[0], rng[1]); +}; +InputRange.prototype._nativeSelection = function(){ + return [this._el.selectionStart, this._el.selectionEnd]; +}; +InputRange.prototype._nativeGetText = function(rng){ + 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]); +}; +InputRange.prototype._nativeEOL = function(){ + this.text('\n'); +}; + +function W3CRange(){} +W3CRange.prototype = new Range(); +W3CRange.prototype._nativeRange = function (bounds){ + var rng = document.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){ + window.getSelection().removeAllRanges(); + window.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 (window.getSelection().rangeCount == 0) return [this.length(), this.length()]; // append to the end + var sel = window.getSelection().getRangeAt(0); + return [ + w3cstart(sel, rng), + w3cend (sel, rng) + ]; + } +W3CRange.prototype._nativeGetText = function (rng){ + return rng.toString(); +}; +W3CRange.prototype._nativeSetText = function (text, rng){ + rng.deleteContents(); + rng.insertNode (document.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 = document.createElement('br'); + br.setAttribute ('_moz_dirty', ''); // for Firefox + rng.insertNode (br); + rng.insertNode (document.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; +} +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); + } +} +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. + // * 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. +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; +} +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; +} + +function NothingRange(){} +NothingRange.prototype = new Range(); +NothingRange.prototype._nativeRange = function(bounds) { + return bounds || [0,this.length()]; +}; +NothingRange.prototype._nativeSelect = function (rng){ // do nothing +}; +NothingRange.prototype._nativeSelection = function(){ + return [0,0]; +}; +NothingRange.prototype._nativeGetText = function (rng){ + 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]); +}; +NothingRange.prototype._nativeEOL = function(){ + this.text('\n'); +}; + +})(); + +// 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 +// 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){ + 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(); + var x = '\n'.charCodeAt(0); + $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); + }, + '{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 339ee6d2e09963a88f9d8a77c0ffd73c17dda32e Mon Sep 17 00:00:00 2001 From: johnyma22 Date: Wed, 3 Oct 2012 21:25:31 +0100 Subject: [PATCH 5/7] working keystroke delete check --- tests/frontend/specs/keystroke_delete.js | 30 ++++++++---------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/tests/frontend/specs/keystroke_delete.js b/tests/frontend/specs/keystroke_delete.js index bd0702535..58a6b5bb9 100644 --- a/tests/frontend/specs/keystroke_delete.js +++ b/tests/frontend/specs/keystroke_delete.js @@ -11,38 +11,28 @@ describe("delete keystroke", function(){ //get the first text element out of the inner iframe var firstTextElement = $inner.find("div").first(); - //select this text element - testHelper.selectText(firstTextElement[0]); - // get the original length of this element var elementLength = firstTextElement.html().length; - console.log(elementLength); - //get the bold keystroke and click it - // var $deletekeystroke = testHelper.$getPadChrome().find(".keystrokeicon-delete"); + // get the original string value minus the last char + var originalTextValue = firstTextElement.text(); + originalTextValue = originalTextValue.substring(0, originalTextValue.length -1); - //put the cursor in the pad - var press = $.Event("keypress"); - press.ctrlKey = false; - press.which = 46; // 46 is delete key - firstTextElement.trigger(press); // simulate a keypress of delete - press.which = 37; // 37 is left key taking user to first place in pad. - firstTextElement.trigger(press); // simulate a keypress of left key + // simulate key presses to delete content + firstTextElement.sendkeys('{leftarrow}'); // simulate a keypress of the left arrow key + firstTextElement.sendkeys('{del}'); // simulate a keypress of delete //ace creates a new dom element when you press a keystroke, so just get the first text element again var newFirstTextElement = $inner.find("div").first(); - // is there a element now? - // var isdelete = newFirstTextElement.find("i").length === 1; - // get the new length of this element var newElementLength = newFirstTextElement.html().length; - console.log(newElementLength); - //expect it to be one char less + //expect it to be one char less in length expect(newElementLength).to.be((elementLength-1)); - //make sure the text hasn't changed - expect(newFirstTextElement.text()).to.eql(firstTextElement.text()); + //make sure the text has changed correctly + expect(newFirstTextElement.text()).to.eql(originalTextValue); + }); }); From 38ef46449b714c4dd4d459d6b3148bcc69814bc3 Mon Sep 17 00:00:00 2001 From: johnyma22 Date: Wed, 3 Oct 2012 21:26:48 +0100 Subject: [PATCH 6/7] add delete stroke test to index page of /tests/frontend --- tests/frontend/index.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/frontend/index.html b/tests/frontend/index.html index 8d5aa2df3..166fb6bc5 100644 --- a/tests/frontend/index.html +++ b/tests/frontend/index.html @@ -15,9 +15,10 @@ - + + From 24988d659c7db1014712f5d3b753cb0e9d5e004e Mon Sep 17 00:00:00 2001 From: johnyma22 Date: Wed, 3 Oct 2012 21:55:23 +0100 Subject: [PATCH 7/7] This test should work, Peter, why does this test not work? --- tests/frontend/index.html | 4 ++- .../specs/keystroke_urls_become_clickable.js | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/frontend/specs/keystroke_urls_become_clickable.js diff --git a/tests/frontend/index.html b/tests/frontend/index.html index 166fb6bc5..76af914ba 100644 --- a/tests/frontend/index.html +++ b/tests/frontend/index.html @@ -16,9 +16,11 @@ + + diff --git a/tests/frontend/specs/keystroke_urls_become_clickable.js b/tests/frontend/specs/keystroke_urls_become_clickable.js new file mode 100644 index 000000000..87de3bae9 --- /dev/null +++ b/tests/frontend/specs/keystroke_urls_become_clickable.js @@ -0,0 +1,29 @@ +describe("urls become clickable", function(){ + //create a new pad before each test run + beforeEach(function(cb){ + testHelper.newPad(cb); + }); + + it("adds a url and makes sure it's clickable", function() { + //get the inner iframe + var $inner = testHelper.$getPadInner(); + + //get the first text element out of the inner iframe + var firstTextElement = $inner.find("div").first(); + + // simulate key presses to delete content + firstTextElement.sendkeys('{selectall}'); // select all + firstTextElement.sendkeys('{del}'); // clear the first line + firstTextElement.sendkeys('http://etherpad.org'); // add a url to the pad + + //ace creates a new dom element when you press a keystroke, so just get the first text element again + var newFirstTextElement = $inner.find("div").first(); + + // is there a url class now? + var isURL = newFirstTextElement.find("href").length === 1; + + //expect it to be bold + expect(isURL).to.be(true); + + }); +});