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-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-08-27 05:46:24 +00:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2013-04-26 06:48:23 +00:00
|
|
|
#include "mozilla/dom/Attr.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-03-10 08:00:33 +00:00
|
|
|
#include "nsIDOMMozNamedAttrMap.h"
|
2010-04-19 15:41:39 +00:00
|
|
|
#include "nsRefPtrHashtable.h"
|
2013-09-23 17:25:00 +00:00
|
|
|
#include "nsString.h"
|
2013-04-26 06:48:27 +00:00
|
|
|
#include "nsWrapperCache.h"
|
1999-01-12 08:45:23 +00:00
|
|
|
|
2006-09-05 10:22:54 +00:00
|
|
|
class nsIAtom;
|
2005-12-29 03:01:58 +00:00
|
|
|
class nsIDocument;
|
2005-06-01 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-09 15:29:44 +00:00
|
|
|
* Structure used as a key for caching Attrs 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;
|
|
|
|
|
2014-08-05 13:19:51 +00:00
|
|
|
explicit nsAttrHashKey(KeyTypePointer aKey) : mKey(*aKey) {}
|
2005-06-01 13:46:20 +00:00
|
|
|
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
|
2013-04-26 06:48:27 +00:00
|
|
|
, public nsWrapperCache
|
1999-01-12 08:45:23 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-04-26 06:48:19 +00:00
|
|
|
typedef mozilla::dom::Attr Attr;
|
2010-07-30 13:42:11 +00:00
|
|
|
typedef mozilla::dom::Element Element;
|
2013-04-26 06:48:19 +00:00
|
|
|
typedef mozilla::ErrorResult ErrorResult;
|
2010-07-30 13:42:11 +00:00
|
|
|
|
2014-08-05 13:19:51 +00:00
|
|
|
explicit nsDOMAttributeMap(Element *aContent);
|
1999-01-12 08:45:23 +00:00
|
|
|
|
2007-01-04 22:31:26 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2013-05-06 23:37:47 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsDOMAttributeMap)
|
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
|
|
|
|
2013-04-26 06:48:19 +00:00
|
|
|
typedef nsRefPtrHashtable<nsAttrHashKey, Attr> 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
|
|
|
|
2013-04-26 06:48:27 +00:00
|
|
|
Element* GetParentObject() const
|
2013-04-26 06:48:23 +00:00
|
|
|
{
|
2013-04-26 06:48:27 +00:00
|
|
|
return mContent;
|
2008-10-22 14:31:14 +00:00
|
|
|
}
|
2014-04-08 22:27:18 +00:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2007-01-04 22:31:26 +00:00
|
|
|
|
2013-04-26 06:48:23 +00:00
|
|
|
// WebIDL
|
|
|
|
Attr* GetNamedItem(const nsAString& aAttrName);
|
|
|
|
Attr* NamedGetter(const nsAString& aAttrName, bool& aFound);
|
2014-04-16 02:58:44 +00:00
|
|
|
bool NameIsEnumerable(const nsAString& aName);
|
2013-04-26 06:48:23 +00:00
|
|
|
already_AddRefed<Attr>
|
|
|
|
SetNamedItem(Attr& aAttr, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
return SetNamedItemInternal(aAttr, false, aError);
|
|
|
|
}
|
|
|
|
already_AddRefed<Attr>
|
|
|
|
RemoveNamedItem(const nsAString& aName, ErrorResult& aError);
|
|
|
|
|
|
|
|
Attr* Item(uint32_t aIndex);
|
|
|
|
Attr* IndexedGetter(uint32_t aIndex, bool& aFound);
|
|
|
|
uint32_t Length() const;
|
|
|
|
|
2013-04-26 06:48:19 +00:00
|
|
|
Attr*
|
|
|
|
GetNamedItemNS(const nsAString& aNamespaceURI,
|
|
|
|
const nsAString& aLocalName);
|
|
|
|
already_AddRefed<Attr>
|
|
|
|
SetNamedItemNS(Attr& aNode, ErrorResult& aError)
|
2012-10-16 11:51:00 +00:00
|
|
|
{
|
|
|
|
return SetNamedItemInternal(aNode, true, aError);
|
|
|
|
}
|
2013-04-26 06:48:23 +00:00
|
|
|
already_AddRefed<Attr>
|
|
|
|
RemoveNamedItemNS(const nsAString& aNamespaceURI, const nsAString& aLocalName,
|
|
|
|
ErrorResult& aError);
|
|
|
|
|
2014-04-16 02:58:44 +00:00
|
|
|
void GetSupportedNames(unsigned, nsTArray<nsString>& aNames)
|
2013-04-26 06:48:23 +00:00
|
|
|
{
|
|
|
|
// No supported names we want to show up in iteration.
|
|
|
|
}
|
2012-10-16 11:51:00 +00:00
|
|
|
|
2013-06-23 12:03:39 +00:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2012-09-30 16:43:47 +00:00
|
|
|
|
2014-06-25 02:09:15 +00:00
|
|
|
protected:
|
|
|
|
virtual ~nsDOMAttributeMap();
|
|
|
|
|
1999-01-12 08:45:23 +00:00
|
|
|
private:
|
2013-05-06 23:37:47 +00:00
|
|
|
nsCOMPtr<Element> mContent;
|
2005-06-01 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
2014-08-27 05:46:24 +00:00
|
|
|
* Cache of Attrs. It's usually empty, and thus initialized lazily.
|
2005-06-01 13:46:20 +00:00
|
|
|
*/
|
2014-08-27 05:46:24 +00:00
|
|
|
mozilla::UniquePtr<AttrCache> mAttributeCache;
|
|
|
|
|
|
|
|
void EnsureAttributeCache();
|
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
|
|
|
*/
|
2013-04-26 06:48:19 +00:00
|
|
|
already_AddRefed<Attr>
|
2013-04-26 06:48:23 +00:00
|
|
|
SetNamedItemInternal(Attr& aNode, bool aWithNS, ErrorResult& aError);
|
2005-06-01 13:46:20 +00:00
|
|
|
|
2014-06-20 02:01:40 +00:00
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>
|
2012-10-16 11:51:00 +00:00
|
|
|
GetAttrNodeInfo(const nsAString& aNamespaceURI,
|
2013-04-04 12:01:11 +00:00
|
|
|
const nsAString& aLocalName);
|
2005-06-01 13:46:20 +00:00
|
|
|
|
2014-06-20 02:01:40 +00:00
|
|
|
Attr* GetAttribute(mozilla::dom::NodeInfo* aNodeInfo, bool aNsAware);
|
2008-10-22 14:31:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an attribute, returns the removed node.
|
|
|
|
*/
|
2014-06-20 02:01:40 +00:00
|
|
|
already_AddRefed<Attr> RemoveAttribute(mozilla::dom::NodeInfo* aNodeInfo);
|
1999-01-12 08:45:23 +00:00
|
|
|
};
|
|
|
|
|
2013-08-23 05:17:11 +00:00
|
|
|
// XXX khuey yes this is crazy. The bindings code needs to see this include,
|
|
|
|
// but if we pull it in at the top of the file we get a circular include
|
|
|
|
// problem.
|
|
|
|
#include "mozilla/dom/Element.h"
|
1999-01-12 08:45:23 +00:00
|
|
|
|
2013-03-10 08:00:33 +00:00
|
|
|
#endif /* nsDOMAttributeMap_h */
|