From e41748a436857516d7437cdbb14a3781f7395510 Mon Sep 17 00:00:00 2001 From: Konrad Kwiatkowski Date: Wed, 19 Oct 2016 20:26:50 +0200 Subject: [PATCH] Nested white space text nodes trimming fix --- src/clamp.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/clamp.js b/src/clamp.js index 2bc9586..b79b2bf 100644 --- a/src/clamp.js +++ b/src/clamp.js @@ -135,9 +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. Nest into 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) { - return getLastChild(element.lastChild); + 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 {