From 97a990425827b99dee2b07f24191f8ae04914a5a Mon Sep 17 00:00:00 2001 From: Huang Yong Date: Fri, 4 Mar 2016 17:03:44 +0800 Subject: [PATCH] fix issue when elem.lastChild is not defined --- clamp.js | 5 ++++- clamp.min.js | 13 +------------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/clamp.js b/clamp.js index f13e8ad..d999868 100644 --- a/clamp.js +++ b/clamp.js @@ -113,6 +113,9 @@ * Gets an element's last child. That may be another node or a node's contents. */ function getLastChild(elem) { + if (!elem.lastChild) { + return; + } //Current element has children, need to go deeper and get last child as a text node if (elem.lastChild.children && elem.lastChild.children.length > 0) { return getLastChild(Array.prototype.slice.call(elem.children).pop()); @@ -133,7 +136,7 @@ * height is beneath the passed-in max param. */ function truncate(target, maxHeight) { - if (!maxHeight) {return;} + if (!target || !maxHeight) {return;} /** * Resets global variables. diff --git a/clamp.min.js b/clamp.min.js index 2d8783a..d310e9d 100644 --- a/clamp.min.js +++ b/clamp.min.js @@ -1,12 +1 @@ -/*! -* Clamp.js 0.5.1 -* -* Copyright 2011-2013, Joseph Schmitt http://joe.sh -* Released under the WTFPL license -* http://sam.zoy.org/wtfpl/ -*/ -(function(){window.$clamp=function(c,d){function s(a,b){n.getComputedStyle||(n.getComputedStyle=function(a,b){this.el=a;this.getPropertyValue=function(b){var c=/(\-([a-z]){1})/g;"float"==b&&(b="styleFloat");c.test(b)&&(b=b.replace(c,function(a,b,c){return c.toUpperCase()}));return a.currentStyle&&a.currentStyle[b]?a.currentStyle[b]:null};return this});return n.getComputedStyle(a,null).getPropertyValue(b)}function t(a){a=a||c.clientHeight;var b=u(c);return Math.max(Math.floor(a/b),0)}function x(a){return u(c)* -a}function u(a){var b=s(a,"line-height");"normal"==b&&(b=1.2*parseInt(s(a,"font-size")));return parseInt(b)}function l(a){if(a.lastChild.children&&00?r(Array.prototype.slice.call(t.children).pop()):t.lastChild&&t.lastChild.nodeValue&&""!=t.lastChild.nodeValue&&t.lastChild.nodeValue!=p.truncationChar?t.lastChild:(t.lastChild.parentNode.removeChild(t.lastChild),r(e)):void 0}function o(t,n){function i(){y=p.splitOnChars.slice(0),H=y[0],v=null,g=null}if(t&&n){var l=t.nodeValue.replace(p.truncationChar,"");if(v||(H=y.length>0?y.shift():"",v=l.split(H)),v.length>1?(g=v.pop(),u(t,v.join(H))):v=null,c&&(t.nodeValue=t.nodeValue.replace(p.truncationChar,""),e.innerHTML=t.nodeValue+" "+c.innerHTML+p.truncationChar),v){if(e.clientHeight<=n){if(!(y.length>=0&&""!=H))return e.innerHTML;u(t,v.join(H)+H+g),v=null}}else""==H&&(u(t,""),t=r(e),i());return p.animate?void setTimeout(function(){o(t,n)},p.animate===!0?10:p.animate):o(t,n)}}function u(e,t){e.nodeValue=t+p.truncationChar}t=t||{};var c,s=window,p={clamp:t.clamp||2,useNativeClamp:"undefined"!=typeof t.useNativeClamp?t.useNativeClamp:!0,splitOnChars:t.splitOnChars||[".","-","–","—"," "],animate:t.animate||!1,truncationChar:t.truncationChar||"…",truncationHTML:t.truncationHTML},h=e.style,d=e.innerHTML,f="undefined"!=typeof e.style.webkitLineClamp,C=p.clamp,m=C.indexOf&&(C.indexOf("px")>-1||C.indexOf("em")>-1);p.truncationHTML&&(c=document.createElement("span"),c.innerHTML=p.truncationHTML);var v,g,y=p.splitOnChars.slice(0),H=y[0];"auto"==C?C=i():m&&(C=i(parseInt(C)));var L;if(f&&p.useNativeClamp)h.overflow="hidden",h.textOverflow="ellipsis",h.webkitBoxOrient="vertical",h.display="-webkit-box",h.webkitLineClamp=C,m&&(h.height=p.clamp+"px");else{var M=l(C);M<=e.clientHeight&&(L=o(r(e),M))}return{original:d,clamped:L}}window.$clamp=e}();