extlink.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @file
  3. */
  4. (function ($) {
  5. 'use strict';
  6. Drupal.extlink = Drupal.extlink || {};
  7. Drupal.extlink.attach = function (context, settings) {
  8. if (!settings.hasOwnProperty('extlink')) {
  9. return;
  10. }
  11. // Strip the host name down, removing ports, subdomains, or www.
  12. var pattern = /^(([^\/:]+?\.)*)([^\.:]{1,})((\.[a-z0-9]{1,253})*)(:[0-9]{1,5})?$/;
  13. var host = window.location.host.replace(pattern, '$2$3');
  14. var subdomain = window.location.host.replace(host, '');
  15. // Determine what subdomains are considered internal.
  16. var subdomains;
  17. if (settings.extlink.extSubdomains) {
  18. subdomains = '([^/]*\\.)?';
  19. }
  20. else if (subdomain === 'www.' || subdomain === '') {
  21. subdomains = '(www\\.)?';
  22. }
  23. else {
  24. subdomains = subdomain.replace('.', '\\.');
  25. }
  26. // Build regular expressions that define an internal link.
  27. var internal_link = new RegExp('^https?://([^@]*@)?' + subdomains + host, 'i');
  28. // Extra internal link matching.
  29. var extInclude = false;
  30. if (settings.extlink.extInclude) {
  31. extInclude = new RegExp(settings.extlink.extInclude.replace(/\\/, '\\'), 'i');
  32. }
  33. // Extra external link matching.
  34. var extExclude = false;
  35. if (settings.extlink.extExclude) {
  36. extExclude = new RegExp(settings.extlink.extExclude.replace(/\\/, '\\'), 'i');
  37. }
  38. // Extra external link CSS selector exclusion.
  39. var extCssExclude = false;
  40. if (settings.extlink.extCssExclude) {
  41. extCssExclude = settings.extlink.extCssExclude;
  42. }
  43. // Extra external link CSS selector explicit.
  44. var extCssExplicit = false;
  45. if (settings.extlink.extCssExplicit) {
  46. extCssExplicit = settings.extlink.extCssExplicit;
  47. }
  48. // Define the jQuery method (either 'append' or 'prepend') of placing the icon, defaults to 'append'.
  49. var extIconPlacement = settings.extlink.extIconPlacement || 'append';
  50. // Find all links which are NOT internal and begin with http as opposed
  51. // to ftp://, javascript:, etc. other kinds of links.
  52. // When operating on the 'this' variable, the host has been appended to
  53. // all links by the browser, even local ones.
  54. // In jQuery 1.1 and higher, we'd use a filter method here, but it is not
  55. // available in jQuery 1.0 (Drupal 5 default).
  56. var external_links = [];
  57. var mailto_links = [];
  58. $('a:not(.' + settings.extlink.extClass + ', .' + settings.extlink.mailtoClass + '), area:not(.' + settings.extlink.extClass + ', .' + settings.extlink.mailtoClass + ')', context).each(function (el) {
  59. try {
  60. var url = '';
  61. if (typeof this.href == 'string') {
  62. url = this.href.toLowerCase();
  63. }
  64. // Handle SVG links (xlink:href).
  65. else if (typeof this.href == 'object') {
  66. url = this.href.baseVal;
  67. }
  68. if (url.indexOf('http') === 0
  69. && ((!url.match(internal_link) && !(extExclude && url.match(extExclude))) || (extInclude && url.match(extInclude)))
  70. && !(extCssExclude && $(this).is(extCssExclude))
  71. && !(extCssExclude && $(this).parents(extCssExclude).length > 0)
  72. && !(extCssExplicit && $(this).parents(extCssExplicit).length < 1)) {
  73. external_links.push(this);
  74. }
  75. // Do not include area tags with begin with mailto: (this prohibits
  76. // icons from being added to image-maps).
  77. else if (this.tagName !== 'AREA'
  78. && url.indexOf('mailto:') === 0
  79. && !(extCssExclude && $(this).parents(extCssExclude).length > 0)
  80. && !(extCssExplicit && $(this).parents(extCssExplicit).length < 1)) {
  81. mailto_links.push(this);
  82. }
  83. }
  84. // IE7 throws errors often when dealing with irregular links, such as:
  85. // <a href="node/10"></a> Empty tags.
  86. // <a href="http://user:pass@example.com">example</a> User:pass syntax.
  87. catch (error) {
  88. return false;
  89. }
  90. });
  91. if (settings.extlink.extClass) {
  92. Drupal.extlink.applyClassAndSpan(external_links, settings.extlink.extClass, extIconPlacement);
  93. }
  94. if (settings.extlink.mailtoClass) {
  95. Drupal.extlink.applyClassAndSpan(mailto_links, settings.extlink.mailtoClass, extIconPlacement);
  96. }
  97. if (settings.extlink.extTarget) {
  98. // Apply the target attribute to all links.
  99. $(external_links).attr('target', settings.extlink.extTarget);
  100. // Add rel attributes noopener and noreferrer.
  101. $(external_links).attr('rel', function (i, val) {
  102. // If no rel attribute is present, create one with the values noopener and noreferrer.
  103. if (val == null) {
  104. return 'noopener noreferrer';
  105. }
  106. // Check to see if rel contains noopener or noreferrer. Add what doesn't exist.
  107. if (val.indexOf('noopener') > -1 || val.indexOf('noreferrer') > -1) {
  108. if (val.indexOf('noopener') === -1) {
  109. return val + ' noopener';
  110. }
  111. if (val.indexOf('noreferrer') === -1) {
  112. return val + ' noreferrer';
  113. }
  114. // Both noopener and noreferrer exist. Nothing needs to be added.
  115. else {
  116. return val;
  117. }
  118. }
  119. // Else, append noopener and noreferrer to val.
  120. else {
  121. return val + ' noopener noreferrer';
  122. }
  123. });
  124. }
  125. Drupal.extlink = Drupal.extlink || {};
  126. // Set up default click function for the external links popup. This should be
  127. // overridden by modules wanting to alter the popup.
  128. Drupal.extlink.popupClickHandler = Drupal.extlink.popupClickHandler || function () {
  129. if (settings.extlink.extAlert) {
  130. return confirm(settings.extlink.extAlertText);
  131. }
  132. };
  133. $(external_links).click(function (e) {
  134. return Drupal.extlink.popupClickHandler(e, this);
  135. });
  136. };
  137. /**
  138. * Apply a class and a trailing <span> to all links not containing images.
  139. *
  140. * @param {object[]} links
  141. * An array of DOM elements representing the links.
  142. * @param {string} class_name
  143. * The class to apply to the links.
  144. * @param {string} icon_placement
  145. * 'append' or 'prepend' the icon to the link.
  146. */
  147. Drupal.extlink.applyClassAndSpan = function (links, class_name, icon_placement) {
  148. var $links_to_process;
  149. if (Drupal.settings.extlink.extImgClass) {
  150. $links_to_process = $(links);
  151. }
  152. else {
  153. var links_with_images = $(links).find('img').parents('a');
  154. $links_to_process = $(links).not(links_with_images);
  155. }
  156. $links_to_process.addClass(class_name);
  157. var i;
  158. var length = $links_to_process.length;
  159. for (i = 0; i < length; i++) {
  160. var $link = $($links_to_process[i]);
  161. if ($link.css('display') === 'inline' || $link.css('display') === 'inline-block') {
  162. if (class_name === Drupal.settings.extlink.mailtoClass) {
  163. $link[icon_placement]('<span class="' + class_name + '" aria-label="' + Drupal.settings.extlink.mailtoLabel + '"></span>');
  164. }
  165. else {
  166. $link[icon_placement]('<span class="' + class_name + '" aria-label="' + Drupal.settings.extlink.extLabel + '"></span>');
  167. }
  168. }
  169. }
  170. };
  171. Drupal.behaviors.extlink = Drupal.behaviors.extlink || {};
  172. Drupal.behaviors.extlink.attach = function (context, settings) {
  173. // Backwards compatibility, for the benefit of modules overriding extlink
  174. // functionality by defining an "extlinkAttach" global function.
  175. if (typeof extlinkAttach === 'function') {
  176. extlinkAttach(context);
  177. }
  178. else {
  179. Drupal.extlink.attach(context, settings);
  180. }
  181. };
  182. })(jQuery);