2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A class for managing namespace IDs and mapping back and forth
|
|
|
|
* between namespace IDs and namespace URIs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsINodeInfo.h"
|
|
|
|
#include "nsCOMArray.h"
|
2009-01-18 12:14:14 -08:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
#ifdef MOZ_XTF
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIXTFService.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
static NS_DEFINE_CID(kXTFServiceCID, NS_XTFSERVICE_CID);
|
|
|
|
#endif
|
|
|
|
|
2010-10-25 05:17:38 -07:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#define kXMLNSNameSpaceURI "http://www.w3.org/2000/xmlns/"
|
|
|
|
#define kXMLNameSpaceURI "http://www.w3.org/XML/1998/namespace"
|
|
|
|
#define kXHTMLNameSpaceURI "http://www.w3.org/1999/xhtml"
|
|
|
|
#define kXLinkNameSpaceURI "http://www.w3.org/1999/xlink"
|
|
|
|
#define kXSLTNameSpaceURI "http://www.w3.org/1999/XSL/Transform"
|
|
|
|
#define kXBLNameSpaceURI "http://www.mozilla.org/xbl"
|
|
|
|
#define kMathMLNameSpaceURI "http://www.w3.org/1998/Math/MathML"
|
|
|
|
#define kRDFNameSpaceURI "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
|
|
|
#define kXULNameSpaceURI "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
|
|
|
|
#define kSVGNameSpaceURI "http://www.w3.org/2000/svg"
|
|
|
|
#define kXMLEventsNameSpaceURI "http://www.w3.org/2001/xml-events"
|
|
|
|
|
|
|
|
class nsNameSpaceKey : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const nsAString* KeyType;
|
|
|
|
typedef const nsAString* KeyTypePointer;
|
|
|
|
|
|
|
|
nsNameSpaceKey(KeyTypePointer aKey) : mKey(aKey)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
nsNameSpaceKey(const nsNameSpaceKey& toCopy) : mKey(toCopy.mKey)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyType GetKey() const
|
|
|
|
{
|
|
|
|
return mKey;
|
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool KeyEquals(KeyType aKey) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mKey->Equals(*aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey)
|
|
|
|
{
|
|
|
|
return aKey;
|
|
|
|
}
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
|
|
|
return HashString(*aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ALLOW_MEMMOVE = PR_TRUE
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
const nsAString* mKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NameSpaceManagerImpl : public nsINameSpaceManager {
|
|
|
|
public:
|
|
|
|
virtual ~NameSpaceManagerImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
nsresult RegisterNameSpace(const nsAString& aURI, PRInt32& aNameSpaceID);
|
|
|
|
|
|
|
|
nsresult GetNameSpaceURI(PRInt32 aNameSpaceID, nsAString& aURI);
|
|
|
|
PRInt32 GetNameSpaceID(const nsAString& aURI);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool HasElementCreator(PRInt32 aNameSpaceID);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsresult AddNameSpace(const nsAString& aURI, const PRInt32 aNameSpaceID);
|
|
|
|
|
|
|
|
nsDataHashtable<nsNameSpaceKey,PRInt32> mURIToIDTable;
|
2009-01-18 12:14:14 -08:00
|
|
|
nsTArray< nsAutoPtr<nsString> > mURIArray;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2007-06-19 22:23:50 -07:00
|
|
|
static NameSpaceManagerImpl* sNameSpaceManager = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(NameSpaceManagerImpl, nsINameSpaceManager)
|
|
|
|
|
|
|
|
nsresult NameSpaceManagerImpl::Init()
|
|
|
|
{
|
|
|
|
nsresult rv = mURIToIDTable.Init(32);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
#define REGISTER_NAMESPACE(uri, id) \
|
|
|
|
rv = AddNameSpace(NS_LITERAL_STRING(uri), id); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv)
|
|
|
|
|
|
|
|
// Need to be ordered according to ID.
|
|
|
|
REGISTER_NAMESPACE(kXMLNSNameSpaceURI, kNameSpaceID_XMLNS);
|
|
|
|
REGISTER_NAMESPACE(kXMLNameSpaceURI, kNameSpaceID_XML);
|
|
|
|
REGISTER_NAMESPACE(kXHTMLNameSpaceURI, kNameSpaceID_XHTML);
|
|
|
|
REGISTER_NAMESPACE(kXLinkNameSpaceURI, kNameSpaceID_XLink);
|
|
|
|
REGISTER_NAMESPACE(kXSLTNameSpaceURI, kNameSpaceID_XSLT);
|
|
|
|
REGISTER_NAMESPACE(kXBLNameSpaceURI, kNameSpaceID_XBL);
|
|
|
|
REGISTER_NAMESPACE(kMathMLNameSpaceURI, kNameSpaceID_MathML);
|
|
|
|
REGISTER_NAMESPACE(kRDFNameSpaceURI, kNameSpaceID_RDF);
|
|
|
|
REGISTER_NAMESPACE(kXULNameSpaceURI, kNameSpaceID_XUL);
|
|
|
|
REGISTER_NAMESPACE(kSVGNameSpaceURI, kNameSpaceID_SVG);
|
|
|
|
REGISTER_NAMESPACE(kXMLEventsNameSpaceURI, kNameSpaceID_XMLEvents);
|
|
|
|
|
|
|
|
#undef REGISTER_NAMESPACE
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NameSpaceManagerImpl::RegisterNameSpace(const nsAString& aURI,
|
|
|
|
PRInt32& aNameSpaceID)
|
|
|
|
{
|
|
|
|
if (aURI.IsEmpty()) {
|
|
|
|
aNameSpaceID = kNameSpaceID_None; // xmlns="", see bug 75700 for details
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!mURIToIDTable.Get(&aURI, &aNameSpaceID)) {
|
2009-01-18 12:14:14 -08:00
|
|
|
aNameSpaceID = mURIArray.Length() + 1; // id is index + 1
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = AddNameSpace(aURI, aNameSpaceID);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aNameSpaceID = kNameSpaceID_Unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_POSTCONDITION(aNameSpaceID >= -1, "Bogus namespace ID");
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NameSpaceManagerImpl::GetNameSpaceURI(PRInt32 aNameSpaceID, nsAString& aURI)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNameSpaceID >= 0, "Bogus namespace ID");
|
|
|
|
|
|
|
|
PRInt32 index = aNameSpaceID - 1; // id is index + 1
|
2009-01-18 12:14:14 -08:00
|
|
|
if (index < 0 || index >= PRInt32(mURIArray.Length())) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aURI.Truncate();
|
|
|
|
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
2009-01-18 12:14:14 -08:00
|
|
|
aURI = *mURIArray.ElementAt(index);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
NameSpaceManagerImpl::GetNameSpaceID(const nsAString& aURI)
|
|
|
|
{
|
|
|
|
if (aURI.IsEmpty()) {
|
|
|
|
return kNameSpaceID_None; // xmlns="", see bug 75700 for details
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 nameSpaceID;
|
|
|
|
|
|
|
|
if (mURIToIDTable.Get(&aURI, &nameSpaceID)) {
|
|
|
|
NS_POSTCONDITION(nameSpaceID >= 0, "Bogus namespace ID");
|
|
|
|
return nameSpaceID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNameSpaceID_Unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewElement(nsIContent** aResult, PRInt32 aElementType,
|
2010-10-25 05:17:38 -07:00
|
|
|
already_AddRefed<nsINodeInfo> aNodeInfo, FromParser aFromParser)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aElementType == kNameSpaceID_XHTML) {
|
2008-04-17 15:30:51 -07:00
|
|
|
return NS_NewHTMLElement(aResult, aNodeInfo, aFromParser);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#ifdef MOZ_XUL
|
|
|
|
if (aElementType == kNameSpaceID_XUL) {
|
|
|
|
return NS_NewXULElement(aResult, aNodeInfo);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (aElementType == kNameSpaceID_MathML) {
|
|
|
|
return NS_NewMathMLElement(aResult, aNodeInfo);
|
|
|
|
}
|
2010-12-08 13:25:06 -08:00
|
|
|
if (aElementType == kNameSpaceID_SVG) {
|
2009-01-14 20:38:07 -08:00
|
|
|
return NS_NewSVGElement(aResult, aNodeInfo, aFromParser);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (aElementType == kNameSpaceID_XMLEvents) {
|
|
|
|
return NS_NewXMLEventsElement(aResult, aNodeInfo);
|
|
|
|
}
|
|
|
|
#ifdef MOZ_XTF
|
|
|
|
if (aElementType > kNameSpaceID_LastBuiltin) {
|
|
|
|
nsIXTFService* xtfService = nsContentUtils::GetXTFService();
|
|
|
|
NS_ASSERTION(xtfService, "could not get xtf service");
|
|
|
|
if (xtfService &&
|
|
|
|
NS_SUCCEEDED(xtfService->CreateElement(aResult, aNodeInfo)))
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return NS_NewXMLElement(aResult, aNodeInfo);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
NameSpaceManagerImpl::HasElementCreator(PRInt32 aNameSpaceID)
|
|
|
|
{
|
|
|
|
return aNameSpaceID == kNameSpaceID_XHTML ||
|
|
|
|
#ifdef MOZ_XUL
|
|
|
|
aNameSpaceID == kNameSpaceID_XUL ||
|
|
|
|
#endif
|
|
|
|
aNameSpaceID == kNameSpaceID_MathML ||
|
|
|
|
aNameSpaceID == kNameSpaceID_SVG ||
|
|
|
|
aNameSpaceID == kNameSpaceID_XMLEvents ||
|
|
|
|
PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NameSpaceManagerImpl::AddNameSpace(const nsAString& aURI,
|
|
|
|
const PRInt32 aNameSpaceID)
|
|
|
|
{
|
|
|
|
if (aNameSpaceID < 0) {
|
|
|
|
// We've wrapped... Can't do anything else here; just bail.
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2010-06-16 09:54:27 -07:00
|
|
|
NS_ASSERTION(aNameSpaceID - 1 == (PRInt32) mURIArray.Length(),
|
2007-03-22 10:30:00 -07:00
|
|
|
"BAD! AddNameSpace not called in right order!");
|
|
|
|
|
2009-01-18 12:14:14 -08:00
|
|
|
nsString* uri = new nsString(aURI);
|
|
|
|
if (!uri || !mURIArray.AppendElement(uri)) {
|
|
|
|
delete uri;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mURIToIDTable.Put(uri, aNameSpaceID)) {
|
2009-01-18 12:14:14 -08:00
|
|
|
mURIArray.RemoveElementAt(aNameSpaceID - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_GetNameSpaceManager(nsINameSpaceManager** aInstancePtrResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInstancePtrResult);
|
|
|
|
|
2007-06-19 22:23:50 -07:00
|
|
|
if (!sNameSpaceManager) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<NameSpaceManagerImpl> manager = new NameSpaceManagerImpl();
|
|
|
|
if (manager) {
|
|
|
|
nsresult rv = manager->Init();
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2007-06-19 22:23:50 -07:00
|
|
|
manager.swap(sNameSpaceManager);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-19 22:23:50 -07:00
|
|
|
*aInstancePtrResult = sNameSpaceManager;
|
|
|
|
NS_ENSURE_TRUE(sNameSpaceManager, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ADDREF(*aInstancePtrResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_NameSpaceManagerShutdown()
|
|
|
|
{
|
2007-06-19 22:23:50 -07:00
|
|
|
NS_IF_RELEASE(sNameSpaceManager);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|