% Prevents erroneous page breaks,
% as proposed by David Carlisle,
% http://tex.stackexchange.com/questions/241543.

\makeatletter

\def\mdf@put@frame@ii{%
  \setlength{\mdf@freevspace@length}{\vsize}%
    \ifbool{mdf@repeatframetitle}%
      {%repeating title -- more robust using hooks.
       \toggletrue{mdf@notfirstframetitle}%
       \splitmaxdepth\z@ \splittopskip\z@%
       \setbox\mdf@splitbox@one=\vbox{\break\unvbox\mdf@splitbox@one}%
       \mdf@ignorevbadness%
       \setbox0=\vsplit\mdf@splitbox@one to \z@\relax%
       \setbox\mdf@splitbox@one=\vbox{\unvbox\mdf@splitbox@one}%
       \setbox\mdf@splitbox@one\vbox%
          {%
           \parskip\z@\relax%
           \parindent\z@\relax%
           \vskip\mdf@frametitleaboveskip@length%
           \unvcopy\mdf@frametitlebox\relax%
           \mdf@@frametitlerule\relax%
           \unvbox\mdf@splitbox@one\relax%
          }%
       \setbox\mdf@splitbox@one=\vbox{\unvbox\mdf@splitbox@one}%
      }{}%
  \setlength{\dimen@}{\dimexpr\ht\mdf@splitbox@one+\dp\mdf@splitbox@one\relax}%
  \mdf@dolist{\mdf@advancelength@add{\dimen@}}%
        {innerbottommargin,innerlinewidth,middlelinewidth,outerlinewidth}%
  \ifboolexpr { bool {mdf@everyline} and bool {mdf@topline} }%
    {%
     \mdf@dolist{\mdf@advancelength@add{\dimen@}}%
        {innerlinewidth,middlelinewidth,outerlinewidth}%
    }{}%
   \notbool{mdf@bottomline}%remove bottom if neccessary
     {%
       \mdf@dolist{\mdf@advancelength@sub{\dimen@}}%
         {innerlinewidth,middlelinewidth,outerlinewidth}%
     }{}%
   \ifdimgreater{\dimen@}{\mdf@freevspace@length}%
    {%have a middle box
     \advance\mdf@freevspace@length by -\mdf@splitbottomskip@length\relax%
     \ifbool{mdf@everyline}%regard top and bottom if everyline is used
       {%
        \ifbool{mdf@topline}%
          {%
           \mdf@dolist{\mdf@advancelength@sub{\mdf@freevspace@length}}%
             {innerlinewidth,middlelinewidth,outerlinewidth}%
          }{}%
        \ifbool{mdf@bottomline}%
          {%
           \mdf@dolist{\mdf@advancelength@sub{\mdf@freevspace@length}}%
             {innerlinewidth,middlelinewidth,outerlinewidth}%
          }{}%
       }{}%
     \setbox\mdf@splitbox@save=\vbox{\unvcopy\mdf@splitbox@one}%
     \splitmaxdepth\z@ \splittopskip\mdf@splittopskip@length%
     \mdf@ignorevbadness%
     \setbox\mdf@splitbox@two\vsplit\mdf@splitbox@one to \mdf@freevspace@length%
     \setbox\mdf@splitbox@two\vbox{\unvbox\mdf@splitbox@two}%
     \setbox\mdf@splitbox@one\vbox{\unvbox\mdf@splitbox@one}%
     \ifdimgreater{\ht\mdf@splitbox@two+\dp\mdf@splitbox@two}%
                  {\mdf@freevspace@length}%
       {%splitted wrong
        \mdf@PackageInfo{Box was splittet wrong^^J
                         starting loop to iterate
                         the splitting point\MessageBreak}%
        \dimen@i=\mdf@freevspace@length%\relax
        \@tempcnta=\z@\relax
        \loop
        \ifdim\dimexpr\ht\mdf@splitbox@two+\dp\mdf@splitbox@two\relax>%
              \mdf@freevspace@length\relax
          \advance\dimen@i by -\p@\relax%
          \advance\@tempcnta by \@ne\relax%
          \ifnum\@tempcnta>100
            \let\iterate\relax%
            \mdf@PackageWarning{correct box splittet fails^^J
                                It seems you are using a non splittable
                                contents\MessageBreak}%
          \fi
          \mdf@ignorevbadness%
          \setbox\mdf@splitbox@one=\vbox{\unvcopy\mdf@splitbox@save}%
          \splitmaxdepth\z@ \splittopskip\mdf@splittopskip@length%
          \mdf@ignorevbadness%
          \setbox\mdf@splitbox@two\vsplit\mdf@splitbox@one to \dimen@i\relax%
          \setbox\mdf@splitbox@two\vbox{\unvbox\mdf@splitbox@two}%
          \setbox\mdf@splitbox@one\vbox{\unvbox\mdf@splitbox@one}%
        \repeat%
       }{}%
     \ifvoid\mdf@splitbox@one\relax%
        \mdf@PackageWarning{You got a bad break because the splittet box is
                            empty^^J
                            You have to change the page settings^^J
                            like enlargethispage or something else^^J
                            the package increases do
                            \enlargethispage{\baselineskip}\MessageBreak}%
        \setbox\mdf@splitbox@one=\vbox{\unvcopy\mdf@splitbox@save}%
        \enlargethispage{\baselineskip}%
        \def\mdf@reserved@a{\mdf@put@frame@ii}%
     \else
      \setbox\mdf@splitbox@two=\vbox%
                   {%
                    \hrule \@height\z@ \@width\mdfboundingboxwidth\relax%
                    \unvcopy\mdf@splitbox@two%
                   }%
        \begingroup%
          \mdf@@setzref%
          \booltrue{mdfmiddleframe}%
          \mdf@beforebreak%
          \mdf@putbox@middle%
          \mdf@afterbreak%
        \endgroup%
          \hrule \@height\z@ \@width\hsize%
          \vfill\eject%
          \def\mdf@reserved@a{\mdf@put@frame@ii}%
        \fi
     }%End middle box case
     {%start last box case
      \ifvoid\mdf@splitbox@one
           \mdf@PackageWarning{You got a bad break\MessageBreak
                               because the last split box is empty\MessageBreak
                               You have to change the settings1}%%
%CHANGE:  leave final box as void
\iffalse
 \setbox\mdf@splitbox@one=\vbox%
                  {%
                   \unvbox\mdf@splitbox@one%
                   \hrule \@height\z@ \@width\mdfboundingboxwidth
                  }%
\fi
%end CHANGE
      \fi%
      \ifdimless{\ht\mdf@splitbox@one}{1sp}%
         {%
          \mdf@PackageWarning{You got a bad break\MessageBreak
                              because the last split box is empty\MessageBreak
                              You have to change the settings2}%

          \let\mdf@reserved@a\relax%
%CHANGE: add discard final box
\iffalse
 \setbox\mdf@splitbox@one=\vbox%
                 {%
                  \unvbox\mdf@splitbox@one%
                  \hrule \@height\z@ \@width\mdfboundingboxwidth
                 }%
\fi
%END CHANGE
\setbox\mdf@splitbox@one\box\voidb@x
         }{}%
      \begingroup%
      \toggletrue{mdflastframe}%
      \mdf@beforebreak%
%Debug{\color{red}\rule{\textwidth}{1pt}}
      \begingroup%
        \mdf@@setzref%
        \booltrue{mdflastframe}%
        \mdf@beforelastframe%
        \mdf@putbox@second%
        \mdf@afterlastframe%
      \endgroup%
      \hrule \@height\z@ \@width\hsize%
      \mdf@afterbreak%
      \endgroup%
      \let\mdf@reserved@a\relax%
     }%
  \mdf@reserved@a%
}

\makeatother
