From 694dc7d80b3d8598b5d99059c2bc732dd98b613e Mon Sep 17 00:00:00 2001 From: Hincu Petru Date: Thu, 13 Mar 2014 21:39:12 +0000 Subject: [PATCH] Check if the hashtag is a link in function .hashtagify --- .../javascripts/app/helpers/text_formatter.js | 14 +++++++++----- .../app/helpers/text_formatter_spec.js | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/app/helpers/text_formatter.js b/app/assets/javascripts/app/helpers/text_formatter.js index 3e122d87a..d5ae68444 100644 --- a/app/assets/javascripts/app/helpers/text_formatter.js +++ b/app/assets/javascripts/app/helpers/text_formatter.js @@ -108,11 +108,15 @@ $(function() { }; textFormatter.hashtagify = function hashtagify(text){ - var utf8WordCharcters =/(\s|^|>)#([\u0080-\uFFFF|\w|-]+|<3)/g - return text.replace(utf8WordCharcters, function(hashtag, preceeder, tagText) { - return preceeder + "#" + tagText + "" - }) + var utf8WordCharcters =/(]*>.*?<\/a>)|(\s|^|>)#([\u0080-\uFFFF|\w|-]+|<3)/g; + + return text.replace(utf8WordCharcters, function(result, linkTag, preceeder, tagText) { + if(linkTag) + return linkTag; + else + return preceeder + "#" + tagText + ""; + }); }; textFormatter.mentionify = function mentionify(text, mentions) { diff --git a/spec/javascripts/app/helpers/text_formatter_spec.js b/spec/javascripts/app/helpers/text_formatter_spec.js index 29aec511e..334321d5a 100644 --- a/spec/javascripts/app/helpers/text_formatter_spec.js +++ b/spec/javascripts/app/helpers/text_formatter_spec.js @@ -238,6 +238,25 @@ describe("app.helpers.textFormatter", function(){ expect(formattedText).toContain("/tags/parties") }) + + it("doesn't create tag if the text is a link", function(){ + var tags = ['diaspora', 'twitter', 'hrabrahabr']; + + var text = $('', { href: 'http://me.co' }).html('#me')[0].outerHTML; + _.each(tags, function(tagName){ + text += ' #'+tagName+','; + }); + text += 'I love'; + + var formattedText = this.formatter.hashtagify(text); + var wrapper = $('
').html(formattedText); + + expect(wrapper.find("a[href='http://me.co']").text()).toContain('#me'); + _.each(tags, function(tagName){ + expect(wrapper.find("a[href='/tags/"+tagName+"']").text()).toContain('#'+tagName); + }); + + }) }) })