2001-09-25 01:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2006-03-30 08:03:04 +00:00
|
|
|
|
|
|
|
/*
|
2013-03-10 08:00:33 +00:00
|
|
|
* Implementation of the |attributes| property of DOM Core's Element object.
|
2006-03-30 08:03:04 +00:00
|
|
|
*/
|
|
|
|
|
2013-03-10 08:00:33 +00:00
|
|
|
#ifndef nsDOMAttributeMap_h
|
|
|
|
#define nsDOMAttributeMap_h
|
1999-01-12 08:45:23 +00:00
|
|
|
|
2013-03-10 08:00:33 +00:00
|
|
|
#include "nsIDOMMozNamedAttrMap.h"
|
2012-06-27 13:01:55 +00:00
|
|
|
#include "nsStringGlue.h"
|
2010-04-19 15:41:39 +00:00
|
|
|
#include "nsRefPtrHashtable.h"
|
2007-01-04 22:31:26 +00:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-03-10 08:00:33 +00:00
|
|
|
#include "nsIDOMAttr.h"
|
2012-10-16 11:51:00 +00:00
|
|
|
#include "mozilla/ErrorResult.h"
|
1999-01-12 08:45:23 +00:00
|
|
|
|
2006-09-05 10:22:54 +00:00
|
|
|
class nsIAtom;
|
2005-06-01 13:46:20 +00:00
|
|
|
class nsDOMAttribute;
|
|
|
|
class nsINodeInfo;
|
2005-12-29 03:01:58 +00:00
|
|
|
class nsIDocument;
|
2005-06-01 13:46:20 +00:00
|
|
|
|
2010-07-30 13:42:11 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class Element;
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2005-06-01 13:46:20 +00:00
|
|
|
/**
|
2006-03-30 08:03:04 +00:00
|
|
|
* Structure used as a key for caching nsDOMAttributes in nsDOMAttributeMap's mAttributeCache.
|
2005-06-01 13:46:20 +00:00
|
|
|
*/
|
|
|
|
class nsAttrKey
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The namespace of the attribute
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mNamespaceID;
|
2005-06-01 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The atom for attribute, weak ref. is fine as we only use it for the
|
|
|
|
* hashcode, we never dereference it.
|
|
|
|
*/
|
|
|
|
nsIAtom* mLocalName;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsAttrKey(int32_t aNs, nsIAtom* aName)
|
2005-06-01 13:46:20 +00:00
|
|
|
: mNamespaceID(aNs), mLocalName(aName) {}
|
|
|
|
|
|
|
|
nsAttrKey(const nsAttrKey& aAttr)
|
|
|
|
: mNamespaceID(aAttr.mNamespaceID), mLocalName(aAttr.mLocalName) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PLDHashEntryHdr implementation for nsAttrKey.
|
|
|
|
*/
|
2005-06-01 14:50:34 +00:00
|
|
|
class nsAttrHashKey : public PLDHashEntryHdr
|
2005-06-01 13:46:20 +00:00
|
|
|
{
|
|
|
|
public:
|
2005-06-09 20:17:40 +00:00
|
|
|
typedef const nsAttrKey& KeyType;
|
2005-06-01 13:46:20 +00:00
|
|
|
typedef const nsAttrKey* KeyTypePointer;
|
|
|
|
|
|
|
|
nsAttrHashKey(KeyTypePointer aKey) : mKey(*aKey) {}
|
|
|
|
nsAttrHashKey(const nsAttrHashKey& aCopy) : mKey(aCopy.mKey) {}
|
|
|
|
~nsAttrHashKey() {}
|
|
|
|
|
|
|
|
KeyType GetKey() const { return mKey; }
|
2011-09-29 06:19:26 +00:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const
|
2005-06-01 13:46:20 +00:00
|
|
|
{
|
|
|
|
return mKey.mLocalName == aKey->mLocalName &&
|
|
|
|
mKey.mNamespaceID == aKey->mNamespaceID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
if (!aKey)
|
|
|
|
return 0;
|
|
|
|
|
2012-03-12 22:53:18 +00:00
|
|
|
return mozilla::HashGeneric(aKey->mNamespaceID, aKey->mLocalName);
|
2005-06-01 13:46:20 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-06-01 13:46:20 +00:00
|
|
|
|
|
|
|
private:
|
2005-06-09 20:17:40 +00:00
|
|
|
nsAttrKey mKey;
|
2005-06-01 13:46:20 +00:00
|
|
|
};
|
1999-01-12 08:45:23 +00:00
|
|
|
|
2013-03-10 08:00:33 +00:00
|
|
|
// Helper class that implements the nsIDOMMozNamedAttrMap interface.
|
|
|
|
class nsDOMAttributeMap : public nsIDOMMozNamedAttrMap
|
1999-01-12 08:45:23 +00:00
|
|
|
{
|
|
|
|
public:
|
2010-07-30 13:42:11 +00:00
|
|
|
typedef mozilla::dom::Element Element;
|
|
|
|
|
|
|
|
nsDOMAttributeMap(Element *aContent);
|
1999-02-12 17:45:58 +00:00
|
|
|
virtual ~nsDOMAttributeMap();
|
1999-01-12 08:45:23 +00:00
|
|
|
|
2007-01-04 22:31:26 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
1999-01-12 08:45:23 +00:00
|
|
|
|
2013-03-10 08:00:33 +00:00
|
|
|
// nsIDOMMozNamedAttrMap interface
|
|
|
|
NS_DECL_NSIDOMMOZNAMEDATTRMAP
|
1999-01-12 08:45:23 +00:00
|
|
|
|
|
|
|
void DropReference();
|
|
|
|
|
2010-07-30 13:42:11 +00:00
|
|
|
Element* GetContent()
|
2005-06-01 13:46:20 +00:00
|
|
|
{
|
|
|
|
return mContent;
|
|
|
|
}
|
|
|
|
|
2005-12-29 03:01:58 +00:00
|
|
|
/**
|
|
|
|
* Called when mContent is moved into a new document.
|
|
|
|
* Updates the nodeinfos of all owned nodes.
|
|
|
|
*/
|
|
|
|
nsresult SetOwnerDocument(nsIDocument* aDocument);
|
|
|
|
|
2005-06-01 13:46:20 +00:00
|
|
|
/**
|
|
|
|
* Drop an attribute from the map's cache (does not remove the attribute
|
|
|
|
* from the node!)
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
void DropAttribute(int32_t aNamespaceID, nsIAtom* aLocalName);
|
2005-06-01 13:46:20 +00:00
|
|
|
|
2006-07-29 16:02:11 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of attribute nodes currently in the map.
|
|
|
|
* Note: this is just the number of cached attribute nodes, not the number of
|
|
|
|
* attributes in mContent.
|
|
|
|
*
|
|
|
|
* @return The number of attribute nodes in the map.
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t Count() const;
|
2006-07-29 16:02:11 +00:00
|
|
|
|
2010-04-19 15:41:39 +00:00
|
|
|
typedef nsRefPtrHashtable<nsAttrHashKey, nsDOMAttribute> AttrCache;
|
2006-07-29 16:02:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enumerates over the attribute nodess in the map and calls aFunc for each
|
|
|
|
* one. If aFunc returns PL_DHASH_STOP we'll stop enumerating at that point.
|
|
|
|
*
|
|
|
|
* @return The number of attribute nodes that aFunc was called for.
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t Enumerate(AttrCache::EnumReadFunction aFunc, void *aUserArg) const;
|
2006-07-29 16:02:11 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDOMAttribute* GetItemAt(uint32_t aIndex, nsresult *rv);
|
2012-10-16 11:51:00 +00:00
|
|
|
nsDOMAttribute* GetNamedItem(const nsAString& aAttrName);
|
2008-10-22 14:31:14 +00:00
|
|
|
|
|
|
|
static nsDOMAttributeMap* FromSupports(nsISupports* aSupports)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2013-03-10 08:00:33 +00:00
|
|
|
nsCOMPtr<nsIDOMMozNamedAttrMap> map_qi = do_QueryInterface(aSupports);
|
2008-10-22 14:31:14 +00:00
|
|
|
|
|
|
|
// If this assertion fires the QI implementation for the object in
|
2013-03-10 08:00:33 +00:00
|
|
|
// question doesn't use the nsIDOMMozNamedAttrMap pointer as the nsISupports
|
2008-10-22 14:31:14 +00:00
|
|
|
// pointer. That must be fixed, or we'll crash...
|
2013-03-10 08:00:33 +00:00
|
|
|
NS_ASSERTION(map_qi == static_cast<nsIDOMMozNamedAttrMap*>(aSupports),
|
2008-10-22 14:31:14 +00:00
|
|
|
"Uh, fix QI!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return static_cast<nsDOMAttributeMap*>(aSupports);
|
|
|
|
}
|
|
|
|
|
2007-01-04 22:31:26 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(nsDOMAttributeMap)
|
|
|
|
|
2012-10-16 11:51:00 +00:00
|
|
|
nsDOMAttribute* GetNamedItemNS(const nsAString& aNamespaceURI,
|
|
|
|
const nsAString& aLocalName,
|
|
|
|
mozilla::ErrorResult& aError);
|
|
|
|
|
2013-03-10 08:00:33 +00:00
|
|
|
already_AddRefed<nsDOMAttribute> SetNamedItemNS(nsIDOMAttr *aNode,
|
2012-10-16 11:51:00 +00:00
|
|
|
mozilla::ErrorResult& aError)
|
|
|
|
{
|
|
|
|
return SetNamedItemInternal(aNode, true, aError);
|
|
|
|
}
|
|
|
|
|
2012-09-30 16:43:47 +00:00
|
|
|
size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const;
|
|
|
|
|
1999-01-12 08:45:23 +00:00
|
|
|
private:
|
2010-07-30 13:42:11 +00:00
|
|
|
Element *mContent; // Weak reference
|
2005-06-01 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache of nsDOMAttributes.
|
|
|
|
*/
|
2006-07-29 16:02:11 +00:00
|
|
|
AttrCache mAttributeCache;
|
2005-06-01 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
2011-10-17 14:59:28 +00:00
|
|
|
* SetNamedItem() (aWithNS = false) and SetNamedItemNS() (aWithNS =
|
|
|
|
* true) implementation.
|
2005-06-01 13:46:20 +00:00
|
|
|
*/
|
2012-10-16 11:51:00 +00:00
|
|
|
already_AddRefed<nsDOMAttribute>
|
2013-03-10 08:00:33 +00:00
|
|
|
SetNamedItemInternal(nsIDOMAttr *aNode,
|
2012-10-16 11:51:00 +00:00
|
|
|
bool aWithNS,
|
|
|
|
mozilla::ErrorResult& aError);
|
2005-06-01 13:46:20 +00:00
|
|
|
|
2012-10-16 11:51:00 +00:00
|
|
|
already_AddRefed<nsINodeInfo>
|
|
|
|
GetAttrNodeInfo(const nsAString& aNamespaceURI,
|
|
|
|
const nsAString& aLocalName,
|
|
|
|
mozilla::ErrorResult& aError);
|
2005-06-01 13:46:20 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
nsDOMAttribute* GetAttribute(nsINodeInfo* aNodeInfo, bool aNsAware);
|
2008-10-22 14:31:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an attribute, returns the removed node.
|
|
|
|
*/
|
2012-10-16 11:51:00 +00:00
|
|
|
already_AddRefed<nsDOMAttribute> RemoveAttribute(nsINodeInfo* aNodeInfo);
|
1999-01-12 08:45:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-10 08:00:33 +00:00
|
|
|
#endif /* nsDOMAttributeMap_h */
|