Debian Codegraph @

linkify-it/test/test.js

raw
1 'use strict';
2
3 /*eslint-env node,mocha*/
4
5 var fs = require('fs');
6 var path = require('path');
7 var assert = require('chai').assert;
8
9 var linkify = require('../');
10
11 var lines;
12
13
14 describe('links', function () {
15
16 var l = linkify({ fuzzyIP: true });
17
18 l.normalize = function () {}; // kill normalizer
19
20 lines = fs.readFileSync(path.join(__dirname, 'fixtures/links.txt'), 'utf8').split(/\r?\n/g);
21
22 var skipNext = false;
23
24 lines.forEach(function (line, idx) {
25 if (skipNext) {
26 skipNext = false;
27 return;
28 }
29
30 line = line.replace(/^%.*/, '');
31
32 var next = (lines[idx + 1] || '').replace(/^%.*/, '');
33
34 if (!line.trim()) { return; }
35
36 if (next.trim()) {
37 it('line ' + (idx + 1), function () {
38 assert.ok(l.pretest(line), '(pretest failed in `' + line + '`)');
39 assert.ok(l.test('\n' + line + '\n'), '(link not found in `\\n' + line + '\\n`)');
40 assert.ok(l.test(line), '(link not found in `' + line + '`)');
41 assert.equal(l.match(line)[0].url, next);
42 });
43 skipNext = true;
44 } else {
45 it('line ' + (idx + 1), function () {
46 assert.ok(l.pretest(line), '(pretest failed in `' + line + '`)');
47 assert.ok(l.test('\n' + line + '\n'), '(link not found in `\\n' + line + '\\n`)');
48 assert.ok(l.test(line), '(link not found in `' + line + '`)');
49 assert.equal(l.match(line)[0].url, line);
50 });
51 }
52 });
53
54 });
55
56
57 describe('not links', function () {
58
59 var l = linkify();
60
61 l.normalize = function () {}; // kill normalizer
62
63 lines = fs.readFileSync(path.join(__dirname, 'fixtures/not_links.txt'), 'utf8').split(/\r?\n/g);
64
65 lines.forEach(function (line, idx) {
66 line = line.replace(/^%.*/, '');
67
68 if (!line.trim()) { return; }
69
70 it('line ' + (idx + 1), function () {
71 assert.notOk(l.test(line),
72 '(should not find link in `' + line + '`, but found `' +
73 JSON.stringify((l.match(line) || [])[0]) + '`)');
74 });
75 });
76
77 });
78
79 describe('API', function () {
80
81 it('extend tlds', function () {
82 var l = linkify();
83
84 assert.notOk(l.test('google.myroot'));
85
86 l.tlds('myroot', true);
87
88 assert.ok(l.test('google.myroot'));
89 assert.notOk(l.test('google.xyz'));
90
91 l.tlds(require('tlds'));
92
93 assert.ok(l.test('google.xyz'));
94 assert.notOk(l.test('google.myroot'));
95 });
96
97
98 it('add rule as regexp, with default normalizer', function () {
99 var l = linkify().add('my:', {
100 validate: /^\/\/[a-z]+/
101 });
102
103 var match = l.match('google.com. my:// my://asdf!');
104
105 assert.equal(match[0].text, 'google.com');
106 assert.equal(match[1].text, 'my://asdf');
107 });
108
109
110 it('add rule with normalizer', function () {
111 var l = linkify().add('my:', {
112 validate: /^\/\/[a-z]+/,
113 normalize: function (m) {
114 m.text = m.text.replace(/^my:\/\//, '').toUpperCase();
115 m.url = m.url.toUpperCase();
116 }
117 });
118
119 var match = l.match('google.com. my:// my://asdf!');
120
121 assert.equal(match[1].text, 'ASDF');
122 assert.equal(match[1].url, 'MY://ASDF');
123 });
124
125
126 it('disable rule', function () {
127 var l = linkify();
128
129 assert.ok(l.test('http://google.com'));
130 assert.ok(l.test('[email protected]'));
131 l.add('http:', null);
132 l.add('mailto:', null);
133 assert.notOk(l.test('http://google.com'));
134 assert.notOk(l.test('[email protected]'));
135 });
136
137
138 it('add bad definition', function () {
139 var l;
140
141 l = linkify();
142
143 assert.throw(function () {
144 l.add('test:', []);
145 });
146
147 l = linkify();
148
149 assert.throw(function () {
150 l.add('test:', { validate: [] });
151 });
152
153 l = linkify();
154
155 assert.throw(function () {
156 l.add('test:', {
157 validate: function () { return false; },
158 normalize: 'bad'
159 });
160 });
161 });
162
163
164 it('test at position', function () {
165 var l = linkify();
166
167 assert.ok(l.testSchemaAt('http://google.com', 'http:', 5));
168 assert.ok(l.testSchemaAt('http://google.com', 'HTTP:', 5));
169 assert.notOk(l.testSchemaAt('http://google.com', 'http:', 6));
170
171 assert.notOk(l.testSchemaAt('http://google.com', 'bad_schema:', 6));
172 });
173
174
175 it('correct cache value', function () {
176 var l = linkify();
177
178 var match = l.match('.com. http://google.com google.com ftp://google.com');
179
180 assert.equal(match[0].text, 'http://google.com');
181 assert.equal(match[1].text, 'google.com');
182 assert.equal(match[2].text, 'ftp://google.com');
183 });
184
185
186 it('normalize', function () {
187 var l = linkify(), m;
188
189 m = l.match('mailto:[email protected]')[0];
190
191 // assert.equal(m.text, '[email protected]');
192 assert.equal(m.url, 'mailto:[email protected]');
193
194 m = l.match('[email protected]')[0];
195
196 // assert.equal(m.text, '[email protected]');
197 assert.equal(m.url, 'mailto:[email protected]');
198 });
199
200
201 it('test @twitter rule', function () {
202 var l = linkify().add('@', {
203 validate: function (text, pos, self) {
204 var tail = text.slice(pos);
205
206 if (!self.re.twitter) {
207 self.re.twitter = new RegExp(
208 '^([a-zA-Z0-9_]){1,15}(?!_)(?=$|' + self.re.src_ZPCc + ')'
209 );
210 }
211 if (self.re.twitter.test(tail)) {
212 if (pos >= 2 && tail[pos - 2] === '@') {
213 return false;
214 }
215 return tail.match(self.re.twitter)[0].length;
216 }
217 return 0;
218 },
219 normalize: function (m) {
220 m.url = 'https://twitter.com/' + m.url.replace(/^@/, '');
221 }
222 });
223
224 assert.equal(l.match('hello, @gamajoba_!')[0].text, '@gamajoba_');
225 assert.equal(l.match(':@givi')[0].text, '@givi');
226 assert.equal(l.match(':@givi')[0].url, 'https://twitter.com/givi');
227 assert.notOk(l.test('@@invalid'));
228 });
229
230
231 it('set option: fuzzyLink', function () {
232 var l = linkify({ fuzzyLink: false });
233
234 assert.equal(l.test('google.com.'), false);
235
236 l.set({ fuzzyLink: true });
237
238 assert.equal(l.test('google.com.'), true);
239 assert.equal(l.match('google.com.')[0].text, 'google.com');
240 });
241
242
243 it('set option: fuzzyEmail', function () {
244 var l = linkify({ fuzzyEmail: false });
245
246 assert.equal(l.test('[email protected].'), false);
247
248 l.set({ fuzzyEmail: true });
249
250 assert.equal(l.test('[email protected].'), true);
251 assert.equal(l.match('[email protected].')[0].text, '[email protected]');
252 });
253
254
255 it('set option: fuzzyIP', function () {
256 var l = linkify();
257
258 assert.equal(l.test('1.1.1.1.'), false);
259
260 l.set({ fuzzyIP: true });
261
262 assert.equal(l.test('1.1.1.1.'), true);
263 assert.equal(l.match('1.1.1.1.')[0].text, '1.1.1.1');
264 });
265
266 it('should not hang in fuzzy mode with sequences of astrals', function () {
267 var l = linkify();
268
269 l.set({ fuzzyLink: true });
270
271 l.match('😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡 .com');
272 });
273
274
275 it('should accept `---` if enabled', function () {
276 var l = linkify();
277
278 assert.equal(l.match('http://e.com/foo---bar')[0].text, 'http://e.com/foo---bar');
279
280 l = linkify(null, { '---': true });
281
282 assert.equal(l.match('http://e.com/foo---bar')[0].text, 'http://e.com/foo');
283 });
284 });