Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 /*
7 * A class for keeping track of prefix-to-namespace-id mappings
8 */
10 #include "nsXMLNameSpaceMap.h"
11 #include "nsContentUtils.h"
12 #include "nsGkAtoms.h"
13 #include "nsNameSpaceManager.h"
15 template <>
16 class nsDefaultComparator <nsNameSpaceEntry, nsIAtom*> {
17 public:
18 bool Equals(const nsNameSpaceEntry& aEntry, nsIAtom* const& aPrefix) const {
19 return aEntry.prefix == aPrefix;
20 }
21 };
23 template <>
24 class nsDefaultComparator <nsNameSpaceEntry, int32_t> {
25 public:
26 bool Equals(const nsNameSpaceEntry& aEntry, const int32_t& aNameSpace) const {
27 return aEntry.nameSpaceID == aNameSpace;
28 }
29 };
32 /* static */ nsXMLNameSpaceMap*
33 nsXMLNameSpaceMap::Create(bool aForXML)
34 {
35 nsXMLNameSpaceMap *map = new nsXMLNameSpaceMap();
36 NS_ENSURE_TRUE(map, nullptr);
38 if (aForXML) {
39 nsresult rv1 = map->AddPrefix(nsGkAtoms::xmlns,
40 kNameSpaceID_XMLNS);
41 nsresult rv2 = map->AddPrefix(nsGkAtoms::xml, kNameSpaceID_XML);
43 if (NS_FAILED(rv1) || NS_FAILED(rv2)) {
44 delete map;
45 map = nullptr;
46 }
47 }
49 return map;
50 }
52 nsXMLNameSpaceMap::nsXMLNameSpaceMap()
53 : mNameSpaces(4)
54 {
55 }
57 nsresult
58 nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, int32_t aNameSpaceID)
59 {
60 if (!mNameSpaces.Contains(aPrefix) && !mNameSpaces.AppendElement(aPrefix)) {
61 return NS_ERROR_OUT_OF_MEMORY;
62 }
63 mNameSpaces[mNameSpaces.IndexOf(aPrefix)].nameSpaceID = aNameSpaceID;
64 return NS_OK;
65 }
67 nsresult
68 nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, nsString &aURI)
69 {
70 int32_t id;
71 nsresult rv = nsContentUtils::NameSpaceManager()->RegisterNameSpace(aURI,
72 id);
74 NS_ENSURE_SUCCESS(rv, rv);
76 return AddPrefix(aPrefix, id);
77 }
79 int32_t
80 nsXMLNameSpaceMap::FindNameSpaceID(nsIAtom *aPrefix) const
81 {
82 uint32_t index = mNameSpaces.IndexOf(aPrefix);
83 if (index != mNameSpaces.NoIndex) {
84 return mNameSpaces[index].nameSpaceID;
85 }
87 // The default mapping for no prefix is no namespace. If a non-null prefix
88 // was specified and we didn't find it, we return an error.
90 return aPrefix ? kNameSpaceID_Unknown : kNameSpaceID_None;
91 }
93 nsIAtom*
94 nsXMLNameSpaceMap::FindPrefix(int32_t aNameSpaceID) const
95 {
96 uint32_t index = mNameSpaces.IndexOf(aNameSpaceID);
97 if (index != mNameSpaces.NoIndex) {
98 return mNameSpaces[index].prefix;
99 }
101 return nullptr;
102 }
104 void
105 nsXMLNameSpaceMap::Clear()
106 {
107 mNameSpaces.Clear();
108 }