txNamespaceMap.cpp (2215B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "txNamespaceMap.h" 7 8 #include "nsGkAtoms.h" 9 #include "txXPathNode.h" 10 11 txNamespaceMap::txNamespaceMap() = default; 12 13 txNamespaceMap::txNamespaceMap(const txNamespaceMap& aOther) 14 : mPrefixes(aOther.mPrefixes.Clone()), 15 mNamespaces(aOther.mNamespaces.Clone()) {} 16 17 nsresult txNamespaceMap::mapNamespace(nsAtom* aPrefix, 18 const nsAString& aNamespaceURI) { 19 nsAtom* prefix = aPrefix == nsGkAtoms::_empty ? nullptr : aPrefix; 20 21 int32_t nsId; 22 if (prefix && aNamespaceURI.IsEmpty()) { 23 // Remove the mapping 24 int32_t index = mPrefixes.IndexOf(prefix); 25 if (index >= 0) { 26 mPrefixes.RemoveElementAt(index); 27 mNamespaces.RemoveElementAt(index); 28 } 29 30 return NS_OK; 31 } 32 33 if (aNamespaceURI.IsEmpty()) { 34 // Set default to empty namespace 35 nsId = kNameSpaceID_None; 36 } else { 37 nsId = txNamespaceManager::getNamespaceID(aNamespaceURI); 38 NS_ENSURE_FALSE(nsId == kNameSpaceID_Unknown, NS_ERROR_FAILURE); 39 } 40 41 // Check if the mapping already exists 42 int32_t index = mPrefixes.IndexOf(prefix); 43 if (index >= 0) { 44 mNamespaces.ElementAt(index) = nsId; 45 46 return NS_OK; 47 } 48 49 // New mapping 50 mPrefixes.AppendElement(prefix); 51 mNamespaces.AppendElement(nsId); 52 53 return NS_OK; 54 } 55 56 int32_t txNamespaceMap::lookupNamespace(nsAtom* aPrefix) { 57 if (aPrefix == nsGkAtoms::xml) { 58 return kNameSpaceID_XML; 59 } 60 61 nsAtom* prefix = aPrefix == nsGkAtoms::_empty ? 0 : aPrefix; 62 63 int32_t index = mPrefixes.IndexOf(prefix); 64 if (index >= 0) { 65 return mNamespaces.SafeElementAt(index, kNameSpaceID_Unknown); 66 } 67 68 if (!prefix) { 69 return kNameSpaceID_None; 70 } 71 72 return kNameSpaceID_Unknown; 73 } 74 75 int32_t txNamespaceMap::lookupNamespaceWithDefault(const nsAString& aPrefix) { 76 RefPtr<nsAtom> prefix = NS_Atomize(aPrefix); 77 if (prefix != nsGkAtoms::_poundDefault) { 78 return lookupNamespace(prefix); 79 } 80 81 return lookupNamespace(nullptr); 82 }