Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / parser / htmlparser / src / nsParserNode.h
blob34214bfd3b4c69c62890b1799d4ace738b49cee4
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 /**
40 * MODULE NOTES:
41 * @update gess 4/1/98
43 * This class is defines the basic interface between the
44 * parser and the content sink. The parser will iterate
45 * over the collection of tokens that it sees from the
46 * tokenizer, coverting each related "group" into one of
47 * these. This object gets passed to the sink, and is
48 * then immediately reused.
50 * If you want to hang onto one of these, you should
51 * make your own copy.
55 #ifndef NS_PARSERNODE__
56 #define NS_PARSERNODE__
58 #include "nsIParserNode.h"
59 #include "nsToken.h"
60 #include "nsString.h"
61 #include "nsParserCIID.h"
62 #include "nsDeque.h"
63 #include "nsDTDUtils.h"
65 class nsTokenAllocator;
67 class nsCParserNode : public nsIParserNode {
69 protected:
71 PRInt32 mRefCnt;
73 public:
75 void AddRef()
77 ++mRefCnt;
80 void Release(nsFixedSizeAllocator& aPool)
82 if (--mRefCnt == 0)
83 Destroy(this, aPool);
86 #ifndef HEAP_ALLOCATED_NODES
87 protected:
89 /**
90 * Hide operator new; clients should use Create() instead.
92 static void* operator new(size_t) CPP_THROW_NEW { return 0; }
94 /**
95 * Hide operator delete; clients should use Destroy() instead.
97 static void operator delete(void*,size_t) {}
99 #endif
101 public:
102 static nsCParserNode* Create(CToken* aToken,
103 nsTokenAllocator* aTokenAllocator,
104 nsNodeAllocator* aNodeAllocator)
106 #ifdef HEAP_ALLOCATED_NODES
107 return new
108 #else
109 nsFixedSizeAllocator& pool = aNodeAllocator->GetArenaPool();
110 void* place = pool.Alloc(sizeof(nsCParserNode));
111 NS_ENSURE_TRUE(place, nsnull);
112 return ::new (place)
113 #endif
114 nsCParserNode(aToken, aTokenAllocator, aNodeAllocator);
117 static void Destroy(nsCParserNode* aNode, nsFixedSizeAllocator& aPool)
119 #ifdef HEAP_ALLOCATED_NODES
120 delete aNode;
121 #else
122 aNode->~nsCParserNode();
123 aPool.Free(aNode, sizeof(*aNode));
124 #endif
128 * Default constructor
130 nsCParserNode();
133 * Constructor
134 * @update gess5/11/98
135 * @param aToken is the token this node "refers" to
137 nsCParserNode(CToken* aToken,
138 nsTokenAllocator* aTokenAllocator,
139 nsNodeAllocator* aNodeAllocator=0);
142 * Destructor
143 * @update gess5/11/98
145 virtual ~nsCParserNode();
148 * Init
149 * @update gess5/11/98
151 virtual nsresult Init(CToken* aToken,
152 nsTokenAllocator* aTokenAllocator,
153 nsNodeAllocator* aNodeAllocator=0);
156 * Retrieve the name of the node
157 * @update gess5/11/98
158 * @return string containing node name
160 virtual const nsAString& GetTagName() const;
163 * Retrieve the text from the given node
164 * @update gess5/11/98
165 * @return string containing node text
167 virtual const nsAString& GetText() const;
170 * Retrieve the type of the parser node.
171 * @update gess5/11/98
172 * @return node type.
174 virtual PRInt32 GetNodeType() const;
177 * Retrieve token type of parser node
178 * @update gess5/11/98
179 * @return token type
181 virtual PRInt32 GetTokenType() const;
184 //***************************************
185 //methods for accessing key/value pairs
186 //***************************************
189 * Retrieve the number of attributes in this node.
190 * @update gess5/11/98
191 * @return count of attributes (may be 0)
193 virtual PRInt32 GetAttributeCount(PRBool askToken=PR_FALSE) const;
196 * Retrieve the key (of key/value pair) at given index
197 * @update gess5/11/98
198 * @param anIndex is the index of the key you want
199 * @return string containing key.
201 virtual const nsAString& GetKeyAt(PRUint32 anIndex) const;
204 * Retrieve the value (of key/value pair) at given index
205 * @update gess5/11/98
206 * @param anIndex is the index of the value you want
207 * @return string containing value.
209 virtual const nsAString& GetValueAt(PRUint32 anIndex) const;
212 * NOTE: When the node is an entity, this will translate the entity
213 * to it's unicode value, and store it in aString.
214 * @update gess5/11/98
215 * @param aString will contain the resulting unicode string value
216 * @return int (unicode char or unicode index from table)
218 virtual PRInt32 TranslateToUnicodeStr(nsString& aString) const;
222 * @update gess5/11/98
223 * @param
224 * @return
226 virtual void AddAttribute(CToken* aToken);
229 * This getter retrieves the line number from the input source where
230 * the token occured. Lines are interpreted as occuring between \n characters.
231 * @update gess7/24/98
232 * @return int containing the line number the token was found on
234 virtual PRInt32 GetSourceLineNumber(void) const;
236 /** This method pop the attribute token from the given index
237 * @update harishd 03/25/99
238 * @return token at anIndex
240 virtual CToken* PopAttributeToken();
242 /** Like PopAttributeToken, but pops off the front of the attribute list */
243 virtual CToken* PopAttributeTokenFront();
245 /** Retrieve a string containing the tag and its attributes in "source" form
246 * @update rickg 06June2000
247 * @return void
249 virtual void GetSource(nsString& aString) const;
252 * This pair of methods allows us to set a generic bit (for arbitrary use)
253 * on each node stored in the context.
254 * @update gess 11May2000
256 virtual PRBool GetGenericState(void) const {return mGenericState;}
257 virtual void SetGenericState(PRBool aState) {mGenericState=aState;}
259 /** Release all the objects you're holding
260 * @update harishd 08/02/00
261 * @return void
263 virtual nsresult ReleaseAll();
265 PRPackedBool mGenericState;
266 PRInt32 mUseCount;
267 CToken* mToken;
269 nsTokenAllocator* mTokenAllocator;
270 #ifdef HEAP_ALLOCATED_NODES
271 nsNodeAllocator* mNodeAllocator; // weak
272 #endif
276 class nsCParserStartNode : public nsCParserNode
278 public:
279 static nsCParserNode* Create(CToken* aToken,
280 nsTokenAllocator* aTokenAllocator,
281 nsNodeAllocator* aNodeAllocator)
283 #ifdef HEAP_ALLOCATED_NODES
284 return new
285 #else
286 nsFixedSizeAllocator& pool = aNodeAllocator->GetArenaPool();
287 void* place = pool.Alloc(sizeof(nsCParserStartNode));
288 NS_ENSURE_TRUE(place, nsnull);
289 return ::new (place)
290 #endif
291 nsCParserStartNode(aToken, aTokenAllocator, aNodeAllocator);
294 nsCParserStartNode()
295 : nsCParserNode(), mAttributes(0) { }
297 nsCParserStartNode(CToken* aToken,
298 nsTokenAllocator* aTokenAllocator,
299 nsNodeAllocator* aNodeAllocator = 0)
300 : nsCParserNode(aToken, aTokenAllocator, aNodeAllocator), mAttributes(0) { }
302 virtual ~nsCParserStartNode()
304 NS_ASSERTION(mTokenAllocator || mAttributes.GetSize() == 0,
305 "Error: no token allocator");
306 CToken* theAttrToken = 0;
307 while ((theAttrToken = static_cast<CToken*>(mAttributes.Pop()))) {
308 IF_FREE(theAttrToken, mTokenAllocator);
312 virtual nsresult Init(CToken* aToken,
313 nsTokenAllocator* aTokenAllocator,
314 nsNodeAllocator* aNodeAllocator = 0);
315 virtual void AddAttribute(CToken* aToken);
316 virtual PRInt32 GetAttributeCount(PRBool askToken = PR_FALSE) const;
317 virtual const nsAString& GetKeyAt(PRUint32 anIndex) const;
318 virtual const nsAString& GetValueAt(PRUint32 anIndex) const;
319 virtual CToken* PopAttributeToken();
320 virtual CToken* PopAttributeTokenFront();
321 virtual void GetSource(nsString& aString) const;
322 virtual nsresult ReleaseAll();
323 protected:
324 nsDeque mAttributes;
327 #endif