Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of inline elements (span, label etc). Closes #5 #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
17 changes: 13 additions & 4 deletions clamp.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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...) ______________________________________
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -246,7 +255,7 @@
}
else {
var height = getMaxHeight(clampValue);
if (height <= element.clientHeight) {
if (height <= getElemHeight(element)) {
clampedText = truncate(getLastChild(element), height);
}
}
Expand Down
12 changes: 6 additions & 6 deletions clamp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.