nsIReflowCommand::ReflowType command = nsIReflowCommand::FrameInserted;
nsInlineFrame::InsertBlockFrames(nsIPresContext& aPresContext,                                 nsIPresShell& aPresShell,                                 nsIFrame* aPrevFrame,                                 nsIFrame* aFrameList){  nsresult rv = NS_OK;  PRBool generateReflowCommand = PR_FALSE;  nsIReflowCommand::ReflowType command = nsIReflowCommand::FrameInserted;  nsIFrame* target = nsnull;  if (nsnull == aPrevFrame) {    // The block is being inserted at the head of all the child    // frames.    nsInlineFrame* flow;    nsAnonymousBlockFrame* anonymousBlock = FindAnonymousBlock(&flow);    if (nsnull == anonymousBlock) {      // There are no anonymous blocks so create one and place the      // frames into it.      nsIFrame* anonymousBlock;      rv = CreateAnonymousBlock(aPresContext, aFrameList, &anonymousBlock);      if (NS_FAILED(rv)) {        return rv;      }      mFrames.InsertFrames(this, nsnull, anonymousBlock);      target = this;      generateReflowCommand = PR_TRUE;#ifdef NOISY_ANON_BLOCK      printf("InsertBlockFrames: case 1\n");#endif    }    else {      // Take all of the frames before the anonymous block, plus the      // frames in aFrameList and insert them into the anonymous      // block.      nsFrameList frames;      frames.AppendFrames(anonymousBlock, aFrameList);      nsInlineFrame* start = this;      while (start != flow) {        frames.AppendFrames(anonymousBlock, start->mFrames);        start->GetNextInFlow((nsIFrame*&) start);      }      anonymousBlock->InsertFrames2(aPresContext, aPresShell, nsnull,                                    nsnull, frames.FirstChild());#ifdef NOISY_ANON_BLOCK      printf("InsertBlockFrames: case 2\n");#endif    }  }  else {    // First see if the insertion is inside the anonymous block    nsIFrame* prevFrameParent;    aPrevFrame->GetParent(prevFrameParent);    if (nsLineLayout::TreatFrameAsBlock(prevFrameParent)) {      // The previous frame's parent is an anonymous block. This means      // that the new block frames can be safely inserted there.      nsIFrame* frame = aFrameList;      while (nsnull != frame) {        frame->SetParent(prevFrameParent);        frame->GetNextSibling(frame);      }      nsAnonymousBlockFrame* anonymousBlock;      anonymousBlock = (nsAnonymousBlockFrame*) prevFrameParent;      anonymousBlock->InsertFrames2(aPresContext, aPresShell, nsnull,                                    aPrevFrame, aFrameList);#ifdef NOISY_ANON_BLOCK      printf("InsertBlockFrames: case 3\n");#endif    }    else {      // The previous frame's parent is an inline frame. First see if      // there is an anonymous block before the insertion point.      nsInlineFrame* flow = (nsInlineFrame*) prevFrameParent;      nsAnonymousBlockFrame* anonymousBlock;      anonymousBlock = flow->FindPrevAnonymousBlock(&flow);      if (nsnull != anonymousBlock) {        // We found an anonymous block before the insertion        // point. Take all of the inline frames between the anonymous        // block and prevFrameParent and strip them away from the        // inline frames that contain them.        nsFrameList frames;        nsInlineFrame* start;        flow->GetNextInFlow((nsIFrame*&) start);   // start after anon block        while (start != prevFrameParent) {          frames.AppendFrames(anonymousBlock, start->mFrames);          start->GetNextInFlow((nsIFrame*&) start);        }        // Now append the frames just before and including aPrevFrame        // to "frames".        flow = (nsInlineFrame*) prevFrameParent;        nsIFrame* remainder;        flow->mFrames.Split(aPrevFrame, &remainder);        frames.AppendFrames(anonymousBlock, flow->mFrames);        flow->mFrames.SetFrames(remainder);        generateReflowCommand = PR_TRUE;        target = flow;        // Finally, append the block frames to "frames" and then        // append the list of frames to the anonymous block.        frames.AppendFrames(anonymousBlock, aFrameList);        anonymousBlock->AppendFrames2(aPresContext, aPresShell, nsnull,                                      frames.FirstChild());#ifdef NOISY_ANON_BLOCK        printf("InsertBlockFrames: case 4\n");#endif      }      else {        // There is no anymous block before the insertion point. See        // if there is one after the insertion point.        flow = (nsInlineFrame*) prevFrameParent;        anonymousBlock = flow->FindAnonymousBlock(&flow);        if (nsnull != anonymousBlock) {          // We found an anonymous block after the insertion          // point. Seed the list of frames to put into the anonymous          // block with the block frames being inserted.          nsFrameList frames;          frames.AppendFrames(anonymousBlock, aFrameList);          nsInlineFrame* start = (nsInlineFrame*) prevFrameParent;          // Take the frames after aPrevFrame and place them at the          // end of the frames list.          nsIFrame* remainder;          start->mFrames.Split(aPrevFrame, &remainder);          if (nsnull != remainder) {            frames.AppendFrames(anonymousBlock, remainder);          }          generateReflowCommand = PR_TRUE;          target = start;          // Gather up all of the inline frames from all of the flow          // between the insertion point and the anonymous block.          start->GetNextInFlow((nsIFrame*&) start);          while (start != flow) {            frames.AppendFrames(anonymousBlock, start->mFrames);            start->GetNextInFlow((nsIFrame*&) start);          }          // Now update the anonymous block          anonymousBlock->InsertFrames2(aPresContext, aPresShell, nsnull,                                        nsnull, frames.FirstChild());#ifdef NOISY_ANON_BLOCK          printf("InsertBlockFrames: case 5\n");#endif        }        else {          // There are no anonymous blocks so create one and place the          // frames into it.          nsIFrame* anonymousBlock;          rv = CreateAnonymousBlock(aPresContext, aFrameList, &anonymousBlock);          if (NS_FAILED(rv)) {            return rv;          }          // Insert the frame into the correct parent (it will not be          // this frame when aPrevFrame's parent != this)          flow = (nsInlineFrame*) prevFrameParent;          flow->mFrames.InsertFrames(flow, aPrevFrame, anonymousBlock);          generateReflowCommand = PR_TRUE;          target = flow;#ifdef NOISY_ANON_BLOCK          printf("InsertBlockFrames: case 6\n");#endif        }      }    }  }  if (generateReflowCommand) {    // generate a reflow command for "this"    nsIReflowCommand* reflowCmd = nsnull;    rv = NS_NewHTMLReflowCommand(&reflowCmd, target, command);    if (NS_SUCCEEDED(rv)) {      aPresShell.AppendReflowCommand(reflowCmd);      NS_RELEASE(reflowCmd);    }  }  return rv;}