2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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"
|
2013-05-01 15:50:08 -07:00
|
|
|
#include "nsINodeInfo.h"
|
2013-07-01 15:09:37 -07:00
|
|
|
#include "mozilla/dom/XBLChildrenElement.h"
|
2013-12-03 06:40:10 -08:00
|
|
|
#include "mozilla/dom/Element.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-12 15:53:18 -07:00
|
|
|
using namespace mozilla;
|
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"
|
|
|
|
|
|
|
|
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 {
|
2011-10-17 07:59:28 -07:00
|
|
|
ALLOW_MEMMOVE = true
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
const nsAString* mKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NameSpaceManagerImpl : public nsINameSpaceManager {
|
|
|
|
public:
|
2013-09-02 01:41:57 -07:00
|
|
|
NameSpaceManagerImpl()
|
|
|
|
: mURIToIDTable(32)
|
|
|
|
{
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual ~NameSpaceManagerImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult RegisterNameSpace(const nsAString& aURI, int32_t& aNameSpaceID);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult GetNameSpaceURI(int32_t aNameSpaceID, nsAString& aURI);
|
|
|
|
int32_t GetNameSpaceID(const nsAString& aURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool HasElementCreator(int32_t aNameSpaceID);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult AddNameSpace(const nsAString& aURI, const int32_t aNameSpaceID);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDataHashtable<nsNameSpaceKey,int32_t> mURIToIDTable;
|
2009-01-18 12:14:14 -08:00
|
|
|
nsTArray< nsAutoPtr<nsString> > mURIArray;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
static NameSpaceManagerImpl* sNameSpaceManager = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(NameSpaceManagerImpl, nsINameSpaceManager)
|
|
|
|
|
|
|
|
nsresult NameSpaceManagerImpl::Init()
|
|
|
|
{
|
2012-05-18 10:30:49 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
#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);
|
|
|
|
|
|
|
|
#undef REGISTER_NAMESPACE
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NameSpaceManagerImpl::RegisterNameSpace(const nsAString& aURI,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t& aNameSpaceID)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
NameSpaceManagerImpl::GetNameSpaceURI(int32_t aNameSpaceID, nsAString& aURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNameSpaceID >= 0, "Bogus namespace ID");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index = aNameSpaceID - 1; // id is index + 1
|
|
|
|
if (index < 0 || index >= int32_t(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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
NameSpaceManagerImpl::GetNameSpaceID(const nsAString& aURI)
|
|
|
|
{
|
|
|
|
if (aURI.IsEmpty()) {
|
|
|
|
return kNameSpaceID_None; // xmlns="", see bug 75700 for details
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nameSpaceID;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mURIToIDTable.Get(&aURI, &nameSpaceID)) {
|
|
|
|
NS_POSTCONDITION(nameSpaceID >= 0, "Bogus namespace ID");
|
|
|
|
return nameSpaceID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNameSpaceID_Unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-11-15 23:50:19 -08:00
|
|
|
NS_NewElement(nsIContent** aResult,
|
2010-10-25 05:17:38 -07:00
|
|
|
already_AddRefed<nsINodeInfo> aNodeInfo, FromParser aFromParser)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t ns = aNodeInfo.get()->NamespaceID();
|
2011-11-15 23:50:19 -08:00
|
|
|
if (ns == kNameSpaceID_XHTML) {
|
2013-12-03 06:40:10 -08:00
|
|
|
nsCOMPtr<Element> el;
|
|
|
|
nsresult rv = NS_NewHTMLElement(getter_AddRefs(el), aNodeInfo, aFromParser);
|
|
|
|
el.forget(aResult);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#ifdef MOZ_XUL
|
2011-11-15 23:50:19 -08:00
|
|
|
if (ns == kNameSpaceID_XUL) {
|
2013-12-03 06:40:11 -08:00
|
|
|
nsCOMPtr<Element> el;
|
|
|
|
nsresult rv = NS_NewXULElement(getter_AddRefs(el), aNodeInfo);
|
|
|
|
el.forget(aResult);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
2011-11-15 23:50:19 -08:00
|
|
|
if (ns == kNameSpaceID_MathML) {
|
2013-12-03 06:40:11 -08:00
|
|
|
nsCOMPtr<Element> el;
|
|
|
|
nsresult rv = NS_NewMathMLElement(getter_AddRefs(el), aNodeInfo);
|
|
|
|
el.forget(aResult);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-11-15 23:50:19 -08:00
|
|
|
if (ns == kNameSpaceID_SVG) {
|
2013-12-03 06:40:11 -08:00
|
|
|
nsCOMPtr<Element> el;
|
|
|
|
nsresult rv = NS_NewSVGElement(getter_AddRefs(el), aNodeInfo, aFromParser);
|
|
|
|
el.forget(aResult);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-05-01 15:50:08 -07:00
|
|
|
if (ns == kNameSpaceID_XBL && aNodeInfo.get()->Equals(nsGkAtoms::children)) {
|
2013-07-01 15:09:37 -07:00
|
|
|
NS_ADDREF(*aResult = new XBLChildrenElement(aNodeInfo));
|
2013-05-01 15:50:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-12-03 06:40:11 -08:00
|
|
|
|
|
|
|
nsCOMPtr<Element> el;
|
|
|
|
nsresult rv = NS_NewXMLElement(getter_AddRefs(el), aNodeInfo);
|
|
|
|
el.forget(aResult);
|
|
|
|
return el;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
NameSpaceManagerImpl::HasElementCreator(int32_t aNameSpaceID)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return aNameSpaceID == kNameSpaceID_XHTML ||
|
|
|
|
#ifdef MOZ_XUL
|
|
|
|
aNameSpaceID == kNameSpaceID_XUL ||
|
|
|
|
#endif
|
|
|
|
aNameSpaceID == kNameSpaceID_MathML ||
|
|
|
|
aNameSpaceID == kNameSpaceID_SVG ||
|
2011-10-17 07:59:28 -07:00
|
|
|
false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NameSpaceManagerImpl::AddNameSpace(const nsAString& aURI,
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t aNameSpaceID)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aNameSpaceID < 0) {
|
|
|
|
// We've wrapped... Can't do anything else here; just bail.
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_ASSERTION(aNameSpaceID - 1 == (int32_t) 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;
|
|
|
|
}
|
|
|
|
|
2012-05-18 10:30:49 -07:00
|
|
|
mURIToIDTable.Put(uri, aNameSpaceID);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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
|
|
|
}
|