This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
link_helper_test.js
281 lines (253 loc) · 8.98 KB
/
link_helper_test.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Link Helper Tests
*/
'use strict';
requireApp('sms/js/link_helper.js');
requireApp('sms/js/utils.js');
requireApp('sms/test/unit/mock_utils.js');
suite('link_helper_test.js', function() {
suite('LinkHelper URL replacements', function() {
function url2msg(url, addprefix) {
var link = url;
if (addprefix) {
url = 'http://' + url;
}
return '<a data-url="' + url + '"' +
' data-action="url-link" >' + link + '</a>';
}
function testURLMatch(url, match, addprefix) {
var expected = url.replace(match, url2msg(match, addprefix));
var result = LinkHelper.searchAndLinkClickableData(url);
assert.equal(result, expected);
}
function testURLOK(url, addprefix) {
var expected = url2msg(url, addprefix);
var result = LinkHelper.searchAndLinkClickableData(url);
assert.equal(result, expected);
}
function testURLNOK(url) {
// we only test url here to make sure it doesn't match
var result = LinkHelper.searchAndLinkUrl(url);
assert.equal(result, url);
}
suite('Matches', function() {
test('Simple URL', function() {
testURLOK('http://www.mozilla.org/');
});
test('Simple URL .co.uk', function() {
testURLOK('http://www.mozilla.co.uk/');
});
test('Simple short URL', function() {
testURLOK('mzl.la', true);
});
test('Simple short URL with parens', function() {
testURLMatch('(mzl.la)', 'mzl.la', true);
});
test('Simple short URL with trailing dot', function() {
testURLMatch('mzl.la.', 'mzl.la', true);
});
test('Simple short URL with trailing dot and port', function() {
testURLMatch('mzl.la.:527', 'mzl.la', true);
});
test('Simple short URL with value', function() {
testURLOK('mzl.la/ac', true);
});
test('Simple URL https', function() {
testURLOK('https://www.mozilla.org');
});
test('Simple URL https without www.', function() {
testURLOK('https://mozilla.org');
});
test('Simple URL https with trailing dot on domain.', function() {
testURLOK('https://mozilla.org./blah');
});
test('Simple URL http with port', function() {
testURLOK('http://www.mozilla.org:8080/');
});
test('Simple URL http without www. and with port', function() {
testURLOK('http://mozilla.org:8080/');
});
test('Simple URL with IPv4', function() {
testURLOK('http://8.8.8.8/');
});
test('Simple URL with IPv4 with zero', function() {
testURLOK('http://8.0.8.8/');
});
test('<br>prefix', function() {
testURLMatch('<br>mozilla.org', 'mozilla.org', true);
});
test('Trailing question mark', function() {
testURLMatch('OMG mozilla.org? it is cool!', 'mozilla.org', true);
});
test('Trailing colon', function() {
testURLMatch('OMG mozilla.org: it is cool!', 'mozilla.org', true);
});
test('Trailing semi-colon', function() {
testURLMatch('OMG mozilla.org; it is cool!', 'mozilla.org', true);
});
test('Trailing bang', function() {
testURLMatch('OMG mozilla.org! it is cool!', 'mozilla.org', true);
});
test('Trailing period', function() {
testURLMatch('Check out mozilla.org.', 'mozilla.org', true);
});
test('Trailing comma', function() {
testURLMatch('Check out mozilla.org, or not', 'mozilla.org', true);
});
test('Trailing parens', function() {
testURLMatch('(Check out mozilla.org)', 'mozilla.org', true);
});
test('Trailing question mark on path', function() {
testURLMatch('OMG mzl.la/huh? it is cool!', 'mzl.la/huh', true);
});
test('Trailing colon on path', function() {
testURLMatch('OMG mzl.la/huh: it is cool!', 'mzl.la/huh', true);
});
test('Trailing semi-colon on path', function() {
testURLMatch('OMG mzl.la/huh; it is cool!', 'mzl.la/huh', true);
});
test('Trailing bang on path', function() {
testURLMatch('OMG mzl.la/huh! it is cool!', 'mzl.la/huh', true);
});
test('Trailing parens on path', function() {
testURLMatch('(Check out mzl.la/ac)', 'mzl.la/ac', true);
});
test('Trailing period on path', function() {
testURLMatch('Check out mzl.la/ac.', 'mzl.la/ac', true);
});
test('Trailing comma on path', function() {
testURLMatch('Check out mzl.la/ac, or not', 'mzl.la/ac', true);
});
test('wiki link that has paren', function() {
testURLOK('http://en.wikipedia.org/wiki/Arete_(disambiguation)');
});
test('Crazy URL from #887146', function() {
testURLOK('http://sani.tiz.ed.com/wap/videos/files_mm/' +
'3GPP%20MPEG4%20-%20176×144_7fps_15kpbs_AACmono_8KHz_5kbps.3gp');
});
test('One letter second-level', function() {
testURLOK('http://x.com');
});
test('URL with phone number in the middle', function() {
testURLOK('http://somesite.com/q,12288296666/');
});
});
suite('Failures', function() {
test('Simple invalid URL', function() {
testURLNOK('htt://www.mozilla.org');
});
test('Simple invalid URL with slashes', function() {
testURLNOK('http://www.a/b/d.com');
});
test('Simple invalid TLD URL', function() {
testURLNOK('http://www.mozilla.o');
});
test('Simple ellipse', function() {
testURLNOK('aaa...ccc');
});
test('Simple URL with username', function() {
testURLNOK('http://user@mozilla.org');
});
test('Simple URL with credentials', function() {
testURLNOK('http://user:pass@mozilla.org');
});
test('Simple URL with IPv6', function() {
testURLNOK('http://[::1]/');
});
test('Bug 890342 1', function() {
testURLNOK('2.74');
});
test('Bug 890342 2', function() {
testURLNOK('21.72');
});
});
});
suite('LinkHelper Email replacements', function() {
function email2msg(email) {
return '<a data-email="' + email + '"' +
' data-action="email-link">' + email + '</a>';
}
function testEmailOK(email) {
var exp = email2msg(email);
var res = LinkHelper.searchAndLinkClickableData(email);
assert.equal(res, exp);
}
function testEmailNOK(email) {
var res = LinkHelper.searchAndLinkEmail(email);
assert.equal(res, email);
}
test('Simple email', function() {
testEmailOK('user@hostname.tld');
});
test('Simple email with alias', function() {
testEmailOK('user+alias@hostname.tld');
});
test('Simple email with subdomain', function() {
testEmailOK('user@cs.subdhostname.tld');
});
test('Invalid email no hostname', function() {
testEmailNOK('user@');
});
test('Invalid email no user', function() {
testEmailNOK('@hostname.tld');
});
});
suite('LinkHelper Phone replacements', function() {
function phone2msg(phone) {
return '<a data-phonenumber="' + phone + '"' +
' data-action="phone-link">' + phone + '</a>';
}
function testPhoneOK(phone) {
var exp = phone2msg(phone);
var res = LinkHelper.searchAndLinkClickableData(phone);
assert.equal(res, exp);
}
function testPhoneNOK(phone) {
var res = LinkHelper.searchAndLinkPhone(phone);
assert.equal(res, phone);
}
suite('Matches', function() {
test('Simple french mobile nat.', function() {
testPhoneOK('0612345678');
});
test('Simple french mobile intl', function() {
testPhoneOK('+33612345678');
});
test('Phone from #887737', function() {
testPhoneOK('+5511 98907-6047');
});
});
suite('Failures', function() {
['0.34', '1200', '5000', '0.122', '0921'].forEach(function(phone) {
test(phone, function() {
testPhoneNOK(phone);
});
});
});
suite('Varied cases', function() {
FixturePhones.forEach(function(fixture) {
suite(fixture.name, function() {
fixture.values.forEach(function(value) {
test(value, function() {
testPhoneOK(value);
});
});
});
});
});
});
suite('Multiple in the same string', function() {
test('Hits every case', function() {
var test = 'Hey, check out http://stackoverflow.com/q/12882966/ and ' +
'call me at +18155551212 or (e-mail user@hostname.tld)';
var expected = 'Hey, check out ' +
'<a data-url="http://stackoverflow.com/q/12882966/" ' +
'data-action="url-link" >http://stackoverflow.com/q/12882966/</a>' +
' and call me at ' +
'<a data-phonenumber="+18155551212" data-action="phone-link">' +
'+18155551212</a> or (e-mail <a data-email="user@hostname.tld"' +
' data-action="email-link">user@hostname.tld</a>)';
assert.equal(LinkHelper.searchAndLinkClickableData(test), expected);
});
});
});