Back to index

lightning-sunbird  0.9+nobinonly
nsGenericDOMDataNode.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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #ifndef nsGenericDOMDataNode_h___
00038 #define nsGenericDOMDataNode_h___
00039 
00040 #include "nsITextContent.h"
00041 #include "nsIDOMCharacterData.h"
00042 #include "nsIDOMEventReceiver.h"
00043 #include "nsTextFragment.h"
00044 #include "nsVoidArray.h"
00045 #include "nsDOMError.h"
00046 #include "nsIEventListenerManager.h"
00047 #include "nsGenericElement.h"
00048 
00049 
00050 class nsIDOMAttr;
00051 class nsIDOMEventListener;
00052 class nsIDOMNodeList;
00053 class nsIFrame;
00054 class nsIDOMText;
00055 class nsINodeInfo;
00056 class nsURI;
00057 
00058 #define PARENT_BIT_IS_IN_A_HASH ((PtrBits)0x1 << 1)
00059 
00060 #define NODEINFOMANAGER_BIT_IS_NATIVE_ANONYMOUS 0x1
00061 
00062 class nsGenericDOMDataNode : public nsITextContent
00063 {
00064 public:
00065   NS_DECL_ISUPPORTS
00066 
00067   nsGenericDOMDataNode(nsNodeInfoManager *aNodeInfoManager);
00068   virtual ~nsGenericDOMDataNode();
00069 
00070   // Implementation for nsIDOMNode
00071   nsresult GetNodeValue(nsAString& aNodeValue);
00072   nsresult SetNodeValue(const nsAString& aNodeValue);
00073   nsresult GetParentNode(nsIDOMNode** aParentNode);
00074   nsresult GetAttributes(nsIDOMNamedNodeMap** aAttributes)
00075   {
00076     NS_ENSURE_ARG_POINTER(aAttributes);
00077     *aAttributes = nsnull;
00078     return NS_OK;
00079   }
00080   nsresult GetPreviousSibling(nsIDOMNode** aPreviousSibling);
00081   nsresult GetNextSibling(nsIDOMNode** aNextSibling);
00082   nsresult GetChildNodes(nsIDOMNodeList** aChildNodes);
00083   nsresult HasChildNodes(PRBool* aHasChildNodes)
00084   {
00085     NS_ENSURE_ARG_POINTER(aHasChildNodes);
00086     *aHasChildNodes = PR_FALSE;
00087     return NS_OK;
00088   }
00089   nsresult HasAttributes(PRBool* aHasAttributes)
00090   {
00091     NS_ENSURE_ARG_POINTER(aHasAttributes);
00092     *aHasAttributes = PR_FALSE;
00093     return NS_OK;
00094   }
00095   nsresult GetFirstChild(nsIDOMNode** aFirstChild)
00096   {
00097     NS_ENSURE_ARG_POINTER(aFirstChild);
00098     *aFirstChild = nsnull;
00099     return NS_OK;
00100   }
00101   nsresult GetLastChild(nsIDOMNode** aLastChild)
00102   {
00103     NS_ENSURE_ARG_POINTER(aLastChild);
00104     *aLastChild = nsnull;
00105     return NS_OK;
00106   }
00107   nsresult InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild,
00108                         nsIDOMNode** aReturn)
00109   {
00110     NS_ENSURE_ARG_POINTER(aReturn);
00111     *aReturn = nsnull;
00112     return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
00113   }
00114   nsresult ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild,
00115                         nsIDOMNode** aReturn)
00116   {
00117     NS_ENSURE_ARG_POINTER(aReturn);
00118     *aReturn = nsnull;
00119 
00120     /*
00121      * Data nodes can't have children.
00122      */
00123     return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
00124   }
00125   nsresult RemoveChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn)
00126   {
00127     NS_ENSURE_ARG_POINTER(aReturn);
00128     *aReturn = nsnull;
00129 
00130     /*
00131      * Data nodes can't have children, i.e. aOldChild can't be a child of
00132      * this node.
00133      */
00134     return NS_ERROR_DOM_NOT_FOUND_ERR;
00135   }
00136   nsresult AppendChild(nsIDOMNode* aNewChild, nsIDOMNode** aReturn)
00137   {
00138     NS_ENSURE_ARG_POINTER(aReturn);
00139     *aReturn = nsnull;
00140     return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
00141   }
00142   nsresult GetOwnerDocument(nsIDOMDocument** aOwnerDocument);
00143   nsresult GetNamespaceURI(nsAString& aNamespaceURI);
00144   nsresult GetLocalName(nsAString& aLocalName);
00145   nsresult GetPrefix(nsAString& aPrefix);
00146   nsresult SetPrefix(const nsAString& aPrefix);
00147   nsresult Normalize();
00148   nsresult IsSupported(const nsAString& aFeature,
00149                        const nsAString& aVersion,
00150                        PRBool* aReturn);
00151   nsresult GetBaseURI(nsAString& aURI);
00152   nsresult LookupPrefix(const nsAString& aNamespaceURI,
00153                         nsAString& aPrefix);
00154   nsresult LookupNamespaceURI(const nsAString& aNamespacePrefix,
00155                               nsAString& aNamespaceURI);
00156 
00157   // Implementation for nsIDOMCharacterData
00158   nsresult GetData(nsAString& aData);
00159   nsresult SetData(const nsAString& aData);
00160   nsresult GetLength(PRUint32* aLength);
00161   nsresult SubstringData(PRUint32 aOffset, PRUint32 aCount,
00162                          nsAString& aReturn);
00163   nsresult AppendData(const nsAString& aArg);
00164   nsresult InsertData(PRUint32 aOffset, const nsAString& aArg);
00165   nsresult DeleteData(PRUint32 aOffset, PRUint32 aCount);
00166   nsresult ReplaceData(PRUint32 aOffset, PRUint32 aCount,
00167                        const nsAString& aArg);
00168 
00169   // nsIDOMGCParticipant interface methods
00170   virtual nsIDOMGCParticipant* GetSCCIndex();
00171   virtual void AppendReachableList(nsCOMArray<nsIDOMGCParticipant>& aArray);
00172 
00173   // Implementation for nsIContent
00174   nsIDocument* GetDocument() const;
00175   virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
00176                               nsIContent* aBindingParent,
00177                               PRBool aCompileEventHandlers);
00178   virtual void UnbindFromTree(PRBool aDeep = PR_TRUE,
00179                               PRBool aNullParent = PR_TRUE);
00180   PRBool IsInDoc() const
00181   {
00182     return mParentPtrBits & PARENT_BIT_INDOCUMENT;
00183   }
00184 
00185   nsIDocument *GetCurrentDoc() const
00186   {
00187     return IsInDoc() ? GetOwnerDoc() : nsnull;
00188   }
00189 
00190   nsIDocument *GetOwnerDoc() const
00191   {
00192     return GetNodeInfoManager()->GetDocument();
00193   }
00194 
00195   virtual PRBool IsNativeAnonymous() const;
00196   virtual void SetNativeAnonymous(PRBool aAnonymous);
00197   virtual PRInt32 GetNameSpaceID() const;
00198   virtual nsIAtom *GetIDAttributeName() const;
00199   virtual already_AddRefed<nsINodeInfo> GetExistingAttrNameFromQName(const nsAString& aStr) const;
00200   nsresult SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
00201                    const nsAString& aValue, PRBool aNotify)
00202   {
00203     return SetAttr(aNameSpaceID, aName, nsnull, aValue, aNotify);
00204   }
00205   virtual nsresult SetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttribute,
00206                            nsIAtom* aPrefix, const nsAString& aValue,
00207                            PRBool aNotify);
00208   virtual nsresult UnsetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttribute,
00209                              PRBool aNotify);
00210   virtual nsresult GetAttr(PRInt32 aNameSpaceID, nsIAtom *aAttribute,
00211                            nsAString& aResult) const;
00212   virtual PRBool HasAttr(PRInt32 aNameSpaceID, nsIAtom *aAttribute) const;
00213   virtual nsresult GetAttrNameAt(PRUint32 aIndex, PRInt32* aNameSpaceID,
00214                                  nsIAtom** aName, nsIAtom** aPrefix) const;
00215   virtual PRUint32 GetAttrCount() const;
00216 #ifdef DEBUG
00217   virtual void List(FILE* out, PRInt32 aIndent) const;
00218   virtual void DumpContent(FILE* out, PRInt32 aIndent, PRBool aDumpAll) const;
00219 #endif
00220   virtual nsresult HandleDOMEvent(nsPresContext* aPresContext,
00221                                   nsEvent* aEvent, nsIDOMEvent** aDOMEvent,
00222                                   PRUint32 aFlags,
00223                                   nsEventStatus* aEventStatus);
00224   virtual PRUint32 ContentID() const;
00225   virtual nsresult RangeAdd(nsIDOMRange* aRange);
00226   virtual void RangeRemove(nsIDOMRange* aRange);
00227   virtual const nsVoidArray *GetRangeList() const;
00228 
00229   virtual nsIContent *GetBindingParent() const;
00230   virtual PRBool IsContentOfType(PRUint32 aFlags) const;
00231 
00232   virtual nsresult GetListenerManager(nsIEventListenerManager **aResult);
00233   virtual already_AddRefed<nsIURI> GetBaseURI() const;
00234 
00235   virtual nsINodeInfo *GetNodeInfo() const;
00236   virtual PRUint32 GetChildCount() const;
00237   virtual nsIContent *GetChildAt(PRUint32 aIndex) const;
00238   virtual PRInt32 IndexOf(nsIContent* aPossibleChild) const;
00239   virtual nsresult InsertChildAt(nsIContent* aKid, PRUint32 aIndex,
00240                                  PRBool aNotify);
00241   virtual nsresult AppendChildTo(nsIContent* aKid, PRBool aNotify);
00242   virtual nsresult RemoveChildAt(PRUint32 aIndex, PRBool aNotify);
00243   virtual PRBool MayHaveFrame() const;
00244 
00245   // nsITextContent
00246   virtual const nsTextFragment *Text();
00247   virtual PRUint32 TextLength();
00248   virtual void SetText(const PRUnichar* aBuffer, PRUint32 aLength,
00249                        PRBool aNotify)
00250   {
00251     DoSetText(aBuffer, aLength, PR_FALSE, aNotify);
00252   }
00253   virtual void SetText(const nsAString& aStr, PRBool aNotify)
00254   {
00255     DoSetText(aStr, PR_FALSE, aNotify);
00256   }
00257   virtual void SetText(const char* aBuffer, PRUint32 aLength, PRBool aNotify)
00258   {
00259     DoSetText(aBuffer, aLength, PR_FALSE, aNotify);
00260   }
00261   virtual PRBool IsOnlyWhitespace();
00262   virtual void AppendTextTo(nsAString& aResult);
00263 
00264   //----------------------------------------
00265 
00266   virtual already_AddRefed<nsITextContent> CloneContent(PRBool aCloneText,
00267                                                         nsNodeInfoManager *aNodeInfoManager);
00268 
00269 #ifdef DEBUG
00270   void ToCString(nsAString& aBuf, PRInt32 aOffset, PRInt32 aLen) const;
00271 #endif
00272 
00273 protected:
00274   nsresult SplitText(PRUint32 aOffset, nsIDOMText** aReturn);
00275 
00276   // Like SetText but allows to specify whether this is an append for
00277   // notifications.
00278   void DoSetText(const PRUnichar* aBuffer, PRUint32 aLength, PRBool aIsAppend,
00279                PRBool aNotify);
00280   void DoSetText(const char* aBuffer, PRUint32 aLength, PRBool aIsAppend,
00281                PRBool aNotify);
00282   void DoSetText(const nsAString& aStr, PRBool aIsAppend, PRBool aNotify);
00283 
00284   nsTextFragment mText;
00285   typedef unsigned long PtrBits;
00286   nsNodeInfoManager* GetNodeInfoManager() const {
00287     return NS_REINTERPRET_CAST(nsNodeInfoManager*, PtrBits(mNodeInfoManagerBits) &
00288                                                    ~NODEINFOMANAGER_BIT_IS_NATIVE_ANONYMOUS);
00289   }
00290   void SetNodeInfoManager(nsNodeInfoManager* aNodeInfoManager);
00291 
00292 private:
00293   // Note: the LSB of this pointer is used for IsNativeAnonymous().
00294   // Use Set/GetNodeInfoManager(), not the raw pointer.
00295   void* mNodeInfoManagerBits;
00296 
00297   void LookupListenerManager(nsIEventListenerManager **aListenerManager) const;
00298   nsVoidArray *LookupRangeList() const;
00299 
00300   void SetBidiStatus();
00301 
00302   already_AddRefed<nsIAtom> GetCurrentValueAtom();
00303 
00304   void SetIsInAHash()
00305   {
00306     mParentPtrBits |= PARENT_BIT_IS_IN_A_HASH;
00307   }
00308   PRBool GetIsInAHash() const
00309   {
00310     return (mParentPtrBits & PARENT_BIT_IS_IN_A_HASH);
00311   }
00312 
00313   void SetHasRangeList()
00314   {
00315     SetIsInAHash();
00316   }
00317 
00318   void SetHasEventListenerManager()
00319   {
00320     SetIsInAHash();
00321   }
00322 
00323   PRBool CouldHaveRangeList() const
00324   {
00325     return GetIsInAHash() && nsGenericElement::sRangeListsHash.ops;
00326   }
00327 
00328   PRBool CouldHaveEventListenerManager() const
00329   {
00330     return GetIsInAHash() && nsGenericElement::sEventListenerManagersHash.ops;
00331   }
00332 };
00333 
00334 //----------------------------------------------------------------------
00335 
00344 #define NS_IMPL_NSIDOMNODE_USING_GENERIC_DOM_DATA                           \
00345   NS_IMETHOD GetNodeName(nsAString& aNodeName);                             \
00346   NS_IMETHOD GetLocalName(nsAString& aLocalName) {                          \
00347     return nsGenericDOMDataNode::GetLocalName(aLocalName);                  \
00348   }                                                                         \
00349   NS_IMETHOD GetNodeValue(nsAString& aNodeValue);                           \
00350   NS_IMETHOD SetNodeValue(const nsAString& aNodeValue);                     \
00351   NS_IMETHOD GetNodeType(PRUint16* aNodeType);                              \
00352   NS_IMETHOD GetParentNode(nsIDOMNode** aParentNode) {                      \
00353     return nsGenericDOMDataNode::GetParentNode(aParentNode);                \
00354   }                                                                         \
00355   NS_IMETHOD GetChildNodes(nsIDOMNodeList** aChildNodes) {                  \
00356     return nsGenericDOMDataNode::GetChildNodes(aChildNodes);                \
00357   }                                                                         \
00358   NS_IMETHOD HasChildNodes(PRBool* aHasChildNodes) {                        \
00359     return nsGenericDOMDataNode::HasChildNodes(aHasChildNodes);             \
00360   }                                                                         \
00361   NS_IMETHOD HasAttributes(PRBool* aHasAttributes) {                        \
00362     return nsGenericDOMDataNode::HasAttributes(aHasAttributes);             \
00363   }                                                                         \
00364   NS_IMETHOD GetFirstChild(nsIDOMNode** aFirstChild) {                      \
00365     return nsGenericDOMDataNode::GetFirstChild(aFirstChild);                \
00366   }                                                                         \
00367   NS_IMETHOD GetLastChild(nsIDOMNode** aLastChild) {                        \
00368     return nsGenericDOMDataNode::GetLastChild(aLastChild);                  \
00369   }                                                                         \
00370   NS_IMETHOD GetPreviousSibling(nsIDOMNode** aPreviousSibling) {            \
00371     return nsGenericDOMDataNode::GetPreviousSibling(aPreviousSibling);      \
00372   }                                                                         \
00373   NS_IMETHOD GetNextSibling(nsIDOMNode** aNextSibling) {                    \
00374     return nsGenericDOMDataNode::GetNextSibling(aNextSibling);              \
00375   }                                                                         \
00376   NS_IMETHOD GetAttributes(nsIDOMNamedNodeMap** aAttributes) {              \
00377     return nsGenericDOMDataNode::GetAttributes(aAttributes);                \
00378   }                                                                         \
00379   NS_IMETHOD InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild,     \
00380                              nsIDOMNode** aReturn) {                        \
00381     return nsGenericDOMDataNode::InsertBefore(aNewChild, aRefChild,         \
00382                                               aReturn);                     \
00383   }                                                                         \
00384   NS_IMETHOD AppendChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn) {     \
00385     return nsGenericDOMDataNode::AppendChild(aOldChild, aReturn);           \
00386   }                                                                         \
00387   NS_IMETHOD ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild,     \
00388                              nsIDOMNode** aReturn) {                        \
00389     return nsGenericDOMDataNode::ReplaceChild(aNewChild, aOldChild,         \
00390                                               aReturn);                     \
00391   }                                                                         \
00392   NS_IMETHOD RemoveChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn) {     \
00393     return nsGenericDOMDataNode::RemoveChild(aOldChild, aReturn);           \
00394   }                                                                         \
00395   NS_IMETHOD GetOwnerDocument(nsIDOMDocument** aOwnerDocument) {            \
00396     return nsGenericDOMDataNode::GetOwnerDocument(aOwnerDocument);          \
00397   }                                                                         \
00398   NS_IMETHOD GetNamespaceURI(nsAString& aNamespaceURI) {                    \
00399     return nsGenericDOMDataNode::GetNamespaceURI(aNamespaceURI);            \
00400   }                                                                         \
00401   NS_IMETHOD GetPrefix(nsAString& aPrefix) {                                \
00402     return nsGenericDOMDataNode::GetPrefix(aPrefix);                        \
00403   }                                                                         \
00404   NS_IMETHOD SetPrefix(const nsAString& aPrefix) {                          \
00405     return nsGenericDOMDataNode::SetPrefix(aPrefix);                        \
00406   }                                                                         \
00407   NS_IMETHOD Normalize() {                                                  \
00408     return NS_OK;                                                           \
00409   }                                                                         \
00410   NS_IMETHOD IsSupported(const nsAString& aFeature,                         \
00411                       const nsAString& aVersion,                            \
00412                       PRBool* aReturn) {                                    \
00413     return nsGenericDOMDataNode::IsSupported(aFeature, aVersion, aReturn);  \
00414   }                                                                         \
00415   NS_IMETHOD CloneNode(PRBool aDeep, nsIDOMNode** aReturn);
00416 
00417 #endif /* nsGenericDOMDataNode_h___ */