Back to index

lightning-sunbird  0.9+nobinonly
nsDOMAttributeMap.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Allan Beaufour <allan@beaufour.dk>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #ifndef nsDOMAttributeMap_h___
00039 #define nsDOMAttributeMap_h___
00040 
00041 #include "nsIAtom.h"
00042 #include "nsIDOMNamedNodeMap.h"
00043 #include "nsVoidArray.h"
00044 #include "nsString.h"
00045 #include "plhash.h"
00046 #include "nsInterfaceHashtable.h"
00047 
00048 class nsIContent;
00049 class nsDOMAttribute;
00050 class nsINodeInfo;
00051 
00055 class nsAttrKey
00056 {
00057 public:
00061   PRInt32  mNamespaceID;
00062 
00067   nsIAtom* mLocalName;
00068 
00069   nsAttrKey(PRInt32 aNs, nsIAtom* aName)
00070     : mNamespaceID(aNs), mLocalName(aName) {}
00071 
00072   nsAttrKey(const nsAttrKey& aAttr)
00073     : mNamespaceID(aAttr.mNamespaceID), mLocalName(aAttr.mLocalName) {}
00074 };
00075 
00079 class nsAttrHashKey : public PLDHashEntryHdr
00080 {
00081 public:
00082   typedef const nsAttrKey& KeyType;
00083   typedef const nsAttrKey* KeyTypePointer;
00084 
00085   nsAttrHashKey(KeyTypePointer aKey) : mKey(*aKey) {}
00086   nsAttrHashKey(const nsAttrHashKey& aCopy) : mKey(aCopy.mKey) {}
00087   ~nsAttrHashKey() {}
00088 
00089   KeyType GetKey() const { return mKey; }
00090   KeyTypePointer GetKeyPointer() const { return &mKey; }
00091   PRBool KeyEquals(KeyTypePointer aKey) const
00092     {
00093       return mKey.mLocalName == aKey->mLocalName &&
00094              mKey.mNamespaceID == aKey->mNamespaceID;
00095     }
00096 
00097   static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
00098   static PLDHashNumber HashKey(KeyTypePointer aKey)
00099     {
00100       if (!aKey)
00101         return 0;
00102 
00103       return (aKey->mNamespaceID >> 28) ^
00104              (aKey->mNamespaceID << 4) ^
00105              NS_PTR_TO_INT32(aKey->mLocalName);
00106     }
00107   enum { ALLOW_MEMMOVE = PR_TRUE };
00108 
00109 private:
00110   nsAttrKey mKey;
00111 };
00112 
00113 // Helper class that implements the nsIDOMNamedNodeMap interface.
00114 class nsDOMAttributeMap : public nsIDOMNamedNodeMap
00115 {
00116 public:
00117   nsDOMAttributeMap(nsIContent* aContent);
00118   virtual ~nsDOMAttributeMap();
00119 
00123   PRBool Init();
00124 
00125   NS_DECL_ISUPPORTS
00126 
00127   // nsIDOMNamedNodeMap interface
00128   NS_DECL_NSIDOMNAMEDNODEMAP
00129 
00130   void DropReference();
00131 
00132   nsIContent* GetContent()
00133   {
00134     return mContent;
00135   }
00136 
00141   void DropAttribute(PRInt32 aNamespaceID, nsIAtom* aLocalName);
00142 
00143 private:
00144   nsIContent* mContent; // Weak reference
00145 
00149   nsInterfaceHashtable<nsAttrHashKey, nsIDOMNode> mAttributeCache;
00150 
00155   nsresult SetNamedItemInternal(nsIDOMNode *aNode,
00156                                 nsIDOMNode **aReturn,
00157                                 PRBool aWithNS);
00158 
00163   nsresult GetNamedItemNSInternal(const nsAString& aNamespaceURI,
00164                                   const nsAString& aLocalName,
00165                                   nsIDOMNode** aReturn,
00166                                   PRBool aRemove = PR_FALSE);
00167 
00172   nsresult GetAttribute(nsINodeInfo*     aNodeInfo,
00173                         nsIDOMNode**     aReturn,
00174                         PRBool aRemove = PR_FALSE);
00175 };
00176 
00177 
00178 #endif /* nsDOMAttributeMap_h___ */