ng-clamp.js
function truncate(target, maxHeight) {
        if (!maxHeight) {
          return;
        }

        /**
         * Resets global variables.
         */
        function reset() {
          splitOnChars = opt.splitOnChars.slice(0);
          splitChar = splitOnChars[0];
          chunks = null;
          lastChunk = null;
        }

        var nodeValue = target.nodeValue.replace(opt.truncationChar, '');

        //Grab the next chunks
        if (!chunks) {
          //If there are more characters to try, grab the next one
          if (splitOnChars.length > 0) {
            splitChar = splitOnChars.shift();
          }
          //No characters to chunk by. Go character-by-character
          else {
            splitChar = '';
          }

          chunks = nodeValue.split(splitChar);
        }

        //If there are chunks left to remove, remove the last one and see if
        // the nodeValue fits.
        if (chunks.length > 1) {
          // console.log('chunks', chunks);
          lastChunk = chunks.pop();
          // console.log('lastChunk', lastChunk);
          applyEllipsis(target, chunks.join(splitChar));
        }
        //No more chunks can be removed using this character
        else {
          chunks = null;
        }

        //Insert the custom HTML before the truncation character
        if (truncationHTMLContainer) {
          target.nodeValue = target.nodeValue.replace(opt.truncationChar, '');
          element.innerHTML = target.nodeValue + ' ' + truncationHTMLContainer.innerHTML + opt.truncationChar;
        }

        //Search produced valid chunks
        if (chunks) {
          //It fits
          if (element.clientHeight <= maxHeight) {
            //There's still more characters to try splitting on, not quite done yet
            if (splitOnChars.length >= 0 && splitChar !== '') {
              applyEllipsis(target, chunks.join(splitChar) + splitChar + lastChunk);
              chunks = null;
            }
            //Finished!
            else {
              return element.innerHTML;
            }
          }
        }
        //No valid chunks produced
        else {
          //No valid chunks even when splitting by letter, time to move
          //on to the next node
          if (splitChar === '') {
            applyEllipsis(target, '');
            target = getLastChild(element);

            reset();
          }
        }

        //If you get here it means still too big, let's keep truncating
        if (opt.animate) {
          setTimeout(function() {
            truncate(target, maxHeight);
          }, opt.animate === true ? 10 : opt.animate);
        } else {
          return truncate(target, maxHeight);
        }
      }
function truncate(target, maxHeight) {
        if (!maxHeight) {
          return;
        }

        /**
         * Resets global variables.
         */
        function reset() {
          splitOnChars = opt.splitOnChars.slice(0);
          splitChar = splitOnChars[0];
          chunks = null;
          lastChunk = null;
        }

        var nodeValue = target.nodeValue.replace(opt.truncationChar, '');

        //Grab the next chunks
        if (!chunks) {
          //If there are more characters to try, grab the next one
          if (splitOnChars.length > 0) {
            splitChar = splitOnChars.shift();
          }
          //No characters to chunk by. Go character-by-character
          else {
            splitChar = '';
          }

          chunks = nodeValue.split(splitChar);
        }

        //If there are chunks left to remove, remove the last one and see if
        // the nodeValue fits.
        if (chunks.length > 1) {
          // console.log('chunks', chunks);
          lastChunk = chunks.pop();
          // console.log('lastChunk', lastChunk);
          applyEllipsis(target, chunks.join(splitChar));
        }
        //No more chunks can be removed using this character
        else {
          chunks = null;
        }

        //Insert the custom HTML before the truncation character
        if (truncationHTMLContainer) {
          target.nodeValue = target.nodeValue.replace(opt.truncationChar, '');
          element.innerHTML = target.nodeValue + ' ' + truncationHTMLContainer.innerHTML + opt.truncationChar;
        }

        //Search produced valid chunks
        if (chunks) {
          //It fits
          if (element.clientHeight <= maxHeight) {
            //There's still more characters to try splitting on, not quite done yet
            if (splitOnChars.length >= 0 && splitChar !== '') {
              applyEllipsis(target, chunks.join(splitChar) + splitChar + lastChunk);
              chunks = null;
            }
            //Finished!
            else {
              return element.innerHTML;
            }
          }
        }
        //No valid chunks produced
        else {
          //No valid chunks even when splitting by letter, time to move
          //on to the next node
          if (splitChar === '') {
            applyEllipsis(target, '');
            target = getLastChild(element);

            reset();
          }
        }

        //If you get here it means still too big, let's keep truncating
        if (opt.animate) {
          setTimeout(function() {
            truncate(target, maxHeight);
          }, opt.animate === true ? 10 : opt.animate);
        } else {
          return truncate(target, maxHeight);
        }
      }
function truncate ( target , maxHeight ) { if ( ! maxHeight ) { return ; } /**
         * Resets global variables.
         */ function reset ( ) { splitOnChars = opt . splitOnChars . slice ( 0 ) ; splitChar = splitOnChars [ 0 ] ; chunks = null ; lastChunk = null ; } var nodeValue = target . nodeValue . replace ( opt . truncationChar , '' ) ; //Grab the next chunks if ( ! chunks ) { //If there are more characters to try, grab the next one if ( splitOnChars . length > 0 ) { splitChar = splitOnChars . shift ( ) ; } //No characters to chunk by. Go character-by-character else { splitChar = '' ; } chunks = nodeValue . split ( splitChar ) ; } //If there are chunks left to remove, remove the last one and see if // the nodeValue fits. if ( chunks . length > 1 ) { // console.log('chunks', chunks); lastChunk = chunks . pop ( ) ; // console.log('lastChunk', lastChunk); applyEllipsis ( target , chunks . join ( splitChar ) ) ; } //No more chunks can be removed using this character else { chunks = null ; } //Insert the custom HTML before the truncation character if ( truncationHTMLContainer ) { target . nodeValue = target . nodeValue . replace ( opt . truncationChar , '' ) ; element . innerHTML = target . nodeValue + ' ' + truncationHTMLContainer . innerHTML + opt . truncationChar ; } //Search produced valid chunks if ( chunks ) { //It fits if ( element . clientHeight <= maxHeight ) { //There's still more characters to try splitting on, not quite done yet if ( splitOnChars . length >= 0 && splitChar !== '' ) { applyEllipsis ( target , chunks . join ( splitChar ) + splitChar + lastChunk ) ; chunks = null ; } //Finished! else { return element . innerHTML ; } } } //No valid chunks produced else { //No valid chunks even when splitting by letter, time to move //on to the next node if ( splitChar === '' ) { applyEllipsis ( target , '' ) ; target = getLastChild ( element ) ; reset ( ) ; } } //If you get here it means still too big, let's keep truncating if ( opt . animate ) { setTimeout ( function ( ) { truncate ( target , maxHeight ) ; } , opt . animate === true ? 10 : opt . animate ) ; } else { return truncate ( target , maxHeight ) ; } }
Removes one character at a time from the text until its width or
height is beneath the passed-in max param.
Removes one character at a time from the text until its width or height is beneath the passed - in max param .