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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Clamps (ie. cuts off) an HTML element's content by adding ellipsis to it if the
content inside is too long.

# use
```
npm i xx-clamp-js
```

# Sample Usage

Expand Down
6 changes: 3 additions & 3 deletions clamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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 || Math.floor(element.getBoundingClientRect().height),
lineHeight = getLineHeight(element);

return Math.max(Math.floor(availHeight / lineHeight), 0);
Expand Down Expand Up @@ -196,7 +196,7 @@
//Search produced valid chunks
if (chunks) {
//It fits
if (element.clientHeight <= maxHeight) {
if (Math.floor(element.getBoundingClientRect().height) <= 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 @@ -256,7 +256,7 @@
}
} else {
var height = getMaxHeight(clampValue);
if (height <= element.clientHeight) {
if (height < Math.floor(element.getBoundingClientRect().height)) {
clampedText = truncate(getLastChild(element), height);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"type": "git",
"url": "https://github.com/xavi160/Clamp.js.git"
}
}
}