All Downloads are FREE. Search and download functionalities are using the official Maven repository.

goog.string.linkify_test.js Maven / Gradle / Ivy

Go to download

The Google Closure Library is a collection of JavaScript code designed for use with the Google Closure JavaScript Compiler. This non-official distribution was prepared by the ClojureScript team at http://clojure.org/

There is a newer version: 0.0-20230227-c7c0a541
Show newest version
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

goog.provide('goog.string.linkifyTest');
goog.setTestOnly('goog.string.linkifyTest');

goog.require('goog.dom.TagName');
goog.require('goog.dom.safe');
goog.require('goog.html.SafeHtml');
goog.require('goog.string');
goog.require('goog.string.linkify');
goog.require('goog.testing.dom');
goog.require('goog.testing.jsunit');

var div = document.createElement(goog.dom.TagName.DIV);

function assertLinkify(comment, input, expected, opt_preserveNewlines) {
  assertEquals(
      comment, expected,
      goog.html.SafeHtml.unwrap(
          goog.string.linkify.linkifyPlainTextAsHtml(
              input, {rel: '', target: ''}, opt_preserveNewlines)));
}

function testContainsNoLink() {
  assertLinkify(
      'Text does not contain any links', 'Text with no links in it.',
      'Text with no links in it.');
}

function testContainsALink() {
  assertLinkify(
      'Text only contains a link', 'http://www.google.com/',
      'http://www.google.com/<\/a>');
}

function testStartsWithALink() {
  assertLinkify(
      'Text starts with a link',
      'http://www.google.com/ is a well known search engine',
      'http://www.google.com/<\/a>' +
          ' is a well known search engine');
}

function testEndsWithALink() {
  assertLinkify(
      'Text ends with a link',
      'Look at this search engine: http://www.google.com/',
      'Look at this search engine: ' +
          'http://www.google.com/<\/a>');
}

function testContainsOnlyEmail() {
  assertLinkify(
      'Text only contains an email address', '[email protected]',
      '[email protected]<\/a>');
}

function testStartsWithAnEmail() {
  assertLinkify(
      'Text starts with an email address',
      '[email protected] wrote this test.',
      '[email protected]<\/a>' +
          ' wrote this test.');
}

function testEndsWithAnEmail() {
  assertLinkify(
      'Text ends with an email address',
      'This test was written by [email protected].',
      'This test was written by ' +
          '[email protected]<\/a>.');
}

function testUrlWithPortNumber() {
  assertLinkify(
      'URL with a port number', 'http://www.google.com:80/',
      'http://www.google.com:80/<\/a>');
}

function testUrlWithUserPasswordAndPortNumber() {
  assertLinkify(
      'URL with a user, a password and a port number',
      'http://lascap:[email protected]:80/s?q=a&hl=en',
      '' +
          'http://lascap:[email protected]:80/s?q=a&hl=en<\/a>');
}

function testUrlWithUnderscore() {
  assertLinkify(
      'URL with an underscore', 'http://www_foo.google.com/',
      'http://www_foo.google.com/<\/a>');
}

function testInternalUrlWithoutDomain() {
  assertLinkify(
      'Internal URL without a proper domain', 'http://tracker/1068594',
      'http://tracker/1068594<\/a>');
}

function testInternalUrlOneChar() {
  assertLinkify(
      'Internal URL with a one char domain', 'http://b',
      'http://b<\/a>');
}

function testSecureInternalUrlWithoutDomain() {
  assertLinkify(
      'Secure Internal URL without a proper domain', 'https://review/6594805',
      'https://review/6594805<\/a>');
}

function testTwoUrls() {
  assertLinkify(
      'Text with two URLs in it',
      'I use both http://www.google.com and http://yahoo.com, don\'t you?',
      'I use both http://www.google.com<\/a> ' +
          'and http://yahoo.com<\/a>, ' +
          goog.string.htmlEscape('don\'t you?'));
}

function testGetParams() {
  assertLinkify(
      'URL with GET params', 'http://google.com/?a=b&c=d&e=f',
      '' +
          'http://google.com/?a=b&c=d&e=f<\/a>');
}

function testGoogleCache() {
  assertLinkify(
      'Google search result from cache',
      'http://66.102.7.104/search?q=cache:I4LoMT6euUUJ:' +
          'www.google.com/intl/en/help/features.html+google+cache&hl=en',
      '' +
          'http://66.102.7.104/search?q=cache:I4LoMT6euUUJ:' +
          'www.google.com/intl/en/help/features.html+google+cache&hl=en' +
          '<\/a>');
}

function testUrlWithoutHttp() {
  assertLinkify(
      'URL without http protocol',
      'It\'s faster to type www.google.com without the http:// in front.',
      goog.string.htmlEscape('It\'s faster to type ') +
          'www.google.com' +
          '<\/a> without the http:// in front.');
}

function testUrlWithCapitalsWithoutHttp() {
  assertLinkify(
      'URL with capital letters without http protocol',
      'It\'s faster to type Www.google.com without the http:// in front.',
      goog.string.htmlEscape('It\'s faster to type ') +
          'Www.google.com' +
          '<\/a> without the http:// in front.');
}

function testUrlHashBang() {
  assertLinkify(
      'URL with #!', 'Another test URL: ' +
          'https://www.google.com/testurls/#!/page',
      'Another test URL: ' +
          '' +
          'https://www.google.com/testurls/#!/page<\/a>');
}

function testTextLooksLikeUrlWithoutHttp() {
  assertLinkify(
      'Text looks like an url but is not', 'This showww.is just great: www.is',
      'This showww.is just great: www.is<\/a>');
}

function testEmailWithSubdomain() {
  assertLinkify(
      'Email with a subdomain', 'Send mail to [email protected].',
      'Send mail to ' +
          '[email protected]<\/a>.');
}

function testEmailWithHyphen() {
  assertLinkify(
      'Email with a hyphen in the domain name',
      'Send mail to [email protected].',
      'Send mail to ' +
          '[email protected]<\/a>.');
}

function testEmailUsernameWithSpecialChars() {
  assertLinkify(
      'Email with a hyphen, period, and + in the user name',
      'Send mail to [email protected]',
      'Send mail to ' +
          '[email protected]<\/a>');
  assertLinkify(
      'Email with all special characters in the user name',
      'Send mail to muad\'dib!#$%&\*/=?^_`{|}[email protected]', 'Send mail to ' +
          '' +
          'muad'dib!#$%&\*/=?^_`{|}[email protected]<\/a>');
}

function testEmailWithUnderscoreInvalid() {
  assertLinkify(
      'Email with an underscore in the domain name, which is invalid',
      'Do not email bolinfest@google_groups.com.',
      'Do not email bolinfest@google_groups.com.');
}

function testUrlNotHttp() {
  assertLinkify(
      'Url using unusual scheme',
      'Looking for some goodies: ftp://ftp.google.com/goodstuff/',
      'Looking for some goodies: ' +
          '' +
          'ftp://ftp.google.com/goodstuff/<\/a>');
}

function testJsInjection() {
  assertLinkify(
      'Text includes some javascript',
      'Welcome in hell