From 43fd48c1d0760793162d961b54ad24255d998f74 Mon Sep 17 00:00:00 2001 From: "Dan.Manastireanu" Date: Mon, 19 May 2014 17:57:42 +0300 Subject: [PATCH 1/2] Fix handling of inline elements (span, label etc). Closes #5 --- clamp.js | 15 ++++++++++++--- clamp.min.js | 10 +++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/clamp.js b/clamp.js index f13e8ad..e54c9f8 100644 --- a/clamp.js +++ b/clamp.js @@ -74,7 +74,7 @@ * on the current height of the element and the line-height of the text. */ function getMaxLines(height) { - var availHeight = height || element.clientHeight, + var availHeight = height || getElemHeight(element), lineHeight = getLineHeight(element); return Math.max(Math.floor(availHeight/lineHeight), 0); @@ -101,6 +101,15 @@ } return parseInt(lh); } + + /** + * Returns the height of an element as an integer (max of scroll/offset/client). + * Note: inline elements return 0 for scrollHeight and clientHeight + */ + function getElemHeight(elem) { + return Math.max(elem.scrollHeight, elem.offsetHeight, elem.clientHeight); + } + // MEAT AND POTATOES (MMMM, POTATOES...) ______________________________________ @@ -183,7 +192,7 @@ //Search produced valid chunks if (chunks) { //It fits - if (element.clientHeight <= maxHeight) { + if (getElemHeight(element) <= maxHeight) { //There's still more characters to try splitting on, not quite done yet if (splitOnChars.length >= 0 && splitChar != '') { applyEllipsis(target, chunks.join(splitChar) + splitChar + lastChunk); @@ -246,7 +255,7 @@ } else { var height = getMaxHeight(clampValue); - if (height <= element.clientHeight) { + if (height <= getElemHeight(element)) { clampedText = truncate(getLastChild(element), height); } } diff --git a/clamp.min.js b/clamp.min.js index 2d8783a..26bde5e 100644 --- a/clamp.min.js +++ b/clamp.min.js @@ -5,8 +5,8 @@ * Released under the WTFPL license * http://sam.zoy.org/wtfpl/ */ -(function(){window.$clamp=function(c,d){function s(a,b){n.getComputedStyle||(n.getComputedStyle=function(a,b){this.el=a;this.getPropertyValue=function(b){var c=/(\-([a-z]){1})/g;"float"==b&&(b="styleFloat");c.test(b)&&(b=b.replace(c,function(a,b,c){return c.toUpperCase()}));return a.currentStyle&&a.currentStyle[b]?a.currentStyle[b]:null};return this});return n.getComputedStyle(a,null).getPropertyValue(b)}function t(a){a=a||c.clientHeight;var b=u(c);return Math.max(Math.floor(a/b),0)}function x(a){return u(c)* -a}function u(a){var b=s(a,"line-height");"normal"==b&&(b=1.2*parseInt(s(a,"font-size")));return parseInt(b)}function l(a){if(a.lastChild.children&&0 Date: Mon, 19 May 2014 18:20:40 +0300 Subject: [PATCH 2/2] Incremented version --- clamp.js | 2 +- clamp.min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clamp.js b/clamp.js index e54c9f8..777bfe9 100644 --- a/clamp.js +++ b/clamp.js @@ -1,5 +1,5 @@ /*! -* Clamp.js 0.5.1 +* Clamp.js 0.5.2 * * Copyright 2011-2013, Joseph Schmitt http://joe.sh * Released under the WTFPL license diff --git a/clamp.min.js b/clamp.min.js index 26bde5e..334f36a 100644 --- a/clamp.min.js +++ b/clamp.min.js @@ -1,5 +1,5 @@ /*! -* Clamp.js 0.5.1 +* Clamp.js 0.5.2 * * Copyright 2011-2013, Joseph Schmitt http://joe.sh * Released under the WTFPL license