PageSourceSearch

https://anvh.com/js/jquery/jquery.truncate.js

js anvh.com collected 2026-10-07 14:10:58 UTC 2,364 bytes, 82 lines download raw bytes

1(function($) {
2
3  // Matches trailing non-space characters.
4  var chop = /(\s*\S+|\s)$/;
5
6  // Return a truncated html string.  Delegates to $.fn.truncate.
7  $.truncate = function(html, options) {
8    return $('<div></div>').append(html).truncate(options).html();
9  };
10
11  // Truncate the contents of an element in place.
12  $.fn.truncate = function(options) {
13    if ($.isNumeric(options)) options = {length: options};
14    var o = $.extend({}, $.truncate.defaults, options);
15
16    return this.each(function() {
17      var self = $(this);
18
19      if (o.noBreaks) self.find('br').replaceWith(' ');
20
21      var text = self.text();
22      var excess = text.length - o.length;
23
24      if (o.stripTags) self.text(text);
25
26      // Chop off any partial words if appropriate.
27      if (o.words && excess > 0) {
28        excess = text.length - text.slice(0, o.length).replace(chop, '').length - 1;
29      }
30
31      if (excess < 0 || !excess && !o.truncated) return;
32
33      // Iterate over each child node in reverse, removing excess text.
34      $.each(self.contents().get().reverse(), function(i, el) {
35        var $el = $(el);
36        var text = $el.text();
37        var length = text.length;
38
39        // If the text is longer than the excess, remove the node and continue.
40        if (length <= excess) {
41          o.truncated = true;
42          excess -= length;
43          $el.remove();
44          return;
45        }
46
47        // Remove the excess text and append the ellipsis.
48        if (el.nodeType === 3) {
49          $(el.splitText(length - excess - 1)).replaceWith(o.ellipsis);
50          return false;
51        }
52
53        // Recursively truncate child nodes.
54        $el.truncate($.extend(o, {length: length - excess}));
55        return false;
56      });
57    });
58  };
59
60  $.truncate.defaults = {
61
62    // Strip all html elements, leaving only plain text.
63    stripTags: false,
64
65    // Only truncate at word boundaries.
66    words: false,
67
68    // Replace instances of <br> with a single space.
69    noBreaks: false,
70
71    // The maximum length of the truncated html.
72    length: Infinity,
73
74    // The character to use as the ellipsis.  The word joiner (U+2060) can be
75    // used to prevent a hanging ellipsis, but displays incorrectly in Chrome
76    // on Windows 7.
77    // http://code.google.com/p/chromium/issues/detail?id=68323
78    ellipsis: '\u2026' // '\u2060\u2026'
79
80  };
81
82})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.