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
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.
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"
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())) ) {
63 NS_IMPL_ISUPPORTS1(nsParserService
, nsIParserService
)
66 nsParserService::HTMLAtomTagToId(nsIAtom
* aAtom
) const
69 aAtom
->ToString(tagName
);
71 return nsHTMLTags::LookupTag(tagName
);
75 nsParserService::HTMLCaseSensitiveAtomTagToId(nsIAtom
* aAtom
) const
77 return nsHTMLTags::CaseSensitiveLookupTag(aAtom
);
81 nsParserService::HTMLStringTagToId(const nsAString
& aTag
) const
83 return nsHTMLTags::LookupTag(aTag
);
87 nsParserService::HTMLIdToStringTag(PRInt32 aId
) const
89 return nsHTMLTags::GetStringValue((nsHTMLTag
)aId
);
93 nsParserService::HTMLIdToAtomTag(PRInt32 aId
) const
95 return nsHTMLTags::GetAtom((nsHTMLTag
)aId
);
99 nsParserService::HTMLConvertEntityToUnicode(const nsAString
& aEntity
,
100 PRInt32
* aUnicode
) const
102 *aUnicode
= nsHTMLEntities::EntityToUnicode(aEntity
);
108 nsParserService::HTMLConvertUnicodeToEntity(PRInt32 aUnicode
,
109 nsCString
& aEntity
) const
111 const char* str
= nsHTMLEntities::UnicodeToEntity(aUnicode
);
120 nsParserService::IsContainer(PRInt32 aId
, PRBool
& aIsContainer
) const
122 aIsContainer
= nsHTMLElement::IsContainer((eHTMLTags
)aId
);
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
)));
145 nsParserService::RegisterObserver(nsIElementObserver
* aObserver
,
146 const nsAString
& aTopic
,
147 const eHTMLTags
* aTags
)
149 nsresult result
= NS_OK
;
150 nsObserverEntry
* entry
= GetEntry(aTopic
);
153 result
= CreateEntry(aTopic
,&entry
);
154 NS_ENSURE_SUCCESS(result
,result
);
158 if (*aTags
<= NS_HTML_TAG_MAX
) {
159 entry
->AddObserver(aObserver
,*aTags
);
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
);
184 nsParserService::GetTopicObservers(const nsAString
& aTopic
,
185 nsIObserverEntry
** aEntry
) {
186 nsresult result
= NS_OK
;
187 nsObserverEntry
* entry
= GetEntry(aTopic
);
190 return NS_ERROR_NULL_POINTER
;
193 NS_ADDREF(*aEntry
= entry
);
199 nsParserService::CheckQName(const nsAString
& aQName
,
200 PRBool aNamespaceAware
,
201 const PRUnichar
** aColon
)
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
);
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
;
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
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
;
254 nsParserService::CreateEntry(const nsAString
& aTopic
, nsObserverEntry
** aEntry
)
256 *aEntry
= new nsObserverEntry(aTopic
);
259 return NS_ERROR_OUT_OF_MEMORY
;
263 mEntries
.Push(*aEntry
);