Align computed DOM styles with mainstream behvior.

This commit is contained in:
Fedor 2020-03-12 20:39:48 +03:00
parent d1171a2e89
commit cbc0c961db
6 changed files with 47 additions and 74 deletions

View File

@ -10,4 +10,4 @@
# hardcoded milestones in the tree from these two files.
#--------------------------------------------------------
4.4.2
4.5.0

View File

@ -10950,35 +10950,12 @@ nsGlobalWindow::GetComputedStyleHelperOuter(Element& aElt,
{
MOZ_RELEASE_ASSERT(IsOuterWindow());
if (!mDocShell) {
if (!mDoc) {
return nullptr;
}
nsCOMPtr<nsIPresShell> presShell = mDocShell->GetPresShell();
if (!presShell) {
// Try flushing frames on our parent in case there's a pending
// style change that will create the presshell.
auto* parent = nsGlobalWindow::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<nsComputedDOMStyle> compStyle =
NS_NewComputedDOMStyle(&aElt, aPseudoElt, presShell,
NS_NewComputedDOMStyle(&aElt, aPseudoElt, mDoc,
aDefaultStylesOnly ? nsComputedDOMStyle::eDefaultOnly :
nsComputedDOMStyle::eAll);

View File

@ -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<nsComputedDOMStyle> computedStyle =
NS_NewComputedDOMStyle(aElem, EmptyString(), shell);
NS_NewComputedDOMStyle(aElem, EmptyString(), doc);
computedStyle->GetPropertyValue(aPropID, aResult);
return true;

View File

@ -569,11 +569,8 @@ CSSEditUtils::GetComputedStyle(Element* aElement)
nsIDocument* doc = aElement->GetUncomposedDoc();
NS_ENSURE_TRUE(doc, nullptr);
nsIPresShell* presShell = doc->GetShell();
NS_ENSURE_TRUE(presShell, nullptr);
RefPtr<nsComputedDOMStyle> style =
NS_NewComputedDOMStyle(aElement, EmptyString(), presShell);
NS_NewComputedDOMStyle(aElement, EmptyString(), doc);
return style.forget();
}

View File

@ -64,13 +64,13 @@ using namespace mozilla::dom;
*/
already_AddRefed<nsComputedDOMStyle>
NS_NewComputedDOMStyle(dom::Element* aElement, const nsAString& aPseudoElt,
nsIPresShell* aPresShell,
NS_NewComputedDOMStyle(dom::Element* aElement,
const nsAString& aPseudoElt,
nsIDocument* aDocument,
nsComputedDOMStyle::StyleType aStyleType)
{
RefPtr<nsComputedDOMStyle> computedStyle;
computedStyle = new nsComputedDOMStyle(aElement, aPseudoElt, aPresShell,
aStyleType);
computedStyle = new nsComputedDOMStyle(aElement, aPseudoElt, aDocument, aStyleType);
return computedStyle.forget();
}
@ -243,7 +243,7 @@ nsComputedStyleMap::Update()
nsComputedDOMStyle::nsComputedDOMStyle(dom::Element* aElement,
const nsAString& aPseudoElt,
nsIPresShell* aPresShell,
nsIDocument* aDocument,
StyleType aStyleType)
: mDocumentWeak(nullptr)
, mOuterFrame(nullptr)
@ -254,11 +254,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(':')) {
@ -285,8 +287,6 @@ nsComputedDOMStyle::nsComputedDOMStyle(dom::Element* aElement,
mPseudo = nullptr;
}
}
MOZ_ASSERT(aPresShell->GetPresContext());
}
nsComputedDOMStyle::~nsComputedDOMStyle()
@ -297,13 +297,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_END
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsComputedDOMStyle)
@ -366,8 +366,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
@ -375,6 +373,8 @@ nsComputedDOMStyle::GetLength(uint32_t* aLength)
UpdateCurrentStyleSources(false);
if (mStyleContext) {
length += StyleVariables()->mVariables.Count();
} else {
length = 0;
}
*aLength = length;
@ -616,7 +616,7 @@ nsComputedDOMStyle::ClearStyleContext()
{
if (mResolvedStyleContext) {
mResolvedStyleContext = false;
mContent->RemoveMutationObserver(this);
mElement->RemoveMutationObserver(this);
}
mStyleContext = nullptr;
}
@ -626,7 +626,7 @@ nsComputedDOMStyle::SetResolvedStyleContext(RefPtr<nsStyleContext>&& aContext)
{
if (!mResolvedStyleContext) {
mResolvedStyleContext = true;
mContent->AddMutationObserver(this);
mElement->AddMutationObserver(this);
}
mStyleContext = aContext;
}
@ -651,7 +651,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);
@ -659,7 +659,7 @@ nsComputedDOMStyle::UpdateCurrentStyleSources(bool aNeedsLayoutFlush)
mFlushedPendingReflows = aNeedsLayoutFlush;
#endif
nsCOMPtr<nsIPresShell> presShellForContent = GetPresShellForContent(mContent);
nsCOMPtr<nsIPresShell> presShellForContent = GetPresShellForContent(mElement);
if (presShellForContent && presShellForContent != document->GetShell()) {
presShellForContent->FlushPendingNotifications(Flush_Style);
}
@ -674,7 +674,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;
}
@ -683,12 +687,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();
@ -731,7 +735,7 @@ nsComputedDOMStyle::UpdateCurrentStyleSources(bool aNeedsLayoutFlush)
// Need to resolve a style context
RefPtr<nsStyleContext> resolvedStyleContext =
nsComputedDOMStyle::GetStyleContextForElementNoFlush(
mContent->AsElement(),
mElement,
mPseudo,
presShellForContent ? presShellForContent.get() : mPresShell,
mStyleType);
@ -824,7 +828,6 @@ nsComputedDOMStyle::GetPropertyCSSValue(const nsAString& aPropertyName, ErrorRes
UpdateCurrentStyleSources(needsLayoutFlush);
if (!mStyleContext) {
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
return nullptr;
}
@ -2861,7 +2864,7 @@ nsComputedDOMStyle::DoGetGridTemplateColumns()
nsGridContainerFrame* gridFrame =
nsGridContainerFrame::GetGridFrameWithComputedInfo(
mContent->GetPrimaryFrame());
mElement->GetPrimaryFrame());
if (gridFrame) {
info = gridFrame->GetComputedTemplateColumns();
@ -2877,7 +2880,7 @@ nsComputedDOMStyle::DoGetGridTemplateRows()
nsGridContainerFrame* gridFrame =
nsGridContainerFrame::GetGridFrameWithComputedInfo(
mContent->GetPrimaryFrame());
mElement->GetPrimaryFrame());
if (gridFrame) {
info = gridFrame->GetComputedTemplateRows();
@ -5214,8 +5217,10 @@ nsComputedDOMStyle::GetLineHeightCoord(nscoord& aCoord)
// lie about font size inflation since we lie about font size (since
// the inflation only applies to text)
aCoord = ReflowInput::CalcLineHeight(mContent, mStyleContext,
blockHeight, 1.0f);
aCoord = ReflowInput::CalcLineHeight(mElement,
mStyleContext,
blockHeight,
1.0f);
// CalcLineHeight uses font->mFont.size, but we want to use
// font->mSize as the font size. Adjust for that. Also adjust for
@ -6642,7 +6647,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");

View File

@ -76,12 +76,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<nsStyleContext>
@ -667,9 +667,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<nsIContent> mContent;
RefPtr<mozilla::dom::Element> mElement;
/**
* Strong reference to the style context we access data from. This can be
@ -735,7 +735,7 @@ private:
already_AddRefed<nsComputedDOMStyle>
NS_NewComputedDOMStyle(mozilla::dom::Element* aElement,
const nsAString& aPseudoElt,
nsIPresShell* aPresShell,
nsIDocument* aDocument,
nsComputedDOMStyle::StyleType aStyleType =
nsComputedDOMStyle::eAll);