summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/filter/emoji_filter_spec.rb
blob: c8e62f528df37a27153aab084a2782be1a1d094f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require 'spec_helper'

describe Banzai::Filter::EmojiFilter, lib: true do
  include FilterSpecHelper

  before do
    @original_asset_host = ActionController::Base.asset_host
    ActionController::Base.asset_host = 'https://foo.com'
  end

  after do
    ActionController::Base.asset_host = @original_asset_host
  end

  it 'replaces supported name emoji' do
    doc = filter('<p>:heart:</p>')
    expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/2764.png'
  end

  it 'replaces supported unicode emoji' do
    doc = filter('<p>❤️</p>')
    expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/2764.png'
  end

  it 'ignores unsupported emoji' do
    exp = act = '<p>:foo:</p>'
    doc = filter(act)
    expect(doc.to_html).to match Regexp.escape(exp)
  end

  it 'correctly encodes the URL' do
    doc = filter('<p>:+1:</p>')
    expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/1F44D.png'
  end

  it 'correctly encodes unicode to the URL' do
    doc = filter('<p>👍</p>')
    expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/1F44D.png'
  end

  it 'matches at the start of a string' do
    doc = filter(':+1:')
    expect(doc.css('img').size).to eq 1
  end

  it 'unicode matches at the start of a string' do
    doc = filter("'👍'")
    expect(doc.css('img').size).to eq 1
  end

  it 'matches at the end of a string' do
    doc = filter('This gets a :-1:')
    expect(doc.css('img').size).to eq 1
  end

  it 'unicode matches at the end of a string' do
    doc = filter('This gets a 👍')
    expect(doc.css('img').size).to eq 1
  end

  it 'matches with adjacent text' do
    doc = filter('+1 (:+1:)')
    expect(doc.css('img').size).to eq 1
  end

  it 'unicode matches with adjacent text' do
    doc = filter('+1 (👍)')
    expect(doc.css('img').size).to eq 1
  end

  it 'matches multiple emoji in a row' do
    doc = filter(':see_no_evil::hear_no_evil::speak_no_evil:')
    expect(doc.css('img').size).to eq 3
  end

  it 'unicode matches multiple emoji in a row' do
    doc = filter("'🙈🙉🙊'")
    expect(doc.css('img').size).to eq 3
  end

  it 'mixed matches multiple emoji in a row' do
    doc = filter("'🙈:see_no_evil:🙉:hear_no_evil:🙊:speak_no_evil:'")
    expect(doc.css('img').size).to eq 6
  end

  it 'has a title attribute' do
    doc = filter(':-1:')
    expect(doc.css('img').first.attr('title')).to eq ':-1:'
  end

  it 'unicode has a title attribute' do
    doc = filter("'👎'")
    expect(doc.css('img').first.attr('title')).to eq ':thumbsdown:'
  end

  it 'has an alt attribute' do
    doc = filter(':-1:')
    expect(doc.css('img').first.attr('alt')).to eq ':-1:'
  end

  it 'unicode has an alt attribute' do
    doc = filter("'👎'")
    expect(doc.css('img').first.attr('alt')).to eq ':thumbsdown:'
  end

  it 'has an align attribute' do
    doc = filter(':8ball:')
    expect(doc.css('img').first.attr('align')).to eq 'absmiddle'
  end

  it 'unicode has an align attribute' do
    doc = filter("'🎱'")
    expect(doc.css('img').first.attr('align')).to eq 'absmiddle'
  end

  it 'has an emoji class' do
    doc = filter(':cat:')
    expect(doc.css('img').first.attr('class')).to eq 'emoji'
  end

  it 'unicode has an emoji class' do
    doc = filter("'🐱'")
    expect(doc.css('img').first.attr('class')).to eq 'emoji'
  end

  it 'has height and width attributes' do
    doc = filter(':dog:')
    img = doc.css('img').first

    expect(img.attr('width')).to eq '20'
    expect(img.attr('height')).to eq '20'
  end

  it 'unicode has height and width attributes' do
    doc = filter("'🐶'")
    img = doc.css('img').first

    expect(img.attr('width')).to eq '20'
    expect(img.attr('height')).to eq '20'
  end

  it 'keeps whitespace intact' do
    doc = filter('This deserves a :+1:, big time.')

    expect(doc.to_html).to match(/^This deserves a <img.+>, big time\.\z/)
  end

  it 'unicode keeps whitespace intact' do
    doc = filter('This deserves a 🎱, big time.')

    expect(doc.to_html).to match(/^This deserves a <img.+>, big time\.\z/)
  end

  it 'uses a custom asset_root context' do
    root = Gitlab.config.gitlab.url + 'gitlab/root'

    doc = filter(':smile:', asset_root: root)
    expect(doc.css('img').first.attr('src')).to start_with(root)
  end

  it 'uses a custom asset_host context' do
    ActionController::Base.asset_host = 'https://cdn.example.com'

    doc = filter(':frowning:', asset_host: 'https://this-is-ignored-i-guess?')
    expect(doc.css('img').first.attr('src')).to start_with('https://cdn.example.com')
  end

  it 'uses a custom asset_root context' do
    root = Gitlab.config.gitlab.url + 'gitlab/root'

    doc = filter("'🎱'", asset_root: root)
    expect(doc.css('img').first.attr('src')).to start_with(root)
  end

  it 'uses a custom asset_host context' do
    ActionController::Base.asset_host = 'https://cdn.example.com'

    doc = filter("'🎱'", asset_host: 'https://this-is-ignored-i-guess?')
    expect(doc.css('img').first.attr('src')).to start_with('https://cdn.example.com')
  end
end