diff --git a/lib/diaspora/taggable.rb b/lib/diaspora/taggable.rb index 0f0d43df8..fae6f177a 100644 --- a/lib/diaspora/taggable.rb +++ b/lib/diaspora/taggable.rb @@ -27,10 +27,11 @@ module Diaspora end def tag_strings - regex = /(?:^|\s)#(#{VALID_TAG_BODY})/ - matches = self.send(self.class.field_with_tags).scan(regex).map do |match| - match.last - end + regex = /(?:^|\s)#([\w-]+|<3)/ + matches = self. + send( self.class.field_with_tags ). + scan(regex). + map { |match| match[0] } unique_matches = matches.inject(Hash.new) do |h,element| h[element.downcase] = element unless h[element.downcase] h diff --git a/spec/shared_behaviors/taggable.rb b/spec/shared_behaviors/taggable.rb index 0b750b38e..c84b7d629 100644 --- a/spec/shared_behaviors/taggable.rb +++ b/spec/shared_behaviors/taggable.rb @@ -44,12 +44,51 @@ describe Diaspora::Taggable do describe '#tag_strings' do it 'returns a string for every #thing' do str = '#what #hey #that"smybike. #@hey ##boo # #THATWASMYBIKE #vöglein #hey#there #135440we #abc/23 ### #h!gh #ok? #see: #re:publica' - arr = ['what', 'hey', 'that', 'THATWASMYBIKE', 'vöglein', '135440we', 'abc', 'h!gh', 'ok', 'see', 're:publica'] + arr = ['what', 'hey', 'that', 'THATWASMYBIKE', 'vöglein', '135440we', 'abc', 'h', 'ok', 'see', 're'] @object.send(@object.class.field_with_tags_setter, str) @object.tag_strings.should =~ arr end + it 'extracts tags despite surrounding text' do + expected = { + '#foobar' => 'foobar', + '#foocar
' => 'foocar', + '#fooo@oo' => 'fooo', + '#num3ric hash tags' => 'num3ric', + '#12345 tag' => '12345', + '#12cde tag' => '12cde', + '#abc45 tag' => 'abc45', + '#<3' => '<3', + '#<4' => nil, + 'test#foo test' => nil, + 'test.#joo bar' => nil, + 'test #foodar test' => 'foodar', + 'test #foofar
test ' => 'foofar', + 'test #gooo@oo test' => 'gooo', + 'test #<3 test' => '<3', + 'test #foo-test test' => 'foo-test', + 'test #hoo' => 'hoo', + 'test #two_word tags' => 'two_word', + 'test #three_word_tags' => 'three_word_tags', + '#terminal_underscore_' => 'terminal_underscore_', + '#terminalunderscore_' => 'terminalunderscore_', + '#_initialunderscore' => '_initialunderscore', + '#_initial_underscore' => '_initial_underscore', + '#terminalhyphen-' => 'terminalhyphen-', + '#terminal-hyphen-' => 'terminal-hyphen-', + '#terminalhyphen- tag' => 'terminalhyphen-', + '#-initialhyphen' => '-initialhyphen', + '#-initialhyphen tag' => '-initialhyphen', + '#-initial-hyphen' => '-initial-hyphen', + } + + expected.each do |text,hashtag| + @object.send @object.class.field_with_tags_setter, text + @object.tag_strings.should == [hashtag].compact + end + end + it 'returns no duplicates' do str = '#what #what #what #whaaaaaaaaaat' arr = ['what','whaaaaaaaaaat']