diff --git a/public/javascripts/jquery.autocomplete-custom.js b/public/javascripts/jquery.autocomplete-custom.js index e210e8cefd26a6662574faec76329769e570d46a..5208f5822f2714a486cfeef22a4d273c6cd4257d 100644 --- a/public/javascripts/jquery.autocomplete-custom.js +++ b/public/javascripts/jquery.autocomplete-custom.js @@ -234,7 +234,7 @@ $.Autocompleter = function(input, options) { previousValue = currentValue; - currentValue = options.searchTermFromValue(currentValue); + currentValue = options.searchTermFromValue(currentValue, $input[0].selectionStart); if ( currentValue.length >= options.minChars) { $input.addClass(options.loadingClass); if (!options.matchCase) diff --git a/public/javascripts/publisher.js b/public/javascripts/publisher.js index ceb114df23f88d42c9f9d30a61df30b5b73075ca..8a3ded14edd6466f90678a20ede9cbd0681bfc31 100644 --- a/public/javascripts/publisher.js +++ b/public/javascripts/publisher.js @@ -54,11 +54,21 @@ var Publisher = { textarea.val(formatted); }, - searchTermFromValue: function(value) + searchTermFromValue: function(value, cursorIndex) { - matches = value.match(/@(.+)/); + var atLocation = value.lastIndexOf('@', cursorIndex); + if(atLocation == -1){return '';} + var nextAt = value.indexOf('@', cursorIndex+1); + + if(nextAt == -1){nextAt = value.length;} + if(atLocation < 2){ + atLocation = 0; + }else{ atLocation = atLocation -2 } + + relevantString = value.slice(atLocation, nextAt).replace(/\s+$/,""); + matches = relevantString.match(/(^|\s)@(.+)/); if(matches){ - return matches[1]; + return matches[2]; }else{ return ''; } diff --git a/spec/javascripts/publisher-spec.js b/spec/javascripts/publisher-spec.js index d4e82fd41dc4960c4577224eb3c15ea5893f6b97..166d64db7e9e34985a35741e1ad415b20f5e76eb 100644 --- a/spec/javascripts/publisher-spec.js +++ b/spec/javascripts/publisher-spec.js @@ -78,24 +78,46 @@ describe("Publisher", function() { Publisher.form().find('#status_message_fake_message').val()); }); }); + describe("input", function(){ + beforeEach(function(){ + spec.loadFixture('aspects_index_prefill'); + }); + it("returns the status_message_fake_message textarea", function(){ + expect(Publisher.input()[0].id).toBe('status_message_fake_message'); + expect(Publisher.input().length).toBe(1); + }); + }); describe("autocompletion", function(){ describe("searchTermFromValue", function(){ var func; beforeEach(function(){func = Publisher.autocompletion.searchTermFromValue;}); - it("returns everything after an @", function(){ - expect(func('not @dan grip')).toBe('dan grip'); + it("returns nothing if the cursor is before the @", function(){ + expect(func('not @dan grip', 2)).toBe(''); + }); + it("returns everything after an @ if the cursor is a word after that @", function(){ + expect(func('not @dan grip', 13)).toBe('dan grip'); + }); + it("returns everything after an @ if the cursor is after that @", function(){ + expect(func('not @dan grip', 7)).toBe('dan grip'); + }); + + it("returns everything after an @ at the start of the line", function(){ + expect(func('@dan grip', 9)).toBe('dan grip'); }); it("returns nothing if there is no @", function(){ - expect(func('dan')).toBe(''); + expect(func('dan', 3)).toBe(''); }); it("returns nothing for just an @", function(){ - expect(func('@')).toBe(''); - }); - it("returns everything after the last @", function(){ - expect(func('@siojfoi @dan"')).toBe('dan"'); + expect(func('@', 1)).toBe(''); }); it("returns nothing if there are letters preceding the @", function(){ - expect(func('ioj@asdo')).toBe(''); + expect(func('ioj@asdo', 8)).toBe(''); + }); + it("returns everything between @s if there are 2 @s and the cursor is between them", function(){ + expect(func('@asdpo aoisdj @asodk', 8)).toBe('asdpo aoisdj'); + }); + it("returns everything after the 2nd @ if there are 2 @s and the cursor after them", function(){ + expect(func('@asod asdo @asd asok', 15)).toBe('asd asok'); }); }); }); diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index f142bd8f1967181476ec1b4ade2f0c60e2ed4656..c812d38c27294c8c23a84836570eab5bb123f842 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -19,6 +19,7 @@ src_files: - public/javascripts/vendor/Mustache.js - public/javascripts/vendor/timeago.js - public/javascripts/vendor/facebox.js + - public/javascripts/jquery.autocomplete-custom.js - public/javascripts/diaspora.js - public/javascripts/widgets/alert.js - public/javascripts/widgets/embedder.js