1/* 2 * jQuery Shorten plugin 1.1.0 3 * 4 * Copyright (c) 2014 Viral Patel 5 * http://viralpatel.net 6 * 7 * Licensed under the MIT license: 8 * http://www.opensource.org/licenses/mit-license.php 9 */ 10 11/* 12** updated by Jeff Richardson 13** Updated to use strict, 14** IE 7 has a "bug" It is returning underfined when trying to reference string characters in this format 15** content[i]. IE 7 allows content.charAt(i) This works fine in all modern browsers. 16** I've also added brackets where they werent added just for readability (mostly for me). 17*/ 18 19(function($) { 20 $.fn.shorten = function(settings) { 21 "use strict"; 22 23 var config = { 24 showChars: 100, 25 ellipsesText: "...", 26 moreText: "more", 27 lessText: "less", 28 errMsg: null, 29 force: false 30 }; 31 32 if (settings) { 33 $.extend(config, settings); 34 } 35 36 if ($(this).data('jquery.shorten') && !config.force) { 37 return false; 38 } 39 $(this).data('jquery.shorten', true); 40 41 $(document).off("click", '.morelink'); 42 43 $(document).on({ 44 click: function() { 45 46 var $this = $(this); 47 if ($this.hasClass('less')) { 48 $this.removeClass('less'); 49 $this.html(config.moreText); 50 $this.parent().prev().prev().show(); // shortcontent 51 $this.parent().prev().hide(); // allcontent 52 53 } else { 54 $this.addClass('less'); 55 $this.html(config.lessText); 56 $this.parent().prev().prev().hide(); // shortcontent 57 $this.parent().prev().show(); // allcontent 58 } 59 return false; 60 } 61 }, '.morelink'); 62 63 return this.each(function() { 64 var $this = $(this); 65 66 var content = $this.html(); 67 var contentlen = $this.text().length; 68 if (contentlen > config.showChars) { 69 var c = content.substr(0, config.showChars); 70 if (c.indexOf('<') >= 0) // If there's HTML don't want to cut it 71 { 72 var inTag = false; // I'm in a tag? 73 var bag = ''; // Put the characters to be shown here 74 var countChars = 0; // Current bag size 75 var openTags = []; // Stack for opened tags, so I can close them later 76 var tagName = null; 77 78 for (var i = 0, r = 0; r <= config.showChars; i++) { 79 if (content[i] == '<' && !inTag) { 80 inTag = true; 81 82 // This could be "tag" or "/tag" 83 tagName = content.substring(i + 1, content.indexOf('>', i)); 84 85 // If its a closing tag 86 if (tagName[0] == '/') { 87 88 89 if (tagName != '/' + openTags[0]) { 90 config.errMsg = 'ERROR en HTML: the top of the stack should be the tag that closes'; 91 } else { 92 openTags.shift(); // Pops the last tag from the open tag stack (the tag is closed in the retult HTML!) 93 } 94 95 } else { 96 // There are some nasty tags that don't have a close tag like <br/> 97 if (tagName.toLowerCase() != 'br') { 98 openTags.unshift(tagName); // Add to start the name of the tag that opens 99 } 100 } 101 } 102 if (inTag && content[i] == '>') { 103 inTag = false; 104 } 105 106 if (inTag) { bag += content.charAt(i); } // Add tag name chars to the result 107 else { 108 r++; 109 if (countChars <= config.showChars) { 110 bag += content.charAt(i); // Fix to ie 7 not allowing you to reference string characters using the [] 111 countChars++; 112 } else // Now I have the characters needed 113 { 114 if (openTags.length > 0) // I have unclosed tags 115 { 116 //console.log('They were open tags'); 117 //console.log(openTags); 118 for (j = 0; j < openTags.length; j++) { 119 //console.log('Cierro tag ' + openTags[j]); 120 bag += '</' + openTags[j] + '>'; // Close all tags that were opened 121 122 // You could shift the tag from the stack to check if you end with an empty stack, that means you have closed all open tags 123 } 124 break; 125 } 126 } 127 } 128 } 129 c = $('<div/>').html(bag + '<span class="ellip">' + config.ellipsesText + '</span>').html(); 130 }else{ 131 c+=config.ellipsesText; 132 } 133 134 var html = '<div class="shortcontent">' + c + 135 '</div><div class="allcontent">' + content + 136 '</div><span><a href="javascript://nop/" class="morelink">' + config.moreText + '</a></span>'; 137 138 $this.html(html); 139 $this.find(".allcontent").hide(); // Hide all text 140 $('.shortcontent p:last', $this).css('margin-bottom', 0); //Remove bottom margin on last paragraph as it's likely shortened 141 } 142 }); 143 144 }; 145 146})(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.