From 9ca563d1bc156ebbd195d8542817cd0307ba1938 Mon Sep 17 00:00:00 2001 From: "waterson%netscape.com" Date: Wed, 4 Jul 2001 05:54:09 +0000 Subject: [PATCH] Bug 85422. Since nsIContent::GetDocument() may now sometimes return a null document back-pointer due to paint suppression, be extra paraniod with a sprinkle of null-check fairy dust. r=brendan, sr=attinasi --- content/xbl/src/nsBindingManager.cpp | 11 +- content/xbl/src/nsXBLBinding.cpp | 4 + content/xbl/src/nsXBLService.cpp | 18 +- content/xbl/src/nsXBLWindowHandler.cpp | 4 +- layout/base/nsCSSFrameConstructor.cpp | 176 +++++++----------- layout/generic/nsImageFrame.cpp | 4 +- layout/generic/nsObjectFrame.cpp | 11 +- layout/html/base/src/nsImageFrame.cpp | 4 +- layout/html/base/src/nsObjectFrame.cpp | 11 +- .../html/style/src/nsCSSFrameConstructor.cpp | 176 +++++++----------- 10 files changed, 183 insertions(+), 236 deletions(-) diff --git a/content/xbl/src/nsBindingManager.cpp b/content/xbl/src/nsBindingManager.cpp index f2fc01cddcd..cac80a6818a 100644 --- a/content/xbl/src/nsBindingManager.cpp +++ b/content/xbl/src/nsBindingManager.cpp @@ -1252,10 +1252,13 @@ nsBindingManager::WalkRules(nsIStyleSet* aStyleSet, nsCOMPtr document; aContent->GetDocument(*getter_AddRefs(document)); nsCOMPtr container(do_QueryInterface(document)); - nsCOMPtr inlineSheet; - container->GetInlineStyleSheet(getter_AddRefs(inlineSheet)); - nsCOMPtr inlineCSS(do_QueryInterface(inlineSheet)); - (*aFunc)((nsISupports*)(inlineCSS.get()), aData); + if (container) { + nsCOMPtr inlineSheet; + container->GetInlineStyleSheet(getter_AddRefs(inlineSheet)); + nsCOMPtr inlineCSS(do_QueryInterface(inlineSheet)); + if (inlineCSS) + (*aFunc)((nsISupports*)(inlineCSS.get()), aData); + } } // Null out our mCurrentStyleRoot. diff --git a/content/xbl/src/nsXBLBinding.cpp b/content/xbl/src/nsXBLBinding.cpp index 9ae40d8a6f7..202c4a4097b 100644 --- a/content/xbl/src/nsXBLBinding.cpp +++ b/content/xbl/src/nsXBLBinding.cpp @@ -698,6 +698,10 @@ nsXBLBinding::GenerateAnonymousContent() if (hasContent || hasInsertionPoints) { nsCOMPtr doc; mBoundElement->GetDocument(*getter_AddRefs(doc)); + + // XXX doc will be null if we're in the midst of paint suppression. + if (! doc) + return NS_OK; nsCOMPtr bindingManager; doc->GetBindingManager(getter_AddRefs(bindingManager)); diff --git a/content/xbl/src/nsXBLService.cpp b/content/xbl/src/nsXBLService.cpp index 2597bbf9116..18347d1b8c9 100644 --- a/content/xbl/src/nsXBLService.cpp +++ b/content/xbl/src/nsXBLService.cpp @@ -633,6 +633,11 @@ nsXBLService::LoadBindings(nsIContent* aContent, const nsAReadableString& aURL, nsCOMPtr document; aContent->GetDocument(*getter_AddRefs(document)); + + // XXX document may be null if we're in the midst of paint suppression + if (!document) + return NS_OK; + nsCOMPtr bindingManager; document->GetBindingManager(getter_AddRefs(bindingManager)); @@ -730,6 +735,11 @@ nsXBLService::FlushStyleBindings(nsIContent* aContent) { nsCOMPtr document; aContent->GetDocument(*getter_AddRefs(document)); + + // XXX doc will be null if we're in the midst of paint suppression. + if (! document) + return NS_OK; + nsCOMPtr bindingManager; document->GetBindingManager(getter_AddRefs(bindingManager)); @@ -791,9 +801,11 @@ nsXBLService::GetXBLDocumentInfo(const nsCString& aURLStr, nsIContent* aBoundEle // The second line of defense is the binding manager's document table. nsCOMPtr boundDocument; aBoundElement->GetDocument(*getter_AddRefs(boundDocument)); - nsCOMPtr bindingManager; - boundDocument->GetBindingManager(getter_AddRefs(bindingManager)); - bindingManager->GetXBLDocumentInfo(aURLStr, aResult); + if (boundDocument) { + nsCOMPtr bindingManager; + boundDocument->GetBindingManager(getter_AddRefs(bindingManager)); + bindingManager->GetXBLDocumentInfo(aURLStr, aResult); + } } return NS_OK; } diff --git a/content/xbl/src/nsXBLWindowHandler.cpp b/content/xbl/src/nsXBLWindowHandler.cpp index 7734d1a2719..f5170f800e2 100644 --- a/content/xbl/src/nsXBLWindowHandler.cpp +++ b/content/xbl/src/nsXBLWindowHandler.cpp @@ -291,7 +291,9 @@ nsXBLWindowHandler::WalkHandlersInternal(nsIDOMEvent* aEvent, nsIAtom* aEventTyp nsCOMPtr doc; elt->GetDocument(*getter_AddRefs(doc)); nsCOMPtr domDoc(do_QueryInterface(doc)); - domDoc->GetElementById(command, getter_AddRefs(commandElt)); + if (domDoc) + domDoc->GetElementById(command, getter_AddRefs(commandElt)); + if (!commandElt) { NS_ASSERTION(PR_FALSE, "A XUL is observing a command that doesn't exist. Unable to execute key binding!\n"); return NS_OK; diff --git a/layout/base/nsCSSFrameConstructor.cpp b/layout/base/nsCSSFrameConstructor.cpp index ed658b5b7a2..549ff6c7ec0 100644 --- a/layout/base/nsCSSFrameConstructor.cpp +++ b/layout/base/nsCSSFrameConstructor.cpp @@ -838,12 +838,21 @@ struct ChildIterator PRUint32 mLength; nsCOMPtr mNodes; - ChildIterator(nsIContent* aContent) + ChildIterator(nsIContent* aContent, nsIDocument* aDocument) :mContent(aContent), mIndex(0), mLength(0), mNodes(nsnull) { + NS_PRECONDITION(aContent != nsnull, "no content"); + NS_PRECONDITION(aDocument != nsnull, "no document"); + + aDocument->GetBindingManager(getter_AddRefs(mBindingManager)); + +#ifdef DEBUG + // Verify that the frame ctor's document is the same as the + // content element's document. nsCOMPtr doc; - aContent->GetDocument(*getter_AddRefs(doc)); - doc->GetBindingManager(getter_AddRefs(mBindingManager)); + mContent->GetDocument(*getter_AddRefs(doc)); + NS_ASSERTION(doc.get() == aDocument, "content doc != frame ctor doc"); +#endif // Retrieve the anonymous content that we should build. mBindingManager->GetXBLChildNodesFor(mContent, getter_AddRefs(mNodes)); @@ -1557,9 +1566,6 @@ nsCSSFrameConstructor::CreateGeneratedContentFrame(nsIPresShell* aPresShe nsIFrame* containerFrame; nsFrameItems childFrames; - nsCOMPtr document; - aContent->GetDocument(*getter_AddRefs(document)); - if (NS_STYLE_DISPLAY_BLOCK == display->mDisplay) { NS_NewBlockFrame(aPresShell, &containerFrame); } else { @@ -1589,7 +1595,7 @@ nsCSSFrameConstructor::CreateGeneratedContentFrame(nsIPresShell* aPresShe // Create a frame nsresult result; - result = CreateGeneratedFrameFor(aPresContext, document, containerFrame, + result = CreateGeneratedFrameFor(aPresContext, mDocument, containerFrame, aContent, textStyleContext, styleContent, contentIndex, &frame); if (NS_SUCCEEDED(result) && frame) { @@ -3112,7 +3118,7 @@ nsCSSFrameConstructor::TableProcessChildren(nsIPresShell* aPresShell, nsCOMPtr parentStyleContext; aParentFrame->GetStyleContext(getter_AddRefs(parentStyleContext)); - ChildIterator iterator(aContent); + ChildIterator iterator(aContent, mDocument); while (iterator.HasMoreChildren()) { nsCOMPtr childContent; iterator.NextChild(getter_AddRefs(childContent)); @@ -3435,8 +3441,6 @@ nsCSSFrameConstructor::ConstructDocElementFrame(nsIPresShell* aPresShell, // build a scrollframe if (!isPaginated && isScrollable) { nsIFrame* newScrollFrame = nsnull; - nsCOMPtr document; - aDocElement->GetDocument(*getter_AddRefs(document)); nsCOMPtr newContext; BeginBuildingScrollFrame( aPresShell, aPresContext, @@ -3445,7 +3449,7 @@ nsCSSFrameConstructor::ConstructDocElementFrame(nsIPresShell* aPresShell, styleContext, aParentFrame, nsLayoutAtoms::scrolledContentPseudo, - document, + mDocument, PR_FALSE, scrollFrame, newContext, @@ -3666,17 +3670,13 @@ nsCSSFrameConstructor::ConstructRootFrame(nsIPresShell* aPresShell, */ // Set up our style rule observer. - nsCOMPtr doc; - aDocElement->GetDocument(*getter_AddRefs(doc)); - if (doc) { - nsCOMPtr bindingManager; - doc->GetBindingManager(getter_AddRefs(bindingManager)); - if (bindingManager) { - nsCOMPtr ruleSupplier(do_QueryInterface(bindingManager)); - nsCOMPtr set; - aPresShell->GetStyleSet(getter_AddRefs(set)); - set->SetStyleRuleSupplier(ruleSupplier); - } + nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); + if (bindingManager) { + nsCOMPtr ruleSupplier(do_QueryInterface(bindingManager)); + nsCOMPtr set; + aPresShell->GetStyleSet(getter_AddRefs(set)); + set->SetStyleRuleSupplier(ruleSupplier); } // --------- BUILD VIEWPORT ----------- @@ -3839,8 +3839,6 @@ nsCSSFrameConstructor::ConstructRootFrame(nsIPresShell* aPresShell, nsIFrame* newScrollableFrame = nsnull; - nsCOMPtr document; - aDocElement->GetDocument(*getter_AddRefs(document)); BeginBuildingScrollFrame( aPresShell, aPresContext, @@ -3849,7 +3847,7 @@ nsCSSFrameConstructor::ConstructRootFrame(nsIPresShell* aPresShell, styleContext, viewportFrame, rootPseudo, - document, + mDocument, PR_TRUE, newFrame, rootPseudoStyle, @@ -5232,13 +5230,8 @@ nsCSSFrameConstructor::CreateAnonymousFrames(nsIPresShell* aPresShell, } #endif - // get the document - nsCOMPtr doc; - nsresult rv = aParent->GetDocument(*getter_AddRefs(doc)); - if (NS_FAILED(rv) || !doc) - return rv; - return CreateAnonymousFrames(aPresShell, aPresContext, aState, aParent, doc, aNewFrame, aChildItems); + return CreateAnonymousFrames(aPresShell, aPresContext, aState, aParent, mDocument, aNewFrame, aChildItems); } // after the node has been constructed and initialized create any @@ -5886,10 +5879,8 @@ nsCSSFrameConstructor::ConstructXULFrame(nsIPresShell* aPresShell, // Process the child content if requested nsFrameItems childItems; if (processChildren || processAnonymousChildren) { - nsCOMPtr doc; - aContent->GetDocument(*getter_AddRefs(doc)); nsCOMPtr bindingManager; - doc->GetBindingManager(getter_AddRefs(bindingManager)); + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); if (processChildren) { bindingManager->ShouldBuildChildFrames(aContent, &processChildren); if (processChildren) @@ -6108,8 +6099,7 @@ nsCSSFrameConstructor::BuildScrollFrame (nsIPresShell* aPresShell, nsIFrame* aScrollPortFrame) { nsIFrame *scrollFrame; - nsCOMPtr document; - aContent->GetDocument(*getter_AddRefs(document)); + nsCOMPtr scrolledContentStyle; @@ -6119,7 +6109,7 @@ nsCSSFrameConstructor::BuildScrollFrame (nsIPresShell* aPresShell, aContentStyle, aParentFrame, nsLayoutAtoms::scrolledContentPseudo, - document, + mDocument, PR_FALSE, aNewFrame, scrolledContentStyle, @@ -7700,23 +7690,20 @@ nsCSSFrameConstructor::AppendFrames(nsIPresContext* aPresContext, static nsIFrame* FindPreviousAnonymousSibling(nsIPresShell* aPresShell, - nsIContent* aContainer, - nsIContent* aChild) + nsIDocument* aDocument, + nsIContent* aContainer, + nsIContent* aChild) { + NS_PRECONDITION(aDocument, "null document from content element in FindPreviousAnonymousSibling"); nsIFrame* prevSibling = nsnull; nsCOMPtr nodeList; - nsCOMPtr doc; - aContainer->GetDocument(*getter_AddRefs(doc)); - NS_ASSERTION(doc, "null document from content element in FindPreviousAnonymousSibling"); - if (doc) { - nsCOMPtr xblDoc(do_QueryInterface(doc)); - NS_ASSERTION(xblDoc, "null xblDoc for content element in FindPreviousAnonymousSibling"); - if (xblDoc) { - nsCOMPtr elt(do_QueryInterface(aContainer)); - xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList)); - } + nsCOMPtr xblDoc(do_QueryInterface(aDocument)); + NS_ASSERTION(xblDoc, "null xblDoc for content element in FindPreviousAnonymousSibling"); + if (xblDoc) { + nsCOMPtr elt(do_QueryInterface(aContainer)); + xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList)); } if (nodeList) { PRUint32 ctr,listLength; @@ -7769,23 +7756,21 @@ FindPreviousAnonymousSibling(nsIPresShell* aPresShell, static nsIFrame* FindNextAnonymousSibling(nsIPresShell* aPresShell, - nsIContent* aContainer, - nsIContent* aChild) + nsIDocument* aDocument, + nsIContent* aContainer, + nsIContent* aChild) { + NS_PRECONDITION(aDocument, "null document from content element in FindNextAnonymousSibling"); + nsIFrame* nextSibling = nsnull; nsCOMPtr nodeList; - nsCOMPtr doc; - aContainer->GetDocument(*getter_AddRefs(doc)); - NS_ASSERTION(doc, "null document from content element in FindNextAnonymousSibling"); - if (doc) { - nsCOMPtr xblDoc(do_QueryInterface(doc)); - NS_ASSERTION(xblDoc, "null xblDoc for content element in FindNextAnonymousSibling"); - if (xblDoc) { - nsCOMPtr elt(do_QueryInterface(aContainer)); - xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList)); - } + nsCOMPtr xblDoc(do_QueryInterface(aDocument)); + NS_ASSERTION(xblDoc, "null xblDoc for content element in FindNextAnonymousSibling"); + if (xblDoc) { + nsCOMPtr elt(do_QueryInterface(aContainer)); + xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList)); } if (nodeList) { PRUint32 ctr,listLength; @@ -7945,17 +7930,12 @@ nsCSSFrameConstructor::ContentAppended(nsIPresContext* aPresContext, #ifdef INCLUDE_XUL if (aContainer) { - nsCOMPtr tag; - nsCOMPtr doc; nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); + + nsCOMPtr tag; PRInt32 namespaceID; - aContainer->GetDocument(*getter_AddRefs(doc)); - if (doc) { - doc->GetBindingManager(getter_AddRefs(bindingManager)); - bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); - } - else - aContainer->GetTag(*getter_AddRefs(tag)); + bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); PRBool treeChildren = tag && tag.get() == nsXULAtoms::treechildren; PRBool treeItem = tag && tag.get() == nsXULAtoms::treeitem; @@ -8403,17 +8383,12 @@ nsCSSFrameConstructor::ContentInserted(nsIPresContext* aPresContext, #ifdef INCLUDE_XUL if (aContainer) { - nsCOMPtr tag; - nsCOMPtr doc; nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); + + nsCOMPtr tag; PRInt32 namespaceID; - aContainer->GetDocument(*getter_AddRefs(doc)); - if (doc) { - doc->GetBindingManager(getter_AddRefs(bindingManager)); - bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); - } - else - aContainer->GetTag(*getter_AddRefs(tag)); + bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); PRBool treeChildren = tag && tag.get() == nsXULAtoms::treechildren; PRBool treeItem = tag && tag.get() == nsXULAtoms::treeitem; @@ -8570,7 +8545,7 @@ nsCSSFrameConstructor::ContentInserted(nsIPresContext* aPresContext, // Find the frame that precedes the insertion point. nsIFrame* prevSibling = (aIndexInContainer == -1) ? - FindPreviousAnonymousSibling(shell, aContainer, aChild) : + FindPreviousAnonymousSibling(shell, mDocument, aContainer, aChild) : FindPreviousSibling(shell, aContainer, aIndexInContainer); nsIFrame* nextSibling = nsnull; @@ -8579,7 +8554,7 @@ nsCSSFrameConstructor::ContentInserted(nsIPresContext* aPresContext, // If there is no previous sibling, then find the frame that follows if (nsnull == prevSibling) { nextSibling = (aIndexInContainer == -1) ? - FindNextAnonymousSibling(shell, aContainer, aChild) : + FindNextAnonymousSibling(shell, mDocument, aContainer, aChild) : FindNextSibling(shell, aContainer, aIndexInContainer); } @@ -9127,17 +9102,12 @@ nsCSSFrameConstructor::ContentRemoved(nsIPresContext* aPresContext, #ifdef INCLUDE_XUL if (aContainer) { - nsCOMPtr tag; - nsCOMPtr doc; nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); + + nsCOMPtr tag; PRInt32 namespaceID; - aContainer->GetDocument(*getter_AddRefs(doc)); - if (doc) { - doc->GetBindingManager(getter_AddRefs(bindingManager)); - bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); - } - else - aContainer->GetTag(*getter_AddRefs(tag)); + bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); PRBool treeChildren = tag && tag.get() == nsXULAtoms::treechildren; PRBool treeItem = tag && tag.get() == nsXULAtoms::treeitem; @@ -9975,18 +9945,12 @@ nsCSSFrameConstructor::AttributeChanged(nsIPresContext* aPresContext, // content from being removed and re-inserted (which is what would // happen otherwise). if (!primaryFrame && !reframe) { - nsCOMPtr tag; - nsCOMPtr doc; - aContent->GetDocument(*getter_AddRefs(doc)); - if (doc) { - nsCOMPtr bindingManager; - doc->GetBindingManager(getter_AddRefs(bindingManager)); + nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); - PRInt32 namespaceID; - bindingManager->ResolveTag(aContent, &namespaceID, getter_AddRefs(tag)); - } - else - aContent->GetTag(*getter_AddRefs(tag)); + PRInt32 namespaceID; + nsCOMPtr tag; + bindingManager->ResolveTag(aContent, &namespaceID, getter_AddRefs(tag)); if (tag && (tag.get() == nsXULAtoms::treechildren || (tag.get() == nsXULAtoms::treeitem && aAttribute != nsXULAtoms::open) || @@ -10303,10 +10267,8 @@ nsCSSFrameConstructor::ConstructAlternateFrame(nsIPresShell* aPresShell, NS_RELEASE(domData); // Set aContent as the parent content and set the document object - nsCOMPtr document; - aContent->GetDocument(*getter_AddRefs(document)); altTextContent->SetParent(aContent); - altTextContent->SetDocument(document, PR_TRUE, PR_TRUE); + altTextContent->SetDocument(mDocument, PR_TRUE, PR_TRUE); // Create either an inline frame, block frame, or area frame nsIFrame* containerFrame; @@ -11520,7 +11482,7 @@ nsCSSFrameConstructor::ProcessChildren(nsIPresShell* aPresShell, nsPseudoFrames priorPseudoFrames; aState.mPseudoFrames.Reset(&priorPseudoFrames); - ChildIterator iterator(aContent); + ChildIterator iterator(aContent, mDocument); while (iterator.HasMoreChildren()) { nsCOMPtr childContent; iterator.NextChild(getter_AddRefs(childContent)); @@ -12677,7 +12639,7 @@ nsCSSFrameConstructor::ProcessBlockChildren(nsIPresShell* aPresShell, } // Iterate the child content objects and construct frames - ChildIterator iterator(aContent); + ChildIterator iterator(aContent, mDocument); while (iterator.HasMoreChildren()) { nsCOMPtr childContent; iterator.NextChild(getter_AddRefs(childContent)); @@ -12974,7 +12936,7 @@ nsCSSFrameConstructor::ProcessInlineChildren(nsIPresShell* aPresShell, // Iterate the child content objects and construct frames PRBool allKidsInline = PR_TRUE; - ChildIterator iterator(aContent); + ChildIterator iterator(aContent, mDocument); while (iterator.HasMoreChildren()) { nsCOMPtr childContent; iterator.NextChild(getter_AddRefs(childContent)); diff --git a/layout/generic/nsImageFrame.cpp b/layout/generic/nsImageFrame.cpp index 426715aa4ed..76c32bed9c6 100644 --- a/layout/generic/nsImageFrame.cpp +++ b/layout/generic/nsImageFrame.cpp @@ -1428,7 +1428,7 @@ nsImageFrame::GetBaseURI(nsIURI **aURI) nsCOMPtr doc; if (mContent) { rv = mContent->GetDocument(*getter_AddRefs(doc)); - if (NS_SUCCEEDED(rv)) { + if (doc) { doc->GetBaseURL(*getter_AddRefs(baseURI)); } } @@ -1489,7 +1489,7 @@ nsImageFrame::CanLoadImage(nsIURI *aURI) nsCOMPtr document; if (mContent) { rv = mContent->GetDocument(*getter_AddRefs(document)); - if (NS_FAILED(rv)) { + if (! document) { NS_ASSERTION(0, "expecting a document"); return shouldLoad; } diff --git a/layout/generic/nsObjectFrame.cpp b/layout/generic/nsObjectFrame.cpp index c2ba0b3af88..b93f33a375c 100644 --- a/layout/generic/nsObjectFrame.cpp +++ b/layout/generic/nsObjectFrame.cpp @@ -1043,6 +1043,9 @@ nsObjectFrame::InstantiatePlugin(nsIPresContext* aPresContext, rv = shell->GetDocument(getter_AddRefs(document)); if (NS_FAILED(rv)) return rv; + if (! document) + return NS_ERROR_FAILURE; + nsCOMPtr globalScript; rv = document->GetScriptGlobalObject(getter_AddRefs(globalScript)); if (NS_FAILED(rv)) return rv; @@ -1222,12 +1225,10 @@ nsObjectFrame::GetBaseURL(nsIURI* &aURL) } else { - nsIDocument* doc = nsnull; - if (NS_SUCCEEDED(mContent->GetDocument(doc))) - { + nsCOMPtr doc; + mContent->GetDocument(*getter_AddRefs(doc)); + if (doc) doc->GetBaseURL(aURL); - NS_RELEASE(doc); - } else return NS_ERROR_FAILURE; } diff --git a/layout/html/base/src/nsImageFrame.cpp b/layout/html/base/src/nsImageFrame.cpp index 426715aa4ed..76c32bed9c6 100644 --- a/layout/html/base/src/nsImageFrame.cpp +++ b/layout/html/base/src/nsImageFrame.cpp @@ -1428,7 +1428,7 @@ nsImageFrame::GetBaseURI(nsIURI **aURI) nsCOMPtr doc; if (mContent) { rv = mContent->GetDocument(*getter_AddRefs(doc)); - if (NS_SUCCEEDED(rv)) { + if (doc) { doc->GetBaseURL(*getter_AddRefs(baseURI)); } } @@ -1489,7 +1489,7 @@ nsImageFrame::CanLoadImage(nsIURI *aURI) nsCOMPtr document; if (mContent) { rv = mContent->GetDocument(*getter_AddRefs(document)); - if (NS_FAILED(rv)) { + if (! document) { NS_ASSERTION(0, "expecting a document"); return shouldLoad; } diff --git a/layout/html/base/src/nsObjectFrame.cpp b/layout/html/base/src/nsObjectFrame.cpp index c2ba0b3af88..b93f33a375c 100644 --- a/layout/html/base/src/nsObjectFrame.cpp +++ b/layout/html/base/src/nsObjectFrame.cpp @@ -1043,6 +1043,9 @@ nsObjectFrame::InstantiatePlugin(nsIPresContext* aPresContext, rv = shell->GetDocument(getter_AddRefs(document)); if (NS_FAILED(rv)) return rv; + if (! document) + return NS_ERROR_FAILURE; + nsCOMPtr globalScript; rv = document->GetScriptGlobalObject(getter_AddRefs(globalScript)); if (NS_FAILED(rv)) return rv; @@ -1222,12 +1225,10 @@ nsObjectFrame::GetBaseURL(nsIURI* &aURL) } else { - nsIDocument* doc = nsnull; - if (NS_SUCCEEDED(mContent->GetDocument(doc))) - { + nsCOMPtr doc; + mContent->GetDocument(*getter_AddRefs(doc)); + if (doc) doc->GetBaseURL(aURL); - NS_RELEASE(doc); - } else return NS_ERROR_FAILURE; } diff --git a/layout/html/style/src/nsCSSFrameConstructor.cpp b/layout/html/style/src/nsCSSFrameConstructor.cpp index ed658b5b7a2..549ff6c7ec0 100644 --- a/layout/html/style/src/nsCSSFrameConstructor.cpp +++ b/layout/html/style/src/nsCSSFrameConstructor.cpp @@ -838,12 +838,21 @@ struct ChildIterator PRUint32 mLength; nsCOMPtr mNodes; - ChildIterator(nsIContent* aContent) + ChildIterator(nsIContent* aContent, nsIDocument* aDocument) :mContent(aContent), mIndex(0), mLength(0), mNodes(nsnull) { + NS_PRECONDITION(aContent != nsnull, "no content"); + NS_PRECONDITION(aDocument != nsnull, "no document"); + + aDocument->GetBindingManager(getter_AddRefs(mBindingManager)); + +#ifdef DEBUG + // Verify that the frame ctor's document is the same as the + // content element's document. nsCOMPtr doc; - aContent->GetDocument(*getter_AddRefs(doc)); - doc->GetBindingManager(getter_AddRefs(mBindingManager)); + mContent->GetDocument(*getter_AddRefs(doc)); + NS_ASSERTION(doc.get() == aDocument, "content doc != frame ctor doc"); +#endif // Retrieve the anonymous content that we should build. mBindingManager->GetXBLChildNodesFor(mContent, getter_AddRefs(mNodes)); @@ -1557,9 +1566,6 @@ nsCSSFrameConstructor::CreateGeneratedContentFrame(nsIPresShell* aPresShe nsIFrame* containerFrame; nsFrameItems childFrames; - nsCOMPtr document; - aContent->GetDocument(*getter_AddRefs(document)); - if (NS_STYLE_DISPLAY_BLOCK == display->mDisplay) { NS_NewBlockFrame(aPresShell, &containerFrame); } else { @@ -1589,7 +1595,7 @@ nsCSSFrameConstructor::CreateGeneratedContentFrame(nsIPresShell* aPresShe // Create a frame nsresult result; - result = CreateGeneratedFrameFor(aPresContext, document, containerFrame, + result = CreateGeneratedFrameFor(aPresContext, mDocument, containerFrame, aContent, textStyleContext, styleContent, contentIndex, &frame); if (NS_SUCCEEDED(result) && frame) { @@ -3112,7 +3118,7 @@ nsCSSFrameConstructor::TableProcessChildren(nsIPresShell* aPresShell, nsCOMPtr parentStyleContext; aParentFrame->GetStyleContext(getter_AddRefs(parentStyleContext)); - ChildIterator iterator(aContent); + ChildIterator iterator(aContent, mDocument); while (iterator.HasMoreChildren()) { nsCOMPtr childContent; iterator.NextChild(getter_AddRefs(childContent)); @@ -3435,8 +3441,6 @@ nsCSSFrameConstructor::ConstructDocElementFrame(nsIPresShell* aPresShell, // build a scrollframe if (!isPaginated && isScrollable) { nsIFrame* newScrollFrame = nsnull; - nsCOMPtr document; - aDocElement->GetDocument(*getter_AddRefs(document)); nsCOMPtr newContext; BeginBuildingScrollFrame( aPresShell, aPresContext, @@ -3445,7 +3449,7 @@ nsCSSFrameConstructor::ConstructDocElementFrame(nsIPresShell* aPresShell, styleContext, aParentFrame, nsLayoutAtoms::scrolledContentPseudo, - document, + mDocument, PR_FALSE, scrollFrame, newContext, @@ -3666,17 +3670,13 @@ nsCSSFrameConstructor::ConstructRootFrame(nsIPresShell* aPresShell, */ // Set up our style rule observer. - nsCOMPtr doc; - aDocElement->GetDocument(*getter_AddRefs(doc)); - if (doc) { - nsCOMPtr bindingManager; - doc->GetBindingManager(getter_AddRefs(bindingManager)); - if (bindingManager) { - nsCOMPtr ruleSupplier(do_QueryInterface(bindingManager)); - nsCOMPtr set; - aPresShell->GetStyleSet(getter_AddRefs(set)); - set->SetStyleRuleSupplier(ruleSupplier); - } + nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); + if (bindingManager) { + nsCOMPtr ruleSupplier(do_QueryInterface(bindingManager)); + nsCOMPtr set; + aPresShell->GetStyleSet(getter_AddRefs(set)); + set->SetStyleRuleSupplier(ruleSupplier); } // --------- BUILD VIEWPORT ----------- @@ -3839,8 +3839,6 @@ nsCSSFrameConstructor::ConstructRootFrame(nsIPresShell* aPresShell, nsIFrame* newScrollableFrame = nsnull; - nsCOMPtr document; - aDocElement->GetDocument(*getter_AddRefs(document)); BeginBuildingScrollFrame( aPresShell, aPresContext, @@ -3849,7 +3847,7 @@ nsCSSFrameConstructor::ConstructRootFrame(nsIPresShell* aPresShell, styleContext, viewportFrame, rootPseudo, - document, + mDocument, PR_TRUE, newFrame, rootPseudoStyle, @@ -5232,13 +5230,8 @@ nsCSSFrameConstructor::CreateAnonymousFrames(nsIPresShell* aPresShell, } #endif - // get the document - nsCOMPtr doc; - nsresult rv = aParent->GetDocument(*getter_AddRefs(doc)); - if (NS_FAILED(rv) || !doc) - return rv; - return CreateAnonymousFrames(aPresShell, aPresContext, aState, aParent, doc, aNewFrame, aChildItems); + return CreateAnonymousFrames(aPresShell, aPresContext, aState, aParent, mDocument, aNewFrame, aChildItems); } // after the node has been constructed and initialized create any @@ -5886,10 +5879,8 @@ nsCSSFrameConstructor::ConstructXULFrame(nsIPresShell* aPresShell, // Process the child content if requested nsFrameItems childItems; if (processChildren || processAnonymousChildren) { - nsCOMPtr doc; - aContent->GetDocument(*getter_AddRefs(doc)); nsCOMPtr bindingManager; - doc->GetBindingManager(getter_AddRefs(bindingManager)); + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); if (processChildren) { bindingManager->ShouldBuildChildFrames(aContent, &processChildren); if (processChildren) @@ -6108,8 +6099,7 @@ nsCSSFrameConstructor::BuildScrollFrame (nsIPresShell* aPresShell, nsIFrame* aScrollPortFrame) { nsIFrame *scrollFrame; - nsCOMPtr document; - aContent->GetDocument(*getter_AddRefs(document)); + nsCOMPtr scrolledContentStyle; @@ -6119,7 +6109,7 @@ nsCSSFrameConstructor::BuildScrollFrame (nsIPresShell* aPresShell, aContentStyle, aParentFrame, nsLayoutAtoms::scrolledContentPseudo, - document, + mDocument, PR_FALSE, aNewFrame, scrolledContentStyle, @@ -7700,23 +7690,20 @@ nsCSSFrameConstructor::AppendFrames(nsIPresContext* aPresContext, static nsIFrame* FindPreviousAnonymousSibling(nsIPresShell* aPresShell, - nsIContent* aContainer, - nsIContent* aChild) + nsIDocument* aDocument, + nsIContent* aContainer, + nsIContent* aChild) { + NS_PRECONDITION(aDocument, "null document from content element in FindPreviousAnonymousSibling"); nsIFrame* prevSibling = nsnull; nsCOMPtr nodeList; - nsCOMPtr doc; - aContainer->GetDocument(*getter_AddRefs(doc)); - NS_ASSERTION(doc, "null document from content element in FindPreviousAnonymousSibling"); - if (doc) { - nsCOMPtr xblDoc(do_QueryInterface(doc)); - NS_ASSERTION(xblDoc, "null xblDoc for content element in FindPreviousAnonymousSibling"); - if (xblDoc) { - nsCOMPtr elt(do_QueryInterface(aContainer)); - xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList)); - } + nsCOMPtr xblDoc(do_QueryInterface(aDocument)); + NS_ASSERTION(xblDoc, "null xblDoc for content element in FindPreviousAnonymousSibling"); + if (xblDoc) { + nsCOMPtr elt(do_QueryInterface(aContainer)); + xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList)); } if (nodeList) { PRUint32 ctr,listLength; @@ -7769,23 +7756,21 @@ FindPreviousAnonymousSibling(nsIPresShell* aPresShell, static nsIFrame* FindNextAnonymousSibling(nsIPresShell* aPresShell, - nsIContent* aContainer, - nsIContent* aChild) + nsIDocument* aDocument, + nsIContent* aContainer, + nsIContent* aChild) { + NS_PRECONDITION(aDocument, "null document from content element in FindNextAnonymousSibling"); + nsIFrame* nextSibling = nsnull; nsCOMPtr nodeList; - nsCOMPtr doc; - aContainer->GetDocument(*getter_AddRefs(doc)); - NS_ASSERTION(doc, "null document from content element in FindNextAnonymousSibling"); - if (doc) { - nsCOMPtr xblDoc(do_QueryInterface(doc)); - NS_ASSERTION(xblDoc, "null xblDoc for content element in FindNextAnonymousSibling"); - if (xblDoc) { - nsCOMPtr elt(do_QueryInterface(aContainer)); - xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList)); - } + nsCOMPtr xblDoc(do_QueryInterface(aDocument)); + NS_ASSERTION(xblDoc, "null xblDoc for content element in FindNextAnonymousSibling"); + if (xblDoc) { + nsCOMPtr elt(do_QueryInterface(aContainer)); + xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList)); } if (nodeList) { PRUint32 ctr,listLength; @@ -7945,17 +7930,12 @@ nsCSSFrameConstructor::ContentAppended(nsIPresContext* aPresContext, #ifdef INCLUDE_XUL if (aContainer) { - nsCOMPtr tag; - nsCOMPtr doc; nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); + + nsCOMPtr tag; PRInt32 namespaceID; - aContainer->GetDocument(*getter_AddRefs(doc)); - if (doc) { - doc->GetBindingManager(getter_AddRefs(bindingManager)); - bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); - } - else - aContainer->GetTag(*getter_AddRefs(tag)); + bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); PRBool treeChildren = tag && tag.get() == nsXULAtoms::treechildren; PRBool treeItem = tag && tag.get() == nsXULAtoms::treeitem; @@ -8403,17 +8383,12 @@ nsCSSFrameConstructor::ContentInserted(nsIPresContext* aPresContext, #ifdef INCLUDE_XUL if (aContainer) { - nsCOMPtr tag; - nsCOMPtr doc; nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); + + nsCOMPtr tag; PRInt32 namespaceID; - aContainer->GetDocument(*getter_AddRefs(doc)); - if (doc) { - doc->GetBindingManager(getter_AddRefs(bindingManager)); - bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); - } - else - aContainer->GetTag(*getter_AddRefs(tag)); + bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); PRBool treeChildren = tag && tag.get() == nsXULAtoms::treechildren; PRBool treeItem = tag && tag.get() == nsXULAtoms::treeitem; @@ -8570,7 +8545,7 @@ nsCSSFrameConstructor::ContentInserted(nsIPresContext* aPresContext, // Find the frame that precedes the insertion point. nsIFrame* prevSibling = (aIndexInContainer == -1) ? - FindPreviousAnonymousSibling(shell, aContainer, aChild) : + FindPreviousAnonymousSibling(shell, mDocument, aContainer, aChild) : FindPreviousSibling(shell, aContainer, aIndexInContainer); nsIFrame* nextSibling = nsnull; @@ -8579,7 +8554,7 @@ nsCSSFrameConstructor::ContentInserted(nsIPresContext* aPresContext, // If there is no previous sibling, then find the frame that follows if (nsnull == prevSibling) { nextSibling = (aIndexInContainer == -1) ? - FindNextAnonymousSibling(shell, aContainer, aChild) : + FindNextAnonymousSibling(shell, mDocument, aContainer, aChild) : FindNextSibling(shell, aContainer, aIndexInContainer); } @@ -9127,17 +9102,12 @@ nsCSSFrameConstructor::ContentRemoved(nsIPresContext* aPresContext, #ifdef INCLUDE_XUL if (aContainer) { - nsCOMPtr tag; - nsCOMPtr doc; nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); + + nsCOMPtr tag; PRInt32 namespaceID; - aContainer->GetDocument(*getter_AddRefs(doc)); - if (doc) { - doc->GetBindingManager(getter_AddRefs(bindingManager)); - bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); - } - else - aContainer->GetTag(*getter_AddRefs(tag)); + bindingManager->ResolveTag(aContainer, &namespaceID, getter_AddRefs(tag)); PRBool treeChildren = tag && tag.get() == nsXULAtoms::treechildren; PRBool treeItem = tag && tag.get() == nsXULAtoms::treeitem; @@ -9975,18 +9945,12 @@ nsCSSFrameConstructor::AttributeChanged(nsIPresContext* aPresContext, // content from being removed and re-inserted (which is what would // happen otherwise). if (!primaryFrame && !reframe) { - nsCOMPtr tag; - nsCOMPtr doc; - aContent->GetDocument(*getter_AddRefs(doc)); - if (doc) { - nsCOMPtr bindingManager; - doc->GetBindingManager(getter_AddRefs(bindingManager)); + nsCOMPtr bindingManager; + mDocument->GetBindingManager(getter_AddRefs(bindingManager)); - PRInt32 namespaceID; - bindingManager->ResolveTag(aContent, &namespaceID, getter_AddRefs(tag)); - } - else - aContent->GetTag(*getter_AddRefs(tag)); + PRInt32 namespaceID; + nsCOMPtr tag; + bindingManager->ResolveTag(aContent, &namespaceID, getter_AddRefs(tag)); if (tag && (tag.get() == nsXULAtoms::treechildren || (tag.get() == nsXULAtoms::treeitem && aAttribute != nsXULAtoms::open) || @@ -10303,10 +10267,8 @@ nsCSSFrameConstructor::ConstructAlternateFrame(nsIPresShell* aPresShell, NS_RELEASE(domData); // Set aContent as the parent content and set the document object - nsCOMPtr document; - aContent->GetDocument(*getter_AddRefs(document)); altTextContent->SetParent(aContent); - altTextContent->SetDocument(document, PR_TRUE, PR_TRUE); + altTextContent->SetDocument(mDocument, PR_TRUE, PR_TRUE); // Create either an inline frame, block frame, or area frame nsIFrame* containerFrame; @@ -11520,7 +11482,7 @@ nsCSSFrameConstructor::ProcessChildren(nsIPresShell* aPresShell, nsPseudoFrames priorPseudoFrames; aState.mPseudoFrames.Reset(&priorPseudoFrames); - ChildIterator iterator(aContent); + ChildIterator iterator(aContent, mDocument); while (iterator.HasMoreChildren()) { nsCOMPtr childContent; iterator.NextChild(getter_AddRefs(childContent)); @@ -12677,7 +12639,7 @@ nsCSSFrameConstructor::ProcessBlockChildren(nsIPresShell* aPresShell, } // Iterate the child content objects and construct frames - ChildIterator iterator(aContent); + ChildIterator iterator(aContent, mDocument); while (iterator.HasMoreChildren()) { nsCOMPtr childContent; iterator.NextChild(getter_AddRefs(childContent)); @@ -12974,7 +12936,7 @@ nsCSSFrameConstructor::ProcessInlineChildren(nsIPresShell* aPresShell, // Iterate the child content objects and construct frames PRBool allKidsInline = PR_TRUE; - ChildIterator iterator(aContent); + ChildIterator iterator(aContent, mDocument); while (iterator.HasMoreChildren()) { nsCOMPtr childContent; iterator.NextChild(getter_AddRefs(childContent));