aPresContext->ResolveStyleContextForNonElement(parentContext,
aPresContext->ResolveStyleContextForNonElement(aParentContext,
FrameManager::ReResolveStyleContext(nsIPresContext* aPresContext,                                    nsIFrame* aFrame,                                    nsIStyleContext* aParentContextIn,                                    nsIContent* aParentContent,                                    PRInt32 aAttrNameSpaceID,                                    nsIAtom* aAttribute,                                    nsStyleChangeList& aChangeList,                                     PRInt32 aMinChange,                                    PRInt32& aResultChange){  nsIStyleContext* oldContext = nsnull;   nsresult result = aFrame->GetStyleContext(&oldContext);  if (NS_SUCCEEDED(result) && oldContext) {    nsIAtom* pseudoTag = nsnull;    oldContext->GetPseudoType(pseudoTag);    nsIContent* localContent = nsnull;    nsIContent* content = nsnull;    result = aFrame->GetContent(&localContent);    if (NS_SUCCEEDED(result) && localContent) {      content = localContent;    }    else {      content = aParentContent;    }    if (aParentContent && aAttribute) { // attribute came from parent, we don't care about it here when recursing      nsFrameState  frameState;      aFrame->GetFrameState(&frameState);      if (0 == (frameState & NS_FRAME_GENERATED_CONTENT)) { // keep it for generated content        aAttribute = nsnull;      }    }    nsIStyleContext* parentContext = aParentContextIn;    nsIFrame* resolvedDescendant = nsnull;    // Get the frame providing the style context. If it differs from aFrame, then    // reslove the provider first, since the provider's context is the parent of aFrame's    nsIFrame* providerFrame = nsnull;    aFrame->GetStyleContextProvider(aPresContext, &providerFrame);     ENSURE_TRUE(providerFrame);    if (providerFrame == aFrame) {      if (parentContext) {        // addref the parent since it gets released later        NS_ADDREF(parentContext);        NOISY_TRACE_FRAME("non-null parent context provided: using it and assuming already resolved",aFrame);      } else {        nsCOMPtr<nsIStyleContext> frameContext;        aFrame->GetStyleContext(getter_AddRefs(frameContext));          ENSURE_TRUE(frameContext);        parentContext = frameContext->GetParent(); /* released later */ ENSURE_TRUE(parentContext);      }    }    else {      nsIFrame* providerFrameParent;      providerFrame->GetParent(&providerFrameParent);                   ENSURE_TRUE(providerFrameParent);      NS_ASSERTION(providerFrameParent == aFrame, "invalid style context provider");      // resolve the provider first      nsCOMPtr<nsIStyleContext> providerContext;      providerFrame->GetStyleContext(getter_AddRefs(providerContext)); ENSURE_TRUE(providerContext);      nsIStyleContext* providerContextParent = providerContext->GetParent(); ENSURE_TRUE(providerContextParent);      ReResolveStyleContext(aPresContext, providerFrame, providerContextParent, content,                             aAttrNameSpaceID, aAttribute, aChangeList, aMinChange, aResultChange);      NS_RELEASE(providerContextParent);      // the provider's new context becomes the parent context of aFrame's context       providerFrame->GetStyleContext(&parentContext); /* released later */ ENSURE_TRUE(parentContext);      resolvedDescendant = providerFrame; // don't want to re-resolve the provider again    }        // do primary context    nsIStyleContext* newContext = nsnull;    if (pseudoTag) {       nsIContent* pseudoContent = (aParentContent ? aParentContent : localContent);       aPresContext->ResolvePseudoStyleContextFor(pseudoContent, pseudoTag, parentContext, PR_FALSE,                                                  &newContext);       NS_RELEASE(pseudoTag);    }    else {      NS_ASSERTION(localContent,                   "non pseudo-element frame without content node");      // XXX This frame type check is a little ugly.  However, it should      // be cheaper than doing useless style resolution on placeholder      // frames, and it should be able to go away if we make it so that      // placeholder frames don't have style contexts at all.      //      // XXX We also probably need to descend into the style contexts of      // the placeholder's out-of-flow frame.      nsCOMPtr<nsIAtom> frameType;      aFrame->GetFrameType(getter_AddRefs(frameType));      if (content->IsContentOfType(nsIContent::eELEMENT) &&          frameType != nsLayoutAtoms::placeholderFrame) {        aPresContext->ResolveStyleContextFor(content, parentContext,                                             PR_TRUE, &newContext);      } else {        aPresContext->ResolveStyleContextForNonElement(parentContext,                                                       PR_TRUE, &newContext);      }    }    NS_ASSERTION(newContext, "failed to get new style context");    if (newContext) {      if (newContext != oldContext) {        aMinChange = CaptureChange(oldContext, newContext, aFrame, content, aChangeList, aMinChange);        if (aMinChange < NS_STYLE_HINT_FRAMECHANGE) { // if frame gets regenerated, let it keep old context          aFrame->SetStyleContext(aPresContext, newContext);        }        // if old context had image and new context does not have the same image,         // stop the image load for the frame        const nsStyleBackground* oldColor = (const nsStyleBackground*)oldContext->GetStyleData(eStyleStruct_Background);         const nsStyleBackground* newColor = (const nsStyleBackground*)newContext->GetStyleData(eStyleStruct_Background);        if(oldColor->mBackgroundImage.Length() > 0 &&          oldColor->mBackgroundImage != newColor->mBackgroundImage ){          // stop the image loading for the frame, the image has changed          aPresContext->StopImagesFor(aFrame);        }      }      else {        // XXXdwh figure this out.        // oldContext->RemapStyle(aPresContext, PR_FALSE);        if (aAttribute && (aMinChange < NS_STYLE_HINT_REFLOW) &&            HasAttributeContent(oldContext, aAttrNameSpaceID, aAttribute)) {          aChangeList.AppendChange(aFrame, content, NS_STYLE_HINT_REFLOW);        }      }      NS_RELEASE(oldContext);    }    else {      NS_ERROR("resolve style context failed");      newContext = oldContext;  // new context failed, recover... (take ref)    }    // do additional contexts     PRInt32 contextIndex = -1;    while (1 == 1) {      nsIStyleContext* oldExtraContext = nsnull;      result = aFrame->GetAdditionalStyleContext(++contextIndex, &oldExtraContext);      if (NS_SUCCEEDED(result)) {        if (oldExtraContext) {          nsIStyleContext* newExtraContext = nsnull;          oldExtraContext->GetPseudoType(pseudoTag);          NS_ASSERTION(pseudoTag, "extra style context is not pseudo element");          result = aPresContext->ResolvePseudoStyleContextFor(content, pseudoTag, newContext,                                                             PR_FALSE, &newExtraContext);          NS_RELEASE(pseudoTag);          if (NS_SUCCEEDED(result) && newExtraContext) {            if (oldExtraContext != newExtraContext) {              aMinChange = CaptureChange(oldExtraContext, newExtraContext, aFrame,                                          content, aChangeList, aMinChange);              if (aMinChange < NS_STYLE_HINT_FRAMECHANGE) {                aFrame->SetAdditionalStyleContext(contextIndex, newExtraContext);              }            }            else {              // XXXdwh figure this out.              // oldExtraContext->RemapStyle(aPresContext, PR_FALSE);              if (aAttribute && (aMinChange < NS_STYLE_HINT_REFLOW) &&                  HasAttributeContent(oldContext, aAttrNameSpaceID, aAttribute)) {                aChangeList.AppendChange(aFrame, content, NS_STYLE_HINT_REFLOW);              }            }            NS_RELEASE(newExtraContext);          }          NS_RELEASE(oldExtraContext);        }      }      else {        break;      }    }    // now look for undisplayed child content and pseudos    if (localContent && mUndisplayedMap) {      UndisplayedNode* undisplayed = mUndisplayedMap->GetFirstNode(localContent);      while (undisplayed) {        nsIStyleContext* undisplayedContext = nsnull;        undisplayed->mStyle->GetPseudoType(pseudoTag);        if (undisplayed->mContent && pseudoTag == nsnull) {  // child content          aPresContext->ResolveStyleContextFor(undisplayed->mContent, newContext,                                               PR_TRUE, &undisplayedContext);        }        else {  // pseudo element          NS_ASSERTION(pseudoTag, "pseudo element without tag");          aPresContext->ResolvePseudoStyleContextFor(localContent, pseudoTag, newContext, PR_FALSE,                                                    &undisplayedContext);        }        NS_IF_RELEASE(pseudoTag);        if (undisplayedContext) {          if (undisplayedContext == undisplayed->mStyle) {            // XXXdwh figure this out.            // undisplayedContext->RemapStyle(aPresContext);          }          const nsStyleDisplay* display =                 (const nsStyleDisplay*)undisplayedContext->GetStyleData(eStyleStruct_Display);          if (display->mDisplay != NS_STYLE_DISPLAY_NONE) {            aChangeList.AppendChange(nsnull, ((undisplayed->mContent) ? undisplayed->mContent : localContent),                                      NS_STYLE_HINT_FRAMECHANGE);          }          NS_RELEASE(undisplayedContext);        }        undisplayed = undisplayed->mNext;      }    }    aResultChange = aMinChange;    if (aMinChange < NS_STYLE_HINT_FRAMECHANGE) {      // There is no need to waste time crawling into a frame's children on a frame change.      // The act of reconstructing frames will force new style contexts to be resolved on all      // of this frame's descendants anyway, so we want to avoid wasting time processing      // style contexts that we're just going to throw away anyway. - dwh      // now do children      PRInt32 listIndex = 0;      nsIAtom* childList = nsnull;      PRInt32 childChange;      nsIFrame* child;      do {        child = nsnull;        result = aFrame->FirstChild(aPresContext, childList, &child);        while ((NS_SUCCEEDED(result)) && (child)) {          nsFrameState  state;          child->GetFrameState(&state);          if (NS_FRAME_OUT_OF_FLOW != (state & NS_FRAME_OUT_OF_FLOW)) {            // only do frames that are in flow            nsCOMPtr<nsIAtom> frameType;            child->GetFrameType(getter_AddRefs(frameType));            if (nsLayoutAtoms::placeholderFrame == frameType.get()) { // placeholder              // get out of flow frame and recurse there              nsIFrame* outOfFlowFrame = ((nsPlaceholderFrame*)child)->GetOutOfFlowFrame();              NS_ASSERTION(outOfFlowFrame, "no out-of-flow frame");              if (outOfFlowFrame != resolvedDescendant) {                ReResolveStyleContext(aPresContext, outOfFlowFrame, nsnull, content,                                      aAttrNameSpaceID, aAttribute,                                      aChangeList, aMinChange, childChange);              } else {                NOISY_TRACE("out of flow frame already resolved as descendent\n");              }              // reresolve placeholder's context under out of flow frame              nsIStyleContext*  outOfFlowContext;              outOfFlowFrame->GetStyleContext(&outOfFlowContext);              ReResolveStyleContext(aPresContext, child, outOfFlowContext, content,                                    kNameSpaceID_Unknown, nsnull,                                    aChangeList, aMinChange, childChange);              NS_RELEASE(outOfFlowContext);            }            else {  // regular child frame              if (child != resolvedDescendant) {                ReResolveStyleContext(aPresContext, child, newContext, content,                                      aAttrNameSpaceID, aAttribute,                                      aChangeList, aMinChange, childChange);              } else {                NOISY_TRACE_FRAME("child frame already resolved as descendent, skipping",aFrame);              }            }          }          child->GetNextSibling(&child);        }        NS_IF_RELEASE(childList);        aFrame->GetAdditionalChildListName(listIndex++, &childList);      } while (childList);      // XXX need to do overflow frames???    }