diff --git a/src/clamp.js b/src/clamp.js index 0beadc2..b79b2bf 100644 --- a/src/clamp.js +++ b/src/clamp.js @@ -135,10 +135,26 @@ if (elem.lastChild.children && elem.lastChild.children.length > 0) { return getLastChild(Array.prototype.slice.call(elem.children).pop()); } - //This is the absolute last child, a text node, but something's wrong with it. Remove it and keep trying + //Handle scenario where the last child is white-space node else if (!elem.lastChild || !elem.lastChild.nodeValue || elem.lastChild.nodeValue === '' || elem.lastChild.nodeValue == opt.truncationChar) { - elem.lastChild.parentNode.removeChild(elem.lastChild); - return getLastChild(element); + var sibling = elem.lastChild; + do { + if (!sibling) { + return; + } + //TEXT_NODE + if (sibling.nodeType === 3 && ['', opt.truncationChar].indexOf(sibling.nodeValue) === -1) { + return sibling; + } + if (sibling.lastChild) { + var lastChild = getLastChild(sibling); + if (lastChild) { + return lastChild; + } + } + //Current sibling is pretty useless + sibling.parentNode.removeChild(sibling); + } while (sibling = sibling.previousSibling); } //This is the last child we want, return it else { diff --git a/src/index.js b/src/index.js index fc332f2..ff21d40 100644 --- a/src/index.js +++ b/src/index.js @@ -9,11 +9,16 @@ function Dotdotdot() { if(!(this instanceof Dotdotdot)) { throw new TypeError("Cannot call a class as a function"); } + this.update = this.update.bind(this); } Dotdotdot.prototype = Object.create(React.Component.prototype); Dotdotdot.prototype.componentDidMount = function() { this.dotdotdot(ReactDOM.findDOMNode(this.refs.container)); + window.addEventListener('resize', this.update, false); +}; +Dotdotdot.prototype.componentWillUnmount = function() { + window.removeEventListener('resize', this.update, false); }; Dotdotdot.prototype.componentDidUpdate = function() { this.dotdotdot(ReactDOM.findDOMNode(this.refs.container)); @@ -31,6 +36,9 @@ Dotdotdot.prototype.dotdotdot = function(container) { }); } }; +Dotdotdot.prototype.update = function() { + this.forceUpdate(); +}; Dotdotdot.prototype.render = function() { return React.createElement(