nsIStyleContext* grandParentContext = aParentContext->GetParent(); if (nsnull != grandParentContext) { inheritColor = (const nsStyleColor*)grandParentContext->GetStyleData(eStyleStruct_Color); }
nsCOMPtr<nsIStyleContext> higherContext = getter_AddRefs(aParentContext->GetParent()); do { if (higherContext) { inheritColor = (const nsStyleColor*)higherContext->GetStyleData(eStyleStruct_Color); if (inheritColor && (!(inheritColor->mBackgroundFlags & NS_STYLE_BG_PROPAGATED_TO_PARENT)) || (inheritColor->mBackgroundFlags & NS_STYLE_BG_PROPAGATED_FROM_CHILD)) {  break; } higherContext = getter_AddRefs(higherContext->GetParent()); } } while (higherContext);
MapDeclarationColorInto(nsICSSDeclaration* aDeclaration,                         nsIMutableStyleContext* aContext, nsIStyleContext* aParentContext,                        nsStyleFont* aFont, nsIPresContext* aPresContext){  nsCSSColor*  ourColor;  if (NS_OK == aDeclaration->GetData(kCSSColorSID, (nsCSSStruct**)&ourColor)) {    if (nsnull != ourColor) {      nsStyleColor* color = (nsStyleColor*)aContext->GetMutableStyleData(eStyleStruct_Color);      const nsStyleColor* parentColor = color;      if (nsnull != aParentContext) {        parentColor = (const nsStyleColor*)aParentContext->GetStyleData(eStyleStruct_Color);      }      // color: color, string, inherit      if (! SetColor(ourColor->mColor, parentColor->mColor, aPresContext, color->mColor)) {      }      // cursor: enum, auto, url, inherit      nsCSSValueList*  list = ourColor->mCursor;      if (nsnull != list) {        // XXX need to deal with multiple URL values        if (eCSSUnit_Enumerated == list->mValue.GetUnit()) {          color->mCursor = list->mValue.GetIntValue();        }        else if (eCSSUnit_Auto == list->mValue.GetUnit()) {          color->mCursor = NS_STYLE_CURSOR_AUTO;        }        else if (eCSSUnit_URL == list->mValue.GetUnit()) {          list->mValue.GetStringValue(color->mCursorImage);        }        else if (eCSSUnit_Inherit == list->mValue.GetUnit()) {          color->mCursor = parentColor->mCursor;        }      }      // background-color: color, string, enum (flags), inherit      if (eCSSUnit_Inherit == ourColor->mBackColor.GetUnit()) { // do inherit first, so SetColor doesn't do it        const nsStyleColor* inheritColor = parentColor;        if (inheritColor->mBackgroundFlags & NS_STYLE_BG_PROPAGATED_TO_PARENT) {          if (nsnull != aParentContext) {            nsIStyleContext* grandParentContext = aParentContext->GetParent();            if (nsnull != grandParentContext) {              inheritColor = (const nsStyleColor*)grandParentContext->GetStyleData(eStyleStruct_Color);            }          }        }        color->mBackgroundColor = inheritColor->mBackgroundColor;        color->mBackgroundFlags &= ~NS_STYLE_BG_COLOR_TRANSPARENT;        color->mBackgroundFlags |= (inheritColor->mBackgroundFlags & NS_STYLE_BG_COLOR_TRANSPARENT);      }      else if (SetColor(ourColor->mBackColor, parentColor->mBackgroundColor,                         aPresContext, color->mBackgroundColor)) {        color->mBackgroundFlags &= ~NS_STYLE_BG_COLOR_TRANSPARENT;      }      else if (eCSSUnit_Enumerated == ourColor->mBackColor.GetUnit()) {        color->mBackgroundColor = parentColor->mBackgroundColor;        color->mBackgroundFlags |= NS_STYLE_BG_COLOR_TRANSPARENT;      }      // background-image: url, none, inherit      if (eCSSUnit_URL == ourColor->mBackImage.GetUnit()) {        ourColor->mBackImage.GetStringValue(color->mBackgroundImage);        color->mBackgroundFlags &= ~NS_STYLE_BG_IMAGE_NONE;      }      else if (eCSSUnit_None == ourColor->mBackImage.GetUnit()) {        color->mBackgroundImage.Truncate();        color->mBackgroundFlags |= NS_STYLE_BG_IMAGE_NONE;      }      else if (eCSSUnit_Inherit == ourColor->mBackImage.GetUnit()) {        color->mBackgroundImage = parentColor->mBackgroundImage;        color->mBackgroundFlags &= ~NS_STYLE_BG_IMAGE_NONE;        color->mBackgroundFlags |= (parentColor->mBackgroundFlags & NS_STYLE_BG_IMAGE_NONE);      }      // background-repeat: enum, inherit      if (eCSSUnit_Enumerated == ourColor->mBackRepeat.GetUnit()) {        color->mBackgroundRepeat = ourColor->mBackRepeat.GetIntValue();      }      else if (eCSSUnit_Inherit == ourColor->mBackRepeat.GetUnit()) {        color->mBackgroundRepeat = parentColor->mBackgroundRepeat;      }      // background-attachment: enum, inherit      if (eCSSUnit_Enumerated == ourColor->mBackAttachment.GetUnit()) {        color->mBackgroundAttachment = ourColor->mBackAttachment.GetIntValue();      }      else if (eCSSUnit_Inherit == ourColor->mBackAttachment.GetUnit()) {        color->mBackgroundAttachment = parentColor->mBackgroundAttachment;      }      // background-position: enum, length, percent (flags), inherit      if (eCSSUnit_Percent == ourColor->mBackPositionX.GetUnit()) {        color->mBackgroundXPosition = (nscoord)(100.0f * ourColor->mBackPositionX.GetPercentValue());        color->mBackgroundFlags |= NS_STYLE_BG_X_POSITION_PERCENT;        color->mBackgroundFlags &= ~NS_STYLE_BG_X_POSITION_LENGTH;      }      else if (ourColor->mBackPositionX.IsLengthUnit()) {        color->mBackgroundXPosition = CalcLength(ourColor->mBackPositionX,                                                 aFont->mFont, aPresContext);        color->mBackgroundFlags |= NS_STYLE_BG_X_POSITION_LENGTH;        color->mBackgroundFlags &= ~NS_STYLE_BG_X_POSITION_PERCENT;      }      else if (eCSSUnit_Enumerated == ourColor->mBackPositionX.GetUnit()) {        color->mBackgroundXPosition = (nscoord)ourColor->mBackPositionX.GetIntValue();        color->mBackgroundFlags |= NS_STYLE_BG_X_POSITION_PERCENT;        color->mBackgroundFlags &= ~NS_STYLE_BG_X_POSITION_LENGTH;      }      else if (eCSSUnit_Inherit == ourColor->mBackPositionX.GetUnit()) {        color->mBackgroundXPosition = parentColor->mBackgroundXPosition;        color->mBackgroundFlags &= ~(NS_STYLE_BG_X_POSITION_LENGTH | NS_STYLE_BG_X_POSITION_PERCENT);        color->mBackgroundFlags |= (parentColor->mBackgroundFlags & (NS_STYLE_BG_X_POSITION_LENGTH | NS_STYLE_BG_X_POSITION_PERCENT));      }      if (eCSSUnit_Percent == ourColor->mBackPositionY.GetUnit()) {        color->mBackgroundYPosition = (nscoord)(100.0f * ourColor->mBackPositionY.GetPercentValue());        color->mBackgroundFlags |= NS_STYLE_BG_Y_POSITION_PERCENT;        color->mBackgroundFlags &= ~NS_STYLE_BG_Y_POSITION_LENGTH;      }      else if (ourColor->mBackPositionY.IsLengthUnit()) {        color->mBackgroundYPosition = CalcLength(ourColor->mBackPositionY,                                                 aFont->mFont, aPresContext);        color->mBackgroundFlags |= NS_STYLE_BG_Y_POSITION_LENGTH;        color->mBackgroundFlags &= ~NS_STYLE_BG_Y_POSITION_PERCENT;      }      else if (eCSSUnit_Enumerated == ourColor->mBackPositionY.GetUnit()) {        color->mBackgroundYPosition = (nscoord)ourColor->mBackPositionY.GetIntValue();        color->mBackgroundFlags |= NS_STYLE_BG_Y_POSITION_PERCENT;        color->mBackgroundFlags &= ~NS_STYLE_BG_Y_POSITION_LENGTH;      }      else if (eCSSUnit_Inherit == ourColor->mBackPositionY.GetUnit()) {        color->mBackgroundYPosition = parentColor->mBackgroundYPosition;        color->mBackgroundFlags &= ~(NS_STYLE_BG_Y_POSITION_LENGTH | NS_STYLE_BG_Y_POSITION_PERCENT);        color->mBackgroundFlags |= (parentColor->mBackgroundFlags & (NS_STYLE_BG_Y_POSITION_LENGTH | NS_STYLE_BG_Y_POSITION_PERCENT));      }      // opacity: factor, percent, inherit      if (eCSSUnit_Percent == ourColor->mOpacity.GetUnit()) {        float opacity = parentColor->mOpacity * ourColor->mOpacity.GetPercentValue();        if (opacity < 0.0f) {          color->mOpacity = 0.0f;        } else if (1.0 < opacity) {          color->mOpacity = 1.0f;        }        else {          color->mOpacity = opacity;        }      }      else if (eCSSUnit_Number == ourColor->mOpacity.GetUnit()) {        color->mOpacity = ourColor->mOpacity.GetFloatValue();      }      else if (eCSSUnit_Inherit == ourColor->mOpacity.GetUnit()) {        color->mOpacity = parentColor->mOpacity;      }    }  }}