Skip to content

Commit

Permalink
Fix handling of inline elements josephschmitt#18
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Alvarez committed Jul 22, 2019
1 parent 9a837a5 commit 9ac3db2
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions clamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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);
Expand Down Expand Up @@ -107,6 +107,14 @@
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...) ______________________________________
var splitOnChars = opt.splitOnChars.slice(0),
splitChar = splitOnChars[0],
Expand Down Expand Up @@ -198,7 +206,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(
Expand Down Expand Up @@ -252,7 +260,7 @@

var clampedText,
height = getMaxHeight(clampValue),
isHigher = height < element.clientHeight;
isHigher = height < getElemHeight(element);
if (supportsNativeClamp && opt.useNativeClamp) {
sty.overflow = "hidden";
sty.textOverflow = "ellipsis";
Expand Down

0 comments on commit 9ac3db2

Please sign in to comment.