2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2006-03-30 12:03:04 +04:00
|
|
|
/*
|
|
|
|
* Class that represents the name (nodeinfo or atom) of an attribute;
|
|
|
|
* using nodeinfos all the time is too slow, so we use atoms when we
|
|
|
|
* can.
|
|
|
|
*/
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
#ifndef nsAttrName_h___
|
|
|
|
#define nsAttrName_h___
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
#include "mozilla/dom/NodeInfo.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2004-04-01 23:44:17 +04:00
|
|
|
#include "nsDOMString.h"
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
#define NS_ATTRNAME_NODEINFO_BIT 1
|
|
|
|
class nsAttrName
|
|
|
|
{
|
|
|
|
public:
|
2004-09-05 23:13:25 +04:00
|
|
|
nsAttrName(const nsAttrName& aOther)
|
2004-01-15 20:07:27 +03:00
|
|
|
: mBits(aOther.mBits)
|
|
|
|
{
|
|
|
|
AddRefInternalName();
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
explicit nsAttrName(nsAtom* aAtom)
|
2012-10-02 12:24:11 +04:00
|
|
|
: mBits(reinterpret_cast<uintptr_t>(aAtom))
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aAtom, "null atom-name in nsAttrName");
|
|
|
|
NS_ADDREF(aAtom);
|
|
|
|
}
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
explicit nsAttrName(mozilla::dom::NodeInfo* aNodeInfo)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aNodeInfo, "null nodeinfo-name in nsAttrName");
|
|
|
|
if (aNodeInfo->NamespaceEquals(kNameSpaceID_None)) {
|
2012-10-02 12:24:11 +04:00
|
|
|
mBits = reinterpret_cast<uintptr_t>(aNodeInfo->NameAtom());
|
2004-01-15 20:07:27 +03:00
|
|
|
NS_ADDREF(aNodeInfo->NameAtom());
|
|
|
|
}
|
|
|
|
else {
|
2012-10-02 12:24:11 +04:00
|
|
|
mBits = reinterpret_cast<uintptr_t>(aNodeInfo) |
|
2004-01-15 20:07:27 +03:00
|
|
|
NS_ATTRNAME_NODEINFO_BIT;
|
|
|
|
NS_ADDREF(aNodeInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsAttrName()
|
|
|
|
{
|
|
|
|
ReleaseInternalName();
|
|
|
|
}
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
void SetTo(mozilla::dom::NodeInfo* aNodeInfo)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aNodeInfo, "null nodeinfo-name in nsAttrName");
|
|
|
|
|
|
|
|
ReleaseInternalName();
|
|
|
|
if (aNodeInfo->NamespaceEquals(kNameSpaceID_None)) {
|
2012-10-02 12:24:11 +04:00
|
|
|
mBits = reinterpret_cast<uintptr_t>(aNodeInfo->NameAtom());
|
2004-01-15 20:07:27 +03:00
|
|
|
NS_ADDREF(aNodeInfo->NameAtom());
|
|
|
|
}
|
|
|
|
else {
|
2012-10-02 12:24:11 +04:00
|
|
|
mBits = reinterpret_cast<uintptr_t>(aNodeInfo) |
|
2004-01-15 20:07:27 +03:00
|
|
|
NS_ATTRNAME_NODEINFO_BIT;
|
|
|
|
NS_ADDREF(aNodeInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
void SetTo(nsAtom* aAtom)
|
2004-02-10 12:08:06 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aAtom, "null atom-name in nsAttrName");
|
|
|
|
|
|
|
|
ReleaseInternalName();
|
2012-10-02 12:24:11 +04:00
|
|
|
mBits = reinterpret_cast<uintptr_t>(aAtom);
|
2004-02-10 12:08:06 +03:00
|
|
|
NS_ADDREF(aAtom);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsAtom() const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
return !(mBits & NS_ATTRNAME_NODEINFO_BIT);
|
|
|
|
}
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
mozilla::dom::NodeInfo* NodeInfo() const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!IsAtom(), "getting nodeinfo-value of atom-name");
|
2014-06-20 06:01:40 +04:00
|
|
|
return reinterpret_cast<mozilla::dom::NodeInfo*>(mBits & ~NS_ATTRNAME_NODEINFO_BIT);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* Atom() const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IsAtom(), "getting atom-value of nodeinfo-name");
|
2017-10-03 01:05:19 +03:00
|
|
|
return reinterpret_cast<nsAtom*>(mBits);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Equals(const nsAttrName& aOther) const
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
|
|
|
return mBits == aOther.mBits;
|
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
// Faster comparison in the case we know the namespace is null
|
2017-06-18 05:39:15 +03:00
|
|
|
// Note that some callers such as nsAttrAndChildArray::IndexOfAttr() will
|
|
|
|
// call this function on nsAttrName structs with 0 mBits, so no attempt
|
|
|
|
// must be made to do anything with mBits besides comparing it with the
|
|
|
|
// incoming aAtom argument.
|
2017-10-03 01:05:19 +03:00
|
|
|
bool Equals(nsAtom* aAtom) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2012-10-02 12:24:11 +04:00
|
|
|
return reinterpret_cast<uintptr_t>(aAtom) == mBits;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2013-05-11 02:57:58 +04:00
|
|
|
// And the same but without forcing callers to atomize
|
|
|
|
bool Equals(const nsAString& aLocalName) const
|
|
|
|
{
|
|
|
|
return IsAtom() && Atom()->Equals(aLocalName);
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
bool Equals(nsAtom* aLocalName, int32_t aNamespaceID) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
|
|
|
return Equals(aLocalName);
|
|
|
|
}
|
|
|
|
return !IsAtom() && NodeInfo()->Equals(aLocalName, aNamespaceID);
|
|
|
|
}
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
bool Equals(mozilla::dom::NodeInfo* aNodeInfo) const
|
2006-05-16 18:51:52 +04:00
|
|
|
{
|
|
|
|
return Equals(aNodeInfo->NameAtom(), aNodeInfo->NamespaceID());
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t NamespaceID() const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
return IsAtom() ? kNameSpaceID_None : NodeInfo()->NamespaceID();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t NamespaceEquals(int32_t aNamespaceID) const
|
2005-12-29 00:52:39 +03:00
|
|
|
{
|
|
|
|
return aNamespaceID == kNameSpaceID_None ?
|
|
|
|
IsAtom() :
|
|
|
|
(!IsAtom() && NodeInfo()->NamespaceEquals(aNamespaceID));
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* LocalName() const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
return IsAtom() ? Atom() : NodeInfo()->NameAtom();
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* GetPrefix() const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
return IsAtom() ? nullptr : NodeInfo()->GetPrefixAtom();
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool QualifiedNameEquals(const nsAString& aName) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
return IsAtom() ? Atom()->Equals(aName) :
|
2004-01-15 20:07:27 +03:00
|
|
|
NodeInfo()->QualifiedNameEquals(aName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetQualifiedName(nsAString& aStr) const
|
|
|
|
{
|
|
|
|
if (IsAtom()) {
|
|
|
|
Atom()->ToString(aStr);
|
|
|
|
}
|
|
|
|
else {
|
2011-05-05 20:26:33 +04:00
|
|
|
aStr = NodeInfo()->QualifiedName();
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-27 17:01:55 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2004-01-15 20:07:27 +03:00
|
|
|
void GetPrefix(nsAString& aStr) const
|
|
|
|
{
|
|
|
|
if (IsAtom()) {
|
|
|
|
SetDOMStringToNull(aStr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NodeInfo()->GetPrefix(aStr);
|
|
|
|
}
|
|
|
|
}
|
2012-06-27 17:01:55 +04:00
|
|
|
#endif
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t HashValue() const
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
// mBits and uint32_t might have different size. This should silence
|
2004-01-26 22:22:05 +03:00
|
|
|
// any warnings or compile-errors. This is what the implementation of
|
|
|
|
// NS_PTR_TO_INT32 does to take care of the same problem.
|
|
|
|
return mBits - 0;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
bool IsSmaller(nsAtom* aOther) const
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
2012-10-02 12:24:11 +04:00
|
|
|
return mBits < reinterpret_cast<uintptr_t>(aOther);
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
private:
|
|
|
|
|
|
|
|
void AddRefInternalName()
|
|
|
|
{
|
2014-06-20 06:01:40 +04:00
|
|
|
if (IsAtom()) {
|
|
|
|
NS_ADDREF(Atom());
|
|
|
|
} else {
|
|
|
|
NS_ADDREF(NodeInfo());
|
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseInternalName()
|
|
|
|
{
|
2014-06-20 06:01:40 +04:00
|
|
|
if (IsAtom()) {
|
|
|
|
Atom()->Release();
|
|
|
|
} else {
|
|
|
|
NodeInfo()->Release();
|
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2012-10-02 12:24:11 +04:00
|
|
|
uintptr_t mBits;
|
2004-01-15 20:07:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|