forked from yowainwright/shave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.shave.min.js
8 lines (8 loc) · 1.65 KB
/
jquery.shave.min.js
1
2
3
4
5
6
7
8
/**
shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height
@version v2.5.9
@link https://github.com/yowainwright/shave#readme
@author Jeff Wainwright <[email protected]> (jeffry.in)
@license MIT
**/
!function(e){"function"==typeof define&&define.amd?define(e):e()}(function(){"use strict";if("undefined"!=typeof window){var e=window.$||window.jQuery||window.Zepto;e&&(e.fn.shave=function(e,t){return function(e,t,n){var i=2<arguments.length&&void 0!==n?n:{};if(void 0===t||isNaN(t))throw Error("maxHeight is required");var a="string"==typeof e?document.querySelectorAll(e):e;if(a){var o=i.character||"…",r=i.classname||"js-shave",s="boolean"!=typeof i.spaces||i.spaces,c='<span class="js-shave-char">'.concat(o,"</span>");"length"in a||(a=[a]);for(var d=0;d<a.length;d+=1){var h=a[d],l=h.style,f=h.querySelector(".".concat(r)),v=void 0===h.textContent?"innerText":"textContent";f&&(h.removeChild(h.querySelector(".js-shave-char")),h[v]=h[v]);var g=h[v],u=s?g.split(" "):g;if(!(u.length<2)){var m=l.height;l.height="auto";var p=l.maxHeight;if(l.maxHeight="none",h.offsetHeight<=t)l.height=m,l.maxHeight=p;else{for(var y=u.length-1,j=0,w=void 0;j<y;)w=j+y+1>>1,h[v]=s?u.slice(0,w).join(" "):u.slice(0,w),h.insertAdjacentHTML("beforeend",c),h.offsetHeight>t?y=w-1:j=w;h[v]=s?u.slice(0,y).join(" "):u.slice(0,y),h.insertAdjacentHTML("beforeend",c);var x=s?" ".concat(u.slice(y).join(" ")):u.slice(y),H=document.createTextNode(x),b=document.createElement("span");b.classList.add(r),b.style.display="none",b.appendChild(H),h.insertAdjacentElement("beforeend",b),l.height=m,l.maxHeight=p}}}}}(this,e,t),this})}});