123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- goog.provide('goog.string.linkify');
- goog.require('goog.html.SafeHtml');
- goog.require('goog.string');
- goog.string.linkify.linkifyPlainTextAsHtml = function(
- text, opt_attributes, opt_preserveNewlines) {
-
-
-
- if (text.indexOf('@') == -1 && text.indexOf('://') == -1 &&
- text.indexOf('www.') == -1 && text.indexOf('Www.') == -1 &&
- text.indexOf('WWW.') == -1) {
- return opt_preserveNewlines ?
- goog.html.SafeHtml.htmlEscapePreservingNewlines(text) :
- goog.html.SafeHtml.htmlEscape(text);
- }
- var attributesMap = {};
- for (var key in opt_attributes) {
- if (!opt_attributes[key]) {
-
- attributesMap[key] = null;
- } else {
- attributesMap[key] = opt_attributes[key];
- }
- }
-
- if (!('rel' in attributesMap)) {
- attributesMap['rel'] = 'nofollow';
- }
- if (!('target' in attributesMap)) {
- attributesMap['target'] = '_blank';
- }
- var output = [];
-
- text.replace(
- goog.string.linkify.FIND_LINKS_RE_,
- function(part, before, original, email, protocol) {
- output.push(
- opt_preserveNewlines ?
- goog.html.SafeHtml.htmlEscapePreservingNewlines(before) :
- before);
- if (!original) {
- return '';
- }
- var href = '';
-
- var linkText;
-
- var afterLink;
- if (email) {
- href = 'mailto:';
- linkText = email;
- afterLink = '';
- } else {
-
- if (!protocol) {
- href = 'http://';
- }
- var splitEndingPunctuation =
- original.match(goog.string.linkify.ENDS_WITH_PUNCTUATION_RE_);
-
-
-
-
- if (splitEndingPunctuation && !goog.string.contains(original, '(')) {
- linkText = splitEndingPunctuation[1];
- afterLink = splitEndingPunctuation[2];
- } else {
- linkText = original;
- afterLink = '';
- }
- }
- attributesMap['href'] = href + linkText;
- output.push(goog.html.SafeHtml.create('a', attributesMap, linkText));
- output.push(
- opt_preserveNewlines ?
- goog.html.SafeHtml.htmlEscapePreservingNewlines(afterLink) :
- afterLink);
- return '';
- });
- return goog.html.SafeHtml.concat(output);
- };
- goog.string.linkify.findFirstUrl = function(text) {
- var link = text.match(goog.string.linkify.URL_RE_);
- return link != null ? link[0] : '';
- };
- goog.string.linkify.findFirstEmail = function(text) {
- var email = text.match(goog.string.linkify.EMAIL_RE_);
- return email != null ? email[0] : '';
- };
- goog.string.linkify.ENDING_PUNCTUATION_CHARS_ = ':;,\\.?}\\]\\)!';
- goog.string.linkify.ENDS_WITH_PUNCTUATION_RE_ = new RegExp(
- '^(.*?)([' + goog.string.linkify.ENDING_PUNCTUATION_CHARS_ + ']+)$');
- goog.string.linkify.ACCEPTABLE_URL_CHARS_ = '\\w#-;!=?@\\[\\\\\\]_`{|}~';
- goog.string.linkify.RECOGNIZED_PROTOCOLS_ = ['https?', 'ftp'];
- goog.string.linkify.PROTOCOL_START_ =
- '(' + goog.string.linkify.RECOGNIZED_PROTOCOLS_.join('|') + ')://';
- goog.string.linkify.WWW_START_ = 'www\\.';
- goog.string.linkify.URL_RE_STRING_ = '(?:' +
- goog.string.linkify.PROTOCOL_START_ + '|' + goog.string.linkify.WWW_START_ +
- ')[' + goog.string.linkify.ACCEPTABLE_URL_CHARS_ + ']+';
- goog.string.linkify.URL_RE_ =
- new RegExp(goog.string.linkify.URL_RE_STRING_, 'i');
- goog.string.linkify.TOP_LEVEL_DOMAIN_ = '(?:com|org|net|edu|gov' +
-
- '|aero|biz|cat|coop|info|int|jobs|mobi|museum|name|pro|travel' +
- '|arpa|asia|xxx' +
-
- '|[a-z][a-z])\\b';
- goog.string.linkify.EMAIL_RE_STRING_ =
- '(?:mailto:)?([\\w.!#$%&\'*+-/=?^_`{|}~]+@[A-Za-z0-9.-]+\\.' +
- goog.string.linkify.TOP_LEVEL_DOMAIN_ + ')';
- goog.string.linkify.EMAIL_RE_ =
- new RegExp(goog.string.linkify.EMAIL_RE_STRING_, 'i');
- goog.string.linkify.FIND_LINKS_RE_ = new RegExp(
-
- '([\\S\\s]*?)(' +
-
- '\\b' + goog.string.linkify.EMAIL_RE_STRING_ + '|' +
-
- '\\b' + goog.string.linkify.URL_RE_STRING_ + '|$)',
- 'gi');
|