extlink.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. (function ($) {
  2. function extlinkAttach(context) {
  3. // Strip the host name down, removing ports, subdomains, or www.
  4. var pattern = /^(([^\/:]+?\.)*)([^\.:]{4,})((\.[a-z]{1,4})*)(:[0-9]{1,5})?$/;
  5. var host = window.location.host.replace(pattern, '$3$4');
  6. var subdomain = window.location.host.replace(pattern, '$1');
  7. // Determine what subdomains are considered internal.
  8. if (Drupal.settings.extlink.extSubdomains) {
  9. var subdomains = "([^/]*\\.)?";
  10. }
  11. else if (subdomain == 'www.' || subdomain == '') {
  12. var subdomains = "(www\\.)?";
  13. }
  14. else {
  15. var subdomains = subdomain.replace(".", "\\.");
  16. }
  17. // Build regular expressions that define an internal link.
  18. var internal_link = new RegExp("^https?://" + subdomains + host, "i");
  19. // Extra internal link matching.
  20. var extInclude = false;
  21. if (Drupal.settings.extlink.extInclude) {
  22. extInclude = new RegExp(Drupal.settings.extlink.extInclude.replace(/\\/, '\\'));
  23. }
  24. // Extra external link matching.
  25. var extExclude = false;
  26. if (Drupal.settings.extlink.extExclude) {
  27. extExclude = new RegExp(Drupal.settings.extlink.extExclude.replace(/\\/, '\\'));
  28. }
  29. // Extra external link CSS selector exclusion.
  30. var extCssExclude = false;
  31. if (Drupal.settings.extlink.extCssExclude) {
  32. extCssExclude = Drupal.settings.extlink.extCssExclude;
  33. }
  34. // Extra external link CSS selector explicit.
  35. var extCssExplicit = false;
  36. if (Drupal.settings.extlink.extCssExplicit) {
  37. extCssExplicit = Drupal.settings.extlink.extCssExplicit;
  38. }
  39. // Find all links which are NOT internal and begin with http (as opposed
  40. // to ftp://, javascript:, etc. other kinds of links.
  41. // When operating on the 'this' variable, the host has been appended to
  42. // all links by the browser, even local ones.
  43. // In jQuery 1.1 and higher, we'd use a filter method here, but it is not
  44. // available in jQuery 1.0 (Drupal 5 default).
  45. var external_links = new Array();
  46. var mailto_links = new Array();
  47. $("a:not(." + Drupal.settings.extlink.extClass + ", ." + Drupal.settings.extlink.mailtoClass + "), area:not(." + Drupal.settings.extlink.extClass + ", ." + Drupal.settings.extlink.mailtoClass + ")", context).each(function(el) {
  48. try {
  49. var url = this.href.toLowerCase();
  50. if (url.indexOf('http') == 0
  51. && (!url.match(internal_link) || (extInclude && url.match(extInclude)))
  52. && !(extExclude && url.match(extExclude))
  53. && !(extCssExclude && $(this).parents(extCssExclude).length > 0)
  54. && !(extCssExplicit && $(this).parents(extCssExplicit).length < 1)) {
  55. external_links.push(this);
  56. }
  57. // Do not include area tags with begin with mailto: (this prohibits
  58. // icons from being added to image-maps).
  59. else if (this.tagName != 'AREA'
  60. && url.indexOf('mailto:') == 0
  61. && !(extCssExclude && $(this).parents(extCssExclude).length > 0)
  62. && !(extCssExplicit && $(this).parents(extCssExplicit).length < 1)) {
  63. mailto_links.push(this);
  64. }
  65. }
  66. // IE7 throws errors often when dealing with irregular links, such as:
  67. // <a href="node/10"></a> Empty tags.
  68. // <a href="http://user:pass@example.com">example</a> User:pass syntax.
  69. catch(error) {
  70. return false;
  71. }
  72. });
  73. if (Drupal.settings.extlink.extClass) {
  74. // Apply the "ext" class to all links not containing images.
  75. if (parseFloat($().jquery) < 1.2) {
  76. $(external_links).not('[img]').addClass(Drupal.settings.extlink.extClass).each(function() { if ($(this).css('display') == 'inline') $(this).after('<span class=' + Drupal.settings.extlink.extClass + '></span>'); });
  77. }
  78. else {
  79. $(external_links).not($(external_links).find('img').parents('a')).addClass(Drupal.settings.extlink.extClass).each(function() { if ($(this).css('display') == 'inline') $(this).after('<span class=' + Drupal.settings.extlink.extClass + '></span>'); });
  80. }
  81. }
  82. if (Drupal.settings.extlink.mailtoClass) {
  83. // Apply the "mailto" class to all mailto links not containing images.
  84. if (parseFloat($().jquery) < 1.2) {
  85. $(mailto_links).not('[img]').addClass(Drupal.settings.extlink.mailtoClass).each(function() { if ($(this).css('display') == 'inline') $(this).after('<span class=' + Drupal.settings.extlink.mailtoClass + '></span>'); });
  86. }
  87. else {
  88. $(mailto_links).not($(mailto_links).find('img').parents('a')).addClass(Drupal.settings.extlink.mailtoClass).each(function() { if ($(this).css('display') == 'inline') $(this).after('<span class=' + Drupal.settings.extlink.mailtoClass + '></span>'); });
  89. }
  90. }
  91. if (Drupal.settings.extlink.extTarget) {
  92. // Apply the target attribute to all links.
  93. $(external_links).attr('target', Drupal.settings.extlink.extTarget);
  94. }
  95. if (Drupal.settings.extlink.extAlert) {
  96. // Add pop-up click-through dialog.
  97. $(external_links).click(function(e) {
  98. return confirm(Drupal.settings.extlink.extAlertText);
  99. });
  100. }
  101. // Work around for Internet Explorer box model problems.
  102. if (($.support && !($.support.boxModel === undefined) && !$.support.boxModel) || ($.browser.msie && parseInt($.browser.version) <= 7)) {
  103. $('span.ext, span.mailto').css('display', 'inline-block');
  104. }
  105. }
  106. Drupal.behaviors.extlink = {
  107. attach: function(context){
  108. extlinkAttach(context);
  109. }
  110. }
  111. })(jQuery);