gemojione 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5c5ad07b1134af205ee5e8717c3303251dd5c1e
4
- data.tar.gz: 911e76bbedddf4e8871b762ee33d3b5acb05e35b
3
+ metadata.gz: 5dd7d95071886d3fbb74ea8bc4d239ece9111d01
4
+ data.tar.gz: 7bbd1128a1b3ec633a3c69fba5332ae2e6a1f44b
5
5
  SHA512:
6
- metadata.gz: ec865da778966185912b434cd925c702f23769e4a8234148f758188c081a1d2642f6267ddb8ba41cf3fe3251780899d121081c319ee083aa4cf8c68d2b3da264
7
- data.tar.gz: ebbee9925229ebaa42a4ec0ef44cd255da2a6c7665392c104f3bc13ecd171d52ecf2b65fa1a2a51832e87e27f44108ecaad096769dd331bba8ebe7a1d10d0aee
6
+ metadata.gz: afc4cbd9eceac9b0c6f4255a4f515f15fea5a54a5bc6d770caa58d0745a9c21b1e73bd78ce32a4427a7b920d07f211a8fb2135f75a56f07103cffa6d389d2467
7
+ data.tar.gz: 3ecf09d1f4f53f41b185b752735ed5ae2c788be3911cfe8144b5de542b4c25bd08bd87854335f69e9ae8aaf6d1ae99f3569760ed1d8a2077a9c32973ece2d473
data/README.md CHANGED
@@ -45,6 +45,9 @@ Image Replacement APIs:
45
45
  > Gemojione.replace_unicode_moji_with_images('I ❤ Emoji')
46
46
  => "I <img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/assets/emoji/2764.png\"> Emoji"
47
47
 
48
+ > Gemojione.replace_named_moji_with_images('I :heart: Emoji')
49
+ => "I <img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/assets/emoji/2764.png\"> Emoji"
50
+
48
51
  > Gemojione.image_url_for_unicode_moji('❤')
49
52
  => "http://localhost:3000/assets/emoji/2764.png"
50
53
 
@@ -58,6 +58,23 @@ module Gemojione
58
58
  safe_string
59
59
  end
60
60
 
61
+ def self.replace_named_moji_with_images(string)
62
+ return string unless string
63
+ unless string.match(index.shortname_moji_regex)
64
+ return safe_string(string)
65
+ end
66
+
67
+ safe_string = safe_string(string.dup)
68
+ safe_string.gsub!(index.shortname_moji_regex) do |code|
69
+ name = code.tr(':','')
70
+ moji = index.find_by_name(name)['moji']
71
+ %Q{<img alt="#{moji}" class="emoji" src="#{ image_url_for_name(name) }">}
72
+ end
73
+ safe_string = safe_string.html_safe if safe_string.respond_to?(:html_safe)
74
+
75
+ safe_string
76
+ end
77
+
61
78
  def self.safe_string(string)
62
79
  if string.respond_to?(:html_safe?) && string.html_safe?
63
80
  string
@@ -9,6 +9,7 @@ module Gemojione
9
9
  @emoji_by_name = {}
10
10
  @emoji_by_moji = {}
11
11
  @emoji_by_ascii = {}
12
+ @emoji_by_code = {}
12
13
 
13
14
  emoji_list.each do |key, emoji_hash|
14
15
 
@@ -25,9 +26,14 @@ module Gemojione
25
26
  @emoji_by_ascii[emoji_ascii] = emoji_hash if emoji_ascii
26
27
  end
27
28
 
29
+ code = emoji_hash['shortname']
30
+ @emoji_by_code[code] = emoji_hash if code
31
+
28
32
  moji = emoji_hash['moji']
29
33
  @emoji_by_moji[moji] = emoji_hash if moji
30
34
  end
35
+
36
+ @emoji_code_regex = /#{@emoji_by_code.keys.join('|')}/
31
37
  @emoji_moji_regex = /#{@emoji_by_moji.keys.join('|')}/
32
38
  end
33
39
 
@@ -47,6 +53,10 @@ module Gemojione
47
53
  @emoji_moji_regex
48
54
  end
49
55
 
56
+ def shortname_moji_regex
57
+ @emoji_code_regex
58
+ end
59
+
50
60
  def images_path
51
61
  File.expand_path("../../assets/images", File.dirname(__FILE__))
52
62
  end
@@ -1,3 +1,3 @@
1
1
  module Gemojione
2
- VERSION = "2.4.0"
2
+ VERSION = "2.5.0"
3
3
  end
@@ -114,6 +114,78 @@ describe Gemojione do
114
114
  end
115
115
  end
116
116
 
117
+ describe 'replace_named_moji_with_images' do
118
+
119
+ it 'should return original string without emoji' do
120
+ assert_equal 'foo', Gemojione.replace_named_moji_with_images('foo')
121
+ end
122
+
123
+ it 'should escape html in non html_safe aware strings' do
124
+ replaced_string = Gemojione.replace_named_moji_with_images(':heart:<script>')
125
+ assert_equal "<img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/2764.png\">&lt;script&gt;", replaced_string
126
+ end
127
+
128
+ it 'should replace coded moji with img tag' do
129
+ base_string = "I :heart: Emoji"
130
+ replaced_string = Gemojione.replace_named_moji_with_images(base_string)
131
+ assert_equal "I <img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/2764.png\"> Emoji", replaced_string
132
+ end
133
+
134
+ it 'should handle nil string' do
135
+ assert_equal nil, Gemojione.replace_named_moji_with_images(nil)
136
+ end
137
+
138
+ describe 'with html_safe buffer' do
139
+ it 'should escape non html_safe? strings in emoji' do
140
+ string = HtmlSafeString.new(':heart:<script>')
141
+
142
+ replaced_string = string.stub(:html_safe?, false) do
143
+ Gemojione.replace_named_moji_with_images(string)
144
+ end
145
+
146
+ assert_equal "<img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/2764.png\">&lt;script&gt;", replaced_string
147
+ end
148
+
149
+ it 'should escape non html_safe? strings in all strings' do
150
+ string = HtmlSafeString.new('XSS<script>')
151
+
152
+ replaced_string = string.stub(:html_safe?, false) do
153
+ Gemojione.replace_named_moji_with_images(string)
154
+ end
155
+
156
+ assert_equal "XSS&lt;script&gt;", replaced_string
157
+ end
158
+
159
+ it 'should not escape html_safe strings' do
160
+ string = HtmlSafeString.new(':heart:<a href="harmless">')
161
+
162
+ replaced_string = string.stub(:html_safe?, true) do
163
+ Gemojione.replace_named_moji_with_images(string)
164
+ end
165
+
166
+ assert_equal "<img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/2764.png\"><a href=\"harmless\">", replaced_string
167
+ end
168
+
169
+ it 'should always return an html_safe string for emoji' do
170
+ string = HtmlSafeString.new(':heart:')
171
+ replaced_string = string.stub(:html_safe, 'safe_buffer') do
172
+ Gemojione.replace_named_moji_with_images(string)
173
+ end
174
+
175
+ assert_equal "safe_buffer", replaced_string
176
+ end
177
+
178
+ it 'should always return an html_safe string for any string' do
179
+ string = HtmlSafeString.new('Content')
180
+ replaced_string = string.stub(:html_safe, 'safe_buffer') do
181
+ Gemojione.replace_named_moji_with_images(string)
182
+ end
183
+
184
+ assert_equal "Content", replaced_string
185
+ end
186
+ end
187
+ end
188
+
117
189
  class HtmlSafeString < String
118
190
  def initialize(*); super; end
119
191
  def html_safe; self; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gemojione
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Klabnik
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-06-02 00:00:00.000000000 Z
13
+ date: 2016-06-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json