2 * jQuery Shorten plugin 1.1.0
4 * Copyright (c) 2014 Viral Patel
5 * http://viralpatel.net
7 * Licensed under the MIT license:
8 * http://www.opensource.org/licenses/mit-license.php
12 ** updated by Jeff Richardson
13 ** Updated to use strict,
14 ** IE 7 has a "bug" It is returning undefined 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 weren't added just for readability (mostly for me).
20 $.fn.shorten = function(settings) {
29 onLess: function() {},
30 onMore: function() {},
36 $.extend(config, settings);
39 if ($(this).data('jquery.shorten') && !config.force) {
42 $(this).data('jquery.shorten', true);
44 $(document).off("click", '.morelink');
50 if ($this.hasClass('less')) {
51 $this.removeClass('less');
52 $this.html(config.moreText);
53 $this.parent().prev().hide();
54 $this.parent().prev().prev().show();
57 $this.addClass('less');
58 $this.html(config.lessText);
59 $this.parent().prev().show();
60 $this.parent().prev().prev().hide();
67 return this.each(function() {
70 var content = $this.html();
71 var contentlen = $this.text().length;
72 if (contentlen > config.showChars + config.minHideChars) {
73 var c = content.substr(0, config.showChars);
74 if (c.indexOf('<') >= 0) // If there's HTML don't want to cut it
76 var inTag = false; // I'm in a tag?
77 var bag = ''; // Put the characters to be shown here
78 var countChars = 0; // Current bag size
79 var openTags = []; // Stack for opened tags, so I can close them later
82 for (var i = 0, r = 0; r <= config.showChars; i++) {
83 if (content[i] == '<' && !inTag) {
86 // This could be "tag" or "/tag"
87 tagName = content.substring(i + 1, content.indexOf('>', i));
89 // If its a closing tag
90 if (tagName[0] == '/') {
93 if (tagName != '/' + openTags[0]) {
94 config.errMsg = 'ERROR en HTML: the top of the stack should be the tag that closes';
96 openTags.shift(); // Pops the last tag from the open tag stack (the tag is closed in the retult HTML!)
100 // There are some nasty tags that don't have a close tag like <br/>
101 if (tagName.toLowerCase() != 'br') {
102 openTags.unshift(tagName); // Add to start the name of the tag that opens
106 if (inTag && content[i] == '>') {
110 if (inTag) { bag += content.charAt(i); } // Add tag name chars to the result
113 if (countChars <= config.showChars) {
114 bag += content.charAt(i); // Fix to ie 7 not allowing you to reference string characters using the []
116 } else // Now I have the characters needed
118 if (openTags.length > 0) // I have unclosed tags
120 //console.log('They were open tags');
121 //console.log(openTags);
122 for (j = 0; j < openTags.length; j++) {
123 //console.log('Cierro tag ' + openTags[j]);
124 bag += '</' + openTags[j] + '>'; // Close all tags that were opened
126 // 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
133 c = $('<div/>').html(bag + '<span class="ellip">' + config.ellipsesText + '</span>').html();
135 c+=config.ellipsesText;
138 var html = '<div class="shortcontent">' + c +
139 '</div><div class="allcontent">' + content +
140 '</div><span><a href="javascript://nop/" class="morelink">' + config.moreText + '</a></span>';
143 $this.find(".allcontent").hide(); // Hide all text
144 $('.shortcontent p:last', $this).css('margin-bottom', 0); //Remove bottom margin on last paragraph as it's likely shortened