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