From f8f9bcc4be6f51e901ab3de3ae17ff308d9bb589 Mon Sep 17 00:00:00 2001 From: "dbaron%dbaron.org" Date: Sun, 8 Jun 2003 19:52:22 +0000 Subject: [PATCH] Fix bugs with colors in tables in cases where some content comes before the start tag of BODY by rechecking the computed color of BODY more often. b=94688 Patch from Mats Palmgren . r+sr=dbaron --- content/html/style/src/nsHTMLStyleSheet.cpp | 113 +++++++------------- layout/style/nsHTMLStyleSheet.cpp | 113 +++++++------------- 2 files changed, 80 insertions(+), 146 deletions(-) diff --git a/content/html/style/src/nsHTMLStyleSheet.cpp b/content/html/style/src/nsHTMLStyleSheet.cpp index 0f61149046fd..ec4f28337545 100644 --- a/content/html/style/src/nsHTMLStyleSheet.cpp +++ b/content/html/style/src/nsHTMLStyleSheet.cpp @@ -84,19 +84,6 @@ public: nsIHTMLStyleSheet* mSheet; }; -class HTMLDocumentColorRule : public HTMLColorRule { -public: - HTMLDocumentColorRule(nsIHTMLStyleSheet* aSheet); - virtual ~HTMLDocumentColorRule(); - - NS_IMETHOD MapRuleInfoInto(nsRuleData* aRuleData); - -protected: - void Initialize(nsIPresContext* aPresContext); - - PRBool mInitialized; -}; - HTMLColorRule::HTMLColorRule(nsIHTMLStyleSheet* aSheet) : mSheet(aSheet) { @@ -134,52 +121,6 @@ HTMLColorRule::List(FILE* out, PRInt32 aIndent) const } #endif -HTMLDocumentColorRule::HTMLDocumentColorRule(nsIHTMLStyleSheet* aSheet) - : HTMLColorRule(aSheet), - mInitialized(PR_FALSE) -{ -} - -HTMLDocumentColorRule::~HTMLDocumentColorRule() -{ -} - -NS_IMETHODIMP -HTMLDocumentColorRule::MapRuleInfoInto(nsRuleData* aRuleData) -{ - if (aRuleData->mSID == eStyleStruct_Color) { - if (aRuleData->mColorData->mColor.GetUnit() == eCSSUnit_Null) { - if (!mInitialized) - Initialize(aRuleData->mPresContext); - nsCSSValue val; val.SetColorValue(mColor); - aRuleData->mColorData->mColor = val; - } - } - return NS_OK; -} - -void -HTMLDocumentColorRule::Initialize(nsIPresContext* aPresContext) -{ - aPresContext->GetDefaultColor(&mColor); // in case something below fails - - nsCOMPtr shell; - aPresContext->GetShell(getter_AddRefs(shell)); - nsCOMPtr doc; - shell->GetDocument(getter_AddRefs(doc)); - nsCOMPtr domdoc = do_QueryInterface(doc); - if (!domdoc) - return; - nsCOMPtr body; - domdoc->GetBody(getter_AddRefs(body)); - nsCOMPtr bodyContent = do_QueryInterface(body); - nsIFrame *bodyFrame; - shell->GetPrimaryFrameFor(bodyContent, &bodyFrame); - if (!bodyFrame) - return; - mColor = bodyFrame->GetStyleColor()->mColor; -} - class GenericTableRule: public nsIStyleRule { public: GenericTableRule(nsIHTMLStyleSheet* aSheet); @@ -643,7 +584,7 @@ protected: HTMLColorRule* mLinkRule; HTMLColorRule* mVisitedRule; HTMLColorRule* mActiveRule; - HTMLDocumentColorRule* mDocumentColorRule; + HTMLColorRule* mDocumentColorRule; TableTbodyRule* mTableTbodyRule; TableRowRule* mTableRowRule; TableColgroupRule* mTableColgroupRule; @@ -695,11 +636,6 @@ HTMLStyleSheetImpl::Init() return NS_ERROR_OUT_OF_MEMORY; NS_ADDREF(mTableTHRule); - mDocumentColorRule = new HTMLDocumentColorRule(this); - if (!mDocumentColorRule) - return NS_ERROR_OUT_OF_MEMORY; - NS_ADDREF(mDocumentColorRule); - return NS_OK; } @@ -799,6 +735,26 @@ HTMLStyleSheetImpl::GetStyleRuleProcessor(nsIStyleRuleProcessor*& aProcessor, return NS_OK; } +static nsresult GetBodyColor(nsIPresContext* aPresContext, nscolor* aColor) +{ + nsCOMPtr shell; + aPresContext->GetShell(getter_AddRefs(shell)); + nsCOMPtr doc; + shell->GetDocument(getter_AddRefs(doc)); + nsCOMPtr domdoc = do_QueryInterface(doc); + if (!domdoc) + return NS_ERROR_FAILURE; + nsCOMPtr body; + domdoc->GetBody(getter_AddRefs(body)); + nsCOMPtr bodyContent = do_QueryInterface(body); + nsIFrame *bodyFrame; + shell->GetPrimaryFrameFor(bodyContent, &bodyFrame); + if (!bodyFrame) + return NS_ERROR_FAILURE; + *aColor = bodyFrame->GetStyleColor()->mColor; + return NS_OK; +} + NS_IMETHODIMP HTMLStyleSheetImpl::RulesMatching(ElementRuleProcessorData* aData, nsIAtom* aMedium) @@ -850,8 +806,24 @@ HTMLStyleSheetImpl::RulesMatching(ElementRuleProcessorData* aData, ruleWalker->Forward(mTableColgroupRule); } else if (tag == nsHTMLAtoms::table) { - if (aData->mCompatMode == eCompatibility_NavQuirks) - ruleWalker->Forward(mDocumentColorRule); + if (aData->mCompatMode == eCompatibility_NavQuirks) { + nscolor bodyColor; + nsresult rv = GetBodyColor(ruleWalker->GetCurrentNode()->PresContext(), &bodyColor); + if (NS_SUCCEEDED(rv) && + (!mDocumentColorRule || bodyColor != mDocumentColorRule->mColor)) { + if (mDocumentColorRule) { + mDocumentColorRule->mSheet = nsnull; + NS_RELEASE(mDocumentColorRule); + } + mDocumentColorRule = new HTMLColorRule(this); + if (mDocumentColorRule) { + NS_ADDREF(mDocumentColorRule); + mDocumentColorRule->mColor = bodyColor; + } + } + if (mDocumentColorRule) + ruleWalker->Forward(mDocumentColorRule); + } } } // end html element @@ -895,7 +867,7 @@ HTMLStyleSheetImpl::HasAttributeDependentStyle(AttributeRuleProcessorData* aData return NS_OK; } - // Don't worry about the HTMLDocumentColorRule since it only applies + // Don't worry about the mDocumentColorRule since it only applies // to descendants of body, when we're already reresolving. // Handle the content style rules. @@ -1069,11 +1041,6 @@ HTMLStyleSheetImpl::Reset(nsIURI* aURL) mMappedAttrTable.ops = nsnull; } - mDocumentColorRule = new HTMLDocumentColorRule(this); - if (!mDocumentColorRule) - return NS_ERROR_OUT_OF_MEMORY; - NS_ADDREF(mDocumentColorRule); - return NS_OK; } diff --git a/layout/style/nsHTMLStyleSheet.cpp b/layout/style/nsHTMLStyleSheet.cpp index 0f61149046fd..ec4f28337545 100644 --- a/layout/style/nsHTMLStyleSheet.cpp +++ b/layout/style/nsHTMLStyleSheet.cpp @@ -84,19 +84,6 @@ public: nsIHTMLStyleSheet* mSheet; }; -class HTMLDocumentColorRule : public HTMLColorRule { -public: - HTMLDocumentColorRule(nsIHTMLStyleSheet* aSheet); - virtual ~HTMLDocumentColorRule(); - - NS_IMETHOD MapRuleInfoInto(nsRuleData* aRuleData); - -protected: - void Initialize(nsIPresContext* aPresContext); - - PRBool mInitialized; -}; - HTMLColorRule::HTMLColorRule(nsIHTMLStyleSheet* aSheet) : mSheet(aSheet) { @@ -134,52 +121,6 @@ HTMLColorRule::List(FILE* out, PRInt32 aIndent) const } #endif -HTMLDocumentColorRule::HTMLDocumentColorRule(nsIHTMLStyleSheet* aSheet) - : HTMLColorRule(aSheet), - mInitialized(PR_FALSE) -{ -} - -HTMLDocumentColorRule::~HTMLDocumentColorRule() -{ -} - -NS_IMETHODIMP -HTMLDocumentColorRule::MapRuleInfoInto(nsRuleData* aRuleData) -{ - if (aRuleData->mSID == eStyleStruct_Color) { - if (aRuleData->mColorData->mColor.GetUnit() == eCSSUnit_Null) { - if (!mInitialized) - Initialize(aRuleData->mPresContext); - nsCSSValue val; val.SetColorValue(mColor); - aRuleData->mColorData->mColor = val; - } - } - return NS_OK; -} - -void -HTMLDocumentColorRule::Initialize(nsIPresContext* aPresContext) -{ - aPresContext->GetDefaultColor(&mColor); // in case something below fails - - nsCOMPtr shell; - aPresContext->GetShell(getter_AddRefs(shell)); - nsCOMPtr doc; - shell->GetDocument(getter_AddRefs(doc)); - nsCOMPtr domdoc = do_QueryInterface(doc); - if (!domdoc) - return; - nsCOMPtr body; - domdoc->GetBody(getter_AddRefs(body)); - nsCOMPtr bodyContent = do_QueryInterface(body); - nsIFrame *bodyFrame; - shell->GetPrimaryFrameFor(bodyContent, &bodyFrame); - if (!bodyFrame) - return; - mColor = bodyFrame->GetStyleColor()->mColor; -} - class GenericTableRule: public nsIStyleRule { public: GenericTableRule(nsIHTMLStyleSheet* aSheet); @@ -643,7 +584,7 @@ protected: HTMLColorRule* mLinkRule; HTMLColorRule* mVisitedRule; HTMLColorRule* mActiveRule; - HTMLDocumentColorRule* mDocumentColorRule; + HTMLColorRule* mDocumentColorRule; TableTbodyRule* mTableTbodyRule; TableRowRule* mTableRowRule; TableColgroupRule* mTableColgroupRule; @@ -695,11 +636,6 @@ HTMLStyleSheetImpl::Init() return NS_ERROR_OUT_OF_MEMORY; NS_ADDREF(mTableTHRule); - mDocumentColorRule = new HTMLDocumentColorRule(this); - if (!mDocumentColorRule) - return NS_ERROR_OUT_OF_MEMORY; - NS_ADDREF(mDocumentColorRule); - return NS_OK; } @@ -799,6 +735,26 @@ HTMLStyleSheetImpl::GetStyleRuleProcessor(nsIStyleRuleProcessor*& aProcessor, return NS_OK; } +static nsresult GetBodyColor(nsIPresContext* aPresContext, nscolor* aColor) +{ + nsCOMPtr shell; + aPresContext->GetShell(getter_AddRefs(shell)); + nsCOMPtr doc; + shell->GetDocument(getter_AddRefs(doc)); + nsCOMPtr domdoc = do_QueryInterface(doc); + if (!domdoc) + return NS_ERROR_FAILURE; + nsCOMPtr body; + domdoc->GetBody(getter_AddRefs(body)); + nsCOMPtr bodyContent = do_QueryInterface(body); + nsIFrame *bodyFrame; + shell->GetPrimaryFrameFor(bodyContent, &bodyFrame); + if (!bodyFrame) + return NS_ERROR_FAILURE; + *aColor = bodyFrame->GetStyleColor()->mColor; + return NS_OK; +} + NS_IMETHODIMP HTMLStyleSheetImpl::RulesMatching(ElementRuleProcessorData* aData, nsIAtom* aMedium) @@ -850,8 +806,24 @@ HTMLStyleSheetImpl::RulesMatching(ElementRuleProcessorData* aData, ruleWalker->Forward(mTableColgroupRule); } else if (tag == nsHTMLAtoms::table) { - if (aData->mCompatMode == eCompatibility_NavQuirks) - ruleWalker->Forward(mDocumentColorRule); + if (aData->mCompatMode == eCompatibility_NavQuirks) { + nscolor bodyColor; + nsresult rv = GetBodyColor(ruleWalker->GetCurrentNode()->PresContext(), &bodyColor); + if (NS_SUCCEEDED(rv) && + (!mDocumentColorRule || bodyColor != mDocumentColorRule->mColor)) { + if (mDocumentColorRule) { + mDocumentColorRule->mSheet = nsnull; + NS_RELEASE(mDocumentColorRule); + } + mDocumentColorRule = new HTMLColorRule(this); + if (mDocumentColorRule) { + NS_ADDREF(mDocumentColorRule); + mDocumentColorRule->mColor = bodyColor; + } + } + if (mDocumentColorRule) + ruleWalker->Forward(mDocumentColorRule); + } } } // end html element @@ -895,7 +867,7 @@ HTMLStyleSheetImpl::HasAttributeDependentStyle(AttributeRuleProcessorData* aData return NS_OK; } - // Don't worry about the HTMLDocumentColorRule since it only applies + // Don't worry about the mDocumentColorRule since it only applies // to descendants of body, when we're already reresolving. // Handle the content style rules. @@ -1069,11 +1041,6 @@ HTMLStyleSheetImpl::Reset(nsIURI* aURL) mMappedAttrTable.ops = nsnull; } - mDocumentColorRule = new HTMLDocumentColorRule(this); - if (!mDocumentColorRule) - return NS_ERROR_OUT_OF_MEMORY; - NS_ADDREF(mDocumentColorRule); - return NS_OK; }