function fitText($container, $target, $document) {
    var containerBottom = $container.offset().top + $container.innerHeight();
    $document.contents().each(function () {
      var thisBottom, $node, $clone, words,
      i = 0,
      text = "",
      rest = "";

      // Proper DOM node. Attempt to fit.
      if (this.nodeType === 1) {
        $node = H5P.jQuery(this);
        $target.append($node); // Need to append it here to get height calculated by browser.
        thisBottom = $node.offset().top + $node.outerHeight();
        if (thisBottom > containerBottom) {
          // Pull back to the document.
          $clone = $node.clone();
          $document.prepend($clone);
          $node.empty();
          fitText($container, $node, $clone);
          return false;
        }
      } else if (this.nodeType === 3) {
        // Text node. Might need to split.
        $target.append(this);
        // Test if $target overflows.
        thisBottom = $target.offset().top + $target.outerHeight();
        if (thisBottom > containerBottom) {
          words = this.data.split(' ');
          do {
            i++;
            text = words.slice(0, i).join(" ");
            rest = words.slice(i).join(" ");
            this.replaceData(0, this.data.length, text);
            thisBottom = $target.offset().top + $target.outerHeight();
          } while (thisBottom < containerBottom && i < words.length);
          // Need to backtrack one word.
          text = words.slice(0, i-1).join(" ");
          rest = words.slice(i-1).join(" ");
          this.replaceData(0, this.data.length, text);
          $document.prepend(rest);

          return false;
        }
      } else {
        // Ignore. Probably a comment.
      }
    });
  }
// if the parent node does not fit.
if the parent node does not fit.
if the parent node does not fit .
function fitText($container, $target, $document) {
    var containerBottom = $container.offset().top + $container.innerHeight();
    $document.contents().each(function () {
      var thisBottom, $node, $clone, words,
      i = 0,
      text = "",
      rest = "";
      if (this.nodeType === 1) {
        $node = H5P.jQuery(this);
        $target.append($node); 
        thisBottom = $node.offset().top + $node.outerHeight();
        if (thisBottom > containerBottom) {
          $clone = $node.clone();
          $document.prepend($clone);
          $node.empty();
          fitText($container, $node, $clone);
          return false;
        }
      } else if (this.nodeType === 3) {
        $target.append(this);
        thisBottom = $target.offset().top + $target.outerHeight();
        if (thisBottom > containerBottom) {
          words = this.data.split(' ');
          do {
            i++;
            text = words.slice(0, i).join(" ");
            rest = words.slice(i).join(" ");
            this.replaceData(0, this.data.length, text);
            thisBottom = $target.offset().top + $target.outerHeight();
          } while (thisBottom < containerBottom && i < words.length);
          text = words.slice(0, i-1).join(" ");
          rest = words.slice(i-1).join(" ");
          this.replaceData(0, this.data.length, text);
          $document.prepend(rest);
          return false;
        }
      } else {
      }
    });
  }
function fitText ( $container , $target , $document ) { var containerBottom = $container . offset ( ) . top + $container . innerHeight ( ) ; $document . contents ( ) . each ( function ( ) { var thisBottom , $node , $clone , words , i = 0 , text = "" , rest = "" ; if ( this . nodeType === 1 ) { $node = H5P . jQuery ( this ) ; $target . append ( $node ) ; thisBottom = $node . offset ( ) . top + $node . outerHeight ( ) ; if ( thisBottom > containerBottom ) { $clone = $node . clone ( ) ; $document . prepend ( $clone ) ; $node . empty ( ) ; fitText ( $container , $node , $clone ) ; return false ; } } else if ( this . nodeType === 3 ) { $target . append ( this ) ; thisBottom = $target . offset ( ) . top + $target . outerHeight ( ) ; if ( thisBottom > containerBottom ) { words = this . data . split ( ' ' ) ; do { i ++ ; text = words . slice ( 0 , i ) . join ( " " ) ; rest = words . slice ( i ) . join ( " " ) ; this . replaceData ( 0 , this . data . length , text ) ; thisBottom = $target . offset ( ) . top + $target . outerHeight ( ) ; } while ( thisBottom < containerBottom && i < words . length ) ; text = words . slice ( 0 , i - 1 ) . join ( " " ) ; rest = words . slice ( i - 1 ) . join ( " " ) ; this . replaceData ( 0 , this . data . length , text ) ; $document . prepend ( rest ) ; return false ; } } else { } } ) ; }
if the parent node does not fit.
if the parent node does not fit .
// Proper DOM node. Attempt to fit. // Need to append it here to get height calculated by browser. // Pull back to the document. // Text node. Might need to split. // Test if $target overflows. // Need to backtrack one word. // Ignore. Probably a comment.