Back to index

lightning-sunbird  0.9+nobinonly
nsCommentNode.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  *
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 #include "nsIDOMComment.h"
00038 #include "nsGenericDOMDataNode.h"
00039 #include "nsLayoutAtoms.h"
00040 #include "nsCOMPtr.h"
00041 #include "nsIDocument.h"
00042 #include "nsContentUtils.h"
00043 
00044 
00045 class nsCommentNode : public nsGenericDOMDataNode,
00046                       public nsIDOMComment
00047 {
00048 public:
00049   nsCommentNode(nsNodeInfoManager *aNodeInfoManager);
00050   virtual ~nsCommentNode();
00051 
00052   // nsISupports
00053   NS_DECL_ISUPPORTS_INHERITED
00054 
00055   // nsIDOMNode
00056   NS_IMPL_NSIDOMNODE_USING_GENERIC_DOM_DATA
00057 
00058   // nsIDOMCharacterData
00059   NS_FORWARD_NSIDOMCHARACTERDATA(nsGenericDOMDataNode::)
00060 
00061   // nsIDOMComment
00062   // Empty interface
00063 
00064   // nsIContent
00065   virtual nsIAtom *Tag() const;
00066   virtual PRBool MayHaveFrame() const;
00067   virtual PRBool IsContentOfType(PRUint32 aFlags) const;
00068 
00069 #ifdef DEBUG
00070   virtual void List(FILE* out, PRInt32 aIndent) const;
00071   virtual void DumpContent(FILE* out = stdout, PRInt32 aIndent = 0,
00072                            PRBool aDumpAll = PR_TRUE) const
00073   {
00074     return;
00075   }
00076 #endif
00077 
00078   virtual already_AddRefed<nsITextContent> CloneContent(PRBool aCloneText,
00079                                                         nsNodeInfoManager *aNodeInfoManager);
00080 };
00081 
00082 nsresult
00083 NS_NewCommentNode(nsIContent** aInstancePtrResult,
00084                   nsNodeInfoManager *aNodeInfoManager)
00085 {
00086   NS_PRECONDITION(aNodeInfoManager, "Missing nodeinfo manager");
00087 
00088   *aInstancePtrResult = nsnull;
00089 
00090   nsCommentNode *instance = new nsCommentNode(aNodeInfoManager);
00091   if (!instance) {
00092     return NS_ERROR_OUT_OF_MEMORY;
00093   }
00094 
00095   NS_ADDREF(*aInstancePtrResult = instance);
00096 
00097   return NS_OK;
00098 }
00099 
00100 nsCommentNode::nsCommentNode(nsNodeInfoManager *aNodeInfoManager)
00101   : nsGenericDOMDataNode(aNodeInfoManager)
00102 {
00103 }
00104 
00105 nsCommentNode::~nsCommentNode()
00106 {
00107 }
00108 
00109 
00110 // QueryInterface implementation for nsCommentNode
00111 NS_INTERFACE_MAP_BEGIN(nsCommentNode)
00112   NS_INTERFACE_MAP_ENTRY(nsITextContent)
00113   NS_INTERFACE_MAP_ENTRY(nsIDOMNode)
00114   NS_INTERFACE_MAP_ENTRY(nsIDOMCharacterData)
00115   NS_INTERFACE_MAP_ENTRY(nsIDOMComment)
00116   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(Comment)
00117 NS_INTERFACE_MAP_END_INHERITING(nsGenericDOMDataNode)
00118 
00119 
00120 NS_IMPL_ADDREF_INHERITED(nsCommentNode, nsGenericDOMDataNode)
00121 NS_IMPL_RELEASE_INHERITED(nsCommentNode, nsGenericDOMDataNode)
00122 
00123 
00124 nsIAtom *
00125 nsCommentNode::Tag() const
00126 {
00127   return nsLayoutAtoms::commentTagName;
00128 }
00129 
00130 // virtual
00131 PRBool
00132 nsCommentNode::MayHaveFrame() const
00133 {
00134   return PR_FALSE;
00135 }
00136 
00137 PRBool
00138 nsCommentNode::IsContentOfType(PRUint32 aFlags) const
00139 {
00140   return !(aFlags & ~eCOMMENT);
00141 }
00142 
00143 NS_IMETHODIMP
00144 nsCommentNode::GetNodeName(nsAString& aNodeName)
00145 {
00146   aNodeName.AssignLiteral("#comment");
00147   return NS_OK;
00148 }
00149 
00150 NS_IMETHODIMP
00151 nsCommentNode::GetNodeValue(nsAString& aNodeValue)
00152 {
00153   return nsGenericDOMDataNode::GetNodeValue(aNodeValue);
00154 }
00155 
00156 NS_IMETHODIMP
00157 nsCommentNode::SetNodeValue(const nsAString& aNodeValue)
00158 {
00159   return nsGenericDOMDataNode::SetNodeValue(aNodeValue);
00160 }
00161 
00162 NS_IMETHODIMP
00163 nsCommentNode::GetNodeType(PRUint16* aNodeType)
00164 {
00165   *aNodeType = (PRUint16)nsIDOMNode::COMMENT_NODE;
00166   return NS_OK;
00167 }
00168 
00169 NS_IMETHODIMP
00170 nsCommentNode::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
00171 {
00172   nsCOMPtr<nsITextContent> textContent = CloneContent(PR_TRUE,
00173                                                       GetNodeInfoManager());
00174   NS_ENSURE_TRUE(textContent, NS_ERROR_OUT_OF_MEMORY);
00175 
00176   return CallQueryInterface(textContent, aReturn);
00177 }
00178 
00179 already_AddRefed<nsITextContent>
00180 nsCommentNode::CloneContent(PRBool aCloneText,
00181                             nsNodeInfoManager *aNodeInfoManager)
00182 {
00183   nsCommentNode* it = new nsCommentNode(aNodeInfoManager);
00184   if (!it)
00185     return nsnull;
00186 
00187   if (aCloneText) {
00188     it->mText = mText;
00189   }
00190 
00191   NS_ADDREF(it);
00192 
00193   return it;
00194 }
00195 
00196 #ifdef DEBUG
00197 void
00198 nsCommentNode::List(FILE* out, PRInt32 aIndent) const
00199 {
00200   NS_PRECONDITION(IsInDoc(), "bad content");
00201 
00202   PRInt32 indx;
00203   for (indx = aIndent; --indx >= 0; ) fputs("  ", out);
00204 
00205   fprintf(out, "Comment@%p refcount=%d<!--", this, mRefCnt.get());
00206 
00207   nsAutoString tmp;
00208   ToCString(tmp, 0, mText.GetLength());
00209   fputs(NS_LossyConvertUCS2toASCII(tmp).get(), out);
00210 
00211   fputs("-->\n", out);
00212 }
00213 #endif