Back to index

lightning-sunbird  0.9+nobinonly
nsDOMAttributeMap.cpp
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 
00039 #include "nsDOMAttributeMap.h"
00040 #include "nsDOMAttribute.h"
00041 #include "nsGenericElement.h"
00042 #include "nsIContent.h"
00043 #include "nsINameSpaceManager.h"
00044 #include "nsDOMError.h"
00045 #include "nsContentUtils.h"
00046 #include "nsNodeInfoManager.h"
00047 
00048 //----------------------------------------------------------------------
00049 
00050 nsDOMAttributeMap::nsDOMAttributeMap(nsIContent* aContent)
00051   : mContent(aContent)
00052 {
00053   // We don't add a reference to our content. If it goes away,
00054   // we'll be told to drop our reference
00055 }
00056 
00057 PRBool
00058 nsDOMAttributeMap::Init()
00059 {
00060   return mAttributeCache.Init();
00061 }
00062 
00066 PLDHashOperator
00067 RemoveMapRef(nsAttrHashKey::KeyType aKey, nsCOMPtr<nsIDOMNode>& aData, void* aUserArg)
00068 {
00069   nsCOMPtr<nsIAttribute> attr(do_QueryInterface(aData));
00070   NS_ASSERTION(attr, "non-nsIAttribute somehow made it into the hashmap?!");
00071   attr->SetMap(nsnull);
00072 
00073   return PL_DHASH_REMOVE;
00074 }
00075 
00076 nsDOMAttributeMap::~nsDOMAttributeMap()
00077 {
00078   mAttributeCache.Enumerate(RemoveMapRef, nsnull);
00079 }
00080 
00081 void
00082 nsDOMAttributeMap::DropReference()
00083 {
00084   mAttributeCache.Enumerate(RemoveMapRef, nsnull);
00085   mContent = nsnull;
00086 }
00087 
00088 
00089 // QueryInterface implementation for nsDOMAttributeMap
00090 NS_INTERFACE_MAP_BEGIN(nsDOMAttributeMap)
00091   NS_INTERFACE_MAP_ENTRY(nsISupports)
00092   NS_INTERFACE_MAP_ENTRY(nsIDOMNamedNodeMap)
00093   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(NamedNodeMap)
00094 NS_INTERFACE_MAP_END
00095 
00096 NS_IMPL_ADDREF(nsDOMAttributeMap)
00097 NS_IMPL_RELEASE(nsDOMAttributeMap)
00098 
00099 
00100 void
00101 nsDOMAttributeMap::DropAttribute(PRInt32 aNamespaceID, nsIAtom* aLocalName)
00102 {
00103   nsAttrKey attr(aNamespaceID, aLocalName);
00104   nsIDOMNode *node = mAttributeCache.GetWeak(attr);
00105   if (node) {
00106     nsCOMPtr<nsIAttribute> iAttr(do_QueryInterface(node));
00107     NS_ASSERTION(iAttr, "non-nsIAttribute somehow made it into the hashmap?!");
00108 
00109     // Break link to map
00110     iAttr->SetMap(nsnull);
00111 
00112     // Remove from cache
00113     mAttributeCache.Remove(attr);
00114   }
00115 }
00116 
00117 nsresult
00118 nsDOMAttributeMap::GetAttribute(nsINodeInfo* aNodeInfo,
00119                                 nsIDOMNode** aReturn,
00120                                 PRBool aRemove)
00121 {
00122   NS_ASSERTION(aNodeInfo, "GetAttribute() called with aNodeInfo == nsnull!");
00123   NS_ASSERTION(aReturn, "GetAttribute() called with aReturn == nsnull");
00124 
00125   *aReturn = nsnull;
00126 
00127   nsAttrKey attr(aNodeInfo->NamespaceID(), aNodeInfo->NameAtom());
00128 
00129   if (!mAttributeCache.Get(attr, aReturn)) {
00130     nsAutoString value;
00131     if (aRemove) {
00132       // As we are removing the attribute we need to set the current value in
00133       // the attribute node.
00134       mContent->GetAttr(aNodeInfo->NamespaceID(), aNodeInfo->NameAtom(), value);
00135     }
00136     nsCOMPtr<nsIDOMNode> newAttr = new nsDOMAttribute(aRemove ? nsnull : this,
00137                                                       aNodeInfo, value);
00138     if (!newAttr) {
00139       return NS_ERROR_OUT_OF_MEMORY;
00140     }
00141     if (!aRemove && !mAttributeCache.Put(attr, newAttr)) {
00142       return NS_ERROR_OUT_OF_MEMORY;
00143     }
00144     newAttr.swap(*aReturn);
00145   }
00146   else if (aRemove) {
00147     nsCOMPtr<nsIAttribute> iAttr(do_QueryInterface(*aReturn));
00148     NS_ASSERTION(iAttr, "non-nsIAttribute somehow made it into the hashmap?!");
00149 
00150     // Break link to map
00151     iAttr->SetMap(nsnull);
00152 
00153     // Remove from cache
00154     mAttributeCache.Remove(attr);
00155   }
00156 
00157   return NS_OK;
00158 }
00159 
00160 NS_IMETHODIMP
00161 nsDOMAttributeMap::GetNamedItem(const nsAString& aAttrName,
00162                                 nsIDOMNode** aAttribute)
00163 {
00164   NS_ENSURE_ARG_POINTER(aAttribute);
00165   *aAttribute = nsnull;
00166 
00167   nsresult rv = NS_OK;
00168   if (mContent) {
00169     nsCOMPtr<nsINodeInfo> ni =
00170       mContent->GetExistingAttrNameFromQName(aAttrName);
00171     if (ni) {
00172       rv = GetAttribute(ni, aAttribute);
00173     }
00174   }
00175 
00176   return rv;
00177 }
00178 
00179 NS_IMETHODIMP
00180 nsDOMAttributeMap::SetNamedItem(nsIDOMNode *aNode, nsIDOMNode **aReturn)
00181 {
00182   return SetNamedItemInternal(aNode, aReturn, PR_FALSE);
00183 }
00184 
00185 NS_IMETHODIMP
00186 nsDOMAttributeMap::SetNamedItemNS(nsIDOMNode *aNode, nsIDOMNode **aReturn)
00187 {
00188   return SetNamedItemInternal(aNode, aReturn, PR_TRUE);
00189 }
00190 
00191 nsresult
00192 nsDOMAttributeMap::SetNamedItemInternal(nsIDOMNode *aNode,
00193                                         nsIDOMNode **aReturn,
00194                                         PRBool aWithNS)
00195 {
00196   NS_ENSURE_ARG_POINTER(aNode);
00197   NS_ENSURE_ARG_POINTER(aReturn);
00198 
00199   nsresult rv = NS_OK;
00200   *aReturn = nsnull;
00201   nsCOMPtr<nsIDOMNode> tmpReturn;
00202 
00203   if (mContent) {
00204     // XXX should check same-origin between mContent and aNode however
00205     // nsContentUtils::CheckSameOrigin can't deal with attributenodes yet
00206     
00207     nsCOMPtr<nsIDOMAttr> attribute(do_QueryInterface(aNode));
00208     nsCOMPtr<nsIAttribute> iAttribute(do_QueryInterface(aNode));
00209     if (!attribute || !iAttribute) {
00210       return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
00211     }
00212 
00213     // Check that attribute is not owned by somebody else
00214     nsCOMPtr<nsIDOMElement> owner;
00215     attribute->GetOwnerElement(getter_AddRefs(owner));
00216     if (owner) {
00217       nsCOMPtr<nsISupports> ownerSupports = do_QueryInterface(owner);
00218       nsCOMPtr<nsISupports> thisSupports = do_QueryInterface(mContent);
00219       if (ownerSupports != thisSupports) {
00220         return NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR;
00221       }
00222     }
00223 
00224     nsCOMPtr<nsINodeInfo> ni;
00225     nsAutoString name;
00226     attribute->GetName(name);
00227 
00228     // SetNamedItemNS()
00229     if (aWithNS) {
00230       // Return existing attribute, if present
00231       nsAutoString nsURI;
00232       attribute->GetNamespaceURI(nsURI);
00233 
00234       nsINodeInfo *contentNi = mContent->GetNodeInfo();
00235       NS_ENSURE_TRUE(contentNi, NS_ERROR_FAILURE);
00236 
00237       contentNi->NodeInfoManager()->GetNodeInfo(name, nsURI,
00238                                                 getter_AddRefs(ni));
00239       NS_ENSURE_TRUE(ni, NS_ERROR_FAILURE);
00240 
00241       if (mContent->HasAttr(ni->NamespaceID(), ni->NameAtom())) {
00242         rv = GetAttribute(ni, getter_AddRefs(tmpReturn), PR_TRUE);
00243         NS_ENSURE_SUCCESS(rv, rv);
00244       }
00245 
00246     }
00247     else { // SetNamedItem()
00248       // get node-info of old attribute
00249       ni = mContent->GetExistingAttrNameFromQName(name);
00250       if (ni) {
00251         rv = GetAttribute(ni, getter_AddRefs(tmpReturn), PR_TRUE);
00252         NS_ENSURE_SUCCESS(rv, rv);
00253       }
00254       else {
00255         nsINodeInfo *contentNi = mContent->GetNodeInfo();
00256         NS_ENSURE_TRUE(contentNi, NS_ERROR_FAILURE);
00257 
00258         rv = contentNi->NodeInfoManager()->GetNodeInfo(name, nsnull,
00259                                                        kNameSpaceID_None,
00260                                                        getter_AddRefs(ni));
00261         NS_ENSURE_SUCCESS(rv, rv);
00262         // value is already empty
00263       }
00264     }
00265     
00266     // Set the new attribute value
00267     nsAutoString value;
00268     attribute->GetValue(value);
00269     if (!aWithNS && ni->NamespaceID() == kNameSpaceID_None &&
00270         mContent->IsContentOfType(nsIContent::eHTML)) {
00271       // Set via setAttribute(), which may do normalization on the
00272       // attribute name for HTML
00273       nsCOMPtr<nsIDOMElement> ourElement(do_QueryInterface(mContent));
00274       NS_ASSERTION(ourElement, "HTML content that's not an element?");
00275       ourElement->SetAttribute(name, value);
00276     }
00277     else {
00278       // It's OK to just use SetAttr
00279       rv = mContent->SetAttr(ni->NamespaceID(), ni->NameAtom(),
00280                              ni->GetPrefixAtom(), value, PR_TRUE);
00281     }
00282     
00283     if (NS_SUCCEEDED(rv)) {
00284       nsAttrKey attrkey(ni->NamespaceID(), ni->NameAtom());
00285       rv = mAttributeCache.Put(attrkey, attribute);
00286       NS_ENSURE_SUCCESS(rv, rv);
00287 
00288       iAttribute->SetMap(this);
00289     }
00290   }
00291 
00292   tmpReturn.swap(*aReturn); // transfers ref.
00293 
00294   return rv;
00295 }
00296 
00297 NS_IMETHODIMP
00298 nsDOMAttributeMap::RemoveNamedItem(const nsAString& aName,
00299                                    nsIDOMNode** aReturn)
00300 {
00301   NS_ENSURE_ARG_POINTER(aReturn);
00302   *aReturn = nsnull;
00303 
00304   nsresult rv = NS_OK;
00305 
00306   if (mContent) {
00307     nsCOMPtr<nsINodeInfo> ni = mContent->GetExistingAttrNameFromQName(aName);
00308     if (!ni) {
00309       return NS_ERROR_DOM_NOT_FOUND_ERR;
00310     }
00311 
00312     rv = GetAttribute(ni, aReturn, PR_TRUE);
00313     NS_ENSURE_SUCCESS(rv, rv);
00314 
00315     rv = mContent->UnsetAttr(ni->NamespaceID(), ni->NameAtom(), PR_TRUE);
00316   }
00317 
00318   return rv;
00319 }
00320 
00321 
00322 NS_IMETHODIMP
00323 nsDOMAttributeMap::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
00324 {
00325   NS_ENSURE_ARG_POINTER(aReturn);
00326   PRInt32 nameSpaceID;
00327   nsCOMPtr<nsIAtom> nameAtom, prefix;
00328 
00329   nsresult rv = NS_OK;
00330   if (mContent &&
00331       NS_SUCCEEDED(mContent->GetAttrNameAt(aIndex,
00332                                            &nameSpaceID,
00333                                            getter_AddRefs(nameAtom),
00334                                            getter_AddRefs(prefix)))) {
00335     nsINodeInfo *contentNi = mContent->GetNodeInfo();
00336     NS_ENSURE_TRUE(contentNi, NS_ERROR_FAILURE);
00337 
00338     nsCOMPtr<nsINodeInfo> ni;
00339     contentNi->NodeInfoManager()->GetNodeInfo(nameAtom, prefix, nameSpaceID,
00340                                               getter_AddRefs(ni));
00341     NS_ENSURE_TRUE(ni, NS_ERROR_FAILURE);
00342 
00343     rv = GetAttribute(ni, aReturn);
00344   }
00345   else {
00346     *aReturn = nsnull;
00347   }
00348 
00349   return rv;
00350 }
00351 
00352 nsresult
00353 nsDOMAttributeMap::GetLength(PRUint32 *aLength)
00354 {
00355   NS_ENSURE_ARG_POINTER(aLength);
00356 
00357   if (mContent) {
00358     *aLength = mContent->GetAttrCount();
00359   }
00360   else {
00361     *aLength = 0;
00362   }
00363 
00364   return NS_OK;
00365 }
00366 
00367 NS_IMETHODIMP
00368 nsDOMAttributeMap::GetNamedItemNS(const nsAString& aNamespaceURI,
00369                                   const nsAString& aLocalName,
00370                                   nsIDOMNode** aReturn)
00371 {
00372   return GetNamedItemNSInternal(aNamespaceURI, aLocalName, aReturn);
00373 }
00374 
00375 nsresult
00376 nsDOMAttributeMap::GetNamedItemNSInternal(const nsAString& aNamespaceURI,
00377                                           const nsAString& aLocalName,
00378                                           nsIDOMNode** aReturn,
00379                                           PRBool aRemove)
00380 {
00381   NS_ENSURE_ARG_POINTER(aReturn);
00382   *aReturn = nsnull;
00383 
00384   if (!mContent) {
00385     return NS_OK;
00386   }
00387 
00388   NS_ConvertUTF16toUTF8 utf8Name(aLocalName);
00389   PRInt32 nameSpaceID = kNameSpaceID_None;
00390 
00391   if (!aNamespaceURI.IsEmpty()) {
00392     nsContentUtils::GetNSManagerWeakRef()->GetNameSpaceID(aNamespaceURI,
00393                                                           &nameSpaceID);
00394 
00395     if (nameSpaceID == kNameSpaceID_Unknown) {
00396       return NS_OK;
00397     }
00398   }
00399 
00400   PRUint32 i, count = mContent->GetAttrCount();
00401   for (i = 0; i < count; ++i) {
00402     PRInt32 attrNS;
00403     nsCOMPtr<nsIAtom> nameAtom, prefix;
00404     mContent->GetAttrNameAt(i, &attrNS, getter_AddRefs(nameAtom),
00405                             getter_AddRefs(prefix));
00406     if (nameSpaceID == attrNS &&
00407         nameAtom->EqualsUTF8(utf8Name)) {
00408       nsCOMPtr<nsINodeInfo> ni;
00409       mContent->GetNodeInfo()->NodeInfoManager()->
00410         GetNodeInfo(nameAtom, prefix, nameSpaceID, getter_AddRefs(ni));
00411       NS_ENSURE_TRUE(ni, NS_ERROR_FAILURE);
00412 
00413       return GetAttribute(ni, aReturn, aRemove);
00414     }
00415   }
00416 
00417   return NS_OK;
00418 }
00419 
00420 NS_IMETHODIMP
00421 nsDOMAttributeMap::RemoveNamedItemNS(const nsAString& aNamespaceURI,
00422                                      const nsAString& aLocalName,
00423                                      nsIDOMNode** aReturn)
00424 {
00425   NS_ENSURE_ARG_POINTER(aReturn);
00426   *aReturn = nsnull;
00427 
00428   nsresult rv = GetNamedItemNSInternal(aNamespaceURI,
00429                                        aLocalName,
00430                                        aReturn,
00431                                        PR_TRUE);
00432   NS_ENSURE_SUCCESS(rv, rv);
00433 
00434   if (!*aReturn) {
00435     return NS_OK;
00436   }
00437 
00438   nsCOMPtr<nsIAttribute> attr = do_QueryInterface(*aReturn);
00439   NS_ASSERTION(attr, "attribute returned from nsDOMAttributeMap::GetNameItemNS "
00440                "didn't implement nsIAttribute");
00441   NS_ENSURE_TRUE(attr, NS_ERROR_UNEXPECTED);
00442 
00443   nsINodeInfo *ni = attr->NodeInfo();
00444   mContent->UnsetAttr(ni->NamespaceID(), ni->NameAtom(), PR_TRUE);
00445 
00446   return NS_OK;
00447 }