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/. */
6 #include "txNamespaceMap.h"
8 #include "txXPathNode.h"
10 txNamespaceMap::txNamespaceMap() = default;
12 txNamespaceMap::txNamespaceMap(const txNamespaceMap
& aOther
)
13 : mPrefixes(aOther
.mPrefixes
.Clone()),
14 mNamespaces(aOther
.mNamespaces
.Clone()) {}
16 nsresult
txNamespaceMap::mapNamespace(nsAtom
* aPrefix
,
17 const nsAString
& aNamespaceURI
) {
18 nsAtom
* prefix
= aPrefix
== nsGkAtoms::_empty
? nullptr : aPrefix
;
21 if (prefix
&& aNamespaceURI
.IsEmpty()) {
23 int32_t index
= mPrefixes
.IndexOf(prefix
);
25 mPrefixes
.RemoveElementAt(index
);
26 mNamespaces
.RemoveElementAt(index
);
32 if (aNamespaceURI
.IsEmpty()) {
33 // Set default to empty namespace
34 nsId
= kNameSpaceID_None
;
36 nsId
= txNamespaceManager::getNamespaceID(aNamespaceURI
);
37 NS_ENSURE_FALSE(nsId
== kNameSpaceID_Unknown
, NS_ERROR_FAILURE
);
40 // Check if the mapping already exists
41 int32_t index
= mPrefixes
.IndexOf(prefix
);
43 mNamespaces
.ElementAt(index
) = nsId
;
49 mPrefixes
.AppendElement(prefix
);
50 mNamespaces
.AppendElement(nsId
);
55 int32_t txNamespaceMap::lookupNamespace(nsAtom
* aPrefix
) {
56 if (aPrefix
== nsGkAtoms::xml
) {
57 return kNameSpaceID_XML
;
60 nsAtom
* prefix
= aPrefix
== nsGkAtoms::_empty
? 0 : aPrefix
;
62 int32_t index
= mPrefixes
.IndexOf(prefix
);
64 return mNamespaces
.SafeElementAt(index
, kNameSpaceID_Unknown
);
68 return kNameSpaceID_None
;
71 return kNameSpaceID_Unknown
;
74 int32_t txNamespaceMap::lookupNamespaceWithDefault(const nsAString
& aPrefix
) {
75 RefPtr
<nsAtom
> prefix
= NS_Atomize(aPrefix
);
76 if (prefix
!= nsGkAtoms::_poundDefault
) {
77 return lookupNamespace(prefix
);
80 return lookupNamespace(nullptr);