diff --git a/dom/base/nsNameSpaceManager.cpp b/dom/base/nsNameSpaceManager.cpp index 4a1c2c7b5013..1ce7b3ff73ef 100644 --- a/dom/base/nsNameSpaceManager.cpp +++ b/dom/base/nsNameSpaceManager.cpp @@ -66,6 +66,8 @@ bool nsNameSpaceManager::Init() // Need to be ordered according to ID. + MOZ_ASSERT(mURIArray.IsEmpty()); + REGISTER_NAMESPACE(nsGkAtoms::empty, kNameSpaceID_None); REGISTER_NAMESPACE(nsGkAtoms::nsuri_xmlns, kNameSpaceID_XMLNS); REGISTER_NAMESPACE(nsGkAtoms::nsuri_xml, kNameSpaceID_XML); REGISTER_NAMESPACE(nsGkAtoms::nsuri_xhtml, kNameSpaceID_XHTML); @@ -97,7 +99,7 @@ nsNameSpaceManager::RegisterNameSpace(const nsAString& aURI, nsCOMPtr atom = NS_Atomize(aURI); nsresult rv = NS_OK; if (!mURIToIDTable.Get(atom, &aNameSpaceID)) { - aNameSpaceID = mURIArray.Length() + 1; // id is index + 1 + aNameSpaceID = mURIArray.Length(); rv = AddNameSpace(atom.forget(), aNameSpaceID); if (NS_FAILED(rv)) { @@ -114,15 +116,16 @@ nsresult nsNameSpaceManager::GetNameSpaceURI(int32_t aNameSpaceID, nsAString& aURI) { NS_PRECONDITION(aNameSpaceID >= 0, "Bogus namespace ID"); - - int32_t index = aNameSpaceID - 1; // id is index + 1 - if (index < 0 || index >= int32_t(mURIArray.Length())) { + + // We have historically treated GetNameSpaceURI calls for kNameSpaceID_None + // as erroneous. + if (aNameSpaceID <= 0 || aNameSpaceID >= int32_t(mURIArray.Length())) { aURI.Truncate(); return NS_ERROR_ILLEGAL_VALUE; } - mURIArray.ElementAt(index)->ToString(aURI); + mURIArray.ElementAt(aNameSpaceID)->ToString(aURI); return NS_OK; } @@ -224,10 +227,8 @@ nsresult nsNameSpaceManager::AddNameSpace(already_AddRefed aURI, // We've wrapped... Can't do anything else here; just bail. return NS_ERROR_OUT_OF_MEMORY; } - - NS_ASSERTION(aNameSpaceID - 1 == (int32_t) mURIArray.Length(), - "BAD! AddNameSpace not called in right order!"); + MOZ_ASSERT(aNameSpaceID == (int32_t) mURIArray.Length()); mURIArray.AppendElement(uri.forget()); mURIToIDTable.Put(mURIArray.LastElement(), aNameSpaceID); @@ -244,9 +245,7 @@ nsNameSpaceManager::AddDisabledNameSpace(already_AddRefed aURI, return NS_ERROR_OUT_OF_MEMORY; } - NS_ASSERTION(aNameSpaceID - 1 == (int32_t) mURIArray.Length(), - "BAD! AddDisabledNameSpace not called in right order!"); - + MOZ_ASSERT(aNameSpaceID == (int32_t) mURIArray.Length()); mURIArray.AppendElement(uri.forget()); mDisabledURIToIDTable.Put(mURIArray.LastElement(), aNameSpaceID); diff --git a/dom/base/nsNameSpaceManager.h b/dom/base/nsNameSpaceManager.h index 6e9ed8e25204..d5c3a25fe819 100644 --- a/dom/base/nsNameSpaceManager.h +++ b/dom/base/nsNameSpaceManager.h @@ -42,9 +42,19 @@ public: virtual nsresult GetNameSpaceURI(int32_t aNameSpaceID, nsAString& aURI); + // Returns the atom for the namespace URI associated with the given ID. The + // ID must be within range and not be kNameSpaceID_None (i.e. zero); nsIAtom* NameSpaceURIAtom(int32_t aNameSpaceID) { - MOZ_ASSERT(aNameSpaceID > 0 && (int64_t) aNameSpaceID <= (int64_t) mURIArray.Length()); - return mURIArray.ElementAt(aNameSpaceID - 1); // id is index + 1 + MOZ_ASSERT(aNameSpaceID > 0); + return NameSpaceURIAtomForServo(aNameSpaceID); + } + + // NB: This function should only be called by Servo code (and the above + // accessor), which uses the empty atom to represent kNameSpaceID_None. + nsIAtom* NameSpaceURIAtomForServo(int32_t aNameSpaceID) { + MOZ_ASSERT(aNameSpaceID >= 0); + MOZ_ASSERT((int64_t) aNameSpaceID < (int64_t) mURIArray.Length()); + return mURIArray.ElementAt(aNameSpaceID); } int32_t GetNameSpaceID(const nsAString& aURI, diff --git a/layout/style/ServoBindings.cpp b/layout/style/ServoBindings.cpp index c888aed167e9..0c6e69df42fa 100644 --- a/layout/style/ServoBindings.cpp +++ b/layout/style/ServoBindings.cpp @@ -155,7 +155,7 @@ nsIAtom* Gecko_Namespace(RawGeckoElement* aElement) { int32_t id = aElement->NodeInfo()->NamespaceID(); - return nsContentUtils::NameSpaceManager()->NameSpaceURIAtom(id); + return nsContentUtils::NameSpaceManager()->NameSpaceURIAtomForServo(id); } nsIAtom*