emoji 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +15 -0
- data/CHANGELOG.md +6 -0
- data/README.md +7 -0
- data/emoji.gemspec +0 -1
- data/lib/emoji.rb +52 -3
- data/lib/emoji/index.rb +16 -13
- data/lib/emoji/railtie.rb +2 -1
- data/lib/emoji/version.rb +1 -1
- data/test/emoji_test.rb +47 -3
- metadata +4 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a107032c52334744a9ea531dc52687ad3734542
|
4
|
+
data.tar.gz: 02a61e602aafe01ed88f7f536f35e06846463854
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 109658ab8eb1bf45b80cbbe27e3eed081bb702d68b16f6e2a05e95a4de14811556ad915ffbc6e23fc5e19d37d335d8b77ae2f47554329dd1f4e149077ab09a73
|
7
|
+
data.tar.gz: 1bdfd0c4687546ff3ba1a6676e93eab048471ac0e19220c576d5b1f9841db71279c97e4a078680938a5cccc6ac9c994c29737d4250c6689875e31b830603e902
|
data/.travis.yml
ADDED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
A Ruby gem. For emoji. For everyone. :heart:
|
4
4
|
|
5
|
+
[](https://travis-ci.org/steveklabnik/emoji)
|
6
|
+
|
5
7
|
This gem exposes the [Phantom Open Emoji library](https://github.com/Genshin/PhantomOpenEmoji) unicode/image assets and APIs for working with them.
|
6
8
|
|
7
9
|
Easily lookup emoji name, unicode character, or image assets and convert emoji representations.
|
@@ -67,6 +69,7 @@ Default configuration integrates with Rails, but you can change it with an initi
|
|
67
69
|
# config/initializers/emoji.rb
|
68
70
|
Emoji.asset_host = "emoji.cdn.com"
|
69
71
|
Emoji.asset_path = '/assets/emoji'
|
72
|
+
Emoji.use_plaintext_alt_tags = true
|
70
73
|
```
|
71
74
|
|
72
75
|
String Helper Methods:
|
@@ -109,6 +112,10 @@ gem 'escape_utils'
|
|
109
112
|
* [@mikowitz](https://github.com/mikowitz): `String` ext helpers
|
110
113
|
* [@semanticart](https://github.com/semanticart): Cleanup/Ruby 1.9.3 support
|
111
114
|
* [@parndt](https://github.com/parndt): README doc fixes
|
115
|
+
* [@neuegram](https://github.com/neuegram): XSS Security Audit
|
116
|
+
* [@tumes](https://github.com/tumes): Plaintext Emoji Alt Tags
|
117
|
+
* [@poporul](https://github.com/poporul): Emoji::Index Refactoring
|
118
|
+
* [@dilkhush](https://github.com/dilkhush): Emoji::Index Search by Unicode
|
112
119
|
|
113
120
|
## Contributing
|
114
121
|
|
data/emoji.gemspec
CHANGED
data/lib/emoji.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'emoji/version'
|
2
2
|
require 'json'
|
3
|
+
require 'uri'
|
3
4
|
|
4
5
|
# Optionally load EscapeUtils if it's available
|
5
6
|
begin
|
@@ -15,14 +16,48 @@ require "emoji/railtie" if defined?(Rails)
|
|
15
16
|
module Emoji
|
16
17
|
@asset_host = nil
|
17
18
|
@asset_path = nil
|
19
|
+
@use_plaintext_alt_tags = nil
|
18
20
|
@escaper = defined?(EscapeUtils) ? EscapeUtils : CGI
|
19
21
|
|
20
22
|
def self.asset_host
|
21
23
|
@asset_host || 'http://localhost:3000'
|
22
24
|
end
|
23
25
|
|
24
|
-
def self.asset_host=(
|
25
|
-
@asset_host =
|
26
|
+
def self.asset_host=(asset_host)
|
27
|
+
@asset_host = parse_and_validate_asset_host(asset_host)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.parse_and_validate_asset_host(asset_host_spec)
|
31
|
+
begin
|
32
|
+
uri = URI.parse(asset_host_spec)
|
33
|
+
scheme_string = extract_uri_scheme_string(asset_host_spec, uri)
|
34
|
+
hostname = uri.hostname || uri.path
|
35
|
+
port_string = extract_port_string(uri)
|
36
|
+
|
37
|
+
return "#{ scheme_string }#{ hostname }#{ port_string }"
|
38
|
+
rescue URI::InvalidURIError
|
39
|
+
raise 'Invalid Emoji.asset_host, should be a hostname or URL prefix'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.extract_uri_scheme_string(asset_host_spec, uri)
|
44
|
+
# Special Case for Protocol Relative Scheme: //hostname.com/
|
45
|
+
if asset_host_spec.size >= 2 && asset_host_spec[0..1] == '//'
|
46
|
+
return '//'
|
47
|
+
end
|
48
|
+
|
49
|
+
# Extract Protocol from asset_host_spec URI or default to HTTP
|
50
|
+
scheme = uri.scheme || 'http'
|
51
|
+
|
52
|
+
"#{ scheme }://"
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.extract_port_string(uri)
|
56
|
+
return nil unless uri.port
|
57
|
+
return nil if uri.port == 80 && uri.scheme == 'http'
|
58
|
+
return nil if uri.port == 443 && uri.scheme == 'https'
|
59
|
+
|
60
|
+
return ":#{uri.port}"
|
26
61
|
end
|
27
62
|
|
28
63
|
def self.asset_path
|
@@ -33,6 +68,14 @@ module Emoji
|
|
33
68
|
@asset_path = path
|
34
69
|
end
|
35
70
|
|
71
|
+
def self.use_plaintext_alt_tags
|
72
|
+
@use_plaintext_alt_tags || false
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.use_plaintext_alt_tags=(bool)
|
76
|
+
@use_plaintext_alt_tags = bool
|
77
|
+
end
|
78
|
+
|
36
79
|
def self.image_url_for_name(name)
|
37
80
|
"#{asset_host}#{ File.join(asset_path, name) }.png"
|
38
81
|
end
|
@@ -42,6 +85,12 @@ module Emoji
|
|
42
85
|
image_url_for_name(emoji['name'])
|
43
86
|
end
|
44
87
|
|
88
|
+
def self.alt_tag_for_moji(moji)
|
89
|
+
return moji unless use_plaintext_alt_tags
|
90
|
+
emoji = index.find_by_moji(moji)
|
91
|
+
emoji['name']
|
92
|
+
end
|
93
|
+
|
45
94
|
def self.replace_unicode_moji_with_images(string)
|
46
95
|
return string unless string
|
47
96
|
unless string.match(index.unicode_moji_regex)
|
@@ -50,7 +99,7 @@ module Emoji
|
|
50
99
|
|
51
100
|
safe_string = safe_string(string.dup)
|
52
101
|
safe_string.gsub!(index.unicode_moji_regex) do |moji|
|
53
|
-
%Q{<img alt="#{moji}" class="emoji" src="#{ image_url_for_unicode_moji(moji) }">}
|
102
|
+
%Q{<img alt="#{alt_tag_for_moji(moji)}" class="emoji" src="#{ image_url_for_unicode_moji(moji) }">}
|
54
103
|
end
|
55
104
|
safe_string = safe_string.html_safe if safe_string.respond_to?(:html_safe)
|
56
105
|
|
data/lib/emoji/index.rb
CHANGED
@@ -1,13 +1,24 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
1
3
|
module Emoji
|
2
4
|
class Index
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
attr_reader :unicode_moji_regex
|
8
|
+
|
9
|
+
def_delegator :@emoji_by_moji, :[], :find_by_moji
|
10
|
+
def_delegator :@emoji_by_name, :[], :find_by_name
|
11
|
+
def_delegator :@emoji_by_unicode, :[], :find_by_unicode
|
12
|
+
|
3
13
|
def initialize(emoji_list=nil)
|
4
14
|
emoji_list ||= begin
|
5
15
|
emoji_json = File.read(File.absolute_path(File.dirname(__FILE__) + '/../../config/index.json'))
|
6
16
|
JSON.parse(emoji_json)
|
7
17
|
end
|
8
|
-
|
18
|
+
|
9
19
|
@emoji_by_name = {}
|
10
20
|
@emoji_by_moji = {}
|
21
|
+
@emoji_by_unicode = {}
|
11
22
|
|
12
23
|
emoji_list.each do |emoji_hash|
|
13
24
|
name = emoji_hash['name']
|
@@ -15,20 +26,12 @@ module Emoji
|
|
15
26
|
|
16
27
|
moji = emoji_hash['moji']
|
17
28
|
@emoji_by_moji[moji] = emoji_hash if moji
|
18
|
-
end
|
19
|
-
@emoji_moji_regex = /#{@emoji_by_moji.keys.join('|')}/
|
20
|
-
end
|
21
|
-
|
22
|
-
def find_by_moji(moji)
|
23
|
-
@emoji_by_moji[moji]
|
24
|
-
end
|
25
29
|
|
26
|
-
|
27
|
-
|
28
|
-
|
30
|
+
unicode = emoji_hash['unicode']
|
31
|
+
@emoji_by_unicode[unicode] = emoji_hash if unicode
|
32
|
+
end
|
29
33
|
|
30
|
-
|
31
|
-
@emoji_moji_regex
|
34
|
+
@unicode_moji_regex = /#{@emoji_by_moji.keys.join('|')}/
|
32
35
|
end
|
33
36
|
end
|
34
37
|
end
|
data/lib/emoji/railtie.rb
CHANGED
@@ -6,10 +6,11 @@ module Emoji
|
|
6
6
|
initializer "emoji.defaults" do
|
7
7
|
Emoji.asset_host = ActionController::Base.asset_host
|
8
8
|
Emoji.asset_path = '/assets/emoji'
|
9
|
+
Emoji.use_plaintext_alt_tags = false
|
9
10
|
end
|
10
11
|
|
11
12
|
rake_tasks do
|
12
13
|
load File.absolute_path(File.dirname(__FILE__) + '/tasks/install.rake')
|
13
14
|
end
|
14
15
|
end
|
15
|
-
end
|
16
|
+
end
|
data/lib/emoji/version.rb
CHANGED
data/test/emoji_test.rb
CHANGED
@@ -20,9 +20,34 @@ describe Emoji do
|
|
20
20
|
assert_equal 'http://localhost:3000', Emoji.asset_host
|
21
21
|
end
|
22
22
|
|
23
|
-
it 'should
|
23
|
+
it 'should allow hostname only and default scheme to http' do
|
24
24
|
with_emoji_config(:asset_host, 'emoji') do
|
25
|
-
assert_equal 'emoji', Emoji.asset_host
|
25
|
+
assert_equal 'http://emoji', Emoji.asset_host
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should allow protocol relative URL' do
|
30
|
+
with_emoji_config(:asset_host, '//emoji') do
|
31
|
+
assert_equal '//emoji', Emoji.asset_host
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should respect protocol scheme' do
|
36
|
+
with_emoji_config(:asset_host, 'https://emoji') do
|
37
|
+
assert_equal 'https://emoji', Emoji.asset_host
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should allow setting port' do
|
42
|
+
with_emoji_config(:asset_host, 'http://emoji:3000') do
|
43
|
+
assert_equal 'http://emoji:3000', Emoji.asset_host
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should validate hostname' do
|
49
|
+
assert_raises(RuntimeError) do
|
50
|
+
with_emoji_config(:asset_host, nil) {}
|
26
51
|
end
|
27
52
|
end
|
28
53
|
end
|
@@ -39,6 +64,18 @@ describe Emoji do
|
|
39
64
|
end
|
40
65
|
end
|
41
66
|
|
67
|
+
describe "use_plaintext_alt_tags" do
|
68
|
+
it 'should default to false' do
|
69
|
+
refute Emoji.use_plaintext_alt_tags
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should be configurable' do
|
73
|
+
with_emoji_config(:use_plaintext_alt_tags, true) do
|
74
|
+
assert Emoji.use_plaintext_alt_tags
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
42
79
|
describe "replace_unicode_moji_with_images" do
|
43
80
|
it 'should return original string without emoji' do
|
44
81
|
assert_equal "foo", Emoji.replace_unicode_moji_with_images('foo')
|
@@ -55,6 +92,14 @@ describe Emoji do
|
|
55
92
|
assert_equal "I <img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/heart.png\"> Emoji", replaced_string
|
56
93
|
end
|
57
94
|
|
95
|
+
it 'should use plaintext alt tags if configured to do so' do
|
96
|
+
with_emoji_config(:use_plaintext_alt_tags, true) do
|
97
|
+
base_string = "I ❤ Emoji"
|
98
|
+
replaced_string = Emoji.replace_unicode_moji_with_images(base_string)
|
99
|
+
assert_equal "I <img alt=\"heart\" class=\"emoji\" src=\"http://localhost:3000/heart.png\"> Emoji", replaced_string
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
58
103
|
it 'should handle nil string' do
|
59
104
|
assert_equal nil, Emoji.replace_unicode_moji_with_images(nil)
|
60
105
|
end
|
@@ -126,5 +171,4 @@ describe Emoji do
|
|
126
171
|
Emoji.send("#{name}=", original_value)
|
127
172
|
end
|
128
173
|
end
|
129
|
-
|
130
174
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: emoji
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Klabnik
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-04-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
@@ -39,20 +39,6 @@ dependencies:
|
|
39
39
|
- - ~>
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '1.3'
|
42
|
-
- !ruby/object:Gem::Dependency
|
43
|
-
name: debugger
|
44
|
-
requirement: !ruby/object:Gem::Requirement
|
45
|
-
requirements:
|
46
|
-
- - '>='
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
version: '0'
|
49
|
-
type: :development
|
50
|
-
prerelease: false
|
51
|
-
version_requirements: !ruby/object:Gem::Requirement
|
52
|
-
requirements:
|
53
|
-
- - '>='
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
version: '0'
|
56
42
|
- !ruby/object:Gem::Dependency
|
57
43
|
name: rake
|
58
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -91,6 +77,7 @@ extra_rdoc_files: []
|
|
91
77
|
files:
|
92
78
|
- .gitignore
|
93
79
|
- .rvmrc
|
80
|
+
- .travis.yml
|
94
81
|
- CHANGELOG.md
|
95
82
|
- Gemfile
|
96
83
|
- LICENSE.txt
|
@@ -618,7 +605,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
618
605
|
version: '0'
|
619
606
|
requirements: []
|
620
607
|
rubyforge_project:
|
621
|
-
rubygems_version: 2.
|
608
|
+
rubygems_version: 2.4.6
|
622
609
|
signing_key:
|
623
610
|
specification_version: 4
|
624
611
|
summary: 'A Ruby gem. For emoji. For everyone. :heart:'
|