Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion trunk8.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"url": "https://raw.github.com/rviscomi/trunk8/master/LICENSE"
}],
"dependencies": {
"jquery": ">=1"
"jquery": ">=1",
"jquery.actual": ">=1"
},
"description": "trunk8 is an intelligent text truncation plugin to jQuery. When applied to a large block of text, trunk8 will cut off just enough text to prevent it from spilling over. Unlike conventional truncation that just limits the character length of text, trunk8 measures the content area for spill-over and intelligently chooses the text that best fits in the given space.",
"keywords": [
Expand Down
64 changes: 32 additions & 32 deletions trunk8.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**!
* trunk8 v1.3.3
* https://github.com/rviscomi/trunk8
*
*
* Copyright 2012 Rick Viscomi
* Released under the MIT License.
*
*
* Date: September 26, 2012
*/

Expand All @@ -22,21 +22,21 @@
WIDTH = {
auto: 'auto'
};

function trunk8(element) {
this.$element = $(element);
this.original_text = $.trim(this.$element.html());
this.settings = $.extend({}, $.fn.trunk8.defaults);
}

trunk8.prototype.updateSettings = function (options) {
this.settings = $.extend(this.settings, options);
};

function stripHTML(html) {
var tmp = document.createElement("DIV");
tmp.innerHTML = html;

if (typeof tmp.textContent != 'undefined') {
return tmp.textContent;
}
Expand Down Expand Up @@ -148,7 +148,7 @@
bite,
text,
htmlObject;

/* Reset the field to the original string. */
this.html(str);
text = this.text();
Expand All @@ -163,7 +163,7 @@

if (width === WIDTH.auto) {
/* Assuming there is no "overflow: hidden". */
if (this.height() <= line_height) {
if (this.actual('height') <= line_height) {
/* Text is already at the optimal trunkage. */
return;
}
Expand All @@ -175,17 +175,17 @@

while (lower <= upper) {
bite_size = lower + ((upper - lower) >> 1);

bite = utils.eatStr(str, side, length - bite_size, fill);

if (parseHTML && htmlObject) {
bite = rebuildHtmlFromBite(bite, htmlObject, fill);
}

this.html(bite);

/* Check for overflow. */
if (this.height() > line_height) {
if (this.actual('height') > line_height) {
upper = bite_size - 1;
}
else {
Expand All @@ -198,10 +198,10 @@

/* Reset the content to eliminate possible existing scroll bars. */
this.html('');

/* Display the biggest bite. */
this.html(max_bite);

if (settings.tooltip) {
this.attr('title', text);
}
Expand All @@ -212,7 +212,7 @@
bite = utils.eatStr(str, side, bite_size, fill);

this.html(bite);

if (settings.tooltip) {
this.attr('title', str);
}
Expand All @@ -229,13 +229,13 @@
return this.each(function () {
var $this = $(this),
data = $this.data('trunk8');

if (!data) {
$this.data('trunk8', (data = new trunk8(this)));
}

data.updateSettings(options);

truncate.call($this);
});
},
Expand All @@ -244,7 +244,7 @@
update: function (new_string) {
return this.each(function () {
var $this = $(this);

/* Update text. */
if (new_string) {
$this.data('trunk8').original_text = new_string;
Expand All @@ -254,12 +254,12 @@
truncate.call($this);
});
},

revert: function () {
return this.each(function () {
/* Get original text. */
var text = $(this).data('trunk8').original_text;

/* Revert element to original text. */
$(this).html(text);
});
Expand All @@ -283,7 +283,7 @@
if (utils.eatStr.cache[key]) {
return utils.eatStr.cache[key];
}

/* Common error handling. */
if ((typeof str !== 'string') || (length === 0)) {
$.error('Invalid source string "' + str + '".');
Expand All @@ -305,12 +305,12 @@
/* str... */
return utils.eatStr.cache[key] =
$.trim(str.substr(0, length - bite_size)) + fill;

case SIDES.left:
/* ...str */
return utils.eatStr.cache[key] =
fill + $.trim(str.substr(bite_size));

case SIDES.center:
/* Bit-shift to the right by one === Math.floor(x / 2) */
half_length = length >> 1; // halve the length
Expand All @@ -321,12 +321,12 @@
$.trim(utils.eatStr(str.substr(0, length - half_length), SIDES.right, bite_size - half_bite_size, '')) +
fill +
$.trim(utils.eatStr(str.substr(length - half_length), SIDES.left, half_bite_size, ''));

default:
$.error('Invalid side "' + side + '".');
}
},

getLineHeight: function (elem) {
var floats = $(elem).css('float');
if (floats !== 'none') {
Expand All @@ -336,20 +336,20 @@
if (pos === 'absolute') {
$(elem).css('position', 'static');
}

var html = $(elem).html(),
wrapper_id = 'line-height-test',
line_height;

/* Set the content to a small single character and wrap. */
$(elem).html('i').wrap('<div id="' + wrapper_id + '" />');

/* Calculate the line height by measuring the wrapper.*/
line_height = $('#' + wrapper_id).innerHeight();
line_height = $('#' + wrapper_id).actual('height');

/* Remove the wrapper and reset the content. */
$(elem).html(html).css({ 'float': floats, 'position': pos }).unwrap();

return line_height;
}
};
Expand All @@ -358,7 +358,7 @@
utils.eatStr.generateKey = function () {
return Array.prototype.join.call(arguments, '');
};

$.fn.trunk8 = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
Expand All @@ -370,7 +370,7 @@
$.error('Method ' + method + ' does not exist on jQuery.trunk8');
}
};

/* Default trunk8 settings. */
$.fn.trunk8.defaults = {
fill: '&hellip;',
Expand Down