Skip to content

Commit 9ac3db2

Browse files
author
Raul Alvarez
committed
Fix handling of inline elements josephschmitt#18
1 parent 9a837a5 commit 9ac3db2

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

clamp.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
* on the current height of the element and the line-height of the text.
8080
*/
8181
function getMaxLines(height) {
82-
var availHeight = height || element.clientHeight,
82+
var availHeight = height || getElemHeight(element),
8383
lineHeight = getLineHeight(element);
8484

8585
return Math.max(Math.floor(availHeight / lineHeight), 0);
@@ -107,6 +107,14 @@
107107
return parseInt(lh);
108108
}
109109

110+
/**
111+
* Returns the height of an element as an integer (max of scroll/offset/client).
112+
* Note: inline elements return 0 for scrollHeight and clientHeight
113+
*/
114+
function getElemHeight(elem) {
115+
return Math.max(elem.scrollHeight, elem.offsetHeight, elem.clientHeight);
116+
}
117+
110118
// MEAT AND POTATOES (MMMM, POTATOES...) ______________________________________
111119
var splitOnChars = opt.splitOnChars.slice(0),
112120
splitChar = splitOnChars[0],
@@ -198,7 +206,7 @@
198206
//Search produced valid chunks
199207
if (chunks) {
200208
//It fits
201-
if (element.clientHeight <= maxHeight) {
209+
if (getElemHeight(element) <= maxHeight) {
202210
//There's still more characters to try splitting on, not quite done yet
203211
if (splitOnChars.length >= 0 && splitChar != "") {
204212
applyEllipsis(
@@ -252,7 +260,7 @@
252260

253261
var clampedText,
254262
height = getMaxHeight(clampValue),
255-
isHigher = height < element.clientHeight;
263+
isHigher = height < getElemHeight(element);
256264
if (supportsNativeClamp && opt.useNativeClamp) {
257265
sty.overflow = "hidden";
258266
sty.textOverflow = "ellipsis";

0 commit comments

Comments
 (0)