2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2013-03-10 00:00:33 -08:00
|
|
|
* Implementation of the |attributes| property of DOM Core's Element object.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
|
2013-03-10 00:00:33 -08:00
|
|
|
#ifndef nsDOMAttributeMap_h
|
|
|
|
#define nsDOMAttributeMap_h
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-08-26 22:46:24 -07:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2013-04-25 23:48:23 -07:00
|
|
|
#include "mozilla/dom/Attr.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-03-10 00:00:33 -08:00
|
|
|
#include "nsIDOMMozNamedAttrMap.h"
|
2010-04-19 08:41:39 -07:00
|
|
|
#include "nsRefPtrHashtable.h"
|
2013-09-23 10:25:00 -07:00
|
|
|
#include "nsString.h"
|
2013-04-25 23:48:27 -07:00
|
|
|
#include "nsWrapperCache.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsIAtom;
|
|
|
|
class nsIDocument;
|
|
|
|
|
|
|
|
/**
|
2013-04-09 08:29:44 -07:00
|
|
|
* Structure used as a key for caching Attrs in nsDOMAttributeMap's mAttributeCache.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
class nsAttrKey
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The namespace of the attribute
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mNamespaceID;
|
2007-03-22 10:30:00 -07: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 08:56:38 -07:00
|
|
|
nsAttrKey(int32_t aNs, nsIAtom* aName)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mNamespaceID(aNs), mLocalName(aName) {}
|
|
|
|
|
|
|
|
nsAttrKey(const nsAttrKey& aAttr)
|
|
|
|
: mNamespaceID(aAttr.mNamespaceID), mLocalName(aAttr.mLocalName) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PLDHashEntryHdr implementation for nsAttrKey.
|
|
|
|
*/
|
|
|
|
class nsAttrHashKey : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const nsAttrKey& KeyType;
|
|
|
|
typedef const nsAttrKey* KeyTypePointer;
|
|
|
|
|
2014-08-05 06:19:51 -07:00
|
|
|
explicit nsAttrHashKey(KeyTypePointer aKey) : mKey(*aKey) {}
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAttrHashKey(const nsAttrHashKey& aCopy) : mKey(aCopy.mKey) {}
|
|
|
|
~nsAttrHashKey() {}
|
|
|
|
|
|
|
|
KeyType GetKey() const { return mKey; }
|
2011-09-28 23:19:26 -07:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const
|
2007-03-22 10:30:00 -07: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 15:53:18 -07:00
|
|
|
return mozilla::HashGeneric(aKey->mNamespaceID, aKey->mLocalName);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsAttrKey mKey;
|
|
|
|
};
|
|
|
|
|
2013-03-10 00:00:33 -08:00
|
|
|
// Helper class that implements the nsIDOMMozNamedAttrMap interface.
|
|
|
|
class nsDOMAttributeMap : public nsIDOMMozNamedAttrMap
|
2013-04-25 23:48:27 -07:00
|
|
|
, public nsWrapperCache
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-04-25 23:48:19 -07:00
|
|
|
typedef mozilla::dom::Attr Attr;
|
2010-07-30 06:42:11 -07:00
|
|
|
typedef mozilla::dom::Element Element;
|
2013-04-25 23:48:19 -07:00
|
|
|
typedef mozilla::ErrorResult ErrorResult;
|
2010-07-30 06:42:11 -07:00
|
|
|
|
2014-08-05 06:19:51 -07:00
|
|
|
explicit nsDOMAttributeMap(Element *aContent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2013-05-06 16:37:47 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsDOMAttributeMap)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-10 00:00:33 -08:00
|
|
|
// nsIDOMMozNamedAttrMap interface
|
|
|
|
NS_DECL_NSIDOMMOZNAMEDATTRMAP
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void DropReference();
|
|
|
|
|
2010-07-30 06:42:11 -07:00
|
|
|
Element* GetContent()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when mContent is moved into a new document.
|
|
|
|
* Updates the nodeinfos of all owned nodes.
|
|
|
|
*/
|
|
|
|
nsresult SetOwnerDocument(nsIDocument* aDocument);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drop an attribute from the map's cache (does not remove the attribute
|
|
|
|
* from the node!)
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
void DropAttribute(int32_t aNamespaceID, nsIAtom* aLocalName);
|
2007-03-22 10:30:00 -07: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 08:56:38 -07:00
|
|
|
uint32_t Count() const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-25 23:48:19 -07:00
|
|
|
typedef nsRefPtrHashtable<nsAttrHashKey, Attr> AttrCache;
|
2007-03-22 10:30:00 -07: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 08:56:38 -07:00
|
|
|
uint32_t Enumerate(AttrCache::EnumReadFunction aFunc, void *aUserArg) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-25 23:48:27 -07:00
|
|
|
Element* GetParentObject() const
|
2013-04-25 23:48:23 -07:00
|
|
|
{
|
2013-04-25 23:48:27 -07:00
|
|
|
return mContent;
|
2008-10-22 07:31:14 -07:00
|
|
|
}
|
2014-04-08 15:27:18 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-25 23:48:23 -07:00
|
|
|
// WebIDL
|
|
|
|
Attr* GetNamedItem(const nsAString& aAttrName);
|
|
|
|
Attr* NamedGetter(const nsAString& aAttrName, bool& aFound);
|
2014-04-15 19:58:44 -07:00
|
|
|
bool NameIsEnumerable(const nsAString& aName);
|
2013-04-25 23:48:23 -07: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-25 23:48:19 -07:00
|
|
|
Attr*
|
|
|
|
GetNamedItemNS(const nsAString& aNamespaceURI,
|
|
|
|
const nsAString& aLocalName);
|
|
|
|
already_AddRefed<Attr>
|
|
|
|
SetNamedItemNS(Attr& aNode, ErrorResult& aError)
|
2012-10-16 04:51:00 -07:00
|
|
|
{
|
|
|
|
return SetNamedItemInternal(aNode, true, aError);
|
|
|
|
}
|
2013-04-25 23:48:23 -07:00
|
|
|
already_AddRefed<Attr>
|
|
|
|
RemoveNamedItemNS(const nsAString& aNamespaceURI, const nsAString& aLocalName,
|
|
|
|
ErrorResult& aError);
|
|
|
|
|
2014-04-15 19:58:44 -07:00
|
|
|
void GetSupportedNames(unsigned, nsTArray<nsString>& aNames)
|
2013-04-25 23:48:23 -07:00
|
|
|
{
|
|
|
|
// No supported names we want to show up in iteration.
|
|
|
|
}
|
2012-10-16 04:51:00 -07:00
|
|
|
|
2013-06-23 05:03:39 -07:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2012-09-30 09:43:47 -07:00
|
|
|
|
2014-06-24 19:09:15 -07:00
|
|
|
protected:
|
|
|
|
virtual ~nsDOMAttributeMap();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
2013-05-06 16:37:47 -07:00
|
|
|
nsCOMPtr<Element> mContent;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
2014-08-26 22:46:24 -07:00
|
|
|
* Cache of Attrs. It's usually empty, and thus initialized lazily.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2014-08-26 22:46:24 -07:00
|
|
|
mozilla::UniquePtr<AttrCache> mAttributeCache;
|
|
|
|
|
|
|
|
void EnsureAttributeCache();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
2011-10-17 07:59:28 -07:00
|
|
|
* SetNamedItem() (aWithNS = false) and SetNamedItemNS() (aWithNS =
|
|
|
|
* true) implementation.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2013-04-25 23:48:19 -07:00
|
|
|
already_AddRefed<Attr>
|
2013-04-25 23:48:23 -07:00
|
|
|
SetNamedItemInternal(Attr& aNode, bool aWithNS, ErrorResult& aError);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-19 19:01:40 -07:00
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>
|
2012-10-16 04:51:00 -07:00
|
|
|
GetAttrNodeInfo(const nsAString& aNamespaceURI,
|
2013-04-04 05:01:11 -07:00
|
|
|
const nsAString& aLocalName);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-19 19:01:40 -07:00
|
|
|
Attr* GetAttribute(mozilla::dom::NodeInfo* aNodeInfo, bool aNsAware);
|
2008-10-22 07:31:14 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an attribute, returns the removed node.
|
|
|
|
*/
|
2014-06-19 19:01:40 -07:00
|
|
|
already_AddRefed<Attr> RemoveAttribute(mozilla::dom::NodeInfo* aNodeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-08-22 22:17:11 -07: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"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-10 00:00:33 -08:00
|
|
|
#endif /* nsDOMAttributeMap_h */
|