Debian Codegraph @

closure-library/closure/goog/string/linkify_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.string.linkifyTest');
16 goog.setTestOnly('goog.string.linkifyTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.dom.safe');
21 goog.require('goog.html.SafeHtml');
22 goog.require('goog.string');
23 goog.require('goog.string.linkify');
24 goog.require('goog.testing.dom');
25 goog.require('goog.testing.jsunit');
26
27 var div = goog.dom.createElement(goog.dom.TagName.DIV);
28
29 function assertLinkify(comment, input, expected, opt_preserveNewlines) {
30 assertEquals(
31 comment, expected,
32 goog.html.SafeHtml.unwrap(
33 goog.string.linkify.linkifyPlainTextAsHtml(
34 input, {rel: '', target: ''}, opt_preserveNewlines)));
35 }
36
37 function testContainsNoLink() {
38 assertLinkify(
39 'Text does not contain any links', 'Text with no links in it.',
40 'Text with no links in it.');
41 }
42
43 function testContainsALink() {
44 assertLinkify(
45 'Text only contains a link', 'http://www.google.com/',
46 '<a href="http://www.google.com/">http://www.google.com/<\/a>');
47 }
48
49 function testStartsWithALink() {
50 assertLinkify(
51 'Text starts with a link',
52 'http://www.google.com/ is a well known search engine',
53 '<a href="http://www.google.com/">http://www.google.com/<\/a>' +
54 ' is a well known search engine');
55 }
56
57 function testEndsWithALink() {
58 assertLinkify(
59 'Text ends with a link',
60 'Look at this search engine: http://www.google.com/',
61 'Look at this search engine: ' +
62 '<a href="http://www.google.com/">http://www.google.com/<\/a>');
63 }
64
65 function testContainsOnlyEmail() {
66 assertLinkify(
67 'Text only contains an email address', '[email protected]',
68 '<a href="mailto:[email protected]">[email protected]<\/a>');
69 }
70
71 function testStartsWithAnEmail() {
72 assertLinkify(
73 'Text starts with an email address',
74 '[email protected] wrote this test.',
75 '<a href="mailto:[email protected]">[email protected]<\/a>' +
76 ' wrote this test.');
77 }
78
79 function testEndsWithAnEmail() {
80 assertLinkify(
81 'Text ends with an email address',
82 'This test was written by [email protected].',
83 'This test was written by ' +
84 '<a href="mailto:[email protected]">[email protected]<\/a>.');
85 }
86
87 function testUrlWithPortNumber() {
88 assertLinkify(
89 'URL with a port number', 'http://www.google.com:80/',
90 '<a href="http://www.google.com:80/">http://www.google.com:80/<\/a>');
91 }
92
93 function testUrlWithUserPasswordAndPortNumber() {
94 assertLinkify(
95 'URL with a user, a password and a port number',
96 'http://lascap:[email protected]:80/s?q=a&hl=en',
97 '<a href="http://lascap:[email protected]:80/s?q=a&amp;hl=en">' +
98 'http://lascap:[email protected]:80/s?q=a&amp;hl=en<\/a>');
99 }
100
101 function testUrlWithUnderscore() {
102 assertLinkify(
103 'URL with an underscore', 'http://www_foo.google.com/',
104 '<a href="http://www_foo.google.com/">http://www_foo.google.com/<\/a>');
105 }
106
107 function testInternalUrlWithoutDomain() {
108 assertLinkify(
109 'Internal URL without a proper domain', 'http://tracker/1068594',
110 '<a href="http://tracker/1068594">http://tracker/1068594<\/a>');
111 }
112
113 function testInternalUrlOneChar() {
114 assertLinkify(
115 'Internal URL with a one char domain', 'http://b',
116 '<a href="http://b">http://b<\/a>');
117 }
118
119 function testSecureInternalUrlWithoutDomain() {
120 assertLinkify(
121 'Secure Internal URL without a proper domain', 'https://review/6594805',
122 '<a href="https://review/6594805">https://review/6594805<\/a>');
123 }
124
125 function testTwoUrls() {
126 assertLinkify(
127 'Text with two URLs in it',
128 'I use both http://www.google.com and http://yahoo.com, don\'t you?',
129 'I use both <a href="http://www.google.com">http://www.google.com<\/a> ' +
130 'and <a href="http://yahoo.com">http://yahoo.com<\/a>, ' +
131 goog.string.htmlEscape('don\'t you?'));
132 }
133
134 function testGetParams() {
135 assertLinkify(
136 'URL with GET params', 'http://google.com/?a=b&c=d&e=f',
137 '<a href="http://google.com/?a=b&amp;c=d&amp;e=f">' +
138 'http://google.com/?a=b&amp;c=d&amp;e=f<\/a>');
139 }
140
141 function testGoogleCache() {
142 assertLinkify(
143 'Google search result from cache',
144 'http://66.102.7.104/search?q=cache:I4LoMT6euUUJ:' +
145 'www.google.com/intl/en/help/features.html+google+cache&hl=en',
146 '<a href="http://66.102.7.104/search?q=cache:I4LoMT6euUUJ:' +
147 'www.google.com/intl/en/help/features.html+google+cache&amp;hl=en">' +
148 'http://66.102.7.104/search?q=cache:I4LoMT6euUUJ:' +
149 'www.google.com/intl/en/help/features.html+google+cache&amp;hl=en' +
150 '<\/a>');
151 }
152
153 function testUrlWithoutHttp() {
154 assertLinkify(
155 'URL without http protocol',
156 'It\'s faster to type www.google.com without the http:// in front.',
157 goog.string.htmlEscape('It\'s faster to type ') +
158 '<a href="http://www.google.com">www.google.com' +
159 '<\/a> without the http:// in front.');
160 }
161
162 function testUrlWithCapitalsWithoutHttp() {
163 assertLinkify(
164 'URL with capital letters without http protocol',
165 'It\'s faster to type Www.google.com without the http:// in front.',
166 goog.string.htmlEscape('It\'s faster to type ') +
167 '<a href="http://Www.google.com">Www.google.com' +
168 '<\/a> without the http:// in front.');
169 }
170
171 function testUrlHashBang() {
172 assertLinkify(
173 'URL with #!', 'Another test URL: ' +
174 'https://www.google.com/testurls/#!/page',
175 'Another test URL: ' +
176 '<a href="https://www.google.com/testurls/#!/page">' +
177 'https://www.google.com/testurls/#!/page<\/a>');
178 }
179
180 function testTextLooksLikeUrlWithoutHttp() {
181 assertLinkify(
182 'Text looks like an url but is not', 'This showww.is just great: www.is',
183 'This showww.is just great: <a href="http://www.is">www.is<\/a>');
184 }
185
186 function testEmailWithSubdomain() {
187 assertLinkify(
188 'Email with a subdomain', 'Send mail to [email protected].',
189 'Send mail to <a href="mailto:[email protected]">' +
190 '[email protected]<\/a>.');
191 }
192
193 function testEmailWithHyphen() {
194 assertLinkify(
195 'Email with a hyphen in the domain name',
196 'Send mail to [email protected].',
197 'Send mail to <a href="mailto:[email protected]">' +
198 '[email protected]<\/a>.');
199 }
200
201 function testEmailUsernameWithSpecialChars() {
202 assertLinkify(
203 'Email with a hyphen, period, and + in the user name',
204 'Send mail to [email protected]',
205 'Send mail to <a href="mailto:[email protected]">' +
206 '[email protected]<\/a>');
207 assertLinkify(
208 'Email with all special characters in the user name',
209 'Send mail to muad\'dib!#$%&\*/=?^_`{|}[email protected]', 'Send mail to ' +
210 '<a href="mailto:muad&#39;dib!#$%&amp;\*/=?^_`{|}[email protected]">' +
211 'muad&#39;dib!#$%&amp;\*/=?^_`{|}[email protected]<\/a>');
212 }
213
214 function testEmailWithUnderscoreInvalid() {
215 assertLinkify(
216 'Email with an underscore in the domain name, which is invalid',
217 'Do not email bolinfest@google_groups.com.',
218 'Do not email bolinfest@google_groups.com.');
219 }
220
221 function testUrlNotHttp() {
222 assertLinkify(
223 'Url using unusual scheme',
224 'Looking for some goodies: ftp://ftp.google.com/goodstuff/',
225 'Looking for some goodies: ' +
226 '<a href="ftp://ftp.google.com/goodstuff/">' +
227 'ftp://ftp.google.com/goodstuff/<\/a>');
228 }
229
230 function testJsInjection() {
231 assertLinkify(
232 'Text includes some javascript',
233 'Welcome in hell <script>alert(\'this is hell\')<\/script>',
234 goog.string.htmlEscape(
235 'Welcome in hell <script>alert(\'this is hell\')<\/script>'));
236 }
237
238 function testJsInjectionDotIsBlind() {
239 assertLinkify(
240 'Javascript injection using regex . blindness to newline chars',
241 '<script>malicious_code()<\/script>\nVery nice url: www.google.com',
242 '&lt;script&gt;malicious_code()&lt;/script&gt;\nVery nice url: ' +
243 '<a href="http://www.google.com">www.google.com<\/a>');
244 }
245
246 function testJsInjectionWithUnicodeLineReturn() {
247 assertLinkify(
248 'Javascript injection using regex . blindness to newline chars with a ' +
249 'unicode newline character.',
250 '<script>malicious_code()<\/script>\u2029Vanilla text',
251 '&lt;script&gt;malicious_code()&lt;/script&gt;\u2029Vanilla text');
252 }
253
254 function testJsInjectionWithIgnorableNonTagChar() {
255 assertLinkify(
256 'Angle brackets are normalized even when followed by an ignorable ' +
257 'non-tag character.',
258 '<\u0000img onerror=alert(1337) src=\n>',
259 '&lt;&#0;img onerror=alert(1337) src=\n&gt;');
260 }
261
262 function testJsInjectionWithTextarea() {
263 assertLinkify(
264 'Putting the result in a textarea can\'t cause other textarea text to ' +
265 'be treated as tag content.',
266 '</textarea', '&lt;/textarea');
267 }
268
269 function testJsInjectionWithNewlineConversion() {
270 assertLinkify(
271 'Any newline conversion and whitespace normalization won\'t cause tag ' +
272 'parts to be recombined.',
273 '<<br>script<br>>alert(1337)<<br>/<br>script<br>>',
274 '&lt;&lt;br&gt;script&lt;br&gt;&gt;alert(1337)&lt;&lt;br&gt;/&lt;' +
275 'br&gt;script&lt;br&gt;&gt;');
276 }
277
278 function testNoProtocolBlacklisting() {
279 assertLinkify(
280 'No protocol blacklisting.',
281 'Click: jscript:alert%281337%29\nClick: JSscript:alert%281337%29\n' +
282 'Click: VBscript:alert%281337%29\nClick: Script:alert%281337%29\n' +
283 'Click: flavascript:alert%281337%29',
284 'Click: jscript:alert%281337%29\nClick: JSscript:alert%281337%29\n' +
285 'Click: VBscript:alert%281337%29\nClick: Script:alert%281337%29\n' +
286 'Click: flavascript:alert%281337%29');
287 }
288
289 function testProtocolWhitelistingEffective() {
290 assertLinkify(
291 'Protocol whitelisting is effective.',
292 'Click httpscript:alert%281337%29\nClick mailtoscript:alert%281337%29\n' +
293 'Click j\u00A0avascript:alert%281337%29\n' +
294 'Click \u00A0javascript:alert%281337%29',
295 'Click httpscript:alert%281337%29\nClick mailtoscript:alert%281337%29\n' +
296 'Click j\u00A0avascript:alert%281337%29\n' +
297 'Click \u00A0javascript:alert%281337%29');
298 }
299
300 function testLinkifyNoOptions() {
301 goog.dom.safe.setInnerHtml(div,
302 goog.string.linkify.linkifyPlainTextAsHtml('http://www.google.com'));
303 goog.testing.dom.assertHtmlContentsMatch(
304 '<a href="http://www.google.com" target="_blank" rel="nofollow">' +
305 'http://www.google.com<\/a>',
306 div, true /* opt_strictAttributes */);
307 }
308
309 function testLinkifyOptionsNoAttributes() {
310 goog.dom.safe.setInnerHtml(div, goog.string.linkify.linkifyPlainTextAsHtml(
311 'The link for www.google.com is located somewhere in ' +
312 'https://www.google.fr/?hl=en, you should find it easily.',
313 {rel: '', target: ''}));
314 goog.testing.dom.assertHtmlContentsMatch(
315 'The link for <a href="http://www.google.com">www.google.com<\/a> is ' +
316 'located somewhere in ' +
317 '<a href="https://www.google.fr/?hl=en">https://www.google.fr/?hl=en' +
318 '<\/a>, you should find it easily.',
319 div, true /* opt_strictAttributes */);
320 }
321
322 function testLinkifyOptionsClassName() {
323 goog.dom.safe.setInnerHtml(div, goog.string.linkify.linkifyPlainTextAsHtml(
324 'Attribute with <class> name www.w3c.org.', {'class': 'link-added'}));
325 goog.testing.dom.assertHtmlContentsMatch(
326 'Attribute with &lt;class&gt; name <a href="http://www.w3c.org" ' +
327 'target="_blank" rel="nofollow" class="link-added">www.w3c.org<\/a>.',
328 div, true /* opt_strictAttributes */);
329 }
330
331 function testFindFirstUrlNoScheme() {
332 assertEquals(
333 'www.google.com', goog.string.linkify.findFirstUrl('www.google.com'));
334 }
335
336 function testFindFirstUrlNoSchemeUppercase() {
337 assertEquals(
338 'WWW.GOOGLE.COM', goog.string.linkify.findFirstUrl('WWW.GOOGLE.COM'));
339 }
340
341 function testFindFirstUrlNoSchemeMixedcase() {
342 assertEquals(
343 'WwW.GoOgLe.CoM', goog.string.linkify.findFirstUrl('WwW.GoOgLe.CoM'));
344 }
345
346 function testFindFirstUrlNoSchemeWithText() {
347 assertEquals(
348 'www.google.com',
349 goog.string.linkify.findFirstUrl('prefix www.google.com something'));
350 }
351
352 function testFindFirstUrlScheme() {
353 assertEquals(
354 'http://www.google.com',
355 goog.string.linkify.findFirstUrl('http://www.google.com'));
356 }
357
358 function testFindFirstUrlSchemeUppercase() {
359 assertEquals(
360 'HTTP://WWW.GOOGLE.COM',
361 goog.string.linkify.findFirstUrl('HTTP://WWW.GOOGLE.COM'));
362 }
363
364 function testFindFirstUrlSchemeMixedcase() {
365 assertEquals(
366 'HtTp://WwW.gOoGlE.cOm',
367 goog.string.linkify.findFirstUrl('HtTp://WwW.gOoGlE.cOm'));
368 }
369
370 function testFindFirstUrlSchemeWithText() {
371 assertEquals(
372 'http://www.google.com', goog.string.linkify.findFirstUrl(
373 'prefix http://www.google.com something'));
374 }
375
376 function testFindFirstUrlNoUrl() {
377 assertEquals(
378 '', goog.string.linkify.findFirstUrl(
379 'ygvtfr676 5v68fk uygbt85F^&%^&I%FVvc .'));
380 }
381
382 function testFindFirstEmailNoScheme() {
383 assertEquals(
384 '[email protected]', goog.string.linkify.findFirstEmail('[email protected]'));
385 }
386
387 function testFindFirstEmailNoSchemeUppercase() {
388 assertEquals(
389 '[email protected]', goog.string.linkify.findFirstEmail('[email protected]'));
390 }
391
392 function testFindFirstEmailNoSchemeMixedcase() {
393 assertEquals(
394 '[email protected]', goog.string.linkify.findFirstEmail('[email protected]'));
395 }
396
397 function testFindFirstEmailNoSchemeWithText() {
398 assertEquals(
399 '[email protected]',
400 goog.string.linkify.findFirstEmail('prefix [email protected] something'));
401 }
402
403 function testFindFirstEmailScheme() {
404 assertEquals(
405 'mailto:[email protected]',
406 goog.string.linkify.findFirstEmail('mailto:[email protected]'));
407 }
408
409 function testFindFirstEmailSchemeUppercase() {
410 assertEquals(
411 'MAILTO:[email protected]',
412 goog.string.linkify.findFirstEmail('MAILTO:[email protected]'));
413 }
414
415 function testFindFirstEmailSchemeMixedcase() {
416 assertEquals(
417 'MaIlTo:[email protected]',
418 goog.string.linkify.findFirstEmail('MaIlTo:[email protected]'));
419 }
420
421 function testFindFirstEmailSchemeWithText() {
422 assertEquals(
423 'mailto:[email protected]', goog.string.linkify.findFirstEmail(
424 'prefix mailto:[email protected] something'));
425 }
426
427 function testFindFirstEmailNoEmail() {
428 assertEquals(
429 '', goog.string.linkify.findFirstEmail(
430 'ygvtfr676 5v68fk uygbt85F^&%^&I%FVvc .'));
431 }
432
433 function testContainsPunctuation_parens() {
434 assertLinkify(
435 'Link contains parens, but does not end with them',
436 'www.google.com/abc(v1).html',
437 '<a href="http://www.google.com/abc(v1).html">' +
438 'www.google.com/abc(v1).html<\/a>');
439 }
440
441 function testEndsWithPunctuation() {
442 assertLinkify(
443 'Link ends with punctuation',
444 'Have you seen www.google.com? It\'s awesome.',
445 'Have you seen <a href="http://www.google.com">www.google.com<\/a>?' +
446 goog.string.htmlEscape(' It\'s awesome.'));
447 }
448
449 function testEndsWithPunctuation_closeParen() {
450 assertLinkify(
451 'Link inside parentheses', '(For more info see www.googl.com)',
452 '(For more info see <a href="http://www.googl.com">www.googl.com<\/a>)');
453 assertLinkify(
454 'Parentheses inside link',
455 'http://en.wikipedia.org/wiki/Titanic_(1997_film)',
456 '<a href="http://en.wikipedia.org/wiki/Titanic_(1997_film)">' +
457 'http://en.wikipedia.org/wiki/Titanic_(1997_film)<\/a>');
458 }
459
460 function testEndsWithPunctuation_openParen() {
461 assertLinkify(
462 'Link followed by open parenthesis', 'www.google.com(',
463 '<a href="http://www.google.com(">www.google.com(<\/a>');
464 }
465
466 function testEndsWithPunctuation_angles() {
467 assertLinkify(
468 'Link inside angled brackets',
469 'Here is a bibliography entry <http://www.google.com/>',
470 'Here is a bibliography entry &lt;<a href="http://www.google.com/">' +
471 'http://www.google.com/<\/a>&gt;');
472 }
473
474 function testEndsWithPunctuation_curlies() {
475 assertLinkify(
476 'Link inside curly brackets', '{http://www.google.com/}',
477 '{<a href="http://www.google.com/">' +
478 'http://www.google.com/<\/a>}');
479 }
480
481 function testEndsWithPunctuation_closingPairThenSingle() {
482 assertLinkify(
483 'Link followed by closing punctuation pair then singular punctuation',
484 'Here is a bibliography entry <http://www.google.com/>, PTAL.',
485 'Here is a bibliography entry &lt;<a href="http://www.google.com/">' +
486 'http://www.google.com/<\/a>&gt;, PTAL.');
487 }
488
489 function testEndsWithPunctuation_ellipses() {
490 assertLinkify(
491 'Link followed by three dots', 'just look it up on www.google.com...',
492 'just look it up on <a href="http://www.google.com">www.google.com' +
493 '<\/a>...');
494 }
495
496 function testBracketsInUrl() {
497 assertLinkify(
498 'Link containing brackets',
499 'before http://google.com/details?answer[0]=42 after',
500 'before <a href="http://google.com/details?answer[0]=42">' +
501 'http://google.com/details?answer[0]=42<\/a> after');
502 }
503
504 function testUrlWithExclamation() {
505 assertLinkify(
506 'URL with exclamation points', 'This is awesome www.google.com!',
507 'This is awesome <a href="http://www.google.com">www.google.com<\/a>!');
508 }
509
510 function testSpecialCharactersInUrl() {
511 assertLinkify(
512 'Link with characters that are neither reserved nor unreserved as per' +
513 'RFC 3986 but that are recognized by other Google properties.',
514 'https://www.google.com/?q=\`{|}recognized',
515 '<a href="https://www.google.com/?q=\`{|}recognized">' +
516 'https://www.google.com/?q=\`{|}recognized<\/a>');
517 }
518
519 function testUsuallyUnrecognizedCharactersAreNotInUrl() {
520 assertLinkify(
521 'Link with characters that are neither reserved nor unreserved as per' +
522 'RFC 3986 and which are not recognized by other Google properties.',
523 'https://www.google.com/?q=<^>"',
524 '<a href="https://www.google.com/?q=">' +
525 'https://www.google.com/?q=<\/a>&lt;^&gt;&quot;');
526 }
527
528 function testIpv6Url() {
529 assertLinkify(
530 'IPv6 URL', 'http://[::FFFF:129.144.52.38]:80/index.html',
531 '<a href="http://[::FFFF:129.144.52.38]:80/index.html">' +
532 'http://[::FFFF:129.144.52.38]:80/index.html<\/a>');
533 }
534
535 function testPreserveNewlines() {
536 assertLinkify(
537 'Preserving newlines', 'Example:\nhttp://www.google.com/',
538 'Example:<br>' +
539 '<a href="http://www.google.com/">http://www.google.com/<\/a>',
540 /* preserveNewlines */ true);
541 assertLinkify(
542 'Preserving newlines with no links', 'Line 1\nLine 2', 'Line 1<br>Line 2',
543 /* preserveNewlines */ true);
544 }