From 5d88584166e10021e6ba6ed3f506830e5cb479f7 Mon Sep 17 00:00:00 2001 From: "cvshook%sicking.cc" Date: Sat, 15 Apr 2006 05:09:16 +0000 Subject: [PATCH] Bug 333942. Move GetParent to nsINode and add GetNodeParent there too. There is a risk that this'll add some binary size, but hopefully not too much. r/sr=bz git-svn-id: svn://10.0.0.236/trunk@194451 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/public/nsIAttribute.h | 4 +- mozilla/content/base/public/nsIContent.h | 25 ++---------- mozilla/content/base/public/nsIDocument.h | 4 +- mozilla/content/base/public/nsINode.h | 37 +++++++++++++++-- .../content/base/src/nsGenericDOMDataNode.cpp | 17 ++++---- .../content/base/src/nsGenericDOMDataNode.h | 6 +-- mozilla/content/base/src/nsGenericElement.cpp | 40 +++++-------------- mozilla/content/base/src/nsTextFragment.cpp | 4 ++ mozilla/content/base/src/nsTextFragment.h | 15 ++++++- .../content/xul/content/src/nsXULElement.cpp | 20 ++++------ 10 files changed, 88 insertions(+), 84 deletions(-) diff --git a/mozilla/content/base/public/nsIAttribute.h b/mozilla/content/base/public/nsIAttribute.h index f5b8ea1caad..58695ea1394 100644 --- a/mozilla/content/base/public/nsIAttribute.h +++ b/mozilla/content/base/public/nsIAttribute.h @@ -46,8 +46,8 @@ class nsDOMAttributeMap; class nsIContent; #define NS_IATTRIBUTE_IID \ -{ 0xba2a0ee1, 0x2484, 0x49d7, \ - { 0xb5, 0x3c, 0xc8, 0xe4, 0x82, 0x9d, 0xa4, 0xf2 } } +{ 0xb65ca480, 0xca76, 0x495c, \ + { 0x95, 0xe4, 0x8c, 0xd0, 0x55, 0x5d, 0xd3, 0x13 } } class nsIAttribute : public nsINode { diff --git a/mozilla/content/base/public/nsIContent.h b/mozilla/content/base/public/nsIContent.h index 921ef83c414..e4c8f3cb0e9 100644 --- a/mozilla/content/base/public/nsIContent.h +++ b/mozilla/content/base/public/nsIContent.h @@ -61,8 +61,8 @@ class nsAttrName; // IID for the nsIContent interface #define NS_ICONTENT_IID \ -{ 0xda0e2f6c, 0x6cd2, 0x4fdc, \ - { 0x9b, 0x03, 0x35, 0xb5, 0x65, 0x09, 0xa6, 0xab } } +{ 0xf967b2d6, 0xa81c, 0x492c, \ + { 0x91, 0x4b, 0x53, 0x8c, 0x0c, 0x19, 0x37, 0x8c } } /** * A node of content in a document's content model. This interface @@ -77,8 +77,7 @@ public: // nsIContent is that it exists with an IID nsIContent(nsINodeInfo *aNodeInfo) - : nsINode(aNodeInfo), - mParentPtrBits(0) + : nsINode(aNodeInfo) { NS_ASSERTION(aNodeInfo, "No nsINodeInfo passed to nsIContent, PREPARE TO CRASH!!!"); @@ -161,15 +160,6 @@ public: return IsInDoc() ? GetOwnerDoc() : nsnull; } - /** - * Get the parent content for this content. - * @return the parent, or null if no parent - */ - nsIContent* GetParent() const - { - return NS_REINTERPRET_CAST(nsIContent *, mParentPtrBits & ~kParentBitMask); - } - /** * Get whether this content is C++-generated anonymous content * @see nsIAnonymousContentCreator @@ -786,15 +776,6 @@ public: // the tabfocus bit field applies to xul elements. static PRBool sTabFocusModelAppliesToXUL; -protected: - typedef PRWord PtrBits; - - // Subclasses may use the 0x2 bit of mParentPtrBits to store other data - enum { PARENT_BIT_INDOCUMENT = 0x1 }; - enum { kParentBitMask = 0x3 }; - - PtrBits mParentPtrBits; - #endif // MOZILLA_INTERNAL_API }; diff --git a/mozilla/content/base/public/nsIDocument.h b/mozilla/content/base/public/nsIDocument.h index 22539326abb..ca2aecb060c 100644 --- a/mozilla/content/base/public/nsIDocument.h +++ b/mozilla/content/base/public/nsIDocument.h @@ -91,8 +91,8 @@ class nsIDocumentObserver; // IID for the nsIDocument interface #define NS_IDOCUMENT_IID \ -{ 0xfa567fd5, 0x5220, 0x436c, \ - { 0xbe, 0x76, 0xdd, 0x1a, 0x78, 0xfb, 0x8c, 0x1a } } +{ 0x3b3cb52f, 0xf604, 0x48d5, \ + { 0xb5, 0x70, 0xf3, 0x25, 0xbb, 0x0a, 0x7a, 0x22 } } // Flag for AddStyleSheet(). diff --git a/mozilla/content/base/public/nsINode.h b/mozilla/content/base/public/nsINode.h index dbdeeb0e651..65a5efe763e 100644 --- a/mozilla/content/base/public/nsINode.h +++ b/mozilla/content/base/public/nsINode.h @@ -59,8 +59,8 @@ class nsIPrincipal; // IID for the nsINode interface // ec67d9d2-be1e-41d8-b7d0-92f72a2667db #define NS_INODE_IID \ -{ 0xec67d9d2, 0xbe1e, 0x41d8, \ - { 0xb7, 0xd0, 0x92, 0xf7, 0x2a, 0x26, 0x67, 0xdb } } +{ 0x2ad78957, 0x52e8, 0x493a, \ + { 0x8b, 0x5a, 0xe6, 0x91, 0xdc, 0xe3, 0x36, 0xe7 } } // hack to make egcs / gcc 2.95.2 happy class nsINode_base : public nsIDOMGCParticipant { @@ -80,7 +80,8 @@ public: // nsINode is that it exists with an IID, if that.... nsINode(nsINodeInfo* aNodeInfo) - : mNodeInfo(aNodeInfo) + : mNodeInfo(aNodeInfo), + mParentPtrBits(0) { } @@ -309,10 +310,40 @@ public: virtual nsresult GetEventListenerManager(PRBool aCreateIfNotFound, nsIEventListenerManager** aResult) = 0; + + /** + * Get the parent nsIContent for this node. + * @return the parent, or null if no parent or the parent is not an nsIContent + */ + nsIContent* GetParent() const + { + return NS_LIKELY(mParentPtrBits & PARENT_BIT_PARENT_IS_CONTENT) ? + NS_REINTERPRET_CAST(nsIContent*, + mParentPtrBits & ~kParentBitMask) : + nsnull; + } + + /** + * Get the parent nsINode for this node. This can be either an nsIContent, + * an nsIDocument or an nsIAttribute. + * @return the parent node + */ + nsINode* GetNodeParent() const + { + return NS_REINTERPRET_CAST(nsINode*, mParentPtrBits & ~kParentBitMask); + } + protected: nsCOMPtr mNodeInfo; + typedef PRUword PtrBits; + + enum { PARENT_BIT_INDOCUMENT = 1 << 0, PARENT_BIT_PARENT_IS_CONTENT = 1 << 1 }; + enum { kParentBitMask = 0x3 }; + + PtrBits mParentPtrBits; + #endif // MOZILLA_INTERNAL_API }; diff --git a/mozilla/content/base/src/nsGenericDOMDataNode.cpp b/mozilla/content/base/src/nsGenericDOMDataNode.cpp index 5b35c275a49..d1359072386 100644 --- a/mozilla/content/base/src/nsGenericDOMDataNode.cpp +++ b/mozilla/content/base/src/nsGenericDOMDataNode.cpp @@ -676,7 +676,8 @@ nsGenericDOMDataNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent, (aParent->IsContentOfType(eXUL) && aDocument == nsnull) || aDocument == aParent->GetCurrentDoc(), "aDocument must be current doc of aParent"); - NS_PRECONDITION(!GetCurrentDoc(), "Already have a document. Unbind first!"); + NS_PRECONDITION(!GetCurrentDoc() && !IsInDoc(), + "Already have a document. Unbind first!"); // Note that as we recurse into the kids, they'll have a non-null parent. So // only assert if our parent is _changing_ while we have a parent. NS_PRECONDITION(!GetParent() || aParent == GetParent(), @@ -694,9 +695,12 @@ nsGenericDOMDataNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent, nsresult rv; // Set parent - PtrBits new_bits = NS_REINTERPRET_CAST(PtrBits, aParent); - new_bits |= mParentPtrBits & nsIContent::kParentBitMask; - mParentPtrBits = new_bits; + if (aParent) { + mParentPtrBits = NS_REINTERPRET_CAST(PtrBits, aParent) | PARENT_BIT_PARENT_IS_CONTENT; + } + else { + mParentPtrBits = NS_REINTERPRET_CAST(PtrBits, aDocument); + } nsIDocument *oldOwnerDocument = GetOwnerDoc(); nsIDocument *newOwnerDocument; @@ -763,10 +767,7 @@ nsGenericDOMDataNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent, void nsGenericDOMDataNode::UnbindFromTree(PRBool aDeep, PRBool aNullParent) { - mParentPtrBits &= ~PARENT_BIT_INDOCUMENT; - if (aNullParent) { - mParentPtrBits &= nsIContent::kParentBitMask; - } + mParentPtrBits = aNullParent ? 0 : mParentPtrBits & ~PARENT_BIT_INDOCUMENT; } PRBool diff --git a/mozilla/content/base/src/nsGenericDOMDataNode.h b/mozilla/content/base/src/nsGenericDOMDataNode.h index a1e019bf783..811c789d688 100644 --- a/mozilla/content/base/src/nsGenericDOMDataNode.h +++ b/mozilla/content/base/src/nsGenericDOMDataNode.h @@ -312,19 +312,17 @@ protected: nsTextFragment mText; private: - enum { PARENT_BIT_IS_IN_A_HASH = 0x2 }; - void SetBidiStatus(); already_AddRefed GetCurrentValueAtom(); void SetIsInAHash() { - mParentPtrBits |= PARENT_BIT_IS_IN_A_HASH; + mText.SetExtraBit(PR_TRUE); } PRBool GetIsInAHash() const { - return (mParentPtrBits & PARENT_BIT_IS_IN_A_HASH); + return mText.GetExtraBit(); } void SetHasRangeList() diff --git a/mozilla/content/base/src/nsGenericElement.cpp b/mozilla/content/base/src/nsGenericElement.cpp index a4ff9b17784..db25f5f8345 100644 --- a/mozilla/content/base/src/nsGenericElement.cpp +++ b/mozilla/content/base/src/nsGenericElement.cpp @@ -949,20 +949,11 @@ nsGenericElement::GetNodeType(PRUint16* aNodeType) NS_IMETHODIMP nsGenericElement::GetParentNode(nsIDOMNode** aParentNode) { - nsIContent *parent = GetParent(); + nsINode *parent = GetNodeParent(); if (parent) { return CallQueryInterface(parent, aParentNode); } - nsIDocument* doc = GetCurrentDoc(); - if (doc) { - // If we don't have a parent, but we're in the document, we must - // be the root node of the document. The DOM says that the root - // is the document. - - return CallQueryInterface(doc, aParentNode); - } - *aParentNode = nsnull; return NS_OK; @@ -1766,12 +1757,13 @@ nsGenericElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, slots->mBindingParent = aBindingParent; // Weak, so no addref happens. } - // Now set the parent; make sure to preserve the bits we have stashed there - // Note that checking whether aParent == GetParent() is probably not worth it - // here. - PtrBits new_bits = NS_REINTERPRET_CAST(PtrBits, aParent); - new_bits |= mParentPtrBits & nsIContent::kParentBitMask; - mParentPtrBits = new_bits; + // Now set the parent + if (aParent) { + mParentPtrBits = NS_REINTERPRET_CAST(PtrBits, aParent) | PARENT_BIT_PARENT_IS_CONTENT; + } + else { + mParentPtrBits = NS_REINTERPRET_CAST(PtrBits, aDocument); + } nsresult rv; @@ -1881,12 +1873,7 @@ nsGenericElement::UnbindFromTree(PRBool aDeep, PRBool aNullParent) } // Unset things in the reverse order from how we set them in BindToTree - mParentPtrBits &= ~PARENT_BIT_INDOCUMENT; - - if (aNullParent) { - // Just mask it out - mParentPtrBits &= nsIContent::kParentBitMask; - } + mParentPtrBits = aNullParent ? 0 : mParentPtrBits & ~PARENT_BIT_INDOCUMENT; nsDOMSlots *slots = GetExistingDOMSlots(); if (slots) { @@ -3049,15 +3036,8 @@ nsGenericElement::doReplaceOrInsertBefore(PRBool aReplace, PRBool newContentIsXUL = newContent->IsContentOfType(eXUL); // Remove the element from the old parent if one exists - nsINode* oldParent = newContent->GetParent(); - if (!oldParent) { - oldParent = newContent->GetCurrentDoc(); + nsINode* oldParent = newContent->GetNodeParent(); - // See bug 53901. Crappy XUL sometimes lies about being in the document - if (oldParent && newContentIsXUL && oldParent->IndexOf(newContent) < 0) { - oldParent = nsnull; - } - } if (oldParent) { PRInt32 removeIndex = oldParent->IndexOf(newContent); diff --git a/mozilla/content/base/src/nsTextFragment.cpp b/mozilla/content/base/src/nsTextFragment.cpp index 34d04f31390..28dee9d43b0 100644 --- a/mozilla/content/base/src/nsTextFragment.cpp +++ b/mozilla/content/base/src/nsTextFragment.cpp @@ -117,7 +117,9 @@ nsTextFragment::ReleaseText() m1b = nsnull; // Set mState.mIs2b, mState.mInHeap, and mState.mLength = 0 with mAllBits; + PRBool tmp = mState.mExtraBit; mAllBits = 0; + mState.mExtraBit = tmp; } nsTextFragment& @@ -136,7 +138,9 @@ nsTextFragment::operator=(const nsTextFragment& aOther) } if (m1b) { + PRBool tmp = mState.mExtraBit; mAllBits = aOther.mAllBits; + mState.mExtraBit = tmp; } } diff --git a/mozilla/content/base/src/nsTextFragment.h b/mozilla/content/base/src/nsTextFragment.h index 31c5b9ef534..c522e89738e 100644 --- a/mozilla/content/base/src/nsTextFragment.h +++ b/mozilla/content/base/src/nsTextFragment.h @@ -143,6 +143,18 @@ public: return PRInt32(mState.mLength); } + /** + * Store an extra bit of general purpose information. Default value is PR_FALSE; + */ + PRBool GetExtraBit() const + { + return mState.mExtraBit; + } + void SetExtraBit(PRBool aExtraBit) + { + mState.mExtraBit = aExtraBit; + } + /** * Change the contents of this fragment to be a copy of the given * buffer. Like operator= except a length is specified instead of @@ -188,7 +200,8 @@ public: PRBool mInHeap : 1; PRBool mIs2b : 1; PRBool mIsBidi : 1; - PRUint32 mLength : 29; + PRBool mExtraBit : 1; + PRUint32 mLength : 28; }; private: diff --git a/mozilla/content/xul/content/src/nsXULElement.cpp b/mozilla/content/xul/content/src/nsXULElement.cpp index b73097611ef..b25b20001cc 100644 --- a/mozilla/content/xul/content/src/nsXULElement.cpp +++ b/mozilla/content/xul/content/src/nsXULElement.cpp @@ -793,12 +793,13 @@ nsXULElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, // First set the binding parent mBindingParent = aBindingParent; - // Now set the parent; make sure to preserve the bits we have - // stashed there Note that checking whether aParent == GetParent() - // is probably not worth it here. - PtrBits new_bits = NS_REINTERPRET_CAST(PtrBits, aParent); - new_bits |= mParentPtrBits & nsIContent::kParentBitMask; - mParentPtrBits = new_bits; + // Now set the parent + if (aParent) { + mParentPtrBits = NS_REINTERPRET_CAST(PtrBits, aParent) | PARENT_BIT_PARENT_IS_CONTENT; + } + else { + mParentPtrBits = NS_REINTERPRET_CAST(PtrBits, aDocument); + } nsIDocument *oldOwnerDocument = GetOwnerDoc(); nsIDocument *newOwnerDocument; @@ -964,12 +965,7 @@ nsXULElement::UnbindFromTree(PRBool aDeep, PRBool aNullParent) } // Unset things in the reverse order from how we set them in BindToTree - mParentPtrBits &= ~PARENT_BIT_INDOCUMENT; - - if (aNullParent) { - // Just mask it out - mParentPtrBits &= nsIContent::kParentBitMask; - } + mParentPtrBits = aNullParent ? 0 : mParentPtrBits & ~PARENT_BIT_INDOCUMENT; mBindingParent = nsnull;