Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / parser / htmlparser / src / nsParserService.cpp
blob7165704dfd28dda52709d637040fc6720003a765
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla Communicator client code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsDOMError.h"
40 #include "nsIAtom.h"
41 #include "nsParserService.h"
42 #include "nsHTMLEntities.h"
43 #include "nsElementTable.h"
44 #include "nsICategoryManager.h"
45 #include "nsCategoryManagerUtils.h"
47 extern "C" int MOZ_XMLCheckQName(const char* ptr, const char* end,
48 int ns_aware, const char** colon);
50 nsParserService::nsParserService() : mEntries(0)
52 mHaveNotifiedCategoryObservers = PR_FALSE;
55 nsParserService::~nsParserService()
57 nsObserverEntry *entry = nsnull;
58 while( (entry = static_cast<nsObserverEntry*>(mEntries.Pop())) ) {
59 NS_RELEASE(entry);
63 NS_IMPL_ISUPPORTS1(nsParserService, nsIParserService)
65 PRInt32
66 nsParserService::HTMLAtomTagToId(nsIAtom* aAtom) const
68 nsAutoString tagName;
69 aAtom->ToString(tagName);
71 return nsHTMLTags::LookupTag(tagName);
74 PRInt32
75 nsParserService::HTMLCaseSensitiveAtomTagToId(nsIAtom* aAtom) const
77 return nsHTMLTags::CaseSensitiveLookupTag(aAtom);
80 PRInt32
81 nsParserService::HTMLStringTagToId(const nsAString& aTag) const
83 return nsHTMLTags::LookupTag(aTag);
86 const PRUnichar*
87 nsParserService::HTMLIdToStringTag(PRInt32 aId) const
89 return nsHTMLTags::GetStringValue((nsHTMLTag)aId);
92 nsIAtom*
93 nsParserService::HTMLIdToAtomTag(PRInt32 aId) const
95 return nsHTMLTags::GetAtom((nsHTMLTag)aId);
98 NS_IMETHODIMP
99 nsParserService::HTMLConvertEntityToUnicode(const nsAString& aEntity,
100 PRInt32* aUnicode) const
102 *aUnicode = nsHTMLEntities::EntityToUnicode(aEntity);
104 return NS_OK;
107 NS_IMETHODIMP
108 nsParserService::HTMLConvertUnicodeToEntity(PRInt32 aUnicode,
109 nsCString& aEntity) const
111 const char* str = nsHTMLEntities::UnicodeToEntity(aUnicode);
112 if (str) {
113 aEntity.Assign(str);
116 return NS_OK;
119 NS_IMETHODIMP
120 nsParserService::IsContainer(PRInt32 aId, PRBool& aIsContainer) const
122 aIsContainer = nsHTMLElement::IsContainer((eHTMLTags)aId);
124 return NS_OK;
127 NS_IMETHODIMP
128 nsParserService::IsBlock(PRInt32 aId, PRBool& aIsBlock) const
130 if((aId>eHTMLTag_unknown) && (aId<eHTMLTag_userdefined)) {
131 aIsBlock=((gHTMLElements[aId].IsMemberOf(kBlock)) ||
132 (gHTMLElements[aId].IsMemberOf(kBlockEntity)) ||
133 (gHTMLElements[aId].IsMemberOf(kHeading)) ||
134 (gHTMLElements[aId].IsMemberOf(kPreformatted))||
135 (gHTMLElements[aId].IsMemberOf(kList)));
137 else {
138 aIsBlock = PR_FALSE;
141 return NS_OK;
144 NS_IMETHODIMP
145 nsParserService::RegisterObserver(nsIElementObserver* aObserver,
146 const nsAString& aTopic,
147 const eHTMLTags* aTags)
149 nsresult result = NS_OK;
150 nsObserverEntry* entry = GetEntry(aTopic);
152 if(!entry) {
153 result = CreateEntry(aTopic,&entry);
154 NS_ENSURE_SUCCESS(result,result);
157 while (*aTags) {
158 if (*aTags <= NS_HTML_TAG_MAX) {
159 entry->AddObserver(aObserver,*aTags);
161 ++aTags;
164 return result;
167 NS_IMETHODIMP
168 nsParserService::UnregisterObserver(nsIElementObserver* aObserver,
169 const nsAString& aTopic)
171 PRInt32 count = mEntries.GetSize();
173 for (PRInt32 i=0; i < count; ++i) {
174 nsObserverEntry* entry = static_cast<nsObserverEntry*>(mEntries.ObjectAt(i));
175 if (entry && entry->Matches(aTopic)) {
176 entry->RemoveObserver(aObserver);
180 return NS_OK;
183 NS_IMETHODIMP
184 nsParserService::GetTopicObservers(const nsAString& aTopic,
185 nsIObserverEntry** aEntry) {
186 nsresult result = NS_OK;
187 nsObserverEntry* entry = GetEntry(aTopic);
189 if (!entry) {
190 return NS_ERROR_NULL_POINTER;
193 NS_ADDREF(*aEntry = entry);
195 return result;
198 nsresult
199 nsParserService::CheckQName(const nsAString& aQName,
200 PRBool aNamespaceAware,
201 const PRUnichar** aColon)
203 const char* colon;
204 const PRUnichar *begin, *end;
205 begin = aQName.BeginReading();
206 end = aQName.EndReading();
207 int result = MOZ_XMLCheckQName(reinterpret_cast<const char*>(begin),
208 reinterpret_cast<const char*>(end),
209 aNamespaceAware, &colon);
210 *aColon = reinterpret_cast<const PRUnichar*>(colon);
212 if (result == 0) {
213 return NS_OK;
216 // MOZ_EXPAT_EMPTY_QNAME || MOZ_EXPAT_INVALID_CHARACTER
217 if (result == (1 << 0) || result == (1 << 1)) {
218 return NS_ERROR_DOM_INVALID_CHARACTER_ERR;
221 return NS_ERROR_DOM_NAMESPACE_ERR;
224 class nsMatchesTopic : public nsDequeFunctor{
225 const nsAString& mString;
226 public:
227 PRBool matched;
228 nsObserverEntry* entry;
229 nsMatchesTopic(const nsAString& aString):mString(aString),matched(PR_FALSE){}
230 virtual void* operator()(void* anObject){
231 entry=static_cast<nsObserverEntry*>(anObject);
232 matched=mString.Equals(entry->mTopic);
233 return matched ? nsnull : anObject;
237 // XXX This may be more efficient as a HashTable instead of linear search
238 nsObserverEntry*
239 nsParserService::GetEntry(const nsAString& aTopic)
241 if (!mHaveNotifiedCategoryObservers) {
242 mHaveNotifiedCategoryObservers = PR_TRUE;
243 NS_CreateServicesFromCategory("parser-service-category",
244 static_cast<nsISupports*>(static_cast<void*>(this)),
245 "parser-service-start");
248 nsMatchesTopic matchesTopic(aTopic);
249 mEntries.FirstThat(*&matchesTopic);
250 return matchesTopic.matched?matchesTopic.entry:nsnull;
253 nsresult
254 nsParserService::CreateEntry(const nsAString& aTopic, nsObserverEntry** aEntry)
256 *aEntry = new nsObserverEntry(aTopic);
258 if (!*aEntry) {
259 return NS_ERROR_OUT_OF_MEMORY;
262 NS_ADDREF(*aEntry);
263 mEntries.Push(*aEntry);
265 return NS_OK;