-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtests.html
233 lines (208 loc) · 10.2 KB
/
tests.html
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
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Linkify.js tests</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-1.10.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.10.0.css" type="text/css">
<script src="linkify.js"></script>
</head>
<body>
<div id="qunit"></div>
</body>
<script type="text/javascript" charset="utf-8">
// keep replacements simple to shorten strings needed for testing
linkify.add_wbrs = false;
linkify.link_target = null;
linkify.link_titles = false;
// emails -> html
var emails = {
'user@example.com':
'<a href="mailto:user@example.com">user@example.com</a>',
'user-foo@blah.example.com':
'<a href="mailto:user-foo@blah.example.com">user-foo@blah.example.com</a>'
};
test("emails html", function() {
for (var line in emails) {
equal(linkify.linkify(line), emails[line], line);
}
});
// full URLs -> html
var urls = {
'http://www.example.com':
'<a href="http://www.example.com">http://www.example.com</a>',
'http://www.example.com/blah':
'<a href="http://www.example.com/blah">http://www.example.com/blah</a>'
};
test("full urls html", function() {
for (var line in urls) {
equal(linkify.linkify(line), urls[line], line);
}
});
// partial URLs -> html
var urls = {
'example.com': '<a href="http://example.com">example.com</a>',
'www.example.com': '<a href="http://www.example.com">www.example.com</a>',
'example.com/foo': '<a href="http://example.com/foo">example.com/foo</a>'
};
test("partial urls html", function() {
for (var line in urls) {
equal(linkify.linkify(line), urls[line], line);
}
});
// test that matched items are returned
test("matches links", function() {
var items = {
// email
'email@example.com': ['email@example.com'],
// simple stuff
'http://example.com': ['http://example.com'],
'http://example.com#foo': ['http://example.com#foo'],
'http://example.com/blah': ['http://example.com/blah'],
'http://example.com/blah#foo': ['http://example.com/blah#foo'],
'http://example.com/blah?foo=bar': ['http://example.com/blah?foo=bar'],
'http://example.com/?foo=bar': ['http://example.com/?foo=bar'],
'http://example.com?foo=bar': ['http://example.com?foo=bar'],
'http://example.com?foo=bar#blah': ['http://example.com?foo=bar#blah'],
'http://example.com?WithCapitalLetters=TRUE': ['http://example.com?WithCapitalLetters=TRUE'],
'example.com': ['http://example.com'],
'example.com#foo': ['http://example.com#foo'],
'example.com/blah': ['http://example.com/blah'],
'example.com/blah#foo': ['http://example.com/blah#foo'],
'example.com/blah?foo=bar': ['http://example.com/blah?foo=bar'],
'example.com/?foo=bar': ['http://example.com/?foo=bar'],
'example.com?foo=bar': ['http://example.com?foo=bar'],
'example.com?foo=bar#blah': ['http://example.com?foo=bar#blah'],
// ports
'http://example.com:8080/blah': ['http://example.com:8080/blah'],
// surrounding punctuation
'link:http://example.com': ['http://example.com'],
'http://example.com.': ['http://example.com'],
',http://example.com,': ['http://example.com'],
'(http://example.com)': ['http://example.com'],
'<http://example.com>': ['http://example.com'],
'`http://example.com`': ['http://example.com'],
'"http://example.com"': ['http://example.com'],
// quotes
'http://example.com?foo=bar"baz': ['http://example.com?foo=bar'],
'http://example.com?foo=bar%22baz': ['http://example.com?foo=bar%22baz'],
'http://example.com???foo=bar%22baz': ['http://example.com???foo=bar%22baz'],
// parens in url
'http://foo.com/x_(wikipedia)': ['http://foo.com/x_(wikipedia)'],
'http://foo.com/x_(wikipedia)_x': ['http://foo.com/x_(wikipedia)_x'],
// make sure lots of stuff in parens doesn't trigger http://stackoverflow.com/questions/12803859/regexp-and-string-combination-crashes-chrome
'https://foo.com/?target=alias(xxxxxxxxxxxxxxxxxxxxxxxxxxxx)': ['https://foo.com/?target=alias(xxxxxxxxxxxxxxxxxxxxxxxxxxxx)'],
'https://foo.com/?target=alias(foo(xxxxxxxxxxxxxxxxxxxxxxxxxxxx))': ['https://foo.com/?target=alias(foo(xxxxxxxxxxxxxxxxxxxxxxxxxxxx))'],
'https://foo.com/?target=alias(foo(x))&b=x': ['https://foo.com/?target=alias(foo(x))&b=x'],
// Supports 2 level nested parens
'(https://foo.com/?target=alias(foo(x)))': ['https://foo.com/?target=alias(foo(x))'],
// file:
'file:/localhost/foo.html': ['file:/localhost/foo.html'],
'file://localhost/foo.html': ['file://localhost/foo.html'],
'file:///localhost/foo.html': ['file:///localhost/foo.html'],
'file:///C:/Program Files': [], // drives & spaces not supported
// local windows path
// '\\\\filez\\blah': ['\\\\filez\\blah'],
// ips
'http://192.168.0.1': ['http://192.168.0.1'],
'http://192.168.0.1/admin': ['http://192.168.0.1/admin'],
// stuff we don't want matched
'file.py': [],
'thing.sh': [],
'javascript://foobar/%0aalert(0)': [],
// custom protocols
'x-proto://blah': ['x-proto://blah'],
'irc://irc.freenode.net:6667/hipchat?nopass': ['irc://irc.freenode.net:6667/hipchat?nopass']
};
for (var text in items) {
var expected_matches = items[text];
var matched = [];
linkify.linkify(text, matched);
deepEqual(matched, expected_matches, text);
}
});
// link target
test("link_target enabled", function() {
linkify.link_target = '_blank';
var links = {
'user@example.com': '<a target="_blank" href="mailto:user@example.com">user@example.com</a>',
'example.com': '<a target="_blank" href="http://example.com">example.com</a>',
'http://www.example.com': '<a target="_blank" href="http://www.example.com">http://www.example.com</a>'
};
for (var text in links) {
equal(linkify.linkify(text), links[text], text);
}
linkify.link_target = null;
});
// link titles
test("link_titles enabled", function() {
linkify.link_titles = true;
var links = {
'user@example.com': '<a href="mailto:user@example.com" title="Email user@example.com">user@example.com</a>',
'example.com': '<a href="http://example.com" title="http://example.com">example.com</a>',
'http://www.example.com': '<a href="http://www.example.com" title="http://www.example.com">http://www.example.com</a>'
};
for (var text in links) {
equal(linkify.linkify(text), links[text], text);
}
linkify.link_titles = false;
});
// proper email links are generated
test("email links", function() {
var obj = {
'foo (user@blah.example.com)': 'foo (<a href="mailto:user@blah.example.com">user@blah.example.com</a>)',
'foo user@blah.example.com': 'foo <a href="mailto:user@blah.example.com">user@blah.example.com</a>'
};
for (var item in obj) {
equal(linkify.linkify(item), obj[item], linkify.linkify(item) + ' == ' + obj[item]);
}
// show that links generated with previous email regex don't match expected values
linkify.RE_EMAIL_PATTERN = '(?:\\s|\\A|^)[\\w.-]+\\+*[\\w.-]+@(?:(?:[\\w-]+\\.)+[A-Za-z]{2,6}|(?:\\d{1,3}\\.){3}\\d{1,3})';
for (var item in obj) {
notEqual(linkify.linkify(item), obj[item], linkify.linkify(item) + ' != ' + obj[item]);
}
// reset to current regex
linkify.RE_EMAIL_PATTERN = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
});
// truncate length
test("truncate_length enabled", function() {
linkify.truncate_length = 15;
var links = {
'user@blah.example.com': '<a href="mailto:user@blah.example.com">user@blah.examp...</a>',
'blah.foo.example.com': '<a href="http://blah.foo.example.com">blah.foo.exampl...</a>',
'http://www.example.com': '<a href="http://www.example.com">http://www.exam...</a>'
};
for (var text in links) {
equal(linkify.linkify(text), links[text], text);
}
linkify.truncate_length = null;
});
// test javascript is not rendered as links
test("do not link these", function() {
var links = {
'javascript://foobar/%0aalert(0)': 'javascript://foobar/%0aalert(0)'
};
for (var text in links) {
equal(linkify.linkify(text), links[text], text);
}
});
// test add_wbrs
test("add_wbrs enabled", function() {
linkify.add_wbrs = true;
var text = 'example.com/foo?a=b';
var expected = '<a href="http://example.com/foo?a=b">example.com<wbr>/foo?a<wbr>=b</a>';
var actual = linkify.linkify(text);
equal(actual, expected, text);
linkify.add_wbrs = false;
});
test("json_block", function() {
var text =
'{"user":{"birthdate":"1986-10-20T00:00:00Z","email":"eric20@ploonky.com","fullname":"Eric Abrahamsen","handle":"eabraham20","id":14,"profile_pic":{"id":1073741823,"file":{"url":"http://tuorbit.ploonky.com/placeholder.png","bounded":{"url":"http://tuorbit.ploonky.com/bounded_placeholder.png"}}}},"authentication_token":"2b025b93f37f803faa59aea602fdf009"}';
var expected =
'{"user":{"birthdate":"1986-10-20T00:00:00Z","email":"<a href="mailto:eric20@ploonky.com">eric20@ploonky.com</a>","fullname":"Eric Abrahamsen","handle":"eabraham20","id":14,"profile_pic":{"id":1073741823,"file":{"url":"<a href="http://tuorbit.ploonky.com/placeholder.png">http://tuorbit.ploonky.com/placeholder.png</a>","bounded":{"url":"<a href="http://tuorbit.ploonky.com/bounded_placeholder.png">http://tuorbit.ploonky.com/bounded_placeholder.png</a>"}}}},"authentication_token":"2b025b93f37f803faa59aea602fdf009"}';
var actual = linkify.linkify(text);
equal(actual, expected, text);
})
</script>
</html>