summaryrefslogtreecommitdiffstats
path: root/plugins/linkit_insert/html_link/html_link.js
blob: fcc4c29dcb34521a619b94f01581bb45e53bbcd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * @file
 * HTML Link insert plugin for Linkit.
 */
(function($) {
  Drupal.linkit.registerInsertPlugin('html_link', {
    insert: function(data) {
      var linkitInstance = Drupal.settings.linkit.currentInstance,
          selection = linkitInstance.selection,
          text = linkitInstance.linkContent || data.path;

       // Delete all attributes that are empty.
      for (var attr in data.attributes) {
        if (data.attributes.hasOwnProperty(attr)) {
          delete data.attributes[attr];
        }
      }

      if (selection && selection.text.length >= 1) {
        text = selection.text;
      }

      // Use document.createElement as it is mush fasten then $('<a/>).
      return $(document.createElement('a'))
        .attr(data.attributes)
        .attr('href', data.path)
        .html(text)
        // Convert the element to a string.
        .get(0)
        .outerHTML;
    }
  });
})(jQuery);