From cfb43390a93aa527698e939214e7b51d802997e8 Mon Sep 17 00:00:00 2001 From: Cameron Kaiser Date: Mon, 17 Feb 2020 21:46:37 -0800 Subject: [PATCH] #591: M1467722 --- dom/base/nsGlobalWindow.cpp | 28 +------------- dom/smil/nsSMILCSSProperty.cpp | 8 +--- editor/libeditor/nsHTMLCSSUtils.cpp | 5 +-- layout/style/nsComputedDOMStyle.cpp | 60 +++++++++++++++-------------- layout/style/nsComputedDOMStyle.h | 12 +++--- 5 files changed, 41 insertions(+), 72 deletions(-) diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 1ce9ca4ae..050fe743f 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -10005,36 +10005,12 @@ nsGlobalWindow::GetComputedStyleHelperOuter(Element& aElt, { MOZ_RELEASE_ASSERT(IsOuterWindow()); - if (!mDocShell) { + if (!mDoc) { return nullptr; } - nsCOMPtr presShell = mDocShell->GetPresShell(); - - if (!presShell) { - // Try flushing frames on our parent in case there's a pending - // style change that will create the presshell. - nsGlobalWindow *parent = - static_cast(GetPrivateParent()); - if (!parent) { - return nullptr; - } - - parent->FlushPendingNotifications(Flush_Frames); - - // Might have killed mDocShell - if (!mDocShell) { - return nullptr; - } - - presShell = mDocShell->GetPresShell(); - if (!presShell) { - return nullptr; - } - } - RefPtr compStyle = - NS_NewComputedDOMStyle(&aElt, aPseudoElt, presShell, + NS_NewComputedDOMStyle(&aElt, aPseudoElt, mDoc, aDefaultStylesOnly ? nsComputedDOMStyle::eDefaultOnly : nsComputedDOMStyle::eAll); diff --git a/dom/smil/nsSMILCSSProperty.cpp b/dom/smil/nsSMILCSSProperty.cpp index 61a7ea29c..80182c679 100644 --- a/dom/smil/nsSMILCSSProperty.cpp +++ b/dom/smil/nsSMILCSSProperty.cpp @@ -38,14 +38,8 @@ GetCSSComputedValue(Element* aElem, return false; } - nsIPresShell* shell = doc->GetShell(); - if (!shell) { - NS_WARNING("Unable to look up computed style -- no pres shell"); - return false; - } - RefPtr computedStyle = - NS_NewComputedDOMStyle(aElem, EmptyString(), shell); + NS_NewComputedDOMStyle(aElem, EmptyString(), doc); computedStyle->GetPropertyValue(aPropID, aResult); return true; diff --git a/editor/libeditor/nsHTMLCSSUtils.cpp b/editor/libeditor/nsHTMLCSSUtils.cpp index 09e0dba21..b20506791 100644 --- a/editor/libeditor/nsHTMLCSSUtils.cpp +++ b/editor/libeditor/nsHTMLCSSUtils.cpp @@ -537,11 +537,8 @@ nsHTMLCSSUtils::GetComputedStyle(dom::Element* aElement) nsIDocument* doc = aElement->GetCurrentDoc(); NS_ENSURE_TRUE(doc, nullptr); - nsIPresShell* presShell = doc->GetShell(); - NS_ENSURE_TRUE(presShell, nullptr); - RefPtr style = - NS_NewComputedDOMStyle(aElement, EmptyString(), presShell); + NS_NewComputedDOMStyle(aElement, EmptyString(), doc); return style.forget(); } diff --git a/layout/style/nsComputedDOMStyle.cpp b/layout/style/nsComputedDOMStyle.cpp index a9702fdab..d5c3abcc5 100644 --- a/layout/style/nsComputedDOMStyle.cpp +++ b/layout/style/nsComputedDOMStyle.cpp @@ -63,13 +63,13 @@ using namespace mozilla::dom; */ already_AddRefed -NS_NewComputedDOMStyle(dom::Element* aElement, const nsAString& aPseudoElt, - nsIPresShell* aPresShell, +NS_NewComputedDOMStyle(dom::Element* aElement, + const nsAString& aPseudoElt, + nsIDocument* aDocument, nsComputedDOMStyle::StyleType aStyleType) { RefPtr computedStyle; - computedStyle = new nsComputedDOMStyle(aElement, aPseudoElt, aPresShell, - aStyleType); + computedStyle = new nsComputedDOMStyle(aElement, aPseudoElt, aDocument, aStyleType); return computedStyle.forget(); } @@ -219,7 +219,7 @@ nsComputedStyleMap::Update() nsComputedDOMStyle::nsComputedDOMStyle(dom::Element* aElement, const nsAString& aPseudoElt, - nsIPresShell* aPresShell, + nsIDocument* aDocument, StyleType aStyleType) : mDocumentWeak(nullptr), mOuterFrame(nullptr), mInnerFrame(nullptr), mPresShell(nullptr), @@ -228,11 +228,13 @@ nsComputedDOMStyle::nsComputedDOMStyle(dom::Element* aElement, mExposeVisitedStyle(false), mResolvedStyleContext(false) { - MOZ_ASSERT(aElement && aPresShell); + MOZ_ASSERT(aElement); + MOZ_ASSERT(aDocument); + // TODO(emilio, bug 548397, https://github.com/w3c/csswg-drafts/issues/2403): + // Should use aElement->OwnerDoc() instead. + mDocumentWeak = do_GetWeakReference(aDocument); - mDocumentWeak = do_GetWeakReference(aPresShell->GetDocument()); - - mContent = aElement; + mElement = aElement; if (!DOMStringIsNull(aPseudoElt) && !aPseudoElt.IsEmpty() && aPseudoElt.First() == char16_t(':')) { @@ -259,11 +261,8 @@ nsComputedDOMStyle::nsComputedDOMStyle(dom::Element* aElement, mPseudo = nullptr; } } - - MOZ_ASSERT(aPresShell->GetPresContext()); } - nsComputedDOMStyle::~nsComputedDOMStyle() { ClearStyleContext(); @@ -272,13 +271,13 @@ nsComputedDOMStyle::~nsComputedDOMStyle() NS_IMPL_CYCLE_COLLECTION_CLASS(nsComputedDOMStyle) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsComputedDOMStyle) - tmp->ClearStyleContext(); // remove observer before clearing mContent - NS_IMPL_CYCLE_COLLECTION_UNLINK(mContent) + tmp->ClearStyleContext(); // remove observer before clearing mElement + NS_IMPL_CYCLE_COLLECTION_UNLINK(mElement) NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsComputedDOMStyle) - NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContent) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mElement) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END @@ -345,8 +344,6 @@ nsComputedDOMStyle::SetCssText(const nsAString& aCssText) NS_IMETHODIMP nsComputedDOMStyle::GetLength(uint32_t* aLength) { - NS_PRECONDITION(aLength, "Null aLength! Prepare to die!"); - uint32_t length = GetComputedStyleMap()->Length(); // Make sure we have up to date style so that we can include custom @@ -354,6 +351,8 @@ nsComputedDOMStyle::GetLength(uint32_t* aLength) UpdateCurrentStyleSources(false); if (mStyleContext) { length += StyleVariables()->mVariables.Count(); + } else { + length = 0; } *aLength = length; @@ -599,7 +598,7 @@ nsComputedDOMStyle::ClearStyleContext() { if (mResolvedStyleContext) { mResolvedStyleContext = false; - mContent->RemoveMutationObserver(this); + mElement->RemoveMutationObserver(this); } mStyleContext = nullptr; } @@ -609,7 +608,7 @@ nsComputedDOMStyle::SetResolvedStyleContext(RefPtr&& aContext) { if (!mResolvedStyleContext) { mResolvedStyleContext = true; - mContent->AddMutationObserver(this); + mElement->AddMutationObserver(this); } mStyleContext = aContext; } @@ -634,7 +633,7 @@ nsComputedDOMStyle::UpdateCurrentStyleSources(bool aNeedsLayoutFlush) // Flush _before_ getting the presshell, since that could create a new // presshell. Also note that we want to flush the style on the document - // we're computing style in, not on the document mContent is in -- the two + // we're computing style in, not on the document mElement is in -- the two // may be different. document->FlushPendingNotifications( aNeedsLayoutFlush ? Flush_Layout : Flush_Style); @@ -642,7 +641,7 @@ nsComputedDOMStyle::UpdateCurrentStyleSources(bool aNeedsLayoutFlush) mFlushedPendingReflows = aNeedsLayoutFlush; #endif - nsCOMPtr presShellForContent = GetPresShellForContent(mContent); + nsCOMPtr presShellForContent = GetPresShellForContent(mElement); if (presShellForContent && presShellForContent != mPresShell) { presShellForContent->FlushPendingNotifications(Flush_Style); } @@ -657,7 +656,11 @@ nsComputedDOMStyle::UpdateCurrentStyleSources(bool aNeedsLayoutFlush) mPresShell->GetPresContext()->GetRestyleGeneration(); if (mStyleContext) { - if (mStyleContextGeneration == currentGeneration) { + // We can't rely on the undisplayed restyle generation if mElement is + // out-of-document, since that generation is not incremented for DOM changes + // on out-of-document elements. + if (mStyleContextGeneration == currentGeneration && + mElement->IsInComposedDoc()) { // Our cached style context is still valid. return; } @@ -666,12 +669,12 @@ nsComputedDOMStyle::UpdateCurrentStyleSources(bool aNeedsLayoutFlush) mStyleContext = nullptr; } - // XXX the !mContent->IsHTMLElement(nsGkAtoms::area) + // XXX the !mElement->IsHTMLElement(nsGkAtoms::area) // check is needed due to bug 135040 (to avoid using // mPrimaryFrame). Remove it once that's fixed. if (!mPseudo && mStyleType == eAll && - !mContent->IsHTMLElement(nsGkAtoms::area)) { - mOuterFrame = mContent->GetPrimaryFrame(); + !mElement->IsHTMLElement(nsGkAtoms::area)) { + mOuterFrame = mElement->GetPrimaryFrame(); mInnerFrame = mOuterFrame; if (mOuterFrame) { nsIAtom* type = mOuterFrame->GetType(); @@ -715,7 +718,7 @@ nsComputedDOMStyle::UpdateCurrentStyleSources(bool aNeedsLayoutFlush) // Need to resolve a style context RefPtr resolvedStyleContext = nsComputedDOMStyle::GetStyleContextForElementNoFlush( - mContent->AsElement(), + mElement, mPseudo, presShellForContent ? presShellForContent.get() : mPresShell, mStyleType); @@ -808,7 +811,6 @@ nsComputedDOMStyle::GetPropertyCSSValue(const nsAString& aPropertyName, ErrorRes UpdateCurrentStyleSources(needsLayoutFlush); if (!mStyleContext) { - aRv.Throw(NS_ERROR_NOT_AVAILABLE); return nullptr; } @@ -4866,7 +4868,7 @@ nsComputedDOMStyle::GetLineHeightCoord(nscoord& aCoord) // lie about font size inflation since we lie about font size (since // the inflation only applies to text) - aCoord = nsHTMLReflowState::CalcLineHeight(mContent, mStyleContext, + aCoord = nsHTMLReflowState::CalcLineHeight(mElement, mStyleContext, blockHeight, 1.0f); // CalcLineHeight uses font->mFont.size, but we want to use @@ -6228,7 +6230,7 @@ nsComputedDOMStyle::DoGetCustomProperty(const nsAString& aPropertyName) void nsComputedDOMStyle::ParentChainChanged(nsIContent* aContent) { - NS_ASSERTION(mContent == aContent, "didn't we register mContent?"); + NS_ASSERTION(mElement == aContent, "didn't we register mElement?"); NS_ASSERTION(mResolvedStyleContext, "should have only registered an observer when " "mResolvedStyleContext is true"); diff --git a/layout/style/nsComputedDOMStyle.h b/layout/style/nsComputedDOMStyle.h index ce7a6d02b..dbe63acce 100644 --- a/layout/style/nsComputedDOMStyle.h +++ b/layout/style/nsComputedDOMStyle.h @@ -70,12 +70,12 @@ public: nsComputedDOMStyle(mozilla::dom::Element* aElement, const nsAString& aPseudoElt, - nsIPresShell* aPresShell, + nsIDocument* aDocument, StyleType aStyleType); - virtual nsINode *GetParentObject() override + nsINode *GetParentObject() override { - return mContent; + return mElement; } static already_AddRefed @@ -614,9 +614,9 @@ private: // We don't really have a good immutable representation of "presentation". // Given the way GetComputedStyle is currently used, we should just grab the - // 0th presshell, if any, from the document. + // presshell, if any, from the document. nsWeakPtr mDocumentWeak; - nsCOMPtr mContent; + RefPtr mElement; /** * Strong reference to the style context we access data from. This can be @@ -682,7 +682,7 @@ private: already_AddRefed NS_NewComputedDOMStyle(mozilla::dom::Element* aElement, const nsAString& aPseudoElt, - nsIPresShell* aPresShell, + nsIDocument* aDocument, nsComputedDOMStyle::StyleType aStyleType = nsComputedDOMStyle::eAll);