gemojione 3.1.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "sprite-factory"
27
+ spec.add_development_dependency 'rmagick'
26
28
  end
@@ -17,6 +17,7 @@ module Gemojione
17
17
  @asset_path = nil
18
18
  @default_size = nil
19
19
  @use_svg = false
20
+ @use_sprite = false
20
21
 
21
22
  @escaper = defined?(EscapeUtils) ? EscapeUtils : CGI
22
23
 
@@ -52,6 +53,14 @@ module Gemojione
52
53
  @use_svg = useit
53
54
  end
54
55
 
56
+ def self.use_sprite
57
+ @use_sprite
58
+ end
59
+
60
+ def self.use_sprite=(useit)
61
+ @use_sprite = useit
62
+ end
63
+
55
64
  def self.image_url_for_name(name)
56
65
  emoji = index.find_by_name(name)
57
66
  "#{asset_host}#{ File.join(asset_path, emoji['unicode']) }.#{ use_svg ? 'svg' : 'png' }"
@@ -63,7 +72,12 @@ module Gemojione
63
72
  end
64
73
 
65
74
  def self.image_tag_for_moji(moji)
66
- %Q{<img alt="#{moji}" class="emoji" src="#{ image_url_for_unicode_moji(moji) }"#{ default_size ? ' style="width: '+default_size+';"' : '' }>}
75
+ if use_sprite
76
+ emoji = index.find_by_moji(moji)
77
+ %Q{<span class="emojione emojione-#{emoji['unicode'].to_s.downcase}" alt="#{ moji }" title="#{ moji }">#{ moji }</span>}
78
+ else
79
+ %Q{<img alt="#{moji}" class="emoji" src="#{ image_url_for_unicode_moji(moji) }"#{ default_size ? ' style="width: '+default_size+';"' : '' }>}
80
+ end
67
81
  end
68
82
 
69
83
  def self.replace_unicode_moji_with_images(string)
@@ -98,6 +112,25 @@ module Gemojione
98
112
  safe_string
99
113
  end
100
114
 
115
+ def self.replace_ascii_moji_with_images(string)
116
+ return string unless string
117
+ unless string.match(index.ascii_moji_regex)
118
+ return safe_string(string)
119
+ end
120
+
121
+ string.gsub!(index.ascii_moji_regex) do |code|
122
+ moji = index.find_by_ascii(code)['moji']
123
+ Gemojione.image_tag_for_moji(moji)
124
+ end
125
+
126
+ unless string.respond_to?(:html_safe?) && string.html_safe?
127
+ safe_string = CGI::unescapeElement(CGI.escape_html(string), %w[span img])
128
+ end
129
+ safe_string = safe_string.html_safe if safe_string.respond_to?(:html_safe)
130
+
131
+ safe_string
132
+ end
133
+
101
134
  def self.safe_string(string)
102
135
  if string.respond_to?(:html_safe?) && string.html_safe?
103
136
  string
@@ -117,4 +150,8 @@ module Gemojione
117
150
  def self.images_path
118
151
  File.expand_path("../assets/#{ use_svg ? 'svg' : 'png' }", File.dirname(__FILE__))
119
152
  end
153
+
154
+ def self.sprites_path
155
+ File.expand_path("../assets/sprites", File.dirname(__FILE__))
156
+ end
120
157
  end
@@ -41,6 +41,7 @@ module Gemojione
41
41
 
42
42
  @emoji_code_regex = /#{@emoji_by_code.keys.map{|ec| Regexp.escape(ec)}.join('|')}/
43
43
  @emoji_moji_regex = /#{@emoji_by_moji.keys.map{|ec| Regexp.escape(ec)}.join('|')}/
44
+ @emoji_ascii_regex = /#{@emoji_by_ascii.keys.map{|ec| Regexp.escape(ec)}.join('|')}/
44
45
  end
45
46
 
46
47
  def find_by_moji(moji)
@@ -66,5 +67,9 @@ module Gemojione
66
67
  def shortname_moji_regex
67
68
  @emoji_code_regex
68
69
  end
70
+
71
+ def ascii_moji_regex
72
+ @emoji_ascii_regex
73
+ end
69
74
  end
70
75
  end
@@ -20,4 +20,5 @@ namespace :gemojione do
20
20
  FileUtils.cp_r(asset, target_dir, verbose: true, preserve: false)
21
21
  end
22
22
  end
23
+
23
24
  end
@@ -1,3 +1,3 @@
1
1
  module Gemojione
2
- VERSION = "3.1.0"
2
+ VERSION = "3.2.0"
3
3
  end
@@ -71,6 +71,12 @@ describe Gemojione do
71
71
  assert_equal '<img alt="🌀" class="emoji" src="http://localhost:3000/1F300.png" style="width: 42px;">', Gemojione.image_tag_for_moji('🌀')
72
72
  Gemojione.default_size=nil
73
73
  end
74
+
75
+ it 'should generate spritesheet tag' do
76
+ with_emoji_config(:use_sprite, true) do
77
+ assert_equal "<span class=\"emojione emojione-1f300\" alt=\"🌀\" title=\"🌀\">🌀</span>", Gemojione.image_tag_for_moji('🌀')
78
+ end
79
+ end
74
80
  end
75
81
 
76
82
  describe "replace_unicode_moji_with_images" do
@@ -89,6 +95,14 @@ describe Gemojione do
89
95
  assert_equal "I <img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/2764.png\"> Emoji", replaced_string
90
96
  end
91
97
 
98
+ it 'should replace unicode moji with span tag for spritesheet' do
99
+ with_emoji_config(:use_sprite, true) do
100
+ base_string = "I ❤ Emoji"
101
+ replaced_string = Gemojione.replace_unicode_moji_with_images(base_string)
102
+ assert_equal "I <span class=\"emojione emojione-2764\" alt=\"❤\" title=\"❤\">❤</span> Emoji", replaced_string
103
+ end
104
+ end
105
+
92
106
  it 'should escape regex breaker mojis' do
93
107
  assert_equal "<img alt=\"*⃣\" class=\"emoji\" src=\"http://localhost:3000/002A-20E3.png\">", Gemojione.replace_unicode_moji_with_images('*⃣')
94
108
  end
@@ -149,6 +163,13 @@ describe Gemojione do
149
163
  end
150
164
 
151
165
  describe 'replace_named_moji_with_images' do
166
+ it 'should replace with span tag for spritesheet' do
167
+ with_emoji_config(:use_sprite, true) do
168
+ base_string = "I :heart: Emoji"
169
+ replaced_string = Gemojione.replace_named_moji_with_images(base_string)
170
+ assert_equal "I <span class=\"emojione emojione-2764\" alt=\"❤\" title=\"❤\">❤</span> Emoji", replaced_string
171
+ end
172
+ end
152
173
 
153
174
  it 'should return original string without emoji' do
154
175
  assert_equal 'foo', Gemojione.replace_named_moji_with_images('foo')
@@ -217,6 +238,30 @@ describe Gemojione do
217
238
 
218
239
  assert_equal "Content", replaced_string
219
240
  end
241
+
242
+ it 'should escape non html_safe? strings with ascii' do
243
+ string = HtmlSafeString.new('Moji is <3 XSS<script> attacks are not')
244
+
245
+ replaced_string = string.stub(:html_safe?, false) do
246
+ Gemojione.replace_ascii_moji_with_images(string)
247
+ end
248
+
249
+ assert_equal "Moji is <img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/2764.png\"> XSS&lt;script&gt; attacks are not", replaced_string
250
+ end
251
+ end
252
+ end
253
+
254
+ describe "replace_ascii_moji_with_images" do
255
+ it 'should replace ascii moji with img tag' do
256
+ replaced_string = Gemojione.replace_ascii_moji_with_images("Emoji is :-)")
257
+ assert_equal "Emoji is <img alt=\"😄\" class=\"emoji\" src=\"http://localhost:3000/1F604.png\">", replaced_string
258
+ end
259
+
260
+ it 'should replace ascii moji with span tag for sprite' do
261
+ with_emoji_config(:use_sprite, true) do
262
+ replaced_string = Gemojione.replace_ascii_moji_with_images("Emoji is :-)")
263
+ assert_equal "Emoji is <span class=\"emojione emojione-1f604\" alt=\"😄\" title=\"😄\">😄</span>", replaced_string
264
+ end
220
265
  end
221
266
  end
222
267
 
@@ -239,6 +284,16 @@ describe Gemojione do
239
284
  end
240
285
  end
241
286
 
287
+ describe "sprites_path" do
288
+ it "should return sprite stylesheet" do
289
+ assert File.exist?("#{Gemojione.sprites_path}/emojione.sprites.scss")
290
+ end
291
+
292
+ it "should return PNG sprites" do
293
+ assert File.exist?("#{Gemojione.sprites_path}/emojione.sprites.png")
294
+ end
295
+ end
296
+
242
297
  class HtmlSafeString < String
243
298
  def initialize(*); super; end
244
299
  def html_safe; self; end
@@ -255,4 +310,4 @@ describe Gemojione do
255
310
  Gemojione.send("#{name}=", original_value)
256
311
  end
257
312
  end
258
- end
313
+ 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: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Klabnik
@@ -10,62 +10,90 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-07-30 00:00:00.000000000 Z
13
+ date: 2016-08-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - '>='
19
+ - - ">="
20
20
  - !ruby/object:Gem::Version
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - '>='
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: '0'
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - ~>
33
+ - - "~>"
34
34
  - !ruby/object:Gem::Version
35
35
  version: '1.3'
36
36
  type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - ~>
40
+ - - "~>"
41
41
  - !ruby/object:Gem::Version
42
42
  version: '1.3'
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: rake
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - '>='
47
+ - - ">="
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  type: :development
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - '>='
54
+ - - ">="
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  - !ruby/object:Gem::Dependency
58
58
  name: minitest
59
59
  requirement: !ruby/object:Gem::Requirement
60
60
  requirements:
61
- - - '>='
61
+ - - ">="
62
62
  - !ruby/object:Gem::Version
63
63
  version: '0'
64
64
  type: :development
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
- - - '>='
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: sprite-factory
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: rmagick
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
69
97
  - !ruby/object:Gem::Version
70
98
  version: '0'
71
99
  description: A gem for EmojiOne
@@ -77,9 +105,9 @@ executables: []
77
105
  extensions: []
78
106
  extra_rdoc_files: []
79
107
  files:
80
- - .gitignore
81
- - .rvmrc
82
- - .travis.yml
108
+ - ".gitignore"
109
+ - ".rvmrc"
110
+ - ".travis.yml"
83
111
  - CHANGELOG.md
84
112
  - Gemfile
85
113
  - LICENSE.txt
@@ -1879,6 +1907,8 @@ files:
1879
1907
  - assets/png/303D.png
1880
1908
  - assets/png/3297.png
1881
1909
  - assets/png/3299.png
1910
+ - assets/sprites/emojione.sprites.png
1911
+ - assets/sprites/emojione.sprites.scss
1882
1912
  - assets/svg/0023-20E3.svg
1883
1913
  - assets/svg/002A-20E3.svg
1884
1914
  - assets/svg/0030-20E3.svg
@@ -3695,17 +3725,17 @@ require_paths:
3695
3725
  - lib
3696
3726
  required_ruby_version: !ruby/object:Gem::Requirement
3697
3727
  requirements:
3698
- - - '>='
3728
+ - - ">="
3699
3729
  - !ruby/object:Gem::Version
3700
3730
  version: '0'
3701
3731
  required_rubygems_version: !ruby/object:Gem::Requirement
3702
3732
  requirements:
3703
- - - '>='
3733
+ - - ">="
3704
3734
  - !ruby/object:Gem::Version
3705
3735
  version: '0'
3706
3736
  requirements: []
3707
3737
  rubyforge_project:
3708
- rubygems_version: 2.0.3
3738
+ rubygems_version: 2.4.8
3709
3739
  signing_key:
3710
3740
  specification_version: 4
3711
3741
  summary: A gem for EmojiOne