Implemented several new DOM methods, including document.createElement() and document.createTextNode(). Fixed some bugs in the XUL content model builder. Code re-organization and cleanup.

This commit is contained in:
waterson%netscape.com 1999-04-16 08:38:17 +00:00
Родитель c0bc14da61
Коммит 378debf61b
15 изменённых файлов: 1873 добавлений и 1214 удалений

Просмотреть файл

@ -539,20 +539,22 @@ RDFElementImpl::GetParentNode(nsIDOMNode** aParentNode)
return mParent->QueryInterface(kIDOMNodeIID, (void**) aParentNode); return mParent->QueryInterface(kIDOMNodeIID, (void**) aParentNode);
} }
else if (mDocument) { else if (mDocument) {
// If we don't have a parent, but we're in the document, we must // XXX This is a mess because of our fun multiple inheritance heirarchy
// be the root node of the document. The DOM says that the root nsCOMPtr<nsIContent> root( dont_QueryInterface(mDocument->GetRootContent()) );
// is the document. nsCOMPtr<nsIContent> thisIContent;
return mDocument->QueryInterface(kIDOMNodeIID, (void**)aParentNode); QueryInterface(kIContentIID, getter_AddRefs(thisIContent));
}
else {
// A standalone element (i.e. one without a parent or a document)
// implicitly has a document fragment as its parent according to
// the DOM.
// XXX create a doc fragment here as a pseudo-parent. if (root == thisIContent) {
NS_NOTYETIMPLEMENTED("can't handle standalone RDF elements"); // If we don't have a parent, and we're the root content
return NS_ERROR_NOT_IMPLEMENTED; // of the document, DOM says that our parent is the
// document.
return mDocument->QueryInterface(kIDOMNodeIID, (void**)aParentNode);
}
} }
// A standalone element (i.e. one without a parent or a document)
*aParentNode = nsnull;
return NS_OK;
} }
@ -562,35 +564,35 @@ RDFElementImpl::GetChildNodes(nsIDOMNodeList** aChildNodes)
nsresult rv; nsresult rv;
nsRDFDOMNodeList* children; nsRDFDOMNodeList* children;
if (NS_FAILED(rv = nsRDFDOMNodeList::Create(&children))) { rv = nsRDFDOMNodeList::Create(&children);
NS_ERROR("unable to create DOM node list"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create DOM node list");
return rv; if (NS_FAILED(rv)) return rv;
}
PRInt32 count; PRInt32 count;
if (NS_SUCCEEDED(rv = ChildCount(count))) { if (NS_SUCCEEDED(rv = ChildCount(count))) {
for (PRInt32 index = 0; index < count; ++index) { for (PRInt32 index = 0; index < count; ++index) {
nsCOMPtr<nsIContent> child; nsCOMPtr<nsIContent> child;
if (NS_FAILED(rv = ChildAt(index, *getter_AddRefs(child)))) { rv = ChildAt(index, *getter_AddRefs(child));
NS_ERROR("unable to get child"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get child");
if (NS_FAILED(rv))
break; break;
}
nsCOMPtr<nsIDOMNode> domNode; nsCOMPtr<nsIDOMNode> domNode;
if (NS_FAILED(rv = child->QueryInterface(kIDOMNodeIID, (void**) getter_AddRefs(domNode)))) { rv = child->QueryInterface(kIDOMNodeIID, (void**) getter_AddRefs(domNode));
if (NS_FAILED(rv)) {
NS_WARNING("child content doesn't support nsIDOMNode"); NS_WARNING("child content doesn't support nsIDOMNode");
continue; continue;
} }
if (NS_FAILED(rv = children->AppendNode(domNode))) { rv = children->AppendNode(domNode);
NS_ERROR("unable to append node to list"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to append node to list");
if (NS_FAILED(rv))
break; break;
}
} }
} }
// Create() addref'd for us
*aChildNodes = children; *aChildNodes = children;
NS_ADDREF(*aChildNodes);
return NS_OK; return NS_OK;
} }
@ -1018,15 +1020,41 @@ RDFElementImpl::SetContainingNameSpace(nsINameSpace* aNameSpace)
NS_IMETHODIMP NS_IMETHODIMP
RDFElementImpl::GetContainingNameSpace(nsINameSpace*& aNameSpace) const RDFElementImpl::GetContainingNameSpace(nsINameSpace*& aNameSpace) const
{ {
nsresult rv;
if (mNameSpace) { if (mNameSpace) {
// If we have a namespace, return it.
NS_ADDREF(mNameSpace); NS_ADDREF(mNameSpace);
aNameSpace = mNameSpace; aNameSpace = mNameSpace;
return NS_OK; return NS_OK;
} }
else if (mParent) {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(mParent) ); // Next, try our parent.
nsCOMPtr<nsIContent> parent( dont_QueryInterface(mParent) );
while (parent) {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(parent) );
if (xml) if (xml)
return xml->GetContainingNameSpace(aNameSpace); return xml->GetContainingNameSpace(aNameSpace);
nsCOMPtr<nsIContent> temp = parent;
rv = temp->GetParent(*getter_AddRefs(parent));
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get parent");
if (NS_FAILED(rv)) return rv;
}
// Allright, we walked all the way to the top of our containment
// hierarchy and couldn't find a parent that supported
// nsIXMLContent. If we're in a document, try to doc's root
// element.
if (mDocument) {
nsCOMPtr<nsIContent> docroot
= dont_QueryInterface( mDocument->GetRootContent() );
if (docroot) {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(docroot) );
if (xml)
return xml->GetContainingNameSpace(aNameSpace);
}
} }
aNameSpace = nsnull; aNameSpace = nsnull;
@ -1616,23 +1644,8 @@ static char kNameSpaceSeparator[] = ":";
name.Cut(0, nsoffset+1); name.Cut(0, nsoffset+1);
} }
// Figure out the namespace ID // Figure out the namespace ID, defaulting to none if there is no
// namespace prefix.
// XXX This is currently broken, because _nobody_ will ever set
// the namespace scope via the nsIXMLElement interface. To make
// that work will require a bit more machinery: something that
// remembers the XML namespace scoping as nodes get created in the
// RDF graph, and can then extract them later when content nodes
// are built via the content model builders.
//
// Since mNameSpace will _always_ be null, specifying
// kNameSpaceID_None allows us to at least match on the
// tag. You'll start seeing problems when the same name is used in
// different namespaces.
//
// See http://bugzilla.mozilla.org/show_bug.cgi?id=3275 and 3334
// for more info.
aNameSpaceID = kNameSpaceID_None; aNameSpaceID = kNameSpaceID_None;
if (0 < prefix.Length()) { if (0 < prefix.Length()) {
nsCOMPtr<nsIAtom> nameSpaceAtom( getter_AddRefs(NS_NewAtom(prefix)) ); nsCOMPtr<nsIAtom> nameSpaceAtom( getter_AddRefs(NS_NewAtom(prefix)) );
@ -1658,9 +1671,6 @@ NS_IMETHODIMP
RDFElementImpl::GetNameSpacePrefixFromId(PRInt32 aNameSpaceID, RDFElementImpl::GetNameSpacePrefixFromId(PRInt32 aNameSpaceID,
nsIAtom*& aPrefix) nsIAtom*& aPrefix)
{ {
// XXX mNameSpace will _always_ be null, so this really depends on
// fixing Bugs 3275 & 3334, resolving how to map scoped namespace
// prefixes back and forth from the graph.
nsresult rv; nsresult rv;
nsCOMPtr<nsINameSpace> ns; nsCOMPtr<nsINameSpace> ns;
@ -2425,7 +2435,7 @@ RDFElementImpl::EnsureContentsGenerated(void) const
(void**) getter_AddRefs(rdfDoc)))) (void**) getter_AddRefs(rdfDoc))))
return rv; return rv;
rv = rdfDoc->CreateContents((nsIStyledContent*) unconstThis); rv = rdfDoc->CreateContents(NS_STATIC_CAST(nsIStyledContent*, unconstThis));
NS_ASSERTION(NS_SUCCEEDED(rv), "problem creating kids"); NS_ASSERTION(NS_SUCCEEDED(rv), "problem creating kids");
return rv; return rv;
} }

Просмотреть файл

@ -29,23 +29,29 @@
*/ */
// Note the ALPHABETICAL ORDERING
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsDOMCID.h"
#include "nsIArena.h" #include "nsIArena.h"
#include "nsIContent.h"
#include "nsICSSParser.h" #include "nsICSSParser.h"
#include "nsICSSStyleSheet.h" #include "nsICSSStyleSheet.h"
#include "nsIContent.h"
#include "nsIDOMElementObserver.h" #include "nsIDOMElementObserver.h"
#include "nsIDOMNodeObserver.h" #include "nsIDOMNodeObserver.h"
#include "nsIDOMScriptObjectFactory.h" #include "nsIDOMScriptObjectFactory.h"
#include "nsIDOMSelection.h"
#include "nsIDOMStyleSheetCollection.h" #include "nsIDOMStyleSheetCollection.h"
#include "nsIDOMText.h"
#include "nsIDOMXULDocument.h" #include "nsIDOMXULDocument.h"
#include "nsIDOMXULElement.h"
#include "nsIDTD.h" #include "nsIDTD.h"
#include "nsIDocument.h" #include "nsIDocument.h"
#include "nsIDocumentObserver.h" #include "nsIDocumentObserver.h"
#include "nsIHTMLContentContainer.h"
#include "nsIHTMLCSSStyleSheet.h" #include "nsIHTMLCSSStyleSheet.h"
#include "nsIHTMLContentContainer.h"
#include "nsIHTMLStyleSheet.h" #include "nsIHTMLStyleSheet.h"
#include "nsIJSScriptObject.h" #include "nsIJSScriptObject.h"
#include "nsINameSpace.h"
#include "nsINameSpaceManager.h" #include "nsINameSpaceManager.h"
#include "nsIParser.h" #include "nsIParser.h"
#include "nsIPresContext.h" #include "nsIPresContext.h"
@ -60,32 +66,32 @@
#include "nsIScriptContextOwner.h" #include "nsIScriptContextOwner.h"
#include "nsIScriptGlobalObject.h" #include "nsIScriptGlobalObject.h"
#include "nsIScriptObjectOwner.h" #include "nsIScriptObjectOwner.h"
#include "nsIDOMSelection.h"
#include "nsIServiceManager.h" #include "nsIServiceManager.h"
#include "nsIStreamListener.h" #include "nsIStreamListener.h"
#include "nsIStyleContext.h"
#include "nsIStyleSet.h" #include "nsIStyleSet.h"
#include "nsIStyleSheet.h" #include "nsIStyleSheet.h"
#include "nsISupportsArray.h" #include "nsISupportsArray.h"
#include "nsITextContent.h"
#include "nsIURL.h" #include "nsIURL.h"
#include "nsIURLGroup.h" #include "nsIURLGroup.h"
#include "nsIWebShell.h" #include "nsIWebShell.h"
#include "nsIStyleContext.h"
#include "nsIXULContentSink.h"
#include "nsIDOMXULElement.h"
#include "nsIXULParentDocument.h"
#include "nsIXULChildDocument.h"
#include "nsIXMLContent.h" #include "nsIXMLContent.h"
#include "nsDOMCID.h" #include "nsIXULChildDocument.h"
#include "nsIXULContentSink.h"
#include "nsIXULParentDocument.h"
#include "nsLayoutCID.h" #include "nsLayoutCID.h"
#include "nsParserCIID.h" #include "nsParserCIID.h"
#include "nsRDFCID.h" #include "nsRDFCID.h"
#include "nsRDFContentUtils.h"
#include "nsRDFDOMNodeList.h" #include "nsRDFDOMNodeList.h"
#include "nsVoidArray.h" #include "nsVoidArray.h"
#include "nsXPIDLString.h" // XXX should go away #include "nsXPIDLString.h" // XXX should go away
#include "plhash.h" #include "plhash.h"
#include "plstr.h" #include "plstr.h"
#include "rdfutil.h"
#include "prlog.h" #include "prlog.h"
#include "rdfutil.h"
#include "rdf.h"
#include "nsILineBreakerFactory.h" #include "nsILineBreakerFactory.h"
#include "nsIWordBreakerFactory.h" #include "nsIWordBreakerFactory.h"
@ -93,57 +99,66 @@
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
static NS_DEFINE_IID(kICSSParserIID, NS_ICSS_PARSER_IID); // XXX grr.. static NS_DEFINE_IID(kICSSParserIID, NS_ICSS_PARSER_IID); // XXX grr..
static NS_DEFINE_IID(kIContentIID, NS_ICONTENT_IID); static NS_DEFINE_IID(kIContentIID, NS_ICONTENT_IID);
static NS_DEFINE_IID(kIDTDIID, NS_IDTD_IID); static NS_DEFINE_IID(kIDTDIID, NS_IDTD_IID);
static NS_DEFINE_IID(kIDOMScriptObjectFactoryIID, NS_IDOM_SCRIPT_OBJECT_FACTORY_IID); static NS_DEFINE_IID(kIDOMScriptObjectFactoryIID, NS_IDOM_SCRIPT_OBJECT_FACTORY_IID);
static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID); static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID);
static NS_DEFINE_IID(kIHTMLContentContainerIID, NS_IHTMLCONTENTCONTAINER_IID); static NS_DEFINE_IID(kIHTMLContentContainerIID, NS_IHTMLCONTENTCONTAINER_IID);
static NS_DEFINE_IID(kIHTMLStyleSheetIID, NS_IHTML_STYLE_SHEET_IID); static NS_DEFINE_IID(kIHTMLStyleSheetIID, NS_IHTML_STYLE_SHEET_IID);
static NS_DEFINE_IID(kIHTMLCSSStyleSheetIID, NS_IHTML_CSS_STYLE_SHEET_IID); static NS_DEFINE_IID(kIHTMLCSSStyleSheetIID, NS_IHTML_CSS_STYLE_SHEET_IID);
static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID); static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID);
static NS_DEFINE_IID(kINameSpaceManagerIID, NS_INAMESPACEMANAGER_IID); static NS_DEFINE_IID(kINameSpaceManagerIID, NS_INAMESPACEMANAGER_IID);
static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID); static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID);
static NS_DEFINE_IID(kIPresShellIID, NS_IPRESSHELL_IID); static NS_DEFINE_IID(kIPresShellIID, NS_IPRESSHELL_IID);
static NS_DEFINE_IID(kIRDFCompositeDataSourceIID, NS_IRDFCOMPOSITEDATASOURCE_IID); static NS_DEFINE_IID(kIRDFCompositeDataSourceIID, NS_IRDFCOMPOSITEDATASOURCE_IID);
static NS_DEFINE_IID(kIRDFContentModelBuilderIID, NS_IRDFCONTENTMODELBUILDER_IID); static NS_DEFINE_IID(kIRDFContentModelBuilderIID, NS_IRDFCONTENTMODELBUILDER_IID);
static NS_DEFINE_IID(kIRDFDataSourceIID, NS_IRDFDATASOURCE_IID); static NS_DEFINE_IID(kIRDFDataSourceIID, NS_IRDFDATASOURCE_IID);
static NS_DEFINE_IID(kIRDFDocumentIID, NS_IRDFDOCUMENT_IID); static NS_DEFINE_IID(kIRDFDocumentIID, NS_IRDFDOCUMENT_IID);
static NS_DEFINE_IID(kIRDFLiteralIID, NS_IRDFLITERAL_IID); static NS_DEFINE_IID(kIRDFLiteralIID, NS_IRDFLITERAL_IID);
static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID); static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID);
static NS_DEFINE_IID(kIRDFServiceIID, NS_IRDFSERVICE_IID); static NS_DEFINE_IID(kIRDFServiceIID, NS_IRDFSERVICE_IID);
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
static NS_DEFINE_IID(kIDOMSelectionIID, NS_IDOMSELECTION_IID); static NS_DEFINE_IID(kIDOMSelectionIID, NS_IDOMSELECTION_IID);
static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID); static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
static NS_DEFINE_IID(kIStreamObserverIID, NS_ISTREAMOBSERVER_IID); static NS_DEFINE_IID(kIStreamObserverIID, NS_ISTREAMOBSERVER_IID);
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIWebShellIID, NS_IWEB_SHELL_IID); static NS_DEFINE_IID(kIWebShellIID, NS_IWEB_SHELL_IID);
static NS_DEFINE_IID(kIXMLDocumentIID, NS_IXMLDOCUMENT_IID); static NS_DEFINE_IID(kIXMLDocumentIID, NS_IXMLDOCUMENT_IID);
static NS_DEFINE_IID(kIXULContentSinkIID, NS_IXULCONTENTSINK_IID); static NS_DEFINE_IID(kIXULContentSinkIID, NS_IXULCONTENTSINK_IID);
static NS_DEFINE_CID(kCSSParserCID, NS_CSSPARSER_CID); static NS_DEFINE_CID(kCSSParserCID, NS_CSSPARSER_CID);
static NS_DEFINE_CID(kDOMScriptObjectFactoryCID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID); static NS_DEFINE_CID(kDOMScriptObjectFactoryCID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID);
static NS_DEFINE_CID(kHTMLStyleSheetCID, NS_HTMLSTYLESHEET_CID); static NS_DEFINE_CID(kHTMLStyleSheetCID, NS_HTMLSTYLESHEET_CID);
static NS_DEFINE_CID(kHTMLCSSStyleSheetCID, NS_HTML_CSS_STYLESHEET_CID); static NS_DEFINE_CID(kHTMLCSSStyleSheetCID, NS_HTML_CSS_STYLESHEET_CID);
static NS_DEFINE_CID(kNameSpaceManagerCID, NS_NAMESPACEMANAGER_CID); static NS_DEFINE_CID(kNameSpaceManagerCID, NS_NAMESPACEMANAGER_CID);
static NS_DEFINE_CID(kParserCID, NS_PARSER_IID); // XXX static NS_DEFINE_CID(kParserCID, NS_PARSER_IID); // XXX
static NS_DEFINE_CID(kPresShellCID, NS_PRESSHELL_CID); static NS_DEFINE_CID(kPresShellCID, NS_PRESSHELL_CID);
static NS_DEFINE_CID(kRDFCompositeDataSourceCID, NS_RDFCOMPOSITEDATASOURCE_CID); static NS_DEFINE_CID(kRDFCompositeDataSourceCID, NS_RDFCOMPOSITEDATASOURCE_CID);
static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID); static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID);
static NS_DEFINE_CID(kLocalStoreCID, NS_LOCALSTORE_CID); static NS_DEFINE_CID(kLocalStoreCID, NS_LOCALSTORE_CID);
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID); static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
static NS_DEFINE_CID(kRDFXULBuilderCID, NS_RDFXULBUILDER_CID); static NS_DEFINE_CID(kRDFXULBuilderCID, NS_RDFXULBUILDER_CID);
static NS_DEFINE_CID(kRangeListCID, NS_RANGELIST_CID); static NS_DEFINE_CID(kRangeListCID, NS_RANGELIST_CID);
static NS_DEFINE_CID(kWellFormedDTDCID, NS_WELLFORMEDDTD_CID); static NS_DEFINE_CID(kTextNodeCID, NS_TEXTNODE_CID);
static NS_DEFINE_CID(kXULContentSinkCID, NS_XULCONTENTSINK_CID); static NS_DEFINE_CID(kWellFormedDTDCID, NS_WELLFORMEDDTD_CID);
static NS_DEFINE_CID(kXULDataSourceCID, NS_XULDATASOURCE_CID); static NS_DEFINE_CID(kXULContentSinkCID, NS_XULCONTENTSINK_CID);
static NS_DEFINE_CID(kXULDataSourceCID, NS_XULDATASOURCE_CID);
static NS_DEFINE_IID(kLWBrkCID, NS_LWBRK_CID); static NS_DEFINE_IID(kLWBrkCID, NS_LWBRK_CID);
static NS_DEFINE_IID(kILineBreakerFactoryIID, NS_ILINEBREAKERFACTORY_IID); static NS_DEFINE_IID(kILineBreakerFactoryIID, NS_ILINEBREAKERFACTORY_IID);
static NS_DEFINE_IID(kIWordBreakerFactoryIID, NS_IWORDBREAKERFACTORY_IID); static NS_DEFINE_IID(kIWordBreakerFactoryIID, NS_IWORDBREAKERFACTORY_IID);
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Standard vocabulary items
DEFINE_RDF_VOCAB(RDF_NAMESPACE_URI, RDF, instanceOf);
DEFINE_RDF_VOCAB(RDF_NAMESPACE_URI, RDF, type);
#define XUL_NAMESPACE_URI "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
#define XUL_NAMESPACE_URI_PREFIX XUL_NAMESPACE_URI "#"
DEFINE_RDF_VOCAB(XUL_NAMESPACE_URI_PREFIX, XUL, element);
static PRLogModuleInfo* gMapLog; static PRLogModuleInfo* gMapLog;
@ -592,12 +607,23 @@ public:
const nsString& aValue, const nsString& aValue,
nsRDFDOMNodeList* aElements); nsRDFDOMNodeList* aElements);
nsresult
ParseTagString(const nsString& aTagName, nsIAtom*& aName, PRInt32& aNameSpaceID);
nsresult
MakeProperty(PRInt32 aNameSpaceID, nsIAtom* aTag, nsIRDFResource** aResult);
protected: protected:
// pseudo constants // pseudo constants
static PRInt32 gRefCnt; static PRInt32 gRefCnt;
static nsIAtom* kIdAtom; static nsIAtom* kIdAtom;
static nsIAtom* kObservesAtom; static nsIAtom* kObservesAtom;
static nsIRDFService* gRDFService;
static nsIRDFResource* kRDF_instanceOf;
static nsIRDFResource* kRDF_type;
static nsIRDFResource* kXUL_element;
nsIContent* nsIContent*
FindContent(const nsIContent* aStartNode, FindContent(const nsIContent* aStartNode,
const nsIContent* aTest1, const nsIContent* aTest1,
@ -627,7 +653,6 @@ protected:
nsINameSpaceManager* mNameSpaceManager; nsINameSpaceManager* mNameSpaceManager;
nsIHTMLStyleSheet* mAttrStyleSheet; nsIHTMLStyleSheet* mAttrStyleSheet;
nsIHTMLCSSStyleSheet* mInlineStyleSheet; nsIHTMLCSSStyleSheet* mInlineStyleSheet;
nsIRDFService* mRDFService;
nsElementMap mResources; nsElementMap mResources;
nsISupportsArray* mBuilders; nsISupportsArray* mBuilders;
nsIRDFContentModelBuilder* mXULBuilder; nsIRDFContentModelBuilder* mXULBuilder;
@ -641,10 +666,15 @@ protected:
nsVoidArray mSubDocuments; nsVoidArray mSubDocuments;
}; };
PRInt32 XULDocumentImpl::gRefCnt; PRInt32 XULDocumentImpl::gRefCnt = 0;
nsIAtom* XULDocumentImpl::kIdAtom; nsIAtom* XULDocumentImpl::kIdAtom;
nsIAtom* XULDocumentImpl::kObservesAtom; nsIAtom* XULDocumentImpl::kObservesAtom;
nsIRDFService* XULDocumentImpl::gRDFService;
nsIRDFResource* XULDocumentImpl::kRDF_instanceOf;
nsIRDFResource* XULDocumentImpl::kRDF_type;
nsIRDFResource* XULDocumentImpl::kXUL_element;
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// ctors & dtors // ctors & dtors
@ -660,7 +690,6 @@ XULDocumentImpl::XULDocumentImpl(void)
mDisplaySelection(PR_FALSE), mDisplaySelection(PR_FALSE),
mNameSpaceManager(nsnull), mNameSpaceManager(nsnull),
mAttrStyleSheet(nsnull), mAttrStyleSheet(nsnull),
mRDFService(nsnull),
mBuilders(nsnull), mBuilders(nsnull),
mXULBuilder(nsnull), mXULBuilder(nsnull),
mLocalDataSource(nsnull), mLocalDataSource(nsnull),
@ -686,6 +715,20 @@ XULDocumentImpl::XULDocumentImpl(void)
if (gRefCnt++ == 0) { if (gRefCnt++ == 0) {
kIdAtom = NS_NewAtom("id"); kIdAtom = NS_NewAtom("id");
kObservesAtom = NS_NewAtom("observes"); kObservesAtom = NS_NewAtom("observes");
// Keep the RDF service cached in a member variable to make using
// it a bit less painful
rv = nsServiceManager::GetService(kRDFServiceCID,
kIRDFServiceIID,
(nsISupports**) &gRDFService);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get RDF Service");
if (gRDFService) {
gRDFService->GetResource(kURIRDF_instanceOf, &kRDF_instanceOf);
gRDFService->GetResource(kURIRDF_type, &kRDF_type);
gRDFService->GetResource(kURIXUL_element, &kXUL_element);
}
} }
#ifdef PR_LOGGING #ifdef PR_LOGGING
@ -699,11 +742,6 @@ XULDocumentImpl::~XULDocumentImpl()
NS_IF_RELEASE(mDocumentDataSource); NS_IF_RELEASE(mDocumentDataSource);
NS_IF_RELEASE(mLocalDataSource); NS_IF_RELEASE(mLocalDataSource);
if (mRDFService) {
nsServiceManager::ReleaseService(kRDFServiceCID, mRDFService);
mRDFService = nsnull;
}
// mParentDocument is never refcounted // mParentDocument is never refcounted
// Delete references to sub-documents // Delete references to sub-documents
PRInt32 index = mSubDocuments.Count(); PRInt32 index = mSubDocuments.Count();
@ -756,6 +794,15 @@ XULDocumentImpl::~XULDocumentImpl()
if (--gRefCnt == 0) { if (--gRefCnt == 0) {
NS_IF_RELEASE(kIdAtom); NS_IF_RELEASE(kIdAtom);
NS_IF_RELEASE(kObservesAtom); NS_IF_RELEASE(kObservesAtom);
if (gRDFService) {
nsServiceManager::ReleaseService(kRDFServiceCID, gRDFService);
gRDFService = nsnull;
}
NS_IF_RELEASE(kRDF_instanceOf);
NS_IF_RELEASE(kRDF_type);
NS_IF_RELEASE(kXUL_element);
} }
} }
@ -972,7 +1019,7 @@ XULDocumentImpl::StartDocumentLoad(nsIURL *aURL,
// XXX This needs to be cloned across windows, and the final // XXX This needs to be cloned across windows, and the final
// instance needs to be flushed to disk. It may be that this is // instance needs to be flushed to disk. It may be that this is
// really an RDFXML data source... // really an RDFXML data source...
rv = mRDFService->GetDataSource("rdf:local-store", &mLocalDataSource); rv = gRDFService->GetDataSource("rdf:local-store", &mLocalDataSource);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
NS_ERROR("couldn't create local data source"); NS_ERROR("couldn't create local data source");
@ -2230,8 +2277,77 @@ XULDocumentImpl::GetDocumentElement(nsIDOMElement** aDocumentElement)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::CreateElement(const nsString& aTagName, nsIDOMElement** aReturn) XULDocumentImpl::CreateElement(const nsString& aTagName, nsIDOMElement** aReturn)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aReturn != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aReturn)
return NS_ERROR_NULL_POINTER;
// we need this so that we can create a resource URI
NS_PRECONDITION(mDocumentURL != nsnull, "not initialized");
if (! mDocumentURL)
return NS_ERROR_NOT_INITIALIZED;
nsresult rv;
nsCOMPtr<nsIAtom> name;
PRInt32 nameSpaceID;
// parse the user-provided string into a tag name and a namespace ID
rv = ParseTagString(aTagName, *getter_AddRefs(name), nameSpaceID);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to parse tag name");
if (NS_FAILED(rv)) return rv;
// construct an element
nsCOMPtr<nsIContent> result;
rv = NS_NewRDFElement(nameSpaceID, name, getter_AddRefs(result));
if (NS_FAILED(rv)) return rv;
// assign it an "anonymous" identifier so that it can be referred
// to in the graph.
nsCOMPtr<nsIRDFResource> resource;
const char* context;
rv = mDocumentURL->GetSpec(&context);
if (NS_FAILED(rv)) return rv;
rv = rdf_CreateAnonymousResource(context, getter_AddRefs(resource));
if (NS_FAILED(rv)) return rv;
nsXPIDLCString uri;
rv = resource->GetValue(getter_Copies(uri));
if (NS_FAILED(rv)) return rv;
rv = result->SetAttribute(kNameSpaceID_None, kIdAtom, (const char*) uri, PR_FALSE);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set element's ID");
if (NS_FAILED(rv)) return rv;
// Set it's RDF:type in the graph s.t. the element's tag can be
// constructed from it.
nsCOMPtr<nsIRDFResource> type;
rv = MakeProperty(nameSpaceID, name, getter_AddRefs(type));
if (NS_FAILED(rv)) return rv;
rv = mDocumentDataSource->Assert(resource, kRDF_type, type, PR_TRUE);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set element's tag info in graph");
if (NS_FAILED(rv)) return rv;
// Mark it as a XUL element
rv = mDocumentDataSource->Assert(resource, kRDF_instanceOf, kXUL_element, PR_TRUE);
NS_ASSERTION(rv == NS_OK, "unable to mark as XUL element");
if (NS_FAILED(rv)) return rv;
rv = rdf_MakeSeq(mDocumentDataSource, resource);
NS_ASSERTION(rv == NS_OK, "unable to mark as XUL element");
if (NS_FAILED(rv)) return rv;
// `this' will be its document
rv = result->SetDocument(this, PR_FALSE);
if (NS_FAILED(rv)) return rv;
// get the DOM interface
rv = result->QueryInterface(nsIDOMElement::GetIID(), (void**) aReturn);
NS_ASSERTION(NS_SUCCEEDED(rv), "not a DOM element");
if (NS_FAILED(rv)) return rv;
return NS_OK;
} }
@ -2246,8 +2362,24 @@ XULDocumentImpl::CreateDocumentFragment(nsIDOMDocumentFragment** aReturn)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::CreateTextNode(const nsString& aData, nsIDOMText** aReturn) XULDocumentImpl::CreateTextNode(const nsString& aData, nsIDOMText** aReturn)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aReturn != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aReturn)
return NS_ERROR_NULL_POINTER;
nsresult rv;
nsCOMPtr<nsITextContent> text;
rv = nsComponentManager::CreateInstance(kTextNodeCID, nsnull, nsITextContent::GetIID(), getter_AddRefs(text));
if (NS_FAILED(rv)) return rv;
rv = text->SetText(aData.GetUnicode(), aData.Length(), PR_FALSE);
if (NS_FAILED(rv)) return rv;
rv = text->QueryInterface(nsIDOMText::GetIID(), (void**) aReturn);
NS_ASSERTION(NS_SUCCEEDED(rv), "not a DOMText");
if (NS_FAILED(rv)) return rv;
return NS_OK;
} }
@ -2383,7 +2515,7 @@ XULDocumentImpl::GetElementById(const nsString& aId, nsIDOMElement** aReturn)
rdf_PossiblyMakeAbsolute(documentURL, uri); rdf_PossiblyMakeAbsolute(documentURL, uri);
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = mRDFService->GetUnicodeResource(uri, getter_AddRefs(resource)))) { if (NS_FAILED(rv = gRDFService->GetUnicodeResource(uri, getter_AddRefs(resource)))) {
NS_ERROR("unable to get resource"); NS_ERROR("unable to get resource");
return rv; return rv;
} }
@ -2514,32 +2646,31 @@ XULDocumentImpl::GetFragmentRoot(nsIRDFResource** aFragmentRoot)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetNodeName(nsString& aNodeName) XULDocumentImpl::GetNodeName(nsString& aNodeName)
{ {
NS_NOTYETIMPLEMENTED("write me!"); aNodeName.SetString("#document");
return NS_ERROR_NOT_IMPLEMENTED; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetNodeValue(nsString& aNodeValue) XULDocumentImpl::GetNodeValue(nsString& aNodeValue)
{ {
NS_NOTYETIMPLEMENTED("write me!"); aNodeValue.Truncate();
return NS_ERROR_NOT_IMPLEMENTED; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::SetNodeValue(const nsString& aNodeValue) XULDocumentImpl::SetNodeValue(const nsString& aNodeValue)
{ {
NS_NOTYETIMPLEMENTED("write me!"); return NS_OK;
return NS_ERROR_NOT_IMPLEMENTED;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetNodeType(PRUint16* aNodeType) XULDocumentImpl::GetNodeType(PRUint16* aNodeType)
{ {
NS_NOTYETIMPLEMENTED("write me!"); *aNodeType = nsIDOMNode::DOCUMENT_NODE;
return NS_ERROR_NOT_IMPLEMENTED; return NS_OK;
} }
@ -2554,64 +2685,139 @@ XULDocumentImpl::GetParentNode(nsIDOMNode** aParentNode)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetChildNodes(nsIDOMNodeList** aChildNodes) XULDocumentImpl::GetChildNodes(nsIDOMNodeList** aChildNodes)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aChildNodes != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aChildNodes)
return NS_ERROR_NULL_POINTER;
if (mRootContent) {
nsresult rv;
*aChildNodes = nsnull;
nsRDFDOMNodeList* children;
rv = nsRDFDOMNodeList::Create(&children);
if (NS_SUCCEEDED(rv)) {
nsIDOMNode* domNode;
rv = mRootContent->QueryInterface(nsIDOMNode::GetIID(), (void**) domNode);
NS_ASSERTION(NS_SUCCEEDED(rv), "root content is not a DOM node");
if (NS_SUCCEEDED(rv)) {
rv = children->AppendNode(domNode);
NS_RELEASE(domNode);
*aChildNodes = children;
return NS_OK;
}
}
// If we get here, something bad happened.
NS_RELEASE(children);
return rv;
}
else {
*aChildNodes = nsnull;
return NS_OK;
}
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::HasChildNodes(PRBool* aHasChildNodes) XULDocumentImpl::HasChildNodes(PRBool* aHasChildNodes)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aHasChildNodes != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aHasChildNodes)
return NS_ERROR_NULL_POINTER;
if (mRootContent) {
*aHasChildNodes = PR_TRUE;
}
else {
*aHasChildNodes = PR_FALSE;
}
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetFirstChild(nsIDOMNode** aFirstChild) XULDocumentImpl::GetFirstChild(nsIDOMNode** aFirstChild)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aFirstChild != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aFirstChild)
return NS_ERROR_NULL_POINTER;
if (mRootContent) {
return mRootContent->QueryInterface(nsIDOMNode::GetIID(), (void**) aFirstChild);
}
else {
*aFirstChild = nsnull;
return NS_OK;
}
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetLastChild(nsIDOMNode** aLastChild) XULDocumentImpl::GetLastChild(nsIDOMNode** aLastChild)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aLastChild != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aLastChild)
return NS_ERROR_NULL_POINTER;
if (mRootContent) {
return mRootContent->QueryInterface(nsIDOMNode::GetIID(), (void**) aLastChild);
}
else {
*aLastChild = nsnull;
return NS_OK;
}
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetPreviousSibling(nsIDOMNode** aPreviousSibling) XULDocumentImpl::GetPreviousSibling(nsIDOMNode** aPreviousSibling)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aPreviousSibling != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aPreviousSibling)
return NS_ERROR_NULL_POINTER;
*aPreviousSibling = nsnull;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetNextSibling(nsIDOMNode** aNextSibling) XULDocumentImpl::GetNextSibling(nsIDOMNode** aNextSibling)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aNextSibling != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aNextSibling)
return NS_ERROR_NULL_POINTER;
*aNextSibling = nsnull;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetAttributes(nsIDOMNamedNodeMap** aAttributes) XULDocumentImpl::GetAttributes(nsIDOMNamedNodeMap** aAttributes)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aAttributes != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aAttributes)
return NS_ERROR_NULL_POINTER;
*aAttributes = nsnull;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetOwnerDocument(nsIDOMDocument** aOwnerDocument) XULDocumentImpl::GetOwnerDocument(nsIDOMDocument** aOwnerDocument)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aOwnerDocument != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aOwnerDocument)
return NS_ERROR_NULL_POINTER;
*aOwnerDocument = nsnull;
return NS_OK;
} }
@ -2650,8 +2856,9 @@ XULDocumentImpl::AppendChild(nsIDOMNode* aNewChild, nsIDOMNode** aReturn)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::CloneNode(PRBool aDeep, nsIDOMNode** aReturn) XULDocumentImpl::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
{ {
NS_NOTYETIMPLEMENTED("write me!"); // We don't allow cloning of a document
return NS_ERROR_NOT_IMPLEMENTED; *aReturn = nsnull;
return NS_OK;
} }
@ -3093,7 +3300,7 @@ XULDocumentImpl::AddNamedDataSource(const char* uri)
nsresult rv; nsresult rv;
nsCOMPtr<nsIRDFDataSource> ds; nsCOMPtr<nsIRDFDataSource> ds;
if (NS_FAILED(rv = mRDFService->GetDataSource(uri, getter_AddRefs(ds)))) { if (NS_FAILED(rv = gRDFService->GetDataSource(uri, getter_AddRefs(ds)))) {
NS_ERROR("unable to get named datasource"); NS_ERROR("unable to get named datasource");
return rv; return rv;
} }
@ -3128,13 +3335,6 @@ XULDocumentImpl::Init(void)
(void**) &mNameSpaceManager))) (void**) &mNameSpaceManager)))
return rv; return rv;
// Keep the RDF service cached in a member variable to make using
// it a bit less painful
if (NS_FAILED(rv = nsServiceManager::GetService(kRDFServiceCID,
kIRDFServiceIID,
(nsISupports**) &mRDFService)))
return rv;
return NS_OK; return NS_OK;
} }
@ -3324,3 +3524,80 @@ XULDocumentImpl::GetElementsByAttribute(nsIDOMNode* aNode,
nsresult
XULDocumentImpl::ParseTagString(const nsString& aTagName, nsIAtom*& aName, PRInt32& aNameSpaceID)
{
// Parse the tag into a name and a namespace ID. This is slightly
// different than nsIContent::ParseAttributeString() because we
// take the default namespace into account (rather than just
// assigning "no namespace") in the case that there is no
// namespace prefix present.
static char kNameSpaceSeparator[] = ":";
// XXX this is a gross hack, but it'll carry us for now. We parse
// the tag name using the root content, which presumably has all
// the namespace info we need.
NS_PRECONDITION(mRootContent != nsnull, "not initialized");
if (! mRootContent)
return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(mRootContent) );
if (! xml) return NS_ERROR_UNEXPECTED;
nsresult rv;
nsCOMPtr<nsINameSpace> ns;
rv = xml->GetContainingNameSpace(*getter_AddRefs(ns));
if (NS_FAILED(rv)) return rv;
NS_ASSERTION(ns != nsnull, "expected xml namespace info to be available");
if (! ns)
return NS_ERROR_UNEXPECTED;
nsAutoString prefix;
nsAutoString name(aTagName);
PRInt32 nsoffset = name.Find(kNameSpaceSeparator);
if (-1 != nsoffset) {
name.Left(prefix, nsoffset);
name.Cut(0, nsoffset+1);
}
// Figure out the namespace ID
nsCOMPtr<nsIAtom> nameSpaceAtom;
if (0 < prefix.Length())
nameSpaceAtom = getter_AddRefs(NS_NewAtom(prefix));
rv = ns->FindNameSpaceID(nameSpaceAtom, aNameSpaceID);
if (NS_FAILED(rv))
aNameSpaceID = kNameSpaceID_None;
aName = NS_NewAtom(name);
return NS_OK;
}
nsresult
XULDocumentImpl::MakeProperty(PRInt32 aNameSpaceID, nsIAtom* aTag, nsIRDFResource** aResult)
{
// Using the namespace ID and the tag, construct a fully-qualified
// URI and turn it into an RDF property.
NS_PRECONDITION(mNameSpaceManager != nsnull, "not initialized");
if (! mNameSpaceManager)
return NS_ERROR_NOT_INITIALIZED;
nsresult rv;
nsAutoString uri;
rv = mNameSpaceManager->GetNameSpaceURI(aNameSpaceID, uri);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get URI for namespace");
if (NS_FAILED(rv)) return rv;
if (uri.Last() != PRUnichar('#') && uri.Last() != PRUnichar('/'))
uri.Append('#');
uri.Append(aTag->GetUnicode());
rv = gRDFService->GetUnicodeResource(uri, aResult);
return rv;
}

Просмотреть файл

@ -464,7 +464,7 @@ RDFGenericBuilderImpl::CreateContents(nsIContent* aElement)
// via the nsIContent interface allows us to support generic nodes // via the nsIContent interface allows us to support generic nodes
// that might get added in by DOM calls. // that might get added in by DOM calls.
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetElementResource(aElement, getter_AddRefs(resource)))) { if (NS_FAILED(rv = nsRDFContentUtils::GetElementResource(aElement, getter_AddRefs(resource)))) {
NS_ERROR("unable to get element resource"); NS_ERROR("unable to get element resource");
return rv; return rv;
} }
@ -1104,101 +1104,6 @@ RDFGenericBuilderImpl::OnRemoveAttributeNode(nsIDOMElement* aElement, nsIDOMAttr
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Implementation methods // Implementation methods
nsresult
RDFGenericBuilderImpl::FindChildByTag(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIContent** aChild)
{
nsresult rv;
PRInt32 count;
if (NS_FAILED(rv = aElement->ChildCount(count)))
return rv;
for (PRInt32 i = 0; i < count; ++i) {
nsCOMPtr<nsIContent> kid;
if (NS_FAILED(rv = aElement->ChildAt(i, *getter_AddRefs(kid))))
return rv; // XXX fatal
PRInt32 nameSpaceID;
if (NS_FAILED(rv = kid->GetNameSpaceID(nameSpaceID)))
return rv; // XXX fatal
if (nameSpaceID != aNameSpaceID)
continue; // wrong namespace
nsCOMPtr<nsIAtom> kidTag;
if (NS_FAILED(rv = kid->GetTag(*getter_AddRefs(kidTag))))
return rv; // XXX fatal
if (kidTag.get() != aTag)
continue;
*aChild = kid;
NS_ADDREF(*aChild);
return NS_OK;
}
return NS_RDF_NO_VALUE; // not found
}
nsresult
RDFGenericBuilderImpl::FindChildByTagAndResource(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIRDFResource* aResource,
nsIContent** aChild)
{
nsresult rv;
PRInt32 count;
if (NS_FAILED(rv = aElement->ChildCount(count)))
return rv;
for (PRInt32 i = 0; i < count; ++i) {
nsCOMPtr<nsIContent> kid;
if (NS_FAILED(rv = aElement->ChildAt(i, *getter_AddRefs(kid))))
return rv; // XXX fatal
// Make sure it's a <xul:treecell>
PRInt32 nameSpaceID;
if (NS_FAILED(rv = kid->GetNameSpaceID(nameSpaceID)))
return rv; // XXX fatal
if (nameSpaceID != aNameSpaceID)
continue; // wrong namespace
nsCOMPtr<nsIAtom> tag;
if (NS_FAILED(rv = kid->GetTag(*getter_AddRefs(tag))))
return rv; // XXX fatal
if (tag.get() != aTag)
continue; // wrong tag
// Now get the resource ID from the RDF:ID attribute. We do it
// via the content model, because you're never sure who
// might've added this stuff in...
nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetElementResource(kid, getter_AddRefs(resource)))) {
NS_ERROR("severe error retrieving resource");
return rv;
}
if (resource.get() != aResource)
continue; // not the resource we want
// Fount it!
*aChild = kid;
NS_ADDREF(*aChild);
return NS_OK;
}
return NS_RDF_NO_VALUE; // not found
}
nsresult nsresult
RDFGenericBuilderImpl::EnsureElementHasGenericChild(nsIContent* parent, RDFGenericBuilderImpl::EnsureElementHasGenericChild(nsIContent* parent,
PRInt32 nameSpaceID, PRInt32 nameSpaceID,
@ -1207,7 +1112,7 @@ RDFGenericBuilderImpl::EnsureElementHasGenericChild(nsIContent* parent,
{ {
nsresult rv; nsresult rv;
rv = FindChildByTag(parent, nameSpaceID, tag, result); rv = nsRDFContentUtils::FindChildByTag(parent, nameSpaceID, tag, result);
if (NS_FAILED(rv)) if (NS_FAILED(rv))
return rv; return rv;
@ -1519,7 +1424,7 @@ RDFGenericBuilderImpl::GetDOMNodeResource(nsIDOMNode* aNode, nsIRDFResource** aR
return rv; return rv;
} }
return GetElementResource(element, aResource); return nsRDFContentUtils::GetElementResource(element, aResource);
} }
nsresult nsresult
@ -1531,15 +1436,23 @@ RDFGenericBuilderImpl::CreateResourceElement(PRInt32 aNameSpaceID,
nsresult rv; nsresult rv;
nsCOMPtr<nsIContent> result; nsCOMPtr<nsIContent> result;
if (NS_FAILED(rv = NS_NewRDFElement(aNameSpaceID, aTag, getter_AddRefs(result)))) rv = NS_NewRDFElement(aNameSpaceID, aTag, getter_AddRefs(result));
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create new RDFElement");
if (NS_FAILED(rv)) return rv;
nsXPIDLCString uri; nsXPIDLCString uri;
if (NS_FAILED(rv = aResource->GetValue( getter_Copies(uri) ))) rv = aResource->GetValue( getter_Copies(uri) );
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource URI");
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv = result->SetAttribute(kNameSpaceID_None, kIdAtom, (const char*) uri, PR_FALSE))) rv = result->SetAttribute(kNameSpaceID_None, kIdAtom, (const char*) uri, PR_FALSE);
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set id attribute");
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIDocument> doc( do_QueryInterface(mDocument) );
rv = result->SetDocument(doc, PR_FALSE);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set element's document");
if (NS_FAILED(rv)) return rv;
*aResult = result; *aResult = result;
NS_ADDREF(*aResult); NS_ADDREF(*aResult);
@ -1607,7 +1520,9 @@ RDFGenericBuilderImpl::CloseWidgetItem(nsIContent* aElement)
parentNode = dont_QueryInterface(aElement); parentNode = dont_QueryInterface(aElement);
rv = NS_OK; rv = NS_OK;
} }
else rv = FindChildByTag(aElement, kNameSpaceID_XUL, parentAtom, getter_AddRefs(parentNode)); else {
rv = nsRDFContentUtils::FindChildByTag(aElement, kNameSpaceID_XUL, parentAtom, getter_AddRefs(parentNode));
}
if (rv == NS_RDF_NO_VALUE) { if (rv == NS_RDF_NO_VALUE) {
// No tag; must've already been closed // No tag; must've already been closed
@ -1651,47 +1566,3 @@ RDFGenericBuilderImpl::CloseWidgetItem(nsIContent* aElement)
} }
nsresult
RDFGenericBuilderImpl::GetElementResource(nsIContent* aElement, nsIRDFResource** aResult)
{
// Perform a reverse mapping from an element in the content model
// to an RDF resource.
nsresult rv;
nsAutoString uri;
if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_None,
kIdAtom,
uri))) {
NS_ERROR("severe error retrieving attribute");
return rv;
}
if (rv != NS_CONTENT_ATTR_HAS_VALUE) {
NS_ERROR("widget element has no ID");
return NS_ERROR_UNEXPECTED;
}
// Since the element will store its ID attribute as a document-relative value,
// we may need to qualify it first...
NS_ASSERTION(mDocument != nsnull, "builder has no document -- can't fully qualify URI");
if (nsnull != mDocument) {
nsCOMPtr<nsIDocument> doc( do_QueryInterface(mDocument) );
NS_ASSERTION(doc, "doesn't support nsIDocument");
if (doc) {
nsIURL* docURL = nsnull;
doc->GetBaseURL(docURL);
if (docURL) {
const char* url;
docURL->GetSpec(&url);
rdf_PossiblyMakeAbsolute(url, uri);
NS_RELEASE(docURL);
}
}
}
if (NS_FAILED(rv = gRDFService->GetUnicodeResource(uri, aResult))) {
NS_ERROR("unable to create resource");
return rv;
}
return NS_OK;
}

Просмотреть файл

@ -25,22 +25,32 @@
*/ */
#include "nsCOMPtr.h"
#include "nsIContent.h" #include "nsIContent.h"
#include "nsIDocument.h"
#include "nsIRDFNode.h" #include "nsIRDFNode.h"
#include "nsINameSpace.h"
#include "nsINameSpaceManager.h"
#include "nsIRDFService.h"
#include "nsIServiceManager.h"
#include "nsITextContent.h" #include "nsITextContent.h"
#include "nsIURL.h"
#include "nsIXMLContent.h"
#include "nsLayoutCID.h" #include "nsLayoutCID.h"
#include "nsRDFCID.h"
#include "nsRDFContentUtils.h" #include "nsRDFContentUtils.h"
#include "nsString.h" #include "nsString.h"
#include "nsXPIDLString.h" #include "nsXPIDLString.h"
#include "prlog.h" #include "prlog.h"
#include "nsCOMPtr.h" #include "rdf.h"
#include "rdfutil.h"
static NS_DEFINE_IID(kIContentIID, NS_ICONTENT_IID); static NS_DEFINE_IID(kIContentIID, NS_ICONTENT_IID);
static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID); static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID);
static NS_DEFINE_IID(kIRDFLiteralIID, NS_IRDFLITERAL_IID); static NS_DEFINE_IID(kIRDFLiteralIID, NS_IRDFLITERAL_IID);
static NS_DEFINE_IID(kITextContentIID, NS_ITEXT_CONTENT_IID); // XXX grr... static NS_DEFINE_IID(kITextContentIID, NS_ITEXT_CONTENT_IID); // XXX grr...
static NS_DEFINE_CID(kTextNodeCID, NS_TEXTNODE_CID); static NS_DEFINE_CID(kTextNodeCID, NS_TEXTNODE_CID);
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
@ -48,67 +58,166 @@ nsresult
nsRDFContentUtils::AttachTextNode(nsIContent* parent, nsIRDFNode* value) nsRDFContentUtils::AttachTextNode(nsIContent* parent, nsIRDFNode* value)
{ {
nsresult rv; nsresult rv;
nsAutoString s; nsAutoString s;
nsIContent* node = nsnull; rv = GetTextForNode(value, s);
nsITextContent* text = nsnull; if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv = GetTextForNode(value, s))) nsCOMPtr<nsITextContent> text;
goto error; rv = nsComponentManager::CreateInstance(kTextNodeCID,
nsnull,
nsITextContent::GetIID(),
getter_AddRefs(text));
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv = nsComponentManager::CreateInstance(kTextNodeCID,
nsnull,
kIContentIID,
(void**) &node)))
goto error;
if (NS_FAILED(rv = node->QueryInterface(kITextContentIID, (void**) &text))) rv = text->SetText(s.GetUnicode(), s.Length(), PR_FALSE);
goto error; if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv = text->SetText(s.GetUnicode(), s.Length(), PR_FALSE)))
goto error;
// hook it up to the child // hook it up to the child
if (NS_FAILED(rv = parent->AppendChildTo(NS_STATIC_CAST(nsIContent*, node), PR_TRUE))) rv = parent->AppendChildTo(nsCOMPtr<nsIContent>( do_QueryInterface(text) ), PR_TRUE);
goto error; if (NS_FAILED(rv)) return rv;
error: return NS_OK;
NS_IF_RELEASE(node);
NS_IF_RELEASE(text);
return rv;
} }
nsresult nsresult
nsRDFContentUtils::FindTreeBodyElement(nsIContent *tree, nsIContent **treeBody) nsRDFContentUtils::FindChildByTag(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIContent** aResult)
{ {
nsCOMPtr<nsIContent> child; nsresult rv;
PRInt32 childIndex = 0, numChildren = 0, nameSpaceID;
nsresult rv;
nsIAtom *treeBodyAtom = NS_NewAtom("treebody"); PRInt32 count;
if (NS_FAILED(rv = aElement->ChildCount(count)))
return rv;
if (NS_FAILED(rv = tree->ChildCount(numChildren))) return(rv); for (PRInt32 i = 0; i < count; ++i) {
for (childIndex=0; childIndex<numChildren; childIndex++) nsCOMPtr<nsIContent> kid;
{ if (NS_FAILED(rv = aElement->ChildAt(i, *getter_AddRefs(kid))))
if (NS_FAILED(rv = tree->ChildAt(childIndex, *getter_AddRefs(child)))) return(rv); return rv; // XXX fatal
if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) return rv;
// if (nameSpaceID == kNameSpaceID_XUL)
if (1)
{
nsCOMPtr<nsIAtom> tag;
if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag)))) return rv;
if (tag.get() == treeBodyAtom)
{
*treeBody = child;
NS_ADDREF(*treeBody);
return NS_OK;
}
}
}
NS_RELEASE(treeBodyAtom); PRInt32 nameSpaceID;
if (NS_FAILED(rv = kid->GetNameSpaceID(nameSpaceID)))
return rv; // XXX fatal
return(NS_ERROR_FAILURE); if (nameSpaceID != aNameSpaceID)
continue; // wrong namespace
nsCOMPtr<nsIAtom> kidTag;
if (NS_FAILED(rv = kid->GetTag(*getter_AddRefs(kidTag))))
return rv; // XXX fatal
if (kidTag.get() != aTag)
continue;
*aResult = kid;
NS_ADDREF(*aResult);
return NS_OK;
}
return NS_RDF_NO_VALUE; // not found
}
nsresult
nsRDFContentUtils::FindChildByTagAndResource(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIRDFResource* aResource,
nsIContent** aResult)
{
nsresult rv;
PRInt32 count;
if (NS_FAILED(rv = aElement->ChildCount(count)))
return rv;
for (PRInt32 i = 0; i < count; ++i) {
nsCOMPtr<nsIContent> kid;
if (NS_FAILED(rv = aElement->ChildAt(i, *getter_AddRefs(kid))))
return rv; // XXX fatal
// Make sure it's a <xul:treecell>
PRInt32 nameSpaceID;
if (NS_FAILED(rv = kid->GetNameSpaceID(nameSpaceID)))
return rv; // XXX fatal
if (nameSpaceID != aNameSpaceID)
continue; // wrong namespace
nsCOMPtr<nsIAtom> tag;
if (NS_FAILED(rv = kid->GetTag(*getter_AddRefs(tag))))
return rv; // XXX fatal
if (tag.get() != aTag)
continue; // wrong tag
// Now get the resource ID from the RDF:ID attribute. We do it
// via the content model, because you're never sure who
// might've added this stuff in...
nsCOMPtr<nsIRDFResource> resource;
rv = GetElementResource(kid, getter_AddRefs(resource));
NS_ASSERTION(NS_SUCCEEDED(rv), "severe error retrieving resource");
if (NS_FAILED(rv)) return rv;
if (resource.get() != aResource)
continue; // not the resource we want
// Fount it!
*aResult = kid;
NS_ADDREF(*aResult);
return NS_OK;
}
return NS_RDF_NO_VALUE; // not found
}
nsresult
nsRDFContentUtils::GetElementResource(nsIContent* aElement, nsIRDFResource** aResult)
{
// Perform a reverse mapping from an element in the content model
// to an RDF resource.
nsresult rv;
nsAutoString uri;
nsCOMPtr<nsIAtom> kIdAtom( dont_QueryInterface(NS_NewAtom("id")) );
rv = aElement->GetAttribute(kNameSpaceID_None, kIdAtom, uri);
NS_ASSERTION(NS_SUCCEEDED(rv), "severe error retrieving attribute");
if (NS_FAILED(rv)) return rv;
if (rv != NS_CONTENT_ATTR_HAS_VALUE)
return NS_ERROR_FAILURE;
// Since the element will store its ID attribute as a document-relative value,
// we may need to qualify it first...
nsCOMPtr<nsIDocument> doc;
rv = aElement->GetDocument(*getter_AddRefs(doc));
if (NS_FAILED(rv)) return rv;
NS_ASSERTION(doc != nsnull, "element is not in any document");
if (doc) {
nsIURL* docURL = nsnull;
doc->GetBaseURL(docURL);
if (docURL) {
const char* url;
docURL->GetSpec(&url);
rdf_PossiblyMakeAbsolute(url, uri);
NS_RELEASE(docURL);
}
}
NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv);
if (NS_FAILED(rv)) return rv;
rv = rdf->GetUnicodeResource(uri, aResult);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create resource");
if (NS_FAILED(rv)) return rv;
return NS_OK;
} }
@ -142,3 +251,114 @@ nsRDFContentUtils::GetTextForNode(nsIRDFNode* aNode, nsString& aResult)
return rv; return rv;
} }
nsresult
nsRDFContentUtils::GetElementLogString(nsIContent* aElement, nsString& aResult)
{
nsresult rv;
aResult = '<';
nsCOMPtr<nsINameSpace> ns;
PRInt32 elementNameSpaceID;
rv = aElement->GetNameSpaceID(elementNameSpaceID);
if (NS_FAILED(rv)) return rv;
if (kNameSpaceID_HTML == elementNameSpaceID) {
aResult.Append("html:");
}
else {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(aElement) );
NS_ASSERTION(xml != nsnull, "not an XML or HTML element");
if (! xml) return NS_ERROR_UNEXPECTED;
rv = xml->GetContainingNameSpace(*getter_AddRefs(ns));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIAtom> prefix;
rv = ns->FindNameSpacePrefix(elementNameSpaceID, *getter_AddRefs(prefix));
if (NS_SUCCEEDED(rv) && (prefix != nsnull)) {
nsAutoString prefixStr;
prefix->ToString(prefixStr);
if (prefixStr.Length()) {
aResult.Append(prefix->GetUnicode());
aResult.Append(':');
}
}
}
nsCOMPtr<nsIAtom> tag;
rv = aElement->GetTag(*getter_AddRefs(tag));
if (NS_FAILED(rv)) return rv;
aResult.Append(tag->GetUnicode());
PRInt32 count;
rv = aElement->GetAttributeCount(count);
if (NS_FAILED(rv)) return rv;
for (PRInt32 i = 0; i < count; ++i) {
aResult.Append(' ');
PRInt32 nameSpaceID;
nsCOMPtr<nsIAtom> name;
rv = aElement->GetAttributeNameAt(i, nameSpaceID, *getter_AddRefs(name));
if (NS_FAILED(rv)) return rv;
nsAutoString attr;
nsRDFContentUtils::GetAttributeLogString(aElement, nameSpaceID, name, attr);
aResult.Append(attr);
aResult.Append("=\"");
nsAutoString value;
rv = aElement->GetAttribute(nameSpaceID, name, value);
if (NS_FAILED(rv)) return rv;
aResult.Append(value);
aResult.Append("\"");
}
aResult.Append('>');
return NS_OK;
}
nsresult
nsRDFContentUtils::GetAttributeLogString(nsIContent* aElement, PRInt32 aNameSpaceID, nsIAtom* aTag, nsString& aResult)
{
nsresult rv;
PRInt32 elementNameSpaceID;
rv = aElement->GetNameSpaceID(elementNameSpaceID);
if (NS_FAILED(rv)) return rv;
if ((kNameSpaceID_HTML == elementNameSpaceID) ||
(kNameSpaceID_None == aNameSpaceID)) {
aResult.Truncate();
}
else {
// we may have a namespace prefix on the attribute
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(aElement) );
NS_ASSERTION(xml != nsnull, "not an XML or HTML element");
if (! xml) return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsINameSpace> ns;
rv = xml->GetContainingNameSpace(*getter_AddRefs(ns));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIAtom> prefix;
rv = ns->FindNameSpacePrefix(aNameSpaceID, *getter_AddRefs(prefix));
if (NS_SUCCEEDED(rv) && (prefix != nsnull)) {
nsAutoString prefixStr;
prefix->ToString(prefixStr);
if (prefixStr.Length()) {
aResult.Append(prefix->GetUnicode());
aResult.Append(':');
}
}
}
aResult.Append(aTag->GetUnicode());
return NS_OK;
}

Просмотреть файл

@ -43,10 +43,29 @@ public:
AttachTextNode(nsIContent* parent, nsIRDFNode* value); AttachTextNode(nsIContent* parent, nsIRDFNode* value);
static nsresult static nsresult
FindTreeBodyElement(nsIContent *tree, nsIContent **treeBody); FindChildByTag(nsIContent *aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIContent **aResult);
static nsresult
FindChildByTagAndResource(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIRDFResource* aResource,
nsIContent** aResult);
static nsresult
GetElementResource(nsIContent* aElement, nsIRDFResource** aResult);
static nsresult static nsresult
GetTextForNode(nsIRDFNode* aNode, nsString& aResult); GetTextForNode(nsIRDFNode* aNode, nsString& aResult);
static nsresult
GetElementLogString(nsIContent* aElement, nsString& aResult);
static nsresult
GetAttributeLogString(nsIContent* aElement, PRInt32 aNameSpaceID, nsIAtom* aTag, nsString& aResult);
}; };

Просмотреть файл

@ -539,20 +539,22 @@ RDFElementImpl::GetParentNode(nsIDOMNode** aParentNode)
return mParent->QueryInterface(kIDOMNodeIID, (void**) aParentNode); return mParent->QueryInterface(kIDOMNodeIID, (void**) aParentNode);
} }
else if (mDocument) { else if (mDocument) {
// If we don't have a parent, but we're in the document, we must // XXX This is a mess because of our fun multiple inheritance heirarchy
// be the root node of the document. The DOM says that the root nsCOMPtr<nsIContent> root( dont_QueryInterface(mDocument->GetRootContent()) );
// is the document. nsCOMPtr<nsIContent> thisIContent;
return mDocument->QueryInterface(kIDOMNodeIID, (void**)aParentNode); QueryInterface(kIContentIID, getter_AddRefs(thisIContent));
}
else {
// A standalone element (i.e. one without a parent or a document)
// implicitly has a document fragment as its parent according to
// the DOM.
// XXX create a doc fragment here as a pseudo-parent. if (root == thisIContent) {
NS_NOTYETIMPLEMENTED("can't handle standalone RDF elements"); // If we don't have a parent, and we're the root content
return NS_ERROR_NOT_IMPLEMENTED; // of the document, DOM says that our parent is the
// document.
return mDocument->QueryInterface(kIDOMNodeIID, (void**)aParentNode);
}
} }
// A standalone element (i.e. one without a parent or a document)
*aParentNode = nsnull;
return NS_OK;
} }
@ -562,35 +564,35 @@ RDFElementImpl::GetChildNodes(nsIDOMNodeList** aChildNodes)
nsresult rv; nsresult rv;
nsRDFDOMNodeList* children; nsRDFDOMNodeList* children;
if (NS_FAILED(rv = nsRDFDOMNodeList::Create(&children))) { rv = nsRDFDOMNodeList::Create(&children);
NS_ERROR("unable to create DOM node list"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create DOM node list");
return rv; if (NS_FAILED(rv)) return rv;
}
PRInt32 count; PRInt32 count;
if (NS_SUCCEEDED(rv = ChildCount(count))) { if (NS_SUCCEEDED(rv = ChildCount(count))) {
for (PRInt32 index = 0; index < count; ++index) { for (PRInt32 index = 0; index < count; ++index) {
nsCOMPtr<nsIContent> child; nsCOMPtr<nsIContent> child;
if (NS_FAILED(rv = ChildAt(index, *getter_AddRefs(child)))) { rv = ChildAt(index, *getter_AddRefs(child));
NS_ERROR("unable to get child"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get child");
if (NS_FAILED(rv))
break; break;
}
nsCOMPtr<nsIDOMNode> domNode; nsCOMPtr<nsIDOMNode> domNode;
if (NS_FAILED(rv = child->QueryInterface(kIDOMNodeIID, (void**) getter_AddRefs(domNode)))) { rv = child->QueryInterface(kIDOMNodeIID, (void**) getter_AddRefs(domNode));
if (NS_FAILED(rv)) {
NS_WARNING("child content doesn't support nsIDOMNode"); NS_WARNING("child content doesn't support nsIDOMNode");
continue; continue;
} }
if (NS_FAILED(rv = children->AppendNode(domNode))) { rv = children->AppendNode(domNode);
NS_ERROR("unable to append node to list"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to append node to list");
if (NS_FAILED(rv))
break; break;
}
} }
} }
// Create() addref'd for us
*aChildNodes = children; *aChildNodes = children;
NS_ADDREF(*aChildNodes);
return NS_OK; return NS_OK;
} }
@ -1018,15 +1020,41 @@ RDFElementImpl::SetContainingNameSpace(nsINameSpace* aNameSpace)
NS_IMETHODIMP NS_IMETHODIMP
RDFElementImpl::GetContainingNameSpace(nsINameSpace*& aNameSpace) const RDFElementImpl::GetContainingNameSpace(nsINameSpace*& aNameSpace) const
{ {
nsresult rv;
if (mNameSpace) { if (mNameSpace) {
// If we have a namespace, return it.
NS_ADDREF(mNameSpace); NS_ADDREF(mNameSpace);
aNameSpace = mNameSpace; aNameSpace = mNameSpace;
return NS_OK; return NS_OK;
} }
else if (mParent) {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(mParent) ); // Next, try our parent.
nsCOMPtr<nsIContent> parent( dont_QueryInterface(mParent) );
while (parent) {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(parent) );
if (xml) if (xml)
return xml->GetContainingNameSpace(aNameSpace); return xml->GetContainingNameSpace(aNameSpace);
nsCOMPtr<nsIContent> temp = parent;
rv = temp->GetParent(*getter_AddRefs(parent));
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get parent");
if (NS_FAILED(rv)) return rv;
}
// Allright, we walked all the way to the top of our containment
// hierarchy and couldn't find a parent that supported
// nsIXMLContent. If we're in a document, try to doc's root
// element.
if (mDocument) {
nsCOMPtr<nsIContent> docroot
= dont_QueryInterface( mDocument->GetRootContent() );
if (docroot) {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(docroot) );
if (xml)
return xml->GetContainingNameSpace(aNameSpace);
}
} }
aNameSpace = nsnull; aNameSpace = nsnull;
@ -1616,23 +1644,8 @@ static char kNameSpaceSeparator[] = ":";
name.Cut(0, nsoffset+1); name.Cut(0, nsoffset+1);
} }
// Figure out the namespace ID // Figure out the namespace ID, defaulting to none if there is no
// namespace prefix.
// XXX This is currently broken, because _nobody_ will ever set
// the namespace scope via the nsIXMLElement interface. To make
// that work will require a bit more machinery: something that
// remembers the XML namespace scoping as nodes get created in the
// RDF graph, and can then extract them later when content nodes
// are built via the content model builders.
//
// Since mNameSpace will _always_ be null, specifying
// kNameSpaceID_None allows us to at least match on the
// tag. You'll start seeing problems when the same name is used in
// different namespaces.
//
// See http://bugzilla.mozilla.org/show_bug.cgi?id=3275 and 3334
// for more info.
aNameSpaceID = kNameSpaceID_None; aNameSpaceID = kNameSpaceID_None;
if (0 < prefix.Length()) { if (0 < prefix.Length()) {
nsCOMPtr<nsIAtom> nameSpaceAtom( getter_AddRefs(NS_NewAtom(prefix)) ); nsCOMPtr<nsIAtom> nameSpaceAtom( getter_AddRefs(NS_NewAtom(prefix)) );
@ -1658,9 +1671,6 @@ NS_IMETHODIMP
RDFElementImpl::GetNameSpacePrefixFromId(PRInt32 aNameSpaceID, RDFElementImpl::GetNameSpacePrefixFromId(PRInt32 aNameSpaceID,
nsIAtom*& aPrefix) nsIAtom*& aPrefix)
{ {
// XXX mNameSpace will _always_ be null, so this really depends on
// fixing Bugs 3275 & 3334, resolving how to map scoped namespace
// prefixes back and forth from the graph.
nsresult rv; nsresult rv;
nsCOMPtr<nsINameSpace> ns; nsCOMPtr<nsINameSpace> ns;
@ -2425,7 +2435,7 @@ RDFElementImpl::EnsureContentsGenerated(void) const
(void**) getter_AddRefs(rdfDoc)))) (void**) getter_AddRefs(rdfDoc))))
return rv; return rv;
rv = rdfDoc->CreateContents((nsIStyledContent*) unconstThis); rv = rdfDoc->CreateContents(NS_STATIC_CAST(nsIStyledContent*, unconstThis));
NS_ASSERTION(NS_SUCCEEDED(rv), "problem creating kids"); NS_ASSERTION(NS_SUCCEEDED(rv), "problem creating kids");
return rv; return rv;
} }

Просмотреть файл

@ -464,7 +464,7 @@ RDFGenericBuilderImpl::CreateContents(nsIContent* aElement)
// via the nsIContent interface allows us to support generic nodes // via the nsIContent interface allows us to support generic nodes
// that might get added in by DOM calls. // that might get added in by DOM calls.
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetElementResource(aElement, getter_AddRefs(resource)))) { if (NS_FAILED(rv = nsRDFContentUtils::GetElementResource(aElement, getter_AddRefs(resource)))) {
NS_ERROR("unable to get element resource"); NS_ERROR("unable to get element resource");
return rv; return rv;
} }
@ -1104,101 +1104,6 @@ RDFGenericBuilderImpl::OnRemoveAttributeNode(nsIDOMElement* aElement, nsIDOMAttr
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Implementation methods // Implementation methods
nsresult
RDFGenericBuilderImpl::FindChildByTag(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIContent** aChild)
{
nsresult rv;
PRInt32 count;
if (NS_FAILED(rv = aElement->ChildCount(count)))
return rv;
for (PRInt32 i = 0; i < count; ++i) {
nsCOMPtr<nsIContent> kid;
if (NS_FAILED(rv = aElement->ChildAt(i, *getter_AddRefs(kid))))
return rv; // XXX fatal
PRInt32 nameSpaceID;
if (NS_FAILED(rv = kid->GetNameSpaceID(nameSpaceID)))
return rv; // XXX fatal
if (nameSpaceID != aNameSpaceID)
continue; // wrong namespace
nsCOMPtr<nsIAtom> kidTag;
if (NS_FAILED(rv = kid->GetTag(*getter_AddRefs(kidTag))))
return rv; // XXX fatal
if (kidTag.get() != aTag)
continue;
*aChild = kid;
NS_ADDREF(*aChild);
return NS_OK;
}
return NS_RDF_NO_VALUE; // not found
}
nsresult
RDFGenericBuilderImpl::FindChildByTagAndResource(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIRDFResource* aResource,
nsIContent** aChild)
{
nsresult rv;
PRInt32 count;
if (NS_FAILED(rv = aElement->ChildCount(count)))
return rv;
for (PRInt32 i = 0; i < count; ++i) {
nsCOMPtr<nsIContent> kid;
if (NS_FAILED(rv = aElement->ChildAt(i, *getter_AddRefs(kid))))
return rv; // XXX fatal
// Make sure it's a <xul:treecell>
PRInt32 nameSpaceID;
if (NS_FAILED(rv = kid->GetNameSpaceID(nameSpaceID)))
return rv; // XXX fatal
if (nameSpaceID != aNameSpaceID)
continue; // wrong namespace
nsCOMPtr<nsIAtom> tag;
if (NS_FAILED(rv = kid->GetTag(*getter_AddRefs(tag))))
return rv; // XXX fatal
if (tag.get() != aTag)
continue; // wrong tag
// Now get the resource ID from the RDF:ID attribute. We do it
// via the content model, because you're never sure who
// might've added this stuff in...
nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetElementResource(kid, getter_AddRefs(resource)))) {
NS_ERROR("severe error retrieving resource");
return rv;
}
if (resource.get() != aResource)
continue; // not the resource we want
// Fount it!
*aChild = kid;
NS_ADDREF(*aChild);
return NS_OK;
}
return NS_RDF_NO_VALUE; // not found
}
nsresult nsresult
RDFGenericBuilderImpl::EnsureElementHasGenericChild(nsIContent* parent, RDFGenericBuilderImpl::EnsureElementHasGenericChild(nsIContent* parent,
PRInt32 nameSpaceID, PRInt32 nameSpaceID,
@ -1207,7 +1112,7 @@ RDFGenericBuilderImpl::EnsureElementHasGenericChild(nsIContent* parent,
{ {
nsresult rv; nsresult rv;
rv = FindChildByTag(parent, nameSpaceID, tag, result); rv = nsRDFContentUtils::FindChildByTag(parent, nameSpaceID, tag, result);
if (NS_FAILED(rv)) if (NS_FAILED(rv))
return rv; return rv;
@ -1519,7 +1424,7 @@ RDFGenericBuilderImpl::GetDOMNodeResource(nsIDOMNode* aNode, nsIRDFResource** aR
return rv; return rv;
} }
return GetElementResource(element, aResource); return nsRDFContentUtils::GetElementResource(element, aResource);
} }
nsresult nsresult
@ -1531,15 +1436,23 @@ RDFGenericBuilderImpl::CreateResourceElement(PRInt32 aNameSpaceID,
nsresult rv; nsresult rv;
nsCOMPtr<nsIContent> result; nsCOMPtr<nsIContent> result;
if (NS_FAILED(rv = NS_NewRDFElement(aNameSpaceID, aTag, getter_AddRefs(result)))) rv = NS_NewRDFElement(aNameSpaceID, aTag, getter_AddRefs(result));
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create new RDFElement");
if (NS_FAILED(rv)) return rv;
nsXPIDLCString uri; nsXPIDLCString uri;
if (NS_FAILED(rv = aResource->GetValue( getter_Copies(uri) ))) rv = aResource->GetValue( getter_Copies(uri) );
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource URI");
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv = result->SetAttribute(kNameSpaceID_None, kIdAtom, (const char*) uri, PR_FALSE))) rv = result->SetAttribute(kNameSpaceID_None, kIdAtom, (const char*) uri, PR_FALSE);
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set id attribute");
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIDocument> doc( do_QueryInterface(mDocument) );
rv = result->SetDocument(doc, PR_FALSE);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set element's document");
if (NS_FAILED(rv)) return rv;
*aResult = result; *aResult = result;
NS_ADDREF(*aResult); NS_ADDREF(*aResult);
@ -1607,7 +1520,9 @@ RDFGenericBuilderImpl::CloseWidgetItem(nsIContent* aElement)
parentNode = dont_QueryInterface(aElement); parentNode = dont_QueryInterface(aElement);
rv = NS_OK; rv = NS_OK;
} }
else rv = FindChildByTag(aElement, kNameSpaceID_XUL, parentAtom, getter_AddRefs(parentNode)); else {
rv = nsRDFContentUtils::FindChildByTag(aElement, kNameSpaceID_XUL, parentAtom, getter_AddRefs(parentNode));
}
if (rv == NS_RDF_NO_VALUE) { if (rv == NS_RDF_NO_VALUE) {
// No tag; must've already been closed // No tag; must've already been closed
@ -1651,47 +1566,3 @@ RDFGenericBuilderImpl::CloseWidgetItem(nsIContent* aElement)
} }
nsresult
RDFGenericBuilderImpl::GetElementResource(nsIContent* aElement, nsIRDFResource** aResult)
{
// Perform a reverse mapping from an element in the content model
// to an RDF resource.
nsresult rv;
nsAutoString uri;
if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_None,
kIdAtom,
uri))) {
NS_ERROR("severe error retrieving attribute");
return rv;
}
if (rv != NS_CONTENT_ATTR_HAS_VALUE) {
NS_ERROR("widget element has no ID");
return NS_ERROR_UNEXPECTED;
}
// Since the element will store its ID attribute as a document-relative value,
// we may need to qualify it first...
NS_ASSERTION(mDocument != nsnull, "builder has no document -- can't fully qualify URI");
if (nsnull != mDocument) {
nsCOMPtr<nsIDocument> doc( do_QueryInterface(mDocument) );
NS_ASSERTION(doc, "doesn't support nsIDocument");
if (doc) {
nsIURL* docURL = nsnull;
doc->GetBaseURL(docURL);
if (docURL) {
const char* url;
docURL->GetSpec(&url);
rdf_PossiblyMakeAbsolute(url, uri);
NS_RELEASE(docURL);
}
}
}
if (NS_FAILED(rv = gRDFService->GetUnicodeResource(uri, aResult))) {
NS_ERROR("unable to create resource");
return rv;
}
return NS_OK;
}

Просмотреть файл

@ -71,19 +71,6 @@ public:
NS_DECL_IDOMELEMENTOBSERVER NS_DECL_IDOMELEMENTOBSERVER
// Implementation methods // Implementation methods
nsresult
FindChildByTag(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIContent** aChild);
nsresult
FindChildByTagAndResource(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIRDFResource* aResource,
nsIContent** aChild);
nsresult nsresult
EnsureElementHasGenericChild(nsIContent* aParent, EnsureElementHasGenericChild(nsIContent* aParent,
PRInt32 aNameSpaceID, PRInt32 aNameSpaceID,
@ -153,9 +140,6 @@ public:
virtual nsresult virtual nsresult
CloseWidgetItem(nsIContent* aElement); CloseWidgetItem(nsIContent* aElement);
nsresult
GetElementResource(nsIContent* aElement, nsIRDFResource** aResult);
virtual nsresult virtual nsresult
GetRootWidgetAtom(nsIAtom** aResult) = 0; GetRootWidgetAtom(nsIAtom** aResult) = 0;

Просмотреть файл

@ -372,7 +372,7 @@ RDFMenuBuilderImpl::RemoveWidgetItem(nsIContent* aMenuItemElement,
// via the content model, because you're never sure who // via the content model, because you're never sure who
// might've added this stuff in... // might've added this stuff in...
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
rv = GetElementResource(kid, getter_AddRefs(resource)); rv = nsRDFContentUtils::GetElementResource(kid, getter_AddRefs(resource));
NS_ASSERTION(NS_SUCCEEDED(rv), "severe error retrieving resource"); NS_ASSERTION(NS_SUCCEEDED(rv), "severe error retrieving resource");
if(NS_FAILED(rv)) return rv; if(NS_FAILED(rv)) return rv;

Просмотреть файл

@ -362,7 +362,7 @@ RDFToolbarBuilderImpl::RemoveWidgetItem(nsIContent* aToolbarItemElement,
// via the content model, because you're never sure who // via the content model, because you're never sure who
// might've added this stuff in... // might've added this stuff in...
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
rv = GetElementResource(kid, getter_AddRefs(resource)); rv = nsRDFContentUtils::GetElementResource(kid, getter_AddRefs(resource));
NS_ASSERTION(NS_SUCCEEDED(rv), "severe error retrieving resource"); NS_ASSERTION(NS_SUCCEEDED(rv), "severe error retrieving resource");
if(NS_FAILED(rv)) return rv; if(NS_FAILED(rv)) return rv;

Просмотреть файл

@ -115,6 +115,7 @@ public:
NS_IMETHOD SetDataBase(nsIRDFCompositeDataSource* aDataBase); NS_IMETHOD SetDataBase(nsIRDFCompositeDataSource* aDataBase);
// nsIDOMNodeObserver interface // nsIDOMNodeObserver interface
NS_IMETHOD OnAppendChild(nsIDOMNode* aParent, nsIDOMNode* aNewChild);
NS_IMETHOD OnRemoveChild(nsIDOMNode* aParent, nsIDOMNode* aOldChild); NS_IMETHOD OnRemoveChild(nsIDOMNode* aParent, nsIDOMNode* aOldChild);
// Implementation methods // Implementation methods
@ -509,14 +510,14 @@ RDFTreeBuilderImpl::CheckRDFGraphForUpdates(nsIContent *container)
} }
} }
unsigned long numElements = childArray.Count(); PRInt32 numElements = childArray.Count();
if (numElements > 0) if (numElements > 0)
{ {
nsIRDFResource ** flatArray = new nsIRDFResource*[numElements]; nsIRDFResource ** flatArray = new nsIRDFResource*[numElements];
if (flatArray) if (flatArray)
{ {
// flatten array of resources, sort them, then add/remove as necessary // flatten array of resources, sort them, then add/remove as necessary
unsigned long loop; PRInt32 loop;
for (loop=0; loop<numElements; loop++) for (loop=0; loop<numElements; loop++)
{ {
flatArray[loop] = (nsIRDFResource *)childArray.ElementAt(loop); flatArray[loop] = (nsIRDFResource *)childArray.ElementAt(loop);
@ -526,7 +527,7 @@ RDFTreeBuilderImpl::CheckRDFGraphForUpdates(nsIContent *container)
// first, remove any nodes that are stale // first, remove any nodes that are stale
nsCOMPtr<nsIContent> child; nsCOMPtr<nsIContent> child;
if (NS_SUCCEEDED(rv = FindChildByTag(container, kNameSpaceID_XUL, kTreeChildrenAtom, getter_AddRefs(child)))) if (NS_SUCCEEDED(rv = nsRDFContentUtils::FindChildByTag(container, kNameSpaceID_XUL, kTreeChildrenAtom, getter_AddRefs(child))))
{ {
// note: enumerate backwards so that indexing is easy // note: enumerate backwards so that indexing is easy
PRInt32 numGrandChildren; PRInt32 numGrandChildren;
@ -541,7 +542,7 @@ RDFTreeBuilderImpl::CheckRDFGraphForUpdates(nsIContent *container)
{ {
nsCOMPtr<nsIRDFResource> aRes; nsCOMPtr<nsIRDFResource> aRes;
PRBool removeNode = PR_TRUE; PRBool removeNode = PR_TRUE;
if (NS_SUCCEEDED(rv = GetElementResource(grandChild, getter_AddRefs(aRes)))) if (NS_SUCCEEDED(rv = nsRDFContentUtils::GetElementResource(grandChild, getter_AddRefs(aRes))))
{ {
PRInt32 innerLoop; PRInt32 innerLoop;
for (innerLoop=0; innerLoop < numElements; innerLoop+=2) for (innerLoop=0; innerLoop < numElements; innerLoop+=2)
@ -569,7 +570,7 @@ RDFTreeBuilderImpl::CheckRDFGraphForUpdates(nsIContent *container)
for (loop=0; loop<numElements; loop+=2) for (loop=0; loop<numElements; loop+=2)
{ {
nsIRDFResource *theRes = flatArray[loop]; nsIRDFResource *theRes = flatArray[loop];
if (NS_SUCCEEDED(rv = FindChildByTag(container, kNameSpaceID_XUL, kTreeChildrenAtom, getter_AddRefs(child)))) if (NS_SUCCEEDED(rv = nsRDFContentUtils::FindChildByTag(container, kNameSpaceID_XUL, kTreeChildrenAtom, getter_AddRefs(child))))
{ {
PRBool nodeFound = PR_FALSE; PRBool nodeFound = PR_FALSE;
PRInt32 numGrandChildren; PRInt32 numGrandChildren;
@ -583,7 +584,7 @@ RDFTreeBuilderImpl::CheckRDFGraphForUpdates(nsIContent *container)
if (nameSpaceID == kNameSpaceID_XUL) if (nameSpaceID == kNameSpaceID_XUL)
{ {
nsCOMPtr<nsIRDFResource> aRes; nsCOMPtr<nsIRDFResource> aRes;
if (NS_SUCCEEDED(rv = GetElementResource(grandChild, getter_AddRefs(aRes)))) if (NS_SUCCEEDED(rv = nsRDFContentUtils::GetElementResource(grandChild, getter_AddRefs(aRes))))
{ {
PRBool equals = PR_FALSE; PRBool equals = PR_FALSE;
if (NS_SUCCEEDED(rv = theRes->EqualsNode(aRes, &equals))) if (NS_SUCCEEDED(rv = theRes->EqualsNode(aRes, &equals)))
@ -642,7 +643,7 @@ RDFTreeBuilderImpl::Notify(nsITimer *timer)
} }
#endif #endif
nsIContent *treeBody; nsIContent *treeBody;
if (NS_SUCCEEDED(rv = nsRDFContentUtils::FindTreeBodyElement(mRoot, &treeBody))) if (NS_SUCCEEDED(rv = nsRDFContentUtils::FindChildByTag(mRoot, kNameSpaceID_XUL, kTreeBodyAtom, &treeBody)))
{ {
UpdateContainer(treeBody); UpdateContainer(treeBody);
NS_RELEASE(treeBody); NS_RELEASE(treeBody);
@ -694,20 +695,20 @@ RDFTreeBuilderImpl::SetDataBase(nsIRDFCompositeDataSource* aDataBase)
// nsIDOMNodeObserver interface // nsIDOMNodeObserver interface
NS_IMETHODIMP NS_IMETHODIMP
RDFTreeBuilderImpl::OnRemoveChild(nsIDOMNode* aParent, nsIDOMNode* aOldChild) RDFTreeBuilderImpl::OnAppendChild(nsIDOMNode* aParent, nsIDOMNode* aNewChild)
{ {
NS_PRECONDITION(aParent != nsnull, "null ptr"); NS_PRECONDITION(aParent != nsnull, "null ptr");
if (!aParent) if (! aParent)
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
NS_PRECONDITION(aOldChild != nsnull, "null ptr"); NS_PRECONDITION(aNewChild != nsnull, "null ptr");
if (!aOldChild) if (! aNewChild)
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
nsresult rv; nsresult rv;
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetDOMNodeResource(aParent, getter_AddRefs(resource)))) { rv = GetDOMNodeResource(aParent, getter_AddRefs(resource));
if (NS_FAILED(rv)) {
// XXX it's not a resource element, so there's no assertions // XXX it's not a resource element, so there's no assertions
// we need to make on the back-end. Should we just do the // we need to make on the back-end. Should we just do the
// update? // update?
@ -716,10 +717,8 @@ RDFTreeBuilderImpl::OnRemoveChild(nsIDOMNode* aParent, nsIDOMNode* aOldChild)
// Get the nsIContent interface, it's a bit more utilitarian // Get the nsIContent interface, it's a bit more utilitarian
nsCOMPtr<nsIContent> parent( do_QueryInterface(aParent) ); nsCOMPtr<nsIContent> parent( do_QueryInterface(aParent) );
if (! parent) { NS_ASSERTION(parent != nsnull, "parent doesn't support nsIContent");
NS_ERROR("parent doesn't support nsIContent"); if (! parent) return NS_ERROR_UNEXPECTED;
return NS_ERROR_UNEXPECTED;
}
// Make sure that the element is in the widget. XXX Even this may be // Make sure that the element is in the widget. XXX Even this may be
// a bit too promiscuous: an element may also be a XUL element... // a bit too promiscuous: an element may also be a XUL element...
@ -728,35 +727,25 @@ RDFTreeBuilderImpl::OnRemoveChild(nsIDOMNode* aParent, nsIDOMNode* aOldChild)
// Split the parent into its namespace and tag components // Split the parent into its namespace and tag components
PRInt32 parentNameSpaceID; PRInt32 parentNameSpaceID;
if (NS_FAILED(rv = parent->GetNameSpaceID(parentNameSpaceID))) { rv = parent->GetNameSpaceID(parentNameSpaceID);
NS_ERROR("unable to get parent namespace ID"); if (NS_FAILED(rv)) return rv;
return rv;
}
nsCOMPtr<nsIAtom> parentNameAtom; nsCOMPtr<nsIAtom> parentNameAtom;
if (NS_FAILED(rv = parent->GetTag( *getter_AddRefs(parentNameAtom) ))) { rv = parent->GetTag( *getter_AddRefs(parentNameAtom) );
NS_ERROR("unable to get parent tag"); if (NS_FAILED(rv)) return rv;
return rv;
}
// Now do the same for the child // Now do the same for the child
nsCOMPtr<nsIContent> child( do_QueryInterface(aOldChild) ); nsCOMPtr<nsIContent> child( do_QueryInterface(aNewChild) );
if (! child) { NS_ASSERTION(child != nsnull, "child doesn't support nsIContent");
NS_ERROR("child doesn't support nsIContent"); if (! child) return NS_ERROR_UNEXPECTED;
return NS_ERROR_UNEXPECTED;
}
PRInt32 childNameSpaceID; PRInt32 childNameSpaceID;
if (NS_FAILED(rv = child->GetNameSpaceID(childNameSpaceID))) { rv = child->GetNameSpaceID(childNameSpaceID);
NS_ERROR("unable to get child's namespace ID"); if (NS_FAILED(rv)) return rv;
return rv;
}
nsCOMPtr<nsIAtom> childNameAtom; nsCOMPtr<nsIAtom> childNameAtom;
if (NS_FAILED(rv = child->GetTag( *getter_AddRefs(childNameAtom) ))) { rv = child->GetTag( *getter_AddRefs(childNameAtom) );
NS_ERROR("unable to get child's tag"); if (NS_FAILED(rv)) return rv;
return rv;
}
// Now see if there's anything we can do about it. // Now see if there's anything we can do about it.
@ -772,28 +761,171 @@ RDFTreeBuilderImpl::OnRemoveChild(nsIDOMNode* aParent, nsIDOMNode* aOldChild)
// get the rdf:property out of the child to see what the // get the rdf:property out of the child to see what the
// relationship was between the parent and the child. // relationship was between the parent and the child.
nsAutoString propertyStr; nsAutoString propertyStr;
if (NS_FAILED(rv = child->GetAttribute(kNameSpaceID_RDF, kPropertyAtom, propertyStr))) { rv = child->GetAttribute(kNameSpaceID_RDF, kPropertyAtom, propertyStr);
NS_ERROR("severe error trying to retrieve attribute"); if (NS_FAILED(rv)) return rv;
return rv;
if (rv == NS_CONTENT_ATTR_HAS_VALUE) {
// It's a relationship that we'll need to set up in
// RDF. So let's assert it into the graph. First we
// need the property resource.
nsCOMPtr<nsIRDFResource> property;
rv = gRDFService->GetUnicodeResource(propertyStr, getter_AddRefs(property));
if (NS_FAILED(rv)) return rv;
// And now we need the child's resource.
nsCOMPtr<nsIRDFResource> target;
rv = nsRDFContentUtils::GetElementResource(child, getter_AddRefs(target));
NS_ASSERTION(NS_SUCCEEDED(rv) && (target != nsnull), "expected child to have resource");
if (NS_FAILED(rv)) return rv;
if (! target)
return NS_ERROR_UNEXPECTED;
// We'll handle things a bit differently if we're
// tinkering with an RDF container...
if (rdf_IsContainer(mDB, resource) &&
rdf_IsOrdinalProperty(property)) {
rv = rdf_ContainerAppendElement(mDB, resource, target);
}
else {
rv = mDB->Assert(resource, property, target, PR_TRUE);
}
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to remove element from DB");
if (NS_FAILED(rv)) return rv;
return NS_OK;
} }
// Otherwise, it's a random element that doesn't
// correspond to anything in the graph. Fall through...
}
}
else if ((parentNameSpaceID == kNameSpaceID_XUL) &&
(parentNameAtom.get() == kTreeItemAtom)) {
// The parent is a xul:treeitem.
// XXX We really only care about treeitems in the body; not
// treeitems in the header...
if ((childNameSpaceID == kNameSpaceID_XUL) &&
(childNameAtom == kTreeCellAtom)) {
// ...and the child is a tree cell. They're adding a value
// for a property.
NS_NOTYETIMPLEMENTED("write me");
}
}
else if ((parentNameSpaceID == kNameSpaceID_XUL) &&
(parentNameAtom.get() == kTreeCellAtom)) {
// The parent is a xul:treecell. They're changing the value of
// a cell.
// XXX We really only care about cells in the body; not cells
// in the header...
NS_NOTYETIMPLEMENTED("write me");
}
// If we get here, then they're trying to manipulate the DOM in
// some way that doesn't translate into a sensible update to the RDF
// graph. So, just whack the change into the content model
rv = parent->AppendChildTo(child, PR_TRUE);
NS_ASSERTION(NS_SUCCEEDED(rv), "error appending child to parent");
if (NS_FAILED(rv)) return rv;
return NS_OK;
}
NS_IMETHODIMP
RDFTreeBuilderImpl::OnRemoveChild(nsIDOMNode* aParent, nsIDOMNode* aOldChild)
{
NS_PRECONDITION(aParent != nsnull, "null ptr");
if (!aParent)
return NS_ERROR_NULL_POINTER;
NS_PRECONDITION(aOldChild != nsnull, "null ptr");
if (!aOldChild)
return NS_ERROR_NULL_POINTER;
nsresult rv;
nsCOMPtr<nsIRDFResource> resource;
rv = GetDOMNodeResource(aParent, getter_AddRefs(resource));
if (NS_FAILED(rv)) {
// XXX it's not a resource element, so there's no assertions
// we need to make on the back-end. Should we just do the
// update?
return NS_OK;
}
// Get the nsIContent interface, it's a bit more utilitarian
nsCOMPtr<nsIContent> parent( do_QueryInterface(aParent) );
NS_ASSERTION(parent != nsnull, "parent doesn't support nsIContent");
if (! parent) return NS_ERROR_UNEXPECTED;
// Make sure that the element is in the widget. XXX Even this may be
// a bit too promiscuous: an element may also be a XUL element...
if (!IsElementInWidget(parent))
return NS_OK;
// Split the parent into its namespace and tag components
PRInt32 parentNameSpaceID;
rv = parent->GetNameSpaceID(parentNameSpaceID);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIAtom> parentNameAtom;
rv = parent->GetTag( *getter_AddRefs(parentNameAtom) );
if (NS_FAILED(rv)) return rv;
// Now do the same for the child
nsCOMPtr<nsIContent> child( do_QueryInterface(aOldChild) );
NS_ASSERTION(child != nsnull, "child doesn't support nsIContent");
if (! child) return NS_ERROR_UNEXPECTED;
PRInt32 childNameSpaceID;
rv = child->GetNameSpaceID(childNameSpaceID);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIAtom> childNameAtom;
rv = child->GetTag( *getter_AddRefs(childNameAtom) );
if (NS_FAILED(rv)) return rv;
// Now see if there's anything we can do about it.
if ((parentNameSpaceID == kNameSpaceID_XUL) &&
((parentNameAtom.get() == kTreeChildrenAtom) ||
(parentNameAtom.get() == kTreeBodyAtom))) {
// The parent is a xul:treechildren or xul:treebody...
if ((childNameSpaceID == kNameSpaceID_XUL) &&
(childNameAtom.get() == kTreeItemAtom)) {
// ...and the child is a tree item. We can do this. First,
// get the rdf:property out of the child to see what the
// relationship was between the parent and the child.
nsAutoString propertyStr;
rv = child->GetAttribute(kNameSpaceID_RDF, kPropertyAtom, propertyStr);
if (NS_FAILED(rv)) return rv;
if (rv == NS_CONTENT_ATTR_HAS_VALUE) { if (rv == NS_CONTENT_ATTR_HAS_VALUE) {
// It's a relationship set up by RDF. So let's // It's a relationship set up by RDF. So let's
// unassert it from the graph. First we need the // unassert it from the graph. First we need the
// property resource. // property resource.
nsCOMPtr<nsIRDFResource> property; nsCOMPtr<nsIRDFResource> property;
if (NS_FAILED(gRDFService->GetUnicodeResource(propertyStr, getter_AddRefs(property)))) { rv = gRDFService->GetUnicodeResource(propertyStr, getter_AddRefs(property));
NS_ERROR("unable to construct resource for property"); if (NS_FAILED(rv)) return rv;
return rv;
}
// And now we need the child's resource. // And now we need the child's resource.
nsCOMPtr<nsIRDFResource> target; nsCOMPtr<nsIRDFResource> target;
if (NS_FAILED(rv = GetElementResource(child, getter_AddRefs(target)))) { rv = nsRDFContentUtils::GetElementResource(child, getter_AddRefs(target));
NS_ERROR("expected child to have resource"); NS_ASSERTION(NS_SUCCEEDED(rv) && (target != nsnull), "expected child to have resource");
return rv; if (NS_FAILED(rv)) return rv;
}
if (! target)
return NS_ERROR_UNEXPECTED;
// We'll handle things a bit differently if we're // We'll handle things a bit differently if we're
// tinkering with an RDF container... // tinkering with an RDF container...
@ -805,49 +937,56 @@ RDFTreeBuilderImpl::OnRemoveChild(nsIDOMNode* aParent, nsIDOMNode* aOldChild)
rv = mDB->Unassert(resource, property, target); rv = mDB->Unassert(resource, property, target);
} }
if (NS_FAILED(rv)) { NS_ASSERTION(NS_SUCCEEDED(rv), "unable to remove element from DB");
NS_ERROR("unable to remove element from DB"); if (NS_FAILED(rv)) return rv;
return rv;
} return NS_OK;
} }
else {
// It's a random (non-RDF inserted) element. Just use // Otherwise, it's a random element that doesn't
// the content interface to remove it. // correspond to anything in the graph. Fall through...
PRInt32 index;
if (NS_FAILED(rv = parent->IndexOf(child, index))) {
NS_ERROR("unable to get index of child in parent node");
return rv;
}
NS_ASSERTION(index >= 0, "child was already removed");
if (index >= 0) {
rv = parent->RemoveChildAt(index, PR_TRUE);
NS_ASSERTION(NS_SUCCEEDED(rv), "error removing child from parent");
}
}
} }
} }
else if ((parentNameSpaceID == kNameSpaceID_XUL) && else if ((parentNameSpaceID == kNameSpaceID_XUL) &&
(parentNameAtom.get() == kTreeItemAtom)) { (parentNameAtom.get() == kTreeItemAtom)) {
// The parent is a xul:treeitem. We really only care about // The parent is a xul:treeitem.
// treeitems in the body; not treeitems in the header...
NS_NOTYETIMPLEMENTED("write me"); // XXX We really only care about treeitems in the body; not
// treeitems in the header...
if ((childNameSpaceID == kNameSpaceID_XUL) &&
(childNameAtom == kTreeCellAtom)) {
// ...and the child is a tree cell. They're adding a value
// for a property.
NS_NOTYETIMPLEMENTED("write me");
}
} }
else if ((parentNameSpaceID == kNameSpaceID_XUL) && else if ((parentNameSpaceID == kNameSpaceID_XUL) &&
(parentNameAtom.get() == kTreeCellAtom)) { (parentNameAtom.get() == kTreeCellAtom)) {
// The parent is a xul:treecell. We really only care about // The parent is a xul:treecell. They're changing the value of
// cells in the body; not cells in the header... // a cell.
// XXX We really only care about cells in the body; not cells
// in the header...
NS_NOTYETIMPLEMENTED("write me"); NS_NOTYETIMPLEMENTED("write me");
} }
// they're trying to manipulate the DOM in some way that we don't // If we get here, then they're trying to manipulate the DOM in
// care about: we should probably just call through and do the // some way that doesn't translate into a sensible update to the RDF
// operation on nsIContent, but...let the XUL builder do that I // graph. So, just whack the change into the content model
// guess. PRInt32 index;
rv = parent->IndexOf(child, index);
if (NS_FAILED(rv)) return rv;
NS_ASSERTION(index >= 0, "child was already removed");
if (index >= 0) {
rv = parent->RemoveChildAt(index, PR_TRUE);
NS_ASSERTION(NS_SUCCEEDED(rv), "error removing child from parent");
if (NS_FAILED(rv)) return rv;
}
return NS_OK; return NS_OK;
} }
@ -1067,14 +1206,13 @@ RDFTreeBuilderImpl::RemoveWidgetItem(nsIContent* aElement,
nsCOMPtr<nsIContent> treechildren; // put it here so it stays in scope nsCOMPtr<nsIContent> treechildren; // put it here so it stays in scope
if ((nameSpaceID == kNameSpaceID_XUL) && (tag.get() == kTreeItemAtom)) { if ((nameSpaceID == kNameSpaceID_XUL) && (tag.get() == kTreeItemAtom)) {
if (NS_FAILED(rv = FindChildByTag(aElement, rv = nsRDFContentUtils::FindChildByTag(aElement,
kNameSpaceID_XUL, kNameSpaceID_XUL,
kTreeChildrenAtom, kTreeChildrenAtom,
getter_AddRefs(treechildren)))) { getter_AddRefs(treechildren));
// XXX make this a warning // XXX make this a warning
NS_ERROR("attempt to remove child from an element with no treechildren"); NS_ASSERTION(NS_OK == rv, "attempt to remove child from an element with no treechildren");
return NS_OK; if (NS_OK != rv) return rv;
}
aElement = treechildren.get(); aElement = treechildren.get();
} }
@ -1123,7 +1261,7 @@ RDFTreeBuilderImpl::RemoveWidgetItem(nsIContent* aElement,
// via the content model, because you're never sure who // via the content model, because you're never sure who
// might've added this stuff in... // might've added this stuff in...
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetElementResource(kid, getter_AddRefs(resource)))) { if (NS_FAILED(rv = nsRDFContentUtils::GetElementResource(kid, getter_AddRefs(resource)))) {
NS_ERROR("severe error retrieving resource"); NS_ERROR("severe error retrieving resource");
return rv; return rv;
} }
@ -1366,10 +1504,9 @@ RDFTreeBuilderImpl::CreateTreeItemCells(nsIContent* aTreeItemElement)
// via the nsIContent interface allows us to support generic nodes // via the nsIContent interface allows us to support generic nodes
// that might get added in by DOM calls. // that might get added in by DOM calls.
nsCOMPtr<nsIRDFResource> treeItemResource; nsCOMPtr<nsIRDFResource> treeItemResource;
if (NS_FAILED(rv = GetElementResource(aTreeItemElement, getter_AddRefs(treeItemResource)))) { rv = nsRDFContentUtils::GetElementResource(aTreeItemElement, getter_AddRefs(treeItemResource));
NS_ERROR("unable to get tree item resource"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get tree item resource");
return rv; if (NS_FAILED(rv)) return rv;
}
PRInt32 count; PRInt32 count;
if (NS_FAILED(rv = mRoot->ChildCount(count))) { if (NS_FAILED(rv = mRoot->ChildCount(count))) {

Просмотреть файл

@ -24,55 +24,53 @@
TO DO TO DO
1) Need to implement nsRDFXULBuilder::RemoveAttribute(), and 1) Implement the remainder of the DOM methods.
figure out how to do nsRDFXULBuilder::Remove() (vanilla) when the
child isn't a resource element itself.
2) Implement the remainder of the DOM methods.
*/ */
// Not the ALPHABETICAL ORDER
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsDebug.h" #include "nsDebug.h"
#include "nsIAtom.h" #include "nsIAtom.h"
#include "nsIContent.h" #include "nsIContent.h"
#include "nsIDocument.h" #include "nsIContentViewerContainer.h"
#include "nsIXULParentDocument.h"
#include "nsIXULChildDocument.h"
#include "nsIDOMElement.h" #include "nsIDOMElement.h"
#include "nsIDOMElementObserver.h" #include "nsIDOMElementObserver.h"
#include "nsIDOMNode.h" #include "nsIDOMNode.h"
#include "nsIDOMNodeObserver.h" #include "nsIDOMNodeObserver.h"
#include "nsIDOMText.h"
#include "nsIDOMXULDocument.h"
#include "nsIDOMXULElement.h"
#include "nsIDocument.h"
#include "nsIDocumentLoader.h"
#include "nsINameSpace.h" #include "nsINameSpace.h"
#include "nsINameSpaceManager.h" #include "nsINameSpaceManager.h"
#include "nsINameSpaceManager.h"
#include "nsIRDFCompositeDataSource.h"
#include "nsIRDFContentModelBuilder.h" #include "nsIRDFContentModelBuilder.h"
#include "nsIRDFCursor.h" #include "nsIRDFCursor.h"
#include "nsIRDFCompositeDataSource.h"
#include "nsIRDFDocument.h" #include "nsIRDFDocument.h"
#include "nsIRDFNode.h" #include "nsIRDFNode.h"
#include "nsIRDFObserver.h" #include "nsIRDFObserver.h"
#include "nsIRDFService.h" #include "nsIRDFService.h"
#include "nsIServiceManager.h" #include "nsIServiceManager.h"
#include "nsIStreamListener.h"
#include "nsINameSpaceManager.h"
#include "nsIServiceManager.h" #include "nsIServiceManager.h"
#include "nsIStreamListener.h"
#include "nsISupportsArray.h" #include "nsISupportsArray.h"
#include "nsIURL.h" #include "nsIURL.h"
#include "nsIWebShell.h"
#include "nsIXMLContent.h"
#include "nsIXULChildDocument.h"
#include "nsIXULDocumentInfo.h"
#include "nsIXULParentDocument.h"
#include "nsLayoutCID.h" #include "nsLayoutCID.h"
#include "nsRDFCID.h" #include "nsRDFCID.h"
#include "nsRDFContentUtils.h" #include "nsRDFContentUtils.h"
#include "nsString.h" #include "nsString.h"
#include "nsXPIDLString.h" #include "nsXPIDLString.h"
#include "prlog.h"
#include "rdf.h" #include "rdf.h"
#include "rdfutil.h" #include "rdfutil.h"
#include "nsIDOMXULElement.h"
#include "nsIDOMXULDocument.h"
#include "nsIXULDocumentInfo.h"
#include "nsIContentViewerContainer.h"
#include "nsIDocumentLoader.h"
#include "nsIWebShell.h"
#include "nsIXMLContent.h"
#include "prlog.h"
// XXX These are needed as scaffolding until we get to a more // XXX These are needed as scaffolding until we get to a more
// DOM-based solution. // DOM-based solution.
@ -231,7 +229,10 @@ public:
const nsString& aDataSources); const nsString& aDataSources);
nsresult nsresult
GetDOMNodeResource(nsIDOMNode* aNode, nsIRDFResource** aResource); GetRDFResourceFromXULElement(nsIDOMNode* aNode, nsIRDFResource** aResult);
nsresult
GetGraphNodeForXULElement(nsIDOMNode* aNode, nsIRDFNode** aResult);
nsresult nsresult
CreateResourceElement(PRInt32 aNameSpaceID, CreateResourceElement(PRInt32 aNameSpaceID,
@ -243,9 +244,6 @@ public:
GetResource(PRInt32 aNameSpaceID, GetResource(PRInt32 aNameSpaceID,
nsIAtom* aNameAtom, nsIAtom* aNameAtom,
nsIRDFResource** aResource); nsIRDFResource** aResource);
nsresult
GetElementResource(nsIContent* aElement, nsIRDFResource** aResult);
}; };
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
@ -331,27 +329,17 @@ RDFXULBuilderImpl::RDFXULBuilderImpl(void)
kXULContentsGeneratedAtom = NS_NewAtom("xulcontentsgenerated"); kXULContentsGeneratedAtom = NS_NewAtom("xulcontentsgenerated");
kXULIncludeSrcAtom = NS_NewAtom("include"); kXULIncludeSrcAtom = NS_NewAtom("include");
if (NS_SUCCEEDED(rv = nsServiceManager::GetService(kRDFServiceCID, rv = nsServiceManager::GetService(kRDFServiceCID,
kIRDFServiceIID, kIRDFServiceIID,
(nsISupports**) &gRDFService))) { (nsISupports**) &gRDFService);
NS_VERIFY(NS_SUCCEEDED(gRDFService->GetResource(kURIRDF_instanceOf, &kRDF_instanceOf)), NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get RDF service");
"unable to get resource"); if (NS_SUCCEEDED(rv)) {
gRDFService->GetResource(kURIRDF_instanceOf, &kRDF_instanceOf);
NS_VERIFY(NS_SUCCEEDED(gRDFService->GetResource(kURIRDF_nextVal, &kRDF_nextVal)), gRDFService->GetResource(kURIRDF_nextVal, &kRDF_nextVal);
"unable to get resource"); gRDFService->GetResource(kURIRDF_type, &kRDF_type);
gRDFService->GetResource(kURIRDF_child, &kRDF_child);
NS_VERIFY(NS_SUCCEEDED(gRDFService->GetResource(kURIRDF_type, &kRDF_type)), gRDFService->GetResource(kURIXUL_element, &kXUL_element);
"unable to get resource");
NS_VERIFY(NS_SUCCEEDED(gRDFService->GetResource(kURIRDF_child, &kRDF_child)),
"unable to get resource");
NS_VERIFY(NS_SUCCEEDED(gRDFService->GetResource(kURIXUL_element, &kXUL_element)),
"unable to get resource");
}
else {
NS_ERROR("couldn't get RDF service");
} }
} }
@ -538,12 +526,27 @@ RDFXULBuilderImpl::CreateContents(nsIContent* aElement)
if ((rv == NS_CONTENT_ATTR_HAS_VALUE) && (attrValue.EqualsIgnoreCase("true"))) if ((rv == NS_CONTENT_ATTR_HAS_VALUE) && (attrValue.EqualsIgnoreCase("true")))
return NS_OK; return NS_OK;
// Get the treeitem's resource so that we can generate cell #ifdef PR_LOGGING
// values. We could QI for the nsIRDFResource here, but doing this if (PR_LOG_TEST(gLog, PR_LOG_DEBUG)) {
nsAutoString elementStr;
rv = nsRDFContentUtils::GetElementLogString(aElement, elementStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get element string");
char* elementCStr = elementStr.ToNewCString();
PR_LOG(gLog, PR_LOG_DEBUG, ("xulbuilder create-contents"));
PR_LOG(gLog, PR_LOG_DEBUG, (" %s", elementCStr));
delete[] elementCStr;
}
#endif
// Get the XUL element's resource so that we can generate
// children. We _don't_ QI for the nsIRDFResource here: doing this
// via the nsIContent interface allows us to support generic nodes // via the nsIContent interface allows us to support generic nodes
// that might get added in by DOM calls. // that might get added in by DOM calls.
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetElementResource(aElement, getter_AddRefs(resource)))) { if (NS_FAILED(rv = nsRDFContentUtils::GetElementResource(aElement, getter_AddRefs(resource)))) {
NS_ERROR("unable to get element resource"); NS_ERROR("unable to get element resource");
return rv; return rv;
} }
@ -551,10 +554,9 @@ RDFXULBuilderImpl::CreateContents(nsIContent* aElement)
// Ignore any elements that aren't XUL elements: we can't // Ignore any elements that aren't XUL elements: we can't
// construct content for them anyway. // construct content for them anyway.
PRBool isXULElement; PRBool isXULElement;
if (NS_FAILED(rv = mDB->HasAssertion(resource, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement))) { rv = mDB->HasAssertion(resource, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement);
NS_ERROR("unable to determine if element is a XUL element"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to determine if element is a XUL element");
return rv; if (NS_FAILED(rv)) return rv;
}
if (! isXULElement) if (! isXULElement)
return NS_OK; return NS_OK;
@ -711,8 +713,7 @@ RDFXULBuilderImpl::OnAssert(nsIRDFResource* aSource,
// Stuff that we can ignore outright // Stuff that we can ignore outright
// XXX is this the best place to put it??? // XXX is this the best place to put it???
if ((aProperty == kRDF_nextVal) || if (aProperty == kRDF_nextVal)
(aProperty == kRDF_instanceOf))
return NS_OK; return NS_OK;
nsresult rv; nsresult rv;
@ -727,6 +728,28 @@ RDFXULBuilderImpl::OnAssert(nsIRDFResource* aSource,
return NS_OK; return NS_OK;
} }
#ifdef PR_LOGGING
if (PR_LOG_TEST(gLog, PR_LOG_DEBUG)) {
nsXPIDLCString source;
aSource->GetValue(getter_Copies(source));
nsXPIDLCString property;
aProperty->GetValue(getter_Copies(property));
nsAutoString targetStr;
nsRDFContentUtils::GetTextForNode(aTarget, targetStr);
char* targetCStr = targetStr.ToNewCString();
PR_LOG(gLog, PR_LOG_DEBUG, ("xulbuilder on-assert"));
PR_LOG(gLog, PR_LOG_DEBUG, (" (%s)", (const char*) source));
PR_LOG(gLog, PR_LOG_DEBUG, (" --[%s]-->", (const char*) property));
PR_LOG(gLog, PR_LOG_DEBUG, (" (%s)", targetCStr));
delete[] targetCStr;
}
#endif
nsCOMPtr<nsISupportsArray> elements; nsCOMPtr<nsISupportsArray> elements;
if (NS_FAILED(rv = NS_NewISupportsArray(getter_AddRefs(elements)))) { if (NS_FAILED(rv = NS_NewISupportsArray(getter_AddRefs(elements)))) {
NS_ERROR("unable to create new ISupportsArray"); NS_ERROR("unable to create new ISupportsArray");
@ -744,17 +767,8 @@ RDFXULBuilderImpl::OnAssert(nsIRDFResource* aSource,
for (PRInt32 i = elements->Count() - 1; i >= 0; --i) { for (PRInt32 i = elements->Count() - 1; i >= 0; --i) {
nsCOMPtr<nsIContent> element( do_QueryInterface(elements->ElementAt(i)) ); nsCOMPtr<nsIContent> element( do_QueryInterface(elements->ElementAt(i)) );
// Make sure that the element we're looking at is really an // XXX Make sure that the element we're looking at is really
// element generated by the XUL content-model builder. // an element generated by this content-model builder?
nsAutoString instanceOf;
if (NS_CONTENT_ATTR_HAS_VALUE !=
element->GetAttribute(kNameSpaceID_RDF,
kInstanceOfAtom,
instanceOf))
continue;
if (! instanceOf.Equals(kURIXUL_element))
continue;
if (rdf_IsOrdinalProperty(aProperty)) { if (rdf_IsOrdinalProperty(aProperty)) {
// It's a child node. If the contents of aElement _haven't_ // It's a child node. If the contents of aElement _haven't_
@ -815,12 +829,33 @@ RDFXULBuilderImpl::OnUnassert(nsIRDFResource* aSource,
// Stuff that we can ignore outright // Stuff that we can ignore outright
// XXX is this the best place to put it??? // XXX is this the best place to put it???
if ((aProperty == kRDF_nextVal) || if (aProperty == kRDF_nextVal)
(aProperty == kRDF_instanceOf))
return NS_OK; return NS_OK;
nsresult rv; nsresult rv;
#ifdef PR_LOGGING
if (PR_LOG_TEST(gLog, PR_LOG_DEBUG)) {
nsXPIDLCString source;
aSource->GetValue(getter_Copies(source));
nsXPIDLCString property;
aProperty->GetValue(getter_Copies(property));
nsAutoString targetStr;
nsRDFContentUtils::GetTextForNode(aTarget, targetStr);
char* targetCStr = targetStr.ToNewCString();
PR_LOG(gLog, PR_LOG_DEBUG, ("xulbuilder on-unassert"));
PR_LOG(gLog, PR_LOG_DEBUG, (" (%s)", (const char*) source));
PR_LOG(gLog, PR_LOG_DEBUG, (" --[%s]-->", (const char*) property));
PR_LOG(gLog, PR_LOG_DEBUG, (" (%s)", targetCStr));
delete[] targetCStr;
}
#endif
nsCOMPtr<nsISupportsArray> elements; nsCOMPtr<nsISupportsArray> elements;
if (NS_FAILED(rv = NS_NewISupportsArray(getter_AddRefs(elements)))) { if (NS_FAILED(rv = NS_NewISupportsArray(getter_AddRefs(elements)))) {
NS_ERROR("unable to create new ISupportsArray"); NS_ERROR("unable to create new ISupportsArray");
@ -919,77 +954,66 @@ RDFXULBuilderImpl::OnInsertBefore(nsIDOMNode* aParent, nsIDOMNode* aNewChild, ns
// XXX If aNewChild doesn't have a resource, then somebody is // XXX If aNewChild doesn't have a resource, then somebody is
// inserting a non-RDF element into aParent. Panic for now. // inserting a non-RDF element into aParent. Panic for now.
nsCOMPtr<nsIRDFResource> newChild; nsCOMPtr<nsIRDFNode> newChild;
if (NS_FAILED(rv = GetDOMNodeResource(aNewChild, getter_AddRefs(newChild)))) { rv = GetGraphNodeForXULElement(aNewChild, getter_AddRefs(newChild));
NS_ERROR("new child doesn't have a resource"); if (NS_FAILED(rv)) return rv;
return rv;
} if (rv == NS_RDF_NO_VALUE)
return NS_OK;
// If there was an old parent for newChild, then make sure to // If there was an old parent for newChild, then make sure to
// remove that relationship. // remove that relationship.
nsCOMPtr<nsIDOMNode> oldParentNode; nsCOMPtr<nsIDOMNode> oldParentNode;
if (NS_FAILED(rv = aNewChild->GetParentNode(getter_AddRefs(oldParentNode)))) { rv = aNewChild->GetParentNode(getter_AddRefs(oldParentNode));
NS_ERROR("unable to get new child's parent"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get new child's parent");
return rv; if (NS_FAILED(rv)) return rv;
}
if (oldParentNode) { if (oldParentNode) {
nsCOMPtr<nsIRDFResource> oldParent; nsCOMPtr<nsIRDFResource> oldParent;
// If the old parent has a resource... // If the old parent has a resource...
if (NS_SUCCEEDED(rv = GetDOMNodeResource(oldParentNode, getter_AddRefs(oldParent)))) { if (NS_SUCCEEDED(rv = GetRDFResourceFromXULElement(oldParentNode, getter_AddRefs(oldParent)))) {
// ...and it's a XUL element... // ...and it's a XUL element...
PRBool isXULElement; PRBool isXULElement;
rv = mDB->HasAssertion(oldParent, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement);
if (NS_FAILED(rv)) return rv;
if (NS_SUCCEEDED(rv = mDB->HasAssertion(oldParent, if (isXULElement) {
kRDF_instanceOf,
kXUL_element,
PR_TRUE,
&isXULElement))
&& isXULElement) {
// remove the child from the old collection // remove the child from the old collection
if (NS_FAILED(rv = rdf_ContainerRemoveElement(mDB, oldParent, newChild))) { rv = rdf_ContainerRemoveElement(mDB, oldParent, newChild);
NS_ERROR("unable to remove newChild from oldParent"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to remove newChild from oldParent");
return rv; if (NS_FAILED(rv)) return rv;
}
} }
} }
} }
// If the new parent has a resource... // If the new parent has a resource...
nsCOMPtr<nsIRDFResource> parent; nsCOMPtr<nsIRDFResource> parent;
if (NS_SUCCEEDED(rv = GetDOMNodeResource(aParent, getter_AddRefs(parent)))) { if (NS_SUCCEEDED(rv = GetRDFResourceFromXULElement(aParent, getter_AddRefs(parent)))) {
// ...and it's a XUL element... // ...and it's a XUL element...
PRBool isXULElement; PRBool isXULElement;
if (NS_SUCCEEDED(rv = mDB->HasAssertion(parent, rv = mDB->HasAssertion(parent, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement);
kRDF_instanceOf, if (NS_FAILED(rv)) return rv;
kXUL_element,
PR_TRUE,
&isXULElement))
&& isXULElement) {
if (isXULElement) {
// XXX For now, we panic if the refChild doesn't have a resouce // XXX For now, we panic if the refChild doesn't have a resouce
nsCOMPtr<nsIRDFResource> refChild; nsCOMPtr<nsIRDFNode> refChild;
if (NS_FAILED(rv = GetDOMNodeResource(aRefChild, getter_AddRefs(refChild)))) { rv = GetGraphNodeForXULElement(aRefChild, getter_AddRefs(refChild));
NS_ERROR("ref child doesn't have a resource"); NS_ASSERTION(NS_SUCCEEDED(rv), "ref child doesn't have a resource");
return rv; if (NS_FAILED(rv)) return rv;
}
// Determine the index of the refChild in the container // Determine the index of the refChild in the container
PRInt32 index; PRInt32 index;
if (NS_FAILED(rv = rdf_ContainerIndexOf(mDB, parent, refChild, &index))) { rv = rdf_ContainerIndexOf(mDB, parent, refChild, &index);
NS_ERROR("unable to determine index of refChild in container"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to determine index of refChild in container");
return rv; if (NS_FAILED(rv)) return rv;
}
// ...and insert the newChild before it. // ...and insert the newChild before it.
if (NS_FAILED(rv = rdf_ContainerInsertElementAt(mDB, parent, newChild, index))) { rv = rdf_ContainerInsertElementAt(mDB, parent, newChild, index);
NS_ERROR("unable to insert new element into container"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to insert new element into container");
return rv; if (NS_FAILED(rv)) return rv;
}
} }
} }
@ -1021,49 +1045,45 @@ RDFXULBuilderImpl::OnReplaceChild(nsIDOMNode* aParent, nsIDOMNode* aNewChild, ns
// XXX If aNewChild doesn't have a resource, then somebody is // XXX If aNewChild doesn't have a resource, then somebody is
// inserting a non-RDF element into aParent. Panic for now. // inserting a non-RDF element into aParent. Panic for now.
nsCOMPtr<nsIRDFResource> newChild; nsCOMPtr<nsIRDFNode> newChild;
if (NS_FAILED(rv = GetDOMNodeResource(aNewChild, getter_AddRefs(newChild)))) { rv = GetGraphNodeForXULElement(aNewChild, getter_AddRefs(newChild));
NS_ERROR("new child doesn't have a resource"); if (NS_FAILED(rv)) return rv;
return rv;
} if (rv == NS_RDF_NO_VALUE)
return NS_OK;
// XXX ibid // XXX ibid
nsCOMPtr<nsIRDFResource> oldChild; nsCOMPtr<nsIRDFNode> oldChild;
if (NS_FAILED(rv = GetDOMNodeResource(aOldChild, getter_AddRefs(oldChild)))) { rv = GetGraphNodeForXULElement(aOldChild, getter_AddRefs(oldChild));
NS_ERROR("old child doesn't have a resource"); if (NS_FAILED(rv)) return rv;
return rv;
}
if (rv == NS_RDF_NO_VALUE)
return NS_OK;
// Now if we can get the parent's resource...
nsCOMPtr<nsIRDFResource> parent; nsCOMPtr<nsIRDFResource> parent;
if (NS_SUCCEEDED(rv = GetDOMNodeResource(aParent, getter_AddRefs(parent)))) { if (NS_SUCCEEDED(rv = GetRDFResourceFromXULElement(aParent, getter_AddRefs(parent)))) {
// ...and it's a XUL element... // ...and it's a XUL element...
PRBool isXULElement; PRBool isXULElement;
rv = mDB->HasAssertion(parent, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement);
if (NS_FAILED(rv)) return rv;
if (NS_SUCCEEDED(rv = mDB->HasAssertion(parent, if (isXULElement) {
kRDF_instanceOf,
kXUL_element,
PR_TRUE,
&isXULElement))
&& isXULElement) {
// Remember the old child's index... // Remember the old child's index...
PRInt32 index; PRInt32 index;
if (NS_FAILED(rv = rdf_ContainerIndexOf(mDB, parent, oldChild, &index))) { rv = rdf_ContainerIndexOf(mDB, parent, oldChild, &index);
NS_ERROR("unable to get index of old child in container"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get index of old child in container");
return rv; if (NS_FAILED(rv)) return rv;
}
// ...then remove the old child from the old collection... // ...then remove the old child from the old collection...
if (NS_FAILED(rv = rdf_ContainerRemoveElement(mDB, parent, oldChild))) { rv = rdf_ContainerRemoveElement(mDB, parent, oldChild);
NS_ERROR("unable to remove old child from container"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to remove old child from container");
return rv; if (NS_FAILED(rv)) return rv;
}
// ...and add the new child to the collection at the old child's index // ...and add the new child to the collection at the old child's index
if (NS_FAILED(rv = rdf_ContainerInsertElementAt(mDB, parent, newChild, index))) { rv = rdf_ContainerInsertElementAt(mDB, parent, newChild, index);
NS_ERROR("unable to add new child to container"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to add new child to container");
return rv; if (NS_FAILED(rv)) return rv;
}
} }
} }
@ -1091,30 +1111,27 @@ RDFXULBuilderImpl::OnRemoveChild(nsIDOMNode* aParent, nsIDOMNode* aOldChild)
// XXX If aOldChild doesn't have a resource, then somebody is // XXX If aOldChild doesn't have a resource, then somebody is
// removing a non-RDF element from aParent. This probably isn't a // removing a non-RDF element from aParent. This probably isn't a
// big deal, but panic for now. // big deal, but panic for now.
nsCOMPtr<nsIRDFResource> oldChild; nsCOMPtr<nsIRDFNode> oldChild;
if (NS_FAILED(rv = GetDOMNodeResource(aOldChild, getter_AddRefs(oldChild)))) { rv = GetGraphNodeForXULElement(aOldChild, getter_AddRefs(oldChild));
NS_ERROR("new child doesn't have a resource"); if (NS_FAILED(rv)) return rv;
return rv;
} if (rv == NS_RDF_NO_VALUE)
return NS_OK;
// If the new parent has a resource... // If the new parent has a resource...
nsCOMPtr<nsIRDFResource> parent; nsCOMPtr<nsIRDFResource> parent;
if (NS_SUCCEEDED(rv = GetDOMNodeResource(aParent, getter_AddRefs(parent)))) { if (NS_SUCCEEDED(rv = GetRDFResourceFromXULElement(aParent, getter_AddRefs(parent)))) {
// ...and it's a XUL element... // ...and it's a XUL element...
PRBool isXULElement; PRBool isXULElement;
if (NS_SUCCEEDED(rv = mDB->HasAssertion(parent, rv = mDB->HasAssertion(parent, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement);
kRDF_instanceOf, if (NS_FAILED(rv)) return rv;
kXUL_element,
PR_TRUE,
&isXULElement))
&& isXULElement) {
if (isXULElement) {
// ...then remove it from the container // ...then remove it from the container
if (NS_FAILED(rv = rdf_ContainerRemoveElement(mDB, parent, oldChild))) { rv = rdf_ContainerRemoveElement(mDB, parent, oldChild);
NS_ERROR("unable to insert new element into container"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to insert new element into container");
return rv; if (NS_FAILED(rv)) return rv;
}
} }
} }
@ -1136,65 +1153,55 @@ RDFXULBuilderImpl::OnAppendChild(nsIDOMNode* aParent, nsIDOMNode* aNewChild)
nsresult rv; nsresult rv;
// XXX If aNewChild doesn't have a resource, then somebody is nsCOMPtr<nsIRDFNode> newChild;
// appending a non-RDF element into aParent. Panic for now. rv = GetGraphNodeForXULElement(aNewChild, getter_AddRefs(newChild));
nsCOMPtr<nsIRDFResource> newChild; if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv = GetDOMNodeResource(aNewChild, getter_AddRefs(newChild)))) {
NS_ERROR("new child doesn't have a resource"); if (rv == NS_RDF_NO_VALUE)
return rv; return NS_OK;
}
// If there was an old parent for newChild, then make sure to // If there was an old parent for newChild, then make sure to
// remove that relationship. // remove that relationship.
nsCOMPtr<nsIDOMNode> oldParentNode; nsCOMPtr<nsIDOMNode> oldParentNode;
if (NS_FAILED(rv = aNewChild->GetParentNode(getter_AddRefs(oldParentNode)))) { rv = aNewChild->GetParentNode(getter_AddRefs(oldParentNode));
NS_ERROR("unable to get new child's parent"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get new child's parent");
return rv; if (NS_FAILED(rv)) return rv;
}
if (oldParentNode) { if (oldParentNode) {
nsCOMPtr<nsIRDFResource> oldParent; nsCOMPtr<nsIRDFResource> oldParent;
// If the old parent has a resource... // If the old parent has a resource...
if (NS_SUCCEEDED(rv = GetDOMNodeResource(oldParentNode, getter_AddRefs(oldParent)))) { if (NS_SUCCEEDED(rv = GetRDFResourceFromXULElement(oldParentNode, getter_AddRefs(oldParent)))) {
// ...and it's a XUL element... // ...and it's a XUL element...
PRBool isXULElement; PRBool isXULElement;
if (NS_SUCCEEDED(rv = mDB->HasAssertion(oldParent, rv = mDB->HasAssertion(oldParent, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement);
kRDF_instanceOf, if (NS_FAILED(rv)) return rv;
kXUL_element,
PR_TRUE,
&isXULElement))
&& isXULElement) {
if (isXULElement) {
// remove the child from the old collection // remove the child from the old collection
if (NS_FAILED(rv = rdf_ContainerRemoveElement(mDB, oldParent, newChild))) { rv = rdf_ContainerRemoveElement(mDB, oldParent, newChild);
NS_ERROR("unable to remove newChild from oldParent"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to remove newChild from oldParent");
return rv; if (NS_FAILED(rv)) return rv;
}
} }
} }
} }
// If the new parent has a resource... // If the new parent has a resource...
nsCOMPtr<nsIRDFResource> parent; nsCOMPtr<nsIRDFResource> parent;
if (NS_SUCCEEDED(rv = GetDOMNodeResource(aParent, getter_AddRefs(parent)))) { if (NS_SUCCEEDED(rv = GetRDFResourceFromXULElement(aParent, getter_AddRefs(parent)))) {
// ...and it's a XUL element... // ...and it's a XUL element...
PRBool isXULElement; PRBool isXULElement;
if (NS_SUCCEEDED(rv = mDB->HasAssertion(parent, rv = mDB->HasAssertion(parent, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement);
kRDF_instanceOf, if (NS_FAILED(rv)) return rv;
kXUL_element,
PR_TRUE,
&isXULElement))
&& isXULElement) {
if (isXULElement) {
// ...then append it to the container // ...then append it to the container
if (NS_FAILED(rv = rdf_ContainerAppendElement(mDB, parent, newChild))) { rv = rdf_ContainerAppendElement(mDB, parent, newChild);
NS_ERROR("unable to insert new element into container"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to insert new element into container");
return rv; if (NS_FAILED(rv)) return rv;
}
} }
} }
@ -1212,7 +1219,7 @@ RDFXULBuilderImpl::OnSetAttribute(nsIDOMElement* aElement, const nsString& aName
nsresult rv; nsresult rv;
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetDOMNodeResource(aElement, getter_AddRefs(resource)))) { if (NS_FAILED(rv = GetRDFResourceFromXULElement(aElement, getter_AddRefs(resource)))) {
// XXX it's not a resource element, so there's no assertions // XXX it's not a resource element, so there's no assertions
// we need to make on the back-end. Should we just do the // we need to make on the back-end. Should we just do the
// update? // update?
@ -1320,7 +1327,7 @@ RDFXULBuilderImpl::OnRemoveAttribute(nsIDOMElement* aElement, const nsString& aN
nsresult rv; nsresult rv;
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetDOMNodeResource(aElement, getter_AddRefs(resource)))) { if (NS_FAILED(rv = GetRDFResourceFromXULElement(aElement, getter_AddRefs(resource)))) {
// XXX it's not a resource element, so there's no assertions // XXX it's not a resource element, so there's no assertions
// we need to make on the back-end. Should we just do the // we need to make on the back-end. Should we just do the
// update? // update?
@ -1396,6 +1403,18 @@ RDFXULBuilderImpl::AppendChild(nsINameSpace* aNameSpace,
nsIContent* aElement, nsIContent* aElement,
nsIRDFNode* aValue) nsIRDFNode* aValue)
{ {
NS_PRECONDITION(aNameSpace != nsnull, "null ptr");
if (! aNameSpace)
return NS_ERROR_NULL_POINTER;
NS_PRECONDITION(aElement != nsnull, "null ptr");
if (! aElement)
return NS_ERROR_NULL_POINTER;
NS_PRECONDITION(aValue != nsnull, "null ptr");
if (! aValue)
return NS_ERROR_NULL_POINTER;
nsresult rv; nsresult rv;
// Add the specified node as a child container of this // Add the specified node as a child container of this
@ -1414,6 +1433,28 @@ RDFXULBuilderImpl::AppendChild(nsINameSpace* aNameSpace,
return rv; return rv;
} }
#ifdef PR_LOGGING
if (PR_LOG_TEST(gLog, PR_LOG_DEBUG)) {
nsAutoString parentStr;
rv = nsRDFContentUtils::GetElementLogString(aElement, parentStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get parent element string");
nsAutoString childStr;
rv = nsRDFContentUtils::GetElementLogString(child, childStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get child element string");
char* parentCStr = parentStr.ToNewCString();
char* childCStr = childStr.ToNewCString();
PR_LOG(gLog, PR_LOG_DEBUG, ("xulbuilder append-child"));
PR_LOG(gLog, PR_LOG_DEBUG, (" %s", parentCStr));
PR_LOG(gLog, PR_LOG_DEBUG, (" %s", childCStr));
delete[] childCStr;
delete[] parentCStr;
}
#endif
if (NS_FAILED(rv = aElement->AppendChildTo(child, PR_TRUE))) { if (NS_FAILED(rv = aElement->AppendChildTo(child, PR_TRUE))) {
NS_ERROR("unable to add element to content model"); NS_ERROR("unable to add element to content model");
return rv; return rv;
@ -1442,6 +1483,14 @@ RDFXULBuilderImpl::AppendChild(nsINameSpace* aNameSpace,
nsresult nsresult
RDFXULBuilderImpl::RemoveChild(nsIContent* aElement, nsIRDFNode* aValue) RDFXULBuilderImpl::RemoveChild(nsIContent* aElement, nsIRDFNode* aValue)
{ {
NS_PRECONDITION(aElement != nsnull, "null ptr");
if (! aElement)
return NS_ERROR_NULL_POINTER;
NS_PRECONDITION(aValue != nsnull, "null ptr");
if (! aValue)
return NS_ERROR_NULL_POINTER;
nsresult rv; nsresult rv;
// Remove the specified node from the children of this // Remove the specified node from the children of this
@ -1459,16 +1508,33 @@ RDFXULBuilderImpl::RemoveChild(nsIContent* aElement, nsIRDFNode* aValue)
nsCOMPtr<nsIContent> child; nsCOMPtr<nsIContent> child;
aElement->ChildAt(count, *getter_AddRefs(child)); aElement->ChildAt(count, *getter_AddRefs(child));
// XXX can't identify HTML elements right now.
if (IsHTMLElement(child))
continue;
nsCOMPtr<nsIRDFResource> elementResource; nsCOMPtr<nsIRDFResource> elementResource;
rv = GetElementResource(child, getter_AddRefs(elementResource)); rv = nsRDFContentUtils::GetElementResource(child, getter_AddRefs(elementResource));
if (resource != elementResource) if (resource != elementResource)
continue; continue;
#ifdef PR_LOGGING
if (PR_LOG_TEST(gLog, PR_LOG_DEBUG)) {
nsAutoString parentStr;
rv = nsRDFContentUtils::GetElementLogString(aElement, parentStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get parent element string");
nsAutoString childStr;
rv = nsRDFContentUtils::GetElementLogString(child, childStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get child element string");
char* parentCStr = parentStr.ToNewCString();
char* childCStr = childStr.ToNewCString();
PR_LOG(gLog, PR_LOG_DEBUG, ("xulbuilder remove-child"));
PR_LOG(gLog, PR_LOG_DEBUG, (" %s", parentCStr));
PR_LOG(gLog, PR_LOG_DEBUG, (" %s", childCStr));
delete[] childCStr;
delete[] parentCStr;
}
#endif
// okay, found it. now blow it away... // okay, found it. now blow it away...
aElement->RemoveChildAt(count, PR_TRUE); aElement->RemoveChildAt(count, PR_TRUE);
return NS_OK; return NS_OK;
@ -1779,13 +1845,13 @@ RDFXULBuilderImpl::CreateXULElement(nsINameSpace* aContainingNameSpace,
nsresult rv; nsresult rv;
nsCOMPtr<nsIContent> element; nsCOMPtr<nsIContent> element;
if (NS_FAILED(rv = CreateResourceElement(aNameSpaceID, rv = CreateResourceElement(aNameSpaceID,
aTag, aTag,
aResource, aResource,
getter_AddRefs(element)))) { getter_AddRefs(element));
NS_ERROR("unable to create new content element");
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create new content element");
} if (NS_FAILED(rv)) return rv;
// Initialize the element's containing namespace to that of its // Initialize the element's containing namespace to that of its
// parent. // parent.
@ -1825,8 +1891,7 @@ RDFXULBuilderImpl::CreateXULElement(nsINameSpace* aContainingNameSpace,
// These are special beacuse they're used to specify the tree // These are special beacuse they're used to specify the tree
// structure of the XUL: ignore them b/c they're not attributes // structure of the XUL: ignore them b/c they're not attributes
if ((property.get() == kRDF_instanceOf) || if ((property.get() == kRDF_nextVal) ||
(property.get() == kRDF_nextVal) ||
(property.get() == kRDF_type) || (property.get() == kRDF_type) ||
rdf_IsOrdinalProperty(property)) rdf_IsOrdinalProperty(property))
continue; continue;
@ -1958,6 +2023,34 @@ RDFXULBuilderImpl::AddAttribute(nsIContent* aElement,
return rv; return rv;
} }
#ifdef PR_LOGGING
if (PR_LOG_TEST(gLog, PR_LOG_DEBUG)) {
nsAutoString elementStr;
rv = nsRDFContentUtils::GetElementLogString(aElement, elementStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get element string");
nsAutoString attrStr;
rv = nsRDFContentUtils::GetAttributeLogString(aElement, nameSpaceID, tag, attrStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get child element string");
nsAutoString valueStr;
rv = nsRDFContentUtils::GetTextForNode(aValue, valueStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get value text");
char* elementCStr = elementStr.ToNewCString();
char* attrCStr = attrStr.ToNewCString();
char* valueCStr = valueStr.ToNewCString();
PR_LOG(gLog, PR_LOG_DEBUG, ("xulbuilder add-attribute"));
PR_LOG(gLog, PR_LOG_DEBUG, (" %s", elementCStr));
PR_LOG(gLog, PR_LOG_DEBUG, (" %s=\"%s\"", attrCStr, valueCStr));
delete[] valueCStr;
delete[] attrCStr;
delete[] elementCStr;
}
#endif
if ((nameSpaceID == kNameSpaceID_XMLNS) || if ((nameSpaceID == kNameSpaceID_XMLNS) ||
((nameSpaceID == kNameSpaceID_None) && (tag.get() == kXMLNSAtom))) { ((nameSpaceID == kNameSpaceID_None) && (tag.get() == kXMLNSAtom))) {
// This is the receiving end of the namespace hack. The XUL // This is the receiving end of the namespace hack. The XUL
@ -2023,31 +2116,14 @@ RDFXULBuilderImpl::AddAttribute(nsIContent* aElement,
} }
} }
nsCOMPtr<nsIRDFResource> resource; nsAutoString value;
nsCOMPtr<nsIRDFLiteral> literal; rv = nsRDFContentUtils::GetTextForNode(aValue, value);
if (NS_FAILED(rv)) return rv;
// Now we need to figure out the attributes value and actually set rv = aElement->SetAttribute(nameSpaceID, tag, value, PR_TRUE);
// it on the element. What we do differs a bit depending on if (NS_FAILED(rv)) return rv;
// whether we're aValue is a resource or a literal.
if (NS_SUCCEEDED(rv = aValue->QueryInterface(kIRDFResourceIID,
(void**) getter_AddRefs(resource)))) {
nsXPIDLCString uri;
resource->GetValue( getter_Copies(uri) );
rv = aElement->SetAttribute(nameSpaceID, tag, (const char*) uri, PR_TRUE);
}
else if (NS_SUCCEEDED(rv = aValue->QueryInterface(kIRDFLiteralIID,
(void**) getter_AddRefs(literal)))) {
nsXPIDLString s;
literal->GetValue( getter_Copies(s) );
rv = aElement->SetAttribute(nameSpaceID, tag, (const PRUnichar*) s, PR_TRUE);
}
else {
// This should _never_ happen.
NS_ERROR("uh, this isn't a resource or a literal!");
rv = NS_ERROR_UNEXPECTED;
}
return rv; return NS_OK;
} }
@ -2066,6 +2142,34 @@ RDFXULBuilderImpl::RemoveAttribute(nsIContent* aElement,
return rv; return rv;
} }
#ifdef PR_LOGGING
if (PR_LOG_TEST(gLog, PR_LOG_DEBUG)) {
nsAutoString elementStr;
rv = nsRDFContentUtils::GetElementLogString(aElement, elementStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get element string");
nsAutoString attrStr;
rv = nsRDFContentUtils::GetAttributeLogString(aElement, nameSpaceID, tag, attrStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get child element string");
nsAutoString valueStr;
rv = nsRDFContentUtils::GetTextForNode(aValue, valueStr);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get value text");
char* elementCStr = elementStr.ToNewCString();
char* attrCStr = attrStr.ToNewCString();
char* valueCStr = valueStr.ToNewCString();
PR_LOG(gLog, PR_LOG_DEBUG, ("xulbuilder remove-attribute"));
PR_LOG(gLog, PR_LOG_DEBUG, (" %s", elementCStr));
PR_LOG(gLog, PR_LOG_DEBUG, (" %s=\"%s\"", attrCStr, valueCStr));
delete[] valueCStr;
delete[] attrCStr;
delete[] elementCStr;
}
#endif
if (IsHTMLElement(aElement)) { if (IsHTMLElement(aElement)) {
// XXX HTML elements are picky and only want attributes from // XXX HTML elements are picky and only want attributes from
// certain namespaces. We'll just assume that, if the // certain namespaces. We'll just assume that, if the
@ -2204,7 +2308,7 @@ RDFXULBuilderImpl::CreateBuilder(const nsCID& aBuilderCID, nsIContent* aElement,
nsresult nsresult
RDFXULBuilderImpl::GetDOMNodeResource(nsIDOMNode* aNode, nsIRDFResource** aResource) RDFXULBuilderImpl::GetRDFResourceFromXULElement(nsIDOMNode* aNode, nsIRDFResource** aResult)
{ {
nsresult rv; nsresult rv;
@ -2213,12 +2317,55 @@ RDFXULBuilderImpl::GetDOMNodeResource(nsIDOMNode* aNode, nsIRDFResource** aResou
// it. // it.
nsCOMPtr<nsIContent> element; nsCOMPtr<nsIContent> element;
if (NS_FAILED(rv = aNode->QueryInterface(kIContentIID, getter_AddRefs(element) ))) { rv = aNode->QueryInterface(kIContentIID, getter_AddRefs(element) );
NS_ERROR("DOM element doesn't support nsIContent"); NS_ASSERTION(NS_SUCCEEDED(rv), "DOM element doesn't support nsIContent");
return rv; if (NS_FAILED(rv)) return rv;
return nsRDFContentUtils::GetElementResource(element, aResult);
}
nsresult
RDFXULBuilderImpl::GetGraphNodeForXULElement(nsIDOMNode* aNode, nsIRDFNode** aResult)
{
nsresult rv;
nsCOMPtr<nsIDOMText> text( do_QueryInterface(aNode) );
if (text) {
nsAutoString data;
rv = text->GetData(data);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIRDFLiteral> literal;
rv = gRDFService->GetLiteral(data, getter_AddRefs(literal));
if (NS_FAILED(rv)) return rv;
*aResult = literal;
NS_ADDREF(*aResult);
}
else {
// XXX If aNode doesn't have a resource, then panic for
// now. (We may be able to safely just ignore this at some
// point.)
nsCOMPtr<nsIRDFResource> resource;
rv = GetRDFResourceFromXULElement(aNode, getter_AddRefs(resource));
NS_ASSERTION(NS_SUCCEEDED(rv), "new child doesn't have a resource");
if (NS_FAILED(rv)) return rv;
// If the node isn't marked as a XUL element in the graph,
// then we'll ignore it.
PRBool isXULElement;
rv = mDB->HasAssertion(resource, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement);
if (NS_FAILED(rv)) return rv;
if (! isXULElement)
return NS_RDF_NO_VALUE;
*aResult = resource;
NS_ADDREF(*aResult);
} }
return GetElementResource(element, aResource); return NS_OK;
} }
nsresult nsresult
@ -2283,48 +2430,3 @@ RDFXULBuilderImpl::GetResource(PRInt32 aNameSpaceID,
return rv; return rv;
} }
nsresult
RDFXULBuilderImpl::GetElementResource(nsIContent* aElement, nsIRDFResource** aResult)
{
// Perform a reverse mapping from an element in the content model
// to an RDF resource.
nsresult rv;
nsAutoString uri;
if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_None,
kIdAtom,
uri))) {
NS_ERROR("severe error retrieving attribute");
return rv;
}
if (rv != NS_CONTENT_ATTR_HAS_VALUE) {
NS_ERROR("tree element has no ID");
return NS_ERROR_UNEXPECTED;
}
// Since the element will store its ID attribute as a document-relative value,
// we may need to qualify it first...
NS_ASSERTION(mDocument != nsnull, "builder has no document -- can't fully qualify URI");
if (nsnull != mDocument) {
nsCOMPtr<nsIDocument> doc( do_QueryInterface(mDocument) );
NS_ASSERTION(doc, "doesn't support nsIDocument");
if (doc) {
nsIURL* docURL = nsnull;
doc->GetBaseURL(docURL);
if (docURL) {
const char* url;
docURL->GetSpec(&url);
rdf_PossiblyMakeAbsolute(url, uri);
NS_RELEASE(docURL);
}
}
}
if (NS_FAILED(rv = gRDFService->GetUnicodeResource(uri, aResult))) {
NS_ERROR("unable to create resource");
return rv;
}
return NS_OK;
}

Просмотреть файл

@ -29,23 +29,29 @@
*/ */
// Note the ALPHABETICAL ORDERING
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsDOMCID.h"
#include "nsIArena.h" #include "nsIArena.h"
#include "nsIContent.h"
#include "nsICSSParser.h" #include "nsICSSParser.h"
#include "nsICSSStyleSheet.h" #include "nsICSSStyleSheet.h"
#include "nsIContent.h"
#include "nsIDOMElementObserver.h" #include "nsIDOMElementObserver.h"
#include "nsIDOMNodeObserver.h" #include "nsIDOMNodeObserver.h"
#include "nsIDOMScriptObjectFactory.h" #include "nsIDOMScriptObjectFactory.h"
#include "nsIDOMSelection.h"
#include "nsIDOMStyleSheetCollection.h" #include "nsIDOMStyleSheetCollection.h"
#include "nsIDOMText.h"
#include "nsIDOMXULDocument.h" #include "nsIDOMXULDocument.h"
#include "nsIDOMXULElement.h"
#include "nsIDTD.h" #include "nsIDTD.h"
#include "nsIDocument.h" #include "nsIDocument.h"
#include "nsIDocumentObserver.h" #include "nsIDocumentObserver.h"
#include "nsIHTMLContentContainer.h"
#include "nsIHTMLCSSStyleSheet.h" #include "nsIHTMLCSSStyleSheet.h"
#include "nsIHTMLContentContainer.h"
#include "nsIHTMLStyleSheet.h" #include "nsIHTMLStyleSheet.h"
#include "nsIJSScriptObject.h" #include "nsIJSScriptObject.h"
#include "nsINameSpace.h"
#include "nsINameSpaceManager.h" #include "nsINameSpaceManager.h"
#include "nsIParser.h" #include "nsIParser.h"
#include "nsIPresContext.h" #include "nsIPresContext.h"
@ -60,32 +66,32 @@
#include "nsIScriptContextOwner.h" #include "nsIScriptContextOwner.h"
#include "nsIScriptGlobalObject.h" #include "nsIScriptGlobalObject.h"
#include "nsIScriptObjectOwner.h" #include "nsIScriptObjectOwner.h"
#include "nsIDOMSelection.h"
#include "nsIServiceManager.h" #include "nsIServiceManager.h"
#include "nsIStreamListener.h" #include "nsIStreamListener.h"
#include "nsIStyleContext.h"
#include "nsIStyleSet.h" #include "nsIStyleSet.h"
#include "nsIStyleSheet.h" #include "nsIStyleSheet.h"
#include "nsISupportsArray.h" #include "nsISupportsArray.h"
#include "nsITextContent.h"
#include "nsIURL.h" #include "nsIURL.h"
#include "nsIURLGroup.h" #include "nsIURLGroup.h"
#include "nsIWebShell.h" #include "nsIWebShell.h"
#include "nsIStyleContext.h"
#include "nsIXULContentSink.h"
#include "nsIDOMXULElement.h"
#include "nsIXULParentDocument.h"
#include "nsIXULChildDocument.h"
#include "nsIXMLContent.h" #include "nsIXMLContent.h"
#include "nsDOMCID.h" #include "nsIXULChildDocument.h"
#include "nsIXULContentSink.h"
#include "nsIXULParentDocument.h"
#include "nsLayoutCID.h" #include "nsLayoutCID.h"
#include "nsParserCIID.h" #include "nsParserCIID.h"
#include "nsRDFCID.h" #include "nsRDFCID.h"
#include "nsRDFContentUtils.h"
#include "nsRDFDOMNodeList.h" #include "nsRDFDOMNodeList.h"
#include "nsVoidArray.h" #include "nsVoidArray.h"
#include "nsXPIDLString.h" // XXX should go away #include "nsXPIDLString.h" // XXX should go away
#include "plhash.h" #include "plhash.h"
#include "plstr.h" #include "plstr.h"
#include "rdfutil.h"
#include "prlog.h" #include "prlog.h"
#include "rdfutil.h"
#include "rdf.h"
#include "nsILineBreakerFactory.h" #include "nsILineBreakerFactory.h"
#include "nsIWordBreakerFactory.h" #include "nsIWordBreakerFactory.h"
@ -93,57 +99,66 @@
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
static NS_DEFINE_IID(kICSSParserIID, NS_ICSS_PARSER_IID); // XXX grr.. static NS_DEFINE_IID(kICSSParserIID, NS_ICSS_PARSER_IID); // XXX grr..
static NS_DEFINE_IID(kIContentIID, NS_ICONTENT_IID); static NS_DEFINE_IID(kIContentIID, NS_ICONTENT_IID);
static NS_DEFINE_IID(kIDTDIID, NS_IDTD_IID); static NS_DEFINE_IID(kIDTDIID, NS_IDTD_IID);
static NS_DEFINE_IID(kIDOMScriptObjectFactoryIID, NS_IDOM_SCRIPT_OBJECT_FACTORY_IID); static NS_DEFINE_IID(kIDOMScriptObjectFactoryIID, NS_IDOM_SCRIPT_OBJECT_FACTORY_IID);
static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID); static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID);
static NS_DEFINE_IID(kIHTMLContentContainerIID, NS_IHTMLCONTENTCONTAINER_IID); static NS_DEFINE_IID(kIHTMLContentContainerIID, NS_IHTMLCONTENTCONTAINER_IID);
static NS_DEFINE_IID(kIHTMLStyleSheetIID, NS_IHTML_STYLE_SHEET_IID); static NS_DEFINE_IID(kIHTMLStyleSheetIID, NS_IHTML_STYLE_SHEET_IID);
static NS_DEFINE_IID(kIHTMLCSSStyleSheetIID, NS_IHTML_CSS_STYLE_SHEET_IID); static NS_DEFINE_IID(kIHTMLCSSStyleSheetIID, NS_IHTML_CSS_STYLE_SHEET_IID);
static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID); static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID);
static NS_DEFINE_IID(kINameSpaceManagerIID, NS_INAMESPACEMANAGER_IID); static NS_DEFINE_IID(kINameSpaceManagerIID, NS_INAMESPACEMANAGER_IID);
static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID); static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID);
static NS_DEFINE_IID(kIPresShellIID, NS_IPRESSHELL_IID); static NS_DEFINE_IID(kIPresShellIID, NS_IPRESSHELL_IID);
static NS_DEFINE_IID(kIRDFCompositeDataSourceIID, NS_IRDFCOMPOSITEDATASOURCE_IID); static NS_DEFINE_IID(kIRDFCompositeDataSourceIID, NS_IRDFCOMPOSITEDATASOURCE_IID);
static NS_DEFINE_IID(kIRDFContentModelBuilderIID, NS_IRDFCONTENTMODELBUILDER_IID); static NS_DEFINE_IID(kIRDFContentModelBuilderIID, NS_IRDFCONTENTMODELBUILDER_IID);
static NS_DEFINE_IID(kIRDFDataSourceIID, NS_IRDFDATASOURCE_IID); static NS_DEFINE_IID(kIRDFDataSourceIID, NS_IRDFDATASOURCE_IID);
static NS_DEFINE_IID(kIRDFDocumentIID, NS_IRDFDOCUMENT_IID); static NS_DEFINE_IID(kIRDFDocumentIID, NS_IRDFDOCUMENT_IID);
static NS_DEFINE_IID(kIRDFLiteralIID, NS_IRDFLITERAL_IID); static NS_DEFINE_IID(kIRDFLiteralIID, NS_IRDFLITERAL_IID);
static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID); static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID);
static NS_DEFINE_IID(kIRDFServiceIID, NS_IRDFSERVICE_IID); static NS_DEFINE_IID(kIRDFServiceIID, NS_IRDFSERVICE_IID);
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
static NS_DEFINE_IID(kIDOMSelectionIID, NS_IDOMSELECTION_IID); static NS_DEFINE_IID(kIDOMSelectionIID, NS_IDOMSELECTION_IID);
static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID); static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
static NS_DEFINE_IID(kIStreamObserverIID, NS_ISTREAMOBSERVER_IID); static NS_DEFINE_IID(kIStreamObserverIID, NS_ISTREAMOBSERVER_IID);
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIWebShellIID, NS_IWEB_SHELL_IID); static NS_DEFINE_IID(kIWebShellIID, NS_IWEB_SHELL_IID);
static NS_DEFINE_IID(kIXMLDocumentIID, NS_IXMLDOCUMENT_IID); static NS_DEFINE_IID(kIXMLDocumentIID, NS_IXMLDOCUMENT_IID);
static NS_DEFINE_IID(kIXULContentSinkIID, NS_IXULCONTENTSINK_IID); static NS_DEFINE_IID(kIXULContentSinkIID, NS_IXULCONTENTSINK_IID);
static NS_DEFINE_CID(kCSSParserCID, NS_CSSPARSER_CID); static NS_DEFINE_CID(kCSSParserCID, NS_CSSPARSER_CID);
static NS_DEFINE_CID(kDOMScriptObjectFactoryCID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID); static NS_DEFINE_CID(kDOMScriptObjectFactoryCID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID);
static NS_DEFINE_CID(kHTMLStyleSheetCID, NS_HTMLSTYLESHEET_CID); static NS_DEFINE_CID(kHTMLStyleSheetCID, NS_HTMLSTYLESHEET_CID);
static NS_DEFINE_CID(kHTMLCSSStyleSheetCID, NS_HTML_CSS_STYLESHEET_CID); static NS_DEFINE_CID(kHTMLCSSStyleSheetCID, NS_HTML_CSS_STYLESHEET_CID);
static NS_DEFINE_CID(kNameSpaceManagerCID, NS_NAMESPACEMANAGER_CID); static NS_DEFINE_CID(kNameSpaceManagerCID, NS_NAMESPACEMANAGER_CID);
static NS_DEFINE_CID(kParserCID, NS_PARSER_IID); // XXX static NS_DEFINE_CID(kParserCID, NS_PARSER_IID); // XXX
static NS_DEFINE_CID(kPresShellCID, NS_PRESSHELL_CID); static NS_DEFINE_CID(kPresShellCID, NS_PRESSHELL_CID);
static NS_DEFINE_CID(kRDFCompositeDataSourceCID, NS_RDFCOMPOSITEDATASOURCE_CID); static NS_DEFINE_CID(kRDFCompositeDataSourceCID, NS_RDFCOMPOSITEDATASOURCE_CID);
static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID); static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID);
static NS_DEFINE_CID(kLocalStoreCID, NS_LOCALSTORE_CID); static NS_DEFINE_CID(kLocalStoreCID, NS_LOCALSTORE_CID);
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID); static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
static NS_DEFINE_CID(kRDFXULBuilderCID, NS_RDFXULBUILDER_CID); static NS_DEFINE_CID(kRDFXULBuilderCID, NS_RDFXULBUILDER_CID);
static NS_DEFINE_CID(kRangeListCID, NS_RANGELIST_CID); static NS_DEFINE_CID(kRangeListCID, NS_RANGELIST_CID);
static NS_DEFINE_CID(kWellFormedDTDCID, NS_WELLFORMEDDTD_CID); static NS_DEFINE_CID(kTextNodeCID, NS_TEXTNODE_CID);
static NS_DEFINE_CID(kXULContentSinkCID, NS_XULCONTENTSINK_CID); static NS_DEFINE_CID(kWellFormedDTDCID, NS_WELLFORMEDDTD_CID);
static NS_DEFINE_CID(kXULDataSourceCID, NS_XULDATASOURCE_CID); static NS_DEFINE_CID(kXULContentSinkCID, NS_XULCONTENTSINK_CID);
static NS_DEFINE_CID(kXULDataSourceCID, NS_XULDATASOURCE_CID);
static NS_DEFINE_IID(kLWBrkCID, NS_LWBRK_CID); static NS_DEFINE_IID(kLWBrkCID, NS_LWBRK_CID);
static NS_DEFINE_IID(kILineBreakerFactoryIID, NS_ILINEBREAKERFACTORY_IID); static NS_DEFINE_IID(kILineBreakerFactoryIID, NS_ILINEBREAKERFACTORY_IID);
static NS_DEFINE_IID(kIWordBreakerFactoryIID, NS_IWORDBREAKERFACTORY_IID); static NS_DEFINE_IID(kIWordBreakerFactoryIID, NS_IWORDBREAKERFACTORY_IID);
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Standard vocabulary items
DEFINE_RDF_VOCAB(RDF_NAMESPACE_URI, RDF, instanceOf);
DEFINE_RDF_VOCAB(RDF_NAMESPACE_URI, RDF, type);
#define XUL_NAMESPACE_URI "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
#define XUL_NAMESPACE_URI_PREFIX XUL_NAMESPACE_URI "#"
DEFINE_RDF_VOCAB(XUL_NAMESPACE_URI_PREFIX, XUL, element);
static PRLogModuleInfo* gMapLog; static PRLogModuleInfo* gMapLog;
@ -592,12 +607,23 @@ public:
const nsString& aValue, const nsString& aValue,
nsRDFDOMNodeList* aElements); nsRDFDOMNodeList* aElements);
nsresult
ParseTagString(const nsString& aTagName, nsIAtom*& aName, PRInt32& aNameSpaceID);
nsresult
MakeProperty(PRInt32 aNameSpaceID, nsIAtom* aTag, nsIRDFResource** aResult);
protected: protected:
// pseudo constants // pseudo constants
static PRInt32 gRefCnt; static PRInt32 gRefCnt;
static nsIAtom* kIdAtom; static nsIAtom* kIdAtom;
static nsIAtom* kObservesAtom; static nsIAtom* kObservesAtom;
static nsIRDFService* gRDFService;
static nsIRDFResource* kRDF_instanceOf;
static nsIRDFResource* kRDF_type;
static nsIRDFResource* kXUL_element;
nsIContent* nsIContent*
FindContent(const nsIContent* aStartNode, FindContent(const nsIContent* aStartNode,
const nsIContent* aTest1, const nsIContent* aTest1,
@ -627,7 +653,6 @@ protected:
nsINameSpaceManager* mNameSpaceManager; nsINameSpaceManager* mNameSpaceManager;
nsIHTMLStyleSheet* mAttrStyleSheet; nsIHTMLStyleSheet* mAttrStyleSheet;
nsIHTMLCSSStyleSheet* mInlineStyleSheet; nsIHTMLCSSStyleSheet* mInlineStyleSheet;
nsIRDFService* mRDFService;
nsElementMap mResources; nsElementMap mResources;
nsISupportsArray* mBuilders; nsISupportsArray* mBuilders;
nsIRDFContentModelBuilder* mXULBuilder; nsIRDFContentModelBuilder* mXULBuilder;
@ -641,10 +666,15 @@ protected:
nsVoidArray mSubDocuments; nsVoidArray mSubDocuments;
}; };
PRInt32 XULDocumentImpl::gRefCnt; PRInt32 XULDocumentImpl::gRefCnt = 0;
nsIAtom* XULDocumentImpl::kIdAtom; nsIAtom* XULDocumentImpl::kIdAtom;
nsIAtom* XULDocumentImpl::kObservesAtom; nsIAtom* XULDocumentImpl::kObservesAtom;
nsIRDFService* XULDocumentImpl::gRDFService;
nsIRDFResource* XULDocumentImpl::kRDF_instanceOf;
nsIRDFResource* XULDocumentImpl::kRDF_type;
nsIRDFResource* XULDocumentImpl::kXUL_element;
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// ctors & dtors // ctors & dtors
@ -660,7 +690,6 @@ XULDocumentImpl::XULDocumentImpl(void)
mDisplaySelection(PR_FALSE), mDisplaySelection(PR_FALSE),
mNameSpaceManager(nsnull), mNameSpaceManager(nsnull),
mAttrStyleSheet(nsnull), mAttrStyleSheet(nsnull),
mRDFService(nsnull),
mBuilders(nsnull), mBuilders(nsnull),
mXULBuilder(nsnull), mXULBuilder(nsnull),
mLocalDataSource(nsnull), mLocalDataSource(nsnull),
@ -686,6 +715,20 @@ XULDocumentImpl::XULDocumentImpl(void)
if (gRefCnt++ == 0) { if (gRefCnt++ == 0) {
kIdAtom = NS_NewAtom("id"); kIdAtom = NS_NewAtom("id");
kObservesAtom = NS_NewAtom("observes"); kObservesAtom = NS_NewAtom("observes");
// Keep the RDF service cached in a member variable to make using
// it a bit less painful
rv = nsServiceManager::GetService(kRDFServiceCID,
kIRDFServiceIID,
(nsISupports**) &gRDFService);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get RDF Service");
if (gRDFService) {
gRDFService->GetResource(kURIRDF_instanceOf, &kRDF_instanceOf);
gRDFService->GetResource(kURIRDF_type, &kRDF_type);
gRDFService->GetResource(kURIXUL_element, &kXUL_element);
}
} }
#ifdef PR_LOGGING #ifdef PR_LOGGING
@ -699,11 +742,6 @@ XULDocumentImpl::~XULDocumentImpl()
NS_IF_RELEASE(mDocumentDataSource); NS_IF_RELEASE(mDocumentDataSource);
NS_IF_RELEASE(mLocalDataSource); NS_IF_RELEASE(mLocalDataSource);
if (mRDFService) {
nsServiceManager::ReleaseService(kRDFServiceCID, mRDFService);
mRDFService = nsnull;
}
// mParentDocument is never refcounted // mParentDocument is never refcounted
// Delete references to sub-documents // Delete references to sub-documents
PRInt32 index = mSubDocuments.Count(); PRInt32 index = mSubDocuments.Count();
@ -756,6 +794,15 @@ XULDocumentImpl::~XULDocumentImpl()
if (--gRefCnt == 0) { if (--gRefCnt == 0) {
NS_IF_RELEASE(kIdAtom); NS_IF_RELEASE(kIdAtom);
NS_IF_RELEASE(kObservesAtom); NS_IF_RELEASE(kObservesAtom);
if (gRDFService) {
nsServiceManager::ReleaseService(kRDFServiceCID, gRDFService);
gRDFService = nsnull;
}
NS_IF_RELEASE(kRDF_instanceOf);
NS_IF_RELEASE(kRDF_type);
NS_IF_RELEASE(kXUL_element);
} }
} }
@ -972,7 +1019,7 @@ XULDocumentImpl::StartDocumentLoad(nsIURL *aURL,
// XXX This needs to be cloned across windows, and the final // XXX This needs to be cloned across windows, and the final
// instance needs to be flushed to disk. It may be that this is // instance needs to be flushed to disk. It may be that this is
// really an RDFXML data source... // really an RDFXML data source...
rv = mRDFService->GetDataSource("rdf:local-store", &mLocalDataSource); rv = gRDFService->GetDataSource("rdf:local-store", &mLocalDataSource);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
NS_ERROR("couldn't create local data source"); NS_ERROR("couldn't create local data source");
@ -2230,8 +2277,77 @@ XULDocumentImpl::GetDocumentElement(nsIDOMElement** aDocumentElement)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::CreateElement(const nsString& aTagName, nsIDOMElement** aReturn) XULDocumentImpl::CreateElement(const nsString& aTagName, nsIDOMElement** aReturn)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aReturn != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aReturn)
return NS_ERROR_NULL_POINTER;
// we need this so that we can create a resource URI
NS_PRECONDITION(mDocumentURL != nsnull, "not initialized");
if (! mDocumentURL)
return NS_ERROR_NOT_INITIALIZED;
nsresult rv;
nsCOMPtr<nsIAtom> name;
PRInt32 nameSpaceID;
// parse the user-provided string into a tag name and a namespace ID
rv = ParseTagString(aTagName, *getter_AddRefs(name), nameSpaceID);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to parse tag name");
if (NS_FAILED(rv)) return rv;
// construct an element
nsCOMPtr<nsIContent> result;
rv = NS_NewRDFElement(nameSpaceID, name, getter_AddRefs(result));
if (NS_FAILED(rv)) return rv;
// assign it an "anonymous" identifier so that it can be referred
// to in the graph.
nsCOMPtr<nsIRDFResource> resource;
const char* context;
rv = mDocumentURL->GetSpec(&context);
if (NS_FAILED(rv)) return rv;
rv = rdf_CreateAnonymousResource(context, getter_AddRefs(resource));
if (NS_FAILED(rv)) return rv;
nsXPIDLCString uri;
rv = resource->GetValue(getter_Copies(uri));
if (NS_FAILED(rv)) return rv;
rv = result->SetAttribute(kNameSpaceID_None, kIdAtom, (const char*) uri, PR_FALSE);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set element's ID");
if (NS_FAILED(rv)) return rv;
// Set it's RDF:type in the graph s.t. the element's tag can be
// constructed from it.
nsCOMPtr<nsIRDFResource> type;
rv = MakeProperty(nameSpaceID, name, getter_AddRefs(type));
if (NS_FAILED(rv)) return rv;
rv = mDocumentDataSource->Assert(resource, kRDF_type, type, PR_TRUE);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set element's tag info in graph");
if (NS_FAILED(rv)) return rv;
// Mark it as a XUL element
rv = mDocumentDataSource->Assert(resource, kRDF_instanceOf, kXUL_element, PR_TRUE);
NS_ASSERTION(rv == NS_OK, "unable to mark as XUL element");
if (NS_FAILED(rv)) return rv;
rv = rdf_MakeSeq(mDocumentDataSource, resource);
NS_ASSERTION(rv == NS_OK, "unable to mark as XUL element");
if (NS_FAILED(rv)) return rv;
// `this' will be its document
rv = result->SetDocument(this, PR_FALSE);
if (NS_FAILED(rv)) return rv;
// get the DOM interface
rv = result->QueryInterface(nsIDOMElement::GetIID(), (void**) aReturn);
NS_ASSERTION(NS_SUCCEEDED(rv), "not a DOM element");
if (NS_FAILED(rv)) return rv;
return NS_OK;
} }
@ -2246,8 +2362,24 @@ XULDocumentImpl::CreateDocumentFragment(nsIDOMDocumentFragment** aReturn)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::CreateTextNode(const nsString& aData, nsIDOMText** aReturn) XULDocumentImpl::CreateTextNode(const nsString& aData, nsIDOMText** aReturn)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aReturn != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aReturn)
return NS_ERROR_NULL_POINTER;
nsresult rv;
nsCOMPtr<nsITextContent> text;
rv = nsComponentManager::CreateInstance(kTextNodeCID, nsnull, nsITextContent::GetIID(), getter_AddRefs(text));
if (NS_FAILED(rv)) return rv;
rv = text->SetText(aData.GetUnicode(), aData.Length(), PR_FALSE);
if (NS_FAILED(rv)) return rv;
rv = text->QueryInterface(nsIDOMText::GetIID(), (void**) aReturn);
NS_ASSERTION(NS_SUCCEEDED(rv), "not a DOMText");
if (NS_FAILED(rv)) return rv;
return NS_OK;
} }
@ -2383,7 +2515,7 @@ XULDocumentImpl::GetElementById(const nsString& aId, nsIDOMElement** aReturn)
rdf_PossiblyMakeAbsolute(documentURL, uri); rdf_PossiblyMakeAbsolute(documentURL, uri);
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = mRDFService->GetUnicodeResource(uri, getter_AddRefs(resource)))) { if (NS_FAILED(rv = gRDFService->GetUnicodeResource(uri, getter_AddRefs(resource)))) {
NS_ERROR("unable to get resource"); NS_ERROR("unable to get resource");
return rv; return rv;
} }
@ -2514,32 +2646,31 @@ XULDocumentImpl::GetFragmentRoot(nsIRDFResource** aFragmentRoot)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetNodeName(nsString& aNodeName) XULDocumentImpl::GetNodeName(nsString& aNodeName)
{ {
NS_NOTYETIMPLEMENTED("write me!"); aNodeName.SetString("#document");
return NS_ERROR_NOT_IMPLEMENTED; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetNodeValue(nsString& aNodeValue) XULDocumentImpl::GetNodeValue(nsString& aNodeValue)
{ {
NS_NOTYETIMPLEMENTED("write me!"); aNodeValue.Truncate();
return NS_ERROR_NOT_IMPLEMENTED; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::SetNodeValue(const nsString& aNodeValue) XULDocumentImpl::SetNodeValue(const nsString& aNodeValue)
{ {
NS_NOTYETIMPLEMENTED("write me!"); return NS_OK;
return NS_ERROR_NOT_IMPLEMENTED;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetNodeType(PRUint16* aNodeType) XULDocumentImpl::GetNodeType(PRUint16* aNodeType)
{ {
NS_NOTYETIMPLEMENTED("write me!"); *aNodeType = nsIDOMNode::DOCUMENT_NODE;
return NS_ERROR_NOT_IMPLEMENTED; return NS_OK;
} }
@ -2554,64 +2685,139 @@ XULDocumentImpl::GetParentNode(nsIDOMNode** aParentNode)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetChildNodes(nsIDOMNodeList** aChildNodes) XULDocumentImpl::GetChildNodes(nsIDOMNodeList** aChildNodes)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aChildNodes != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aChildNodes)
return NS_ERROR_NULL_POINTER;
if (mRootContent) {
nsresult rv;
*aChildNodes = nsnull;
nsRDFDOMNodeList* children;
rv = nsRDFDOMNodeList::Create(&children);
if (NS_SUCCEEDED(rv)) {
nsIDOMNode* domNode;
rv = mRootContent->QueryInterface(nsIDOMNode::GetIID(), (void**) domNode);
NS_ASSERTION(NS_SUCCEEDED(rv), "root content is not a DOM node");
if (NS_SUCCEEDED(rv)) {
rv = children->AppendNode(domNode);
NS_RELEASE(domNode);
*aChildNodes = children;
return NS_OK;
}
}
// If we get here, something bad happened.
NS_RELEASE(children);
return rv;
}
else {
*aChildNodes = nsnull;
return NS_OK;
}
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::HasChildNodes(PRBool* aHasChildNodes) XULDocumentImpl::HasChildNodes(PRBool* aHasChildNodes)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aHasChildNodes != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aHasChildNodes)
return NS_ERROR_NULL_POINTER;
if (mRootContent) {
*aHasChildNodes = PR_TRUE;
}
else {
*aHasChildNodes = PR_FALSE;
}
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetFirstChild(nsIDOMNode** aFirstChild) XULDocumentImpl::GetFirstChild(nsIDOMNode** aFirstChild)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aFirstChild != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aFirstChild)
return NS_ERROR_NULL_POINTER;
if (mRootContent) {
return mRootContent->QueryInterface(nsIDOMNode::GetIID(), (void**) aFirstChild);
}
else {
*aFirstChild = nsnull;
return NS_OK;
}
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetLastChild(nsIDOMNode** aLastChild) XULDocumentImpl::GetLastChild(nsIDOMNode** aLastChild)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aLastChild != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aLastChild)
return NS_ERROR_NULL_POINTER;
if (mRootContent) {
return mRootContent->QueryInterface(nsIDOMNode::GetIID(), (void**) aLastChild);
}
else {
*aLastChild = nsnull;
return NS_OK;
}
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetPreviousSibling(nsIDOMNode** aPreviousSibling) XULDocumentImpl::GetPreviousSibling(nsIDOMNode** aPreviousSibling)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aPreviousSibling != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aPreviousSibling)
return NS_ERROR_NULL_POINTER;
*aPreviousSibling = nsnull;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetNextSibling(nsIDOMNode** aNextSibling) XULDocumentImpl::GetNextSibling(nsIDOMNode** aNextSibling)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aNextSibling != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aNextSibling)
return NS_ERROR_NULL_POINTER;
*aNextSibling = nsnull;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetAttributes(nsIDOMNamedNodeMap** aAttributes) XULDocumentImpl::GetAttributes(nsIDOMNamedNodeMap** aAttributes)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aAttributes != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aAttributes)
return NS_ERROR_NULL_POINTER;
*aAttributes = nsnull;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::GetOwnerDocument(nsIDOMDocument** aOwnerDocument) XULDocumentImpl::GetOwnerDocument(nsIDOMDocument** aOwnerDocument)
{ {
NS_NOTYETIMPLEMENTED("write me!"); NS_PRECONDITION(aOwnerDocument != nsnull, "null ptr");
return NS_ERROR_NOT_IMPLEMENTED; if (! aOwnerDocument)
return NS_ERROR_NULL_POINTER;
*aOwnerDocument = nsnull;
return NS_OK;
} }
@ -2650,8 +2856,9 @@ XULDocumentImpl::AppendChild(nsIDOMNode* aNewChild, nsIDOMNode** aReturn)
NS_IMETHODIMP NS_IMETHODIMP
XULDocumentImpl::CloneNode(PRBool aDeep, nsIDOMNode** aReturn) XULDocumentImpl::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
{ {
NS_NOTYETIMPLEMENTED("write me!"); // We don't allow cloning of a document
return NS_ERROR_NOT_IMPLEMENTED; *aReturn = nsnull;
return NS_OK;
} }
@ -3093,7 +3300,7 @@ XULDocumentImpl::AddNamedDataSource(const char* uri)
nsresult rv; nsresult rv;
nsCOMPtr<nsIRDFDataSource> ds; nsCOMPtr<nsIRDFDataSource> ds;
if (NS_FAILED(rv = mRDFService->GetDataSource(uri, getter_AddRefs(ds)))) { if (NS_FAILED(rv = gRDFService->GetDataSource(uri, getter_AddRefs(ds)))) {
NS_ERROR("unable to get named datasource"); NS_ERROR("unable to get named datasource");
return rv; return rv;
} }
@ -3128,13 +3335,6 @@ XULDocumentImpl::Init(void)
(void**) &mNameSpaceManager))) (void**) &mNameSpaceManager)))
return rv; return rv;
// Keep the RDF service cached in a member variable to make using
// it a bit less painful
if (NS_FAILED(rv = nsServiceManager::GetService(kRDFServiceCID,
kIRDFServiceIID,
(nsISupports**) &mRDFService)))
return rv;
return NS_OK; return NS_OK;
} }
@ -3324,3 +3524,80 @@ XULDocumentImpl::GetElementsByAttribute(nsIDOMNode* aNode,
nsresult
XULDocumentImpl::ParseTagString(const nsString& aTagName, nsIAtom*& aName, PRInt32& aNameSpaceID)
{
// Parse the tag into a name and a namespace ID. This is slightly
// different than nsIContent::ParseAttributeString() because we
// take the default namespace into account (rather than just
// assigning "no namespace") in the case that there is no
// namespace prefix present.
static char kNameSpaceSeparator[] = ":";
// XXX this is a gross hack, but it'll carry us for now. We parse
// the tag name using the root content, which presumably has all
// the namespace info we need.
NS_PRECONDITION(mRootContent != nsnull, "not initialized");
if (! mRootContent)
return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(mRootContent) );
if (! xml) return NS_ERROR_UNEXPECTED;
nsresult rv;
nsCOMPtr<nsINameSpace> ns;
rv = xml->GetContainingNameSpace(*getter_AddRefs(ns));
if (NS_FAILED(rv)) return rv;
NS_ASSERTION(ns != nsnull, "expected xml namespace info to be available");
if (! ns)
return NS_ERROR_UNEXPECTED;
nsAutoString prefix;
nsAutoString name(aTagName);
PRInt32 nsoffset = name.Find(kNameSpaceSeparator);
if (-1 != nsoffset) {
name.Left(prefix, nsoffset);
name.Cut(0, nsoffset+1);
}
// Figure out the namespace ID
nsCOMPtr<nsIAtom> nameSpaceAtom;
if (0 < prefix.Length())
nameSpaceAtom = getter_AddRefs(NS_NewAtom(prefix));
rv = ns->FindNameSpaceID(nameSpaceAtom, aNameSpaceID);
if (NS_FAILED(rv))
aNameSpaceID = kNameSpaceID_None;
aName = NS_NewAtom(name);
return NS_OK;
}
nsresult
XULDocumentImpl::MakeProperty(PRInt32 aNameSpaceID, nsIAtom* aTag, nsIRDFResource** aResult)
{
// Using the namespace ID and the tag, construct a fully-qualified
// URI and turn it into an RDF property.
NS_PRECONDITION(mNameSpaceManager != nsnull, "not initialized");
if (! mNameSpaceManager)
return NS_ERROR_NOT_INITIALIZED;
nsresult rv;
nsAutoString uri;
rv = mNameSpaceManager->GetNameSpaceURI(aNameSpaceID, uri);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get URI for namespace");
if (NS_FAILED(rv)) return rv;
if (uri.Last() != PRUnichar('#') && uri.Last() != PRUnichar('/'))
uri.Append('#');
uri.Append(aTag->GetUnicode());
rv = gRDFService->GetUnicodeResource(uri, aResult);
return rv;
}

Просмотреть файл

@ -539,20 +539,22 @@ RDFElementImpl::GetParentNode(nsIDOMNode** aParentNode)
return mParent->QueryInterface(kIDOMNodeIID, (void**) aParentNode); return mParent->QueryInterface(kIDOMNodeIID, (void**) aParentNode);
} }
else if (mDocument) { else if (mDocument) {
// If we don't have a parent, but we're in the document, we must // XXX This is a mess because of our fun multiple inheritance heirarchy
// be the root node of the document. The DOM says that the root nsCOMPtr<nsIContent> root( dont_QueryInterface(mDocument->GetRootContent()) );
// is the document. nsCOMPtr<nsIContent> thisIContent;
return mDocument->QueryInterface(kIDOMNodeIID, (void**)aParentNode); QueryInterface(kIContentIID, getter_AddRefs(thisIContent));
}
else {
// A standalone element (i.e. one without a parent or a document)
// implicitly has a document fragment as its parent according to
// the DOM.
// XXX create a doc fragment here as a pseudo-parent. if (root == thisIContent) {
NS_NOTYETIMPLEMENTED("can't handle standalone RDF elements"); // If we don't have a parent, and we're the root content
return NS_ERROR_NOT_IMPLEMENTED; // of the document, DOM says that our parent is the
// document.
return mDocument->QueryInterface(kIDOMNodeIID, (void**)aParentNode);
}
} }
// A standalone element (i.e. one without a parent or a document)
*aParentNode = nsnull;
return NS_OK;
} }
@ -562,35 +564,35 @@ RDFElementImpl::GetChildNodes(nsIDOMNodeList** aChildNodes)
nsresult rv; nsresult rv;
nsRDFDOMNodeList* children; nsRDFDOMNodeList* children;
if (NS_FAILED(rv = nsRDFDOMNodeList::Create(&children))) { rv = nsRDFDOMNodeList::Create(&children);
NS_ERROR("unable to create DOM node list"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create DOM node list");
return rv; if (NS_FAILED(rv)) return rv;
}
PRInt32 count; PRInt32 count;
if (NS_SUCCEEDED(rv = ChildCount(count))) { if (NS_SUCCEEDED(rv = ChildCount(count))) {
for (PRInt32 index = 0; index < count; ++index) { for (PRInt32 index = 0; index < count; ++index) {
nsCOMPtr<nsIContent> child; nsCOMPtr<nsIContent> child;
if (NS_FAILED(rv = ChildAt(index, *getter_AddRefs(child)))) { rv = ChildAt(index, *getter_AddRefs(child));
NS_ERROR("unable to get child"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get child");
if (NS_FAILED(rv))
break; break;
}
nsCOMPtr<nsIDOMNode> domNode; nsCOMPtr<nsIDOMNode> domNode;
if (NS_FAILED(rv = child->QueryInterface(kIDOMNodeIID, (void**) getter_AddRefs(domNode)))) { rv = child->QueryInterface(kIDOMNodeIID, (void**) getter_AddRefs(domNode));
if (NS_FAILED(rv)) {
NS_WARNING("child content doesn't support nsIDOMNode"); NS_WARNING("child content doesn't support nsIDOMNode");
continue; continue;
} }
if (NS_FAILED(rv = children->AppendNode(domNode))) { rv = children->AppendNode(domNode);
NS_ERROR("unable to append node to list"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to append node to list");
if (NS_FAILED(rv))
break; break;
}
} }
} }
// Create() addref'd for us
*aChildNodes = children; *aChildNodes = children;
NS_ADDREF(*aChildNodes);
return NS_OK; return NS_OK;
} }
@ -1018,15 +1020,41 @@ RDFElementImpl::SetContainingNameSpace(nsINameSpace* aNameSpace)
NS_IMETHODIMP NS_IMETHODIMP
RDFElementImpl::GetContainingNameSpace(nsINameSpace*& aNameSpace) const RDFElementImpl::GetContainingNameSpace(nsINameSpace*& aNameSpace) const
{ {
nsresult rv;
if (mNameSpace) { if (mNameSpace) {
// If we have a namespace, return it.
NS_ADDREF(mNameSpace); NS_ADDREF(mNameSpace);
aNameSpace = mNameSpace; aNameSpace = mNameSpace;
return NS_OK; return NS_OK;
} }
else if (mParent) {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(mParent) ); // Next, try our parent.
nsCOMPtr<nsIContent> parent( dont_QueryInterface(mParent) );
while (parent) {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(parent) );
if (xml) if (xml)
return xml->GetContainingNameSpace(aNameSpace); return xml->GetContainingNameSpace(aNameSpace);
nsCOMPtr<nsIContent> temp = parent;
rv = temp->GetParent(*getter_AddRefs(parent));
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get parent");
if (NS_FAILED(rv)) return rv;
}
// Allright, we walked all the way to the top of our containment
// hierarchy and couldn't find a parent that supported
// nsIXMLContent. If we're in a document, try to doc's root
// element.
if (mDocument) {
nsCOMPtr<nsIContent> docroot
= dont_QueryInterface( mDocument->GetRootContent() );
if (docroot) {
nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(docroot) );
if (xml)
return xml->GetContainingNameSpace(aNameSpace);
}
} }
aNameSpace = nsnull; aNameSpace = nsnull;
@ -1616,23 +1644,8 @@ static char kNameSpaceSeparator[] = ":";
name.Cut(0, nsoffset+1); name.Cut(0, nsoffset+1);
} }
// Figure out the namespace ID // Figure out the namespace ID, defaulting to none if there is no
// namespace prefix.
// XXX This is currently broken, because _nobody_ will ever set
// the namespace scope via the nsIXMLElement interface. To make
// that work will require a bit more machinery: something that
// remembers the XML namespace scoping as nodes get created in the
// RDF graph, and can then extract them later when content nodes
// are built via the content model builders.
//
// Since mNameSpace will _always_ be null, specifying
// kNameSpaceID_None allows us to at least match on the
// tag. You'll start seeing problems when the same name is used in
// different namespaces.
//
// See http://bugzilla.mozilla.org/show_bug.cgi?id=3275 and 3334
// for more info.
aNameSpaceID = kNameSpaceID_None; aNameSpaceID = kNameSpaceID_None;
if (0 < prefix.Length()) { if (0 < prefix.Length()) {
nsCOMPtr<nsIAtom> nameSpaceAtom( getter_AddRefs(NS_NewAtom(prefix)) ); nsCOMPtr<nsIAtom> nameSpaceAtom( getter_AddRefs(NS_NewAtom(prefix)) );
@ -1658,9 +1671,6 @@ NS_IMETHODIMP
RDFElementImpl::GetNameSpacePrefixFromId(PRInt32 aNameSpaceID, RDFElementImpl::GetNameSpacePrefixFromId(PRInt32 aNameSpaceID,
nsIAtom*& aPrefix) nsIAtom*& aPrefix)
{ {
// XXX mNameSpace will _always_ be null, so this really depends on
// fixing Bugs 3275 & 3334, resolving how to map scoped namespace
// prefixes back and forth from the graph.
nsresult rv; nsresult rv;
nsCOMPtr<nsINameSpace> ns; nsCOMPtr<nsINameSpace> ns;
@ -2425,7 +2435,7 @@ RDFElementImpl::EnsureContentsGenerated(void) const
(void**) getter_AddRefs(rdfDoc)))) (void**) getter_AddRefs(rdfDoc))))
return rv; return rv;
rv = rdfDoc->CreateContents((nsIStyledContent*) unconstThis); rv = rdfDoc->CreateContents(NS_STATIC_CAST(nsIStyledContent*, unconstThis));
NS_ASSERTION(NS_SUCCEEDED(rv), "problem creating kids"); NS_ASSERTION(NS_SUCCEEDED(rv), "problem creating kids");
return rv; return rv;
} }

Просмотреть файл

@ -464,7 +464,7 @@ RDFGenericBuilderImpl::CreateContents(nsIContent* aElement)
// via the nsIContent interface allows us to support generic nodes // via the nsIContent interface allows us to support generic nodes
// that might get added in by DOM calls. // that might get added in by DOM calls.
nsCOMPtr<nsIRDFResource> resource; nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetElementResource(aElement, getter_AddRefs(resource)))) { if (NS_FAILED(rv = nsRDFContentUtils::GetElementResource(aElement, getter_AddRefs(resource)))) {
NS_ERROR("unable to get element resource"); NS_ERROR("unable to get element resource");
return rv; return rv;
} }
@ -1104,101 +1104,6 @@ RDFGenericBuilderImpl::OnRemoveAttributeNode(nsIDOMElement* aElement, nsIDOMAttr
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Implementation methods // Implementation methods
nsresult
RDFGenericBuilderImpl::FindChildByTag(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIContent** aChild)
{
nsresult rv;
PRInt32 count;
if (NS_FAILED(rv = aElement->ChildCount(count)))
return rv;
for (PRInt32 i = 0; i < count; ++i) {
nsCOMPtr<nsIContent> kid;
if (NS_FAILED(rv = aElement->ChildAt(i, *getter_AddRefs(kid))))
return rv; // XXX fatal
PRInt32 nameSpaceID;
if (NS_FAILED(rv = kid->GetNameSpaceID(nameSpaceID)))
return rv; // XXX fatal
if (nameSpaceID != aNameSpaceID)
continue; // wrong namespace
nsCOMPtr<nsIAtom> kidTag;
if (NS_FAILED(rv = kid->GetTag(*getter_AddRefs(kidTag))))
return rv; // XXX fatal
if (kidTag.get() != aTag)
continue;
*aChild = kid;
NS_ADDREF(*aChild);
return NS_OK;
}
return NS_RDF_NO_VALUE; // not found
}
nsresult
RDFGenericBuilderImpl::FindChildByTagAndResource(nsIContent* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aTag,
nsIRDFResource* aResource,
nsIContent** aChild)
{
nsresult rv;
PRInt32 count;
if (NS_FAILED(rv = aElement->ChildCount(count)))
return rv;
for (PRInt32 i = 0; i < count; ++i) {
nsCOMPtr<nsIContent> kid;
if (NS_FAILED(rv = aElement->ChildAt(i, *getter_AddRefs(kid))))
return rv; // XXX fatal
// Make sure it's a <xul:treecell>
PRInt32 nameSpaceID;
if (NS_FAILED(rv = kid->GetNameSpaceID(nameSpaceID)))
return rv; // XXX fatal
if (nameSpaceID != aNameSpaceID)
continue; // wrong namespace
nsCOMPtr<nsIAtom> tag;
if (NS_FAILED(rv = kid->GetTag(*getter_AddRefs(tag))))
return rv; // XXX fatal
if (tag.get() != aTag)
continue; // wrong tag
// Now get the resource ID from the RDF:ID attribute. We do it
// via the content model, because you're never sure who
// might've added this stuff in...
nsCOMPtr<nsIRDFResource> resource;
if (NS_FAILED(rv = GetElementResource(kid, getter_AddRefs(resource)))) {
NS_ERROR("severe error retrieving resource");
return rv;
}
if (resource.get() != aResource)
continue; // not the resource we want
// Fount it!
*aChild = kid;
NS_ADDREF(*aChild);
return NS_OK;
}
return NS_RDF_NO_VALUE; // not found
}
nsresult nsresult
RDFGenericBuilderImpl::EnsureElementHasGenericChild(nsIContent* parent, RDFGenericBuilderImpl::EnsureElementHasGenericChild(nsIContent* parent,
PRInt32 nameSpaceID, PRInt32 nameSpaceID,
@ -1207,7 +1112,7 @@ RDFGenericBuilderImpl::EnsureElementHasGenericChild(nsIContent* parent,
{ {
nsresult rv; nsresult rv;
rv = FindChildByTag(parent, nameSpaceID, tag, result); rv = nsRDFContentUtils::FindChildByTag(parent, nameSpaceID, tag, result);
if (NS_FAILED(rv)) if (NS_FAILED(rv))
return rv; return rv;
@ -1519,7 +1424,7 @@ RDFGenericBuilderImpl::GetDOMNodeResource(nsIDOMNode* aNode, nsIRDFResource** aR
return rv; return rv;
} }
return GetElementResource(element, aResource); return nsRDFContentUtils::GetElementResource(element, aResource);
} }
nsresult nsresult
@ -1531,15 +1436,23 @@ RDFGenericBuilderImpl::CreateResourceElement(PRInt32 aNameSpaceID,
nsresult rv; nsresult rv;
nsCOMPtr<nsIContent> result; nsCOMPtr<nsIContent> result;
if (NS_FAILED(rv = NS_NewRDFElement(aNameSpaceID, aTag, getter_AddRefs(result)))) rv = NS_NewRDFElement(aNameSpaceID, aTag, getter_AddRefs(result));
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create new RDFElement");
if (NS_FAILED(rv)) return rv;
nsXPIDLCString uri; nsXPIDLCString uri;
if (NS_FAILED(rv = aResource->GetValue( getter_Copies(uri) ))) rv = aResource->GetValue( getter_Copies(uri) );
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource URI");
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv = result->SetAttribute(kNameSpaceID_None, kIdAtom, (const char*) uri, PR_FALSE))) rv = result->SetAttribute(kNameSpaceID_None, kIdAtom, (const char*) uri, PR_FALSE);
return rv; NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set id attribute");
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIDocument> doc( do_QueryInterface(mDocument) );
rv = result->SetDocument(doc, PR_FALSE);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set element's document");
if (NS_FAILED(rv)) return rv;
*aResult = result; *aResult = result;
NS_ADDREF(*aResult); NS_ADDREF(*aResult);
@ -1607,7 +1520,9 @@ RDFGenericBuilderImpl::CloseWidgetItem(nsIContent* aElement)
parentNode = dont_QueryInterface(aElement); parentNode = dont_QueryInterface(aElement);
rv = NS_OK; rv = NS_OK;
} }
else rv = FindChildByTag(aElement, kNameSpaceID_XUL, parentAtom, getter_AddRefs(parentNode)); else {
rv = nsRDFContentUtils::FindChildByTag(aElement, kNameSpaceID_XUL, parentAtom, getter_AddRefs(parentNode));
}
if (rv == NS_RDF_NO_VALUE) { if (rv == NS_RDF_NO_VALUE) {
// No tag; must've already been closed // No tag; must've already been closed
@ -1651,47 +1566,3 @@ RDFGenericBuilderImpl::CloseWidgetItem(nsIContent* aElement)
} }
nsresult
RDFGenericBuilderImpl::GetElementResource(nsIContent* aElement, nsIRDFResource** aResult)
{
// Perform a reverse mapping from an element in the content model
// to an RDF resource.
nsresult rv;
nsAutoString uri;
if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_None,
kIdAtom,
uri))) {
NS_ERROR("severe error retrieving attribute");
return rv;
}
if (rv != NS_CONTENT_ATTR_HAS_VALUE) {
NS_ERROR("widget element has no ID");
return NS_ERROR_UNEXPECTED;
}
// Since the element will store its ID attribute as a document-relative value,
// we may need to qualify it first...
NS_ASSERTION(mDocument != nsnull, "builder has no document -- can't fully qualify URI");
if (nsnull != mDocument) {
nsCOMPtr<nsIDocument> doc( do_QueryInterface(mDocument) );
NS_ASSERTION(doc, "doesn't support nsIDocument");
if (doc) {
nsIURL* docURL = nsnull;
doc->GetBaseURL(docURL);
if (docURL) {
const char* url;
docURL->GetSpec(&url);
rdf_PossiblyMakeAbsolute(url, uri);
NS_RELEASE(docURL);
}
}
}
if (NS_FAILED(rv = gRDFService->GetUnicodeResource(uri, aResult))) {
NS_ERROR("unable to create resource");
return rv;
}
return NS_OK;
}