pvdspek / jquery.autoellipsis

Auto-ellipsis plugin for jQuery
http://pvdspek.github.com/jquery.autoellipsis
MIT License
142 stars 32 forks source link

Throws exception when text contains <BR> #24

Open andrewkolesnikov opened 11 years ago

andrewkolesnikov commented 11 years ago

It's trying to append an empty text node inside <BR>.

See isolated test at http://jsfiddle.net/v8eK7/12/

Inline fix (sry sry):

--- jquery.autoellipsis.js  (revision xxXx)
+++ jquery.autoellipsis.js  (working copy)
@@ -276,7 +276,7 @@
                 lastTextNode.get(0).nodeValue = text;

             } else {
-                lastTextNode.get(0).nodeValue = '';
+                lastTextNode.remove();
salazarm commented 11 years ago

The fix should be

  function getLastTextNode(element) {
    if (element.contents().length) {
      var contents = element.contents();
      var lastNode = contents.eq(contents.length - 1);
      if (lastNode.filter(textNodeFilter).length) {
        return lastNode;
      } else {
        return getLastTextNode(lastNode);
      }
    } else {
      if (element.parent().contents().length) {
        element = element.parent()
        removeLastEmptyElements(element)
        return getLastTextNode(element)
      } else {
        element.append("");
        var contents = element.contents();
        return contents.eq(contents.length - 1);
      }
    }
  }