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/. */
7 * Class that represents the name (nodeinfo or atom) of an attribute;
8 * using nodeinfos all the time is too slow, so we use atoms when we
12 #ifndef nsAttrName_h___
13 #define nsAttrName_h___
15 #include "mozilla/dom/NodeInfo.h"
17 #include "nsDOMString.h"
19 #define NS_ATTRNAME_NODEINFO_BIT 1
23 nsAttrName(const nsAttrName
& aOther
)
29 explicit nsAttrName(nsIAtom
* aAtom
)
30 : mBits(reinterpret_cast<uintptr_t>(aAtom
))
32 NS_ASSERTION(aAtom
, "null atom-name in nsAttrName");
36 explicit nsAttrName(mozilla::dom::NodeInfo
* aNodeInfo
)
38 NS_ASSERTION(aNodeInfo
, "null nodeinfo-name in nsAttrName");
39 if (aNodeInfo
->NamespaceEquals(kNameSpaceID_None
)) {
40 mBits
= reinterpret_cast<uintptr_t>(aNodeInfo
->NameAtom());
41 NS_ADDREF(aNodeInfo
->NameAtom());
44 mBits
= reinterpret_cast<uintptr_t>(aNodeInfo
) |
45 NS_ATTRNAME_NODEINFO_BIT
;
52 ReleaseInternalName();
55 void SetTo(mozilla::dom::NodeInfo
* aNodeInfo
)
57 NS_ASSERTION(aNodeInfo
, "null nodeinfo-name in nsAttrName");
59 ReleaseInternalName();
60 if (aNodeInfo
->NamespaceEquals(kNameSpaceID_None
)) {
61 mBits
= reinterpret_cast<uintptr_t>(aNodeInfo
->NameAtom());
62 NS_ADDREF(aNodeInfo
->NameAtom());
65 mBits
= reinterpret_cast<uintptr_t>(aNodeInfo
) |
66 NS_ATTRNAME_NODEINFO_BIT
;
71 void SetTo(nsIAtom
* aAtom
)
73 NS_ASSERTION(aAtom
, "null atom-name in nsAttrName");
75 ReleaseInternalName();
76 mBits
= reinterpret_cast<uintptr_t>(aAtom
);
82 return !(mBits
& NS_ATTRNAME_NODEINFO_BIT
);
85 mozilla::dom::NodeInfo
* NodeInfo() const
87 NS_ASSERTION(!IsAtom(), "getting nodeinfo-value of atom-name");
88 return reinterpret_cast<mozilla::dom::NodeInfo
*>(mBits
& ~NS_ATTRNAME_NODEINFO_BIT
);
93 NS_ASSERTION(IsAtom(), "getting atom-value of nodeinfo-name");
94 return reinterpret_cast<nsIAtom
*>(mBits
);
97 bool Equals(const nsAttrName
& aOther
) const
99 return mBits
== aOther
.mBits
;
102 // Faster comparison in the case we know the namespace is null
103 bool Equals(nsIAtom
* aAtom
) const
105 return reinterpret_cast<uintptr_t>(aAtom
) == mBits
;
108 // And the same but without forcing callers to atomize
109 bool Equals(const nsAString
& aLocalName
) const
111 return IsAtom() && Atom()->Equals(aLocalName
);
114 bool Equals(nsIAtom
* aLocalName
, int32_t aNamespaceID
) const
116 if (aNamespaceID
== kNameSpaceID_None
) {
117 return Equals(aLocalName
);
119 return !IsAtom() && NodeInfo()->Equals(aLocalName
, aNamespaceID
);
122 bool Equals(mozilla::dom::NodeInfo
* aNodeInfo
) const
124 return Equals(aNodeInfo
->NameAtom(), aNodeInfo
->NamespaceID());
127 int32_t NamespaceID() const
129 return IsAtom() ? kNameSpaceID_None
: NodeInfo()->NamespaceID();
132 int32_t NamespaceEquals(int32_t aNamespaceID
) const
134 return aNamespaceID
== kNameSpaceID_None
?
136 (!IsAtom() && NodeInfo()->NamespaceEquals(aNamespaceID
));
139 nsIAtom
* LocalName() const
141 return IsAtom() ? Atom() : NodeInfo()->NameAtom();
144 nsIAtom
* GetPrefix() const
146 return IsAtom() ? nullptr : NodeInfo()->GetPrefixAtom();
149 bool QualifiedNameEquals(const nsAString
& aName
) const
151 return IsAtom() ? Atom()->Equals(aName
) :
152 NodeInfo()->QualifiedNameEquals(aName
);
155 void GetQualifiedName(nsAString
& aStr
) const
158 Atom()->ToString(aStr
);
161 aStr
= NodeInfo()->QualifiedName();
165 #ifdef MOZILLA_INTERNAL_API
166 void GetPrefix(nsAString
& aStr
) const
169 SetDOMStringToNull(aStr
);
172 NodeInfo()->GetPrefix(aStr
);
177 uint32_t HashValue() const
179 // mBits and uint32_t might have different size. This should silence
180 // any warnings or compile-errors. This is what the implementation of
181 // NS_PTR_TO_INT32 does to take care of the same problem.
185 bool IsSmaller(nsIAtom
* aOther
) const
187 return mBits
< reinterpret_cast<uintptr_t>(aOther
);
192 void AddRefInternalName()
197 NS_ADDREF(NodeInfo());
201 void ReleaseInternalName()
206 NodeInfo()->Release();