From a8674b4e10581d538a902061aff172234fb373e4 Mon Sep 17 00:00:00 2001 From: "sicking%bigfoot.com" Date: Fri, 19 Apr 2002 13:26:19 +0000 Subject: [PATCH] Bug 132300. A better way to bootstrap XPathEvaluator by following the specs recommendation and making documents implement the interface. r=pike sr=jst a=asa git-svn-id: svn://10.0.0.236/branches/MOZILLA_1_0_BRANCH@119393 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/src/nsDocument.cpp | 54 ++++++++++++++++++- mozilla/content/base/src/nsDocument.h | 4 ++ mozilla/content/base/src/nsGenericElement.cpp | 14 +++++ mozilla/dom/public/nsDOMCID.h | 2 + mozilla/dom/src/base/nsDOMClassInfo.cpp | 24 +++++++-- .../build/XSLTProcessorModule.cpp | 2 +- .../source/xpath/nsXPathEvaluator.h | 3 -- 7 files changed, 95 insertions(+), 8 deletions(-) diff --git a/mozilla/content/base/src/nsDocument.cpp b/mozilla/content/base/src/nsDocument.cpp index d3149226a01..7be0d7f1253 100644 --- a/mozilla/content/base/src/nsDocument.cpp +++ b/mozilla/content/base/src/nsDocument.cpp @@ -440,12 +440,41 @@ nsDocumentChildNodes::DropReference() mDocument = nsnull; } +class nsXPathDocumentTearoff : public nsIDOMXPathEvaluator +{ +public: + nsXPathDocumentTearoff(nsIDOMXPathEvaluator* aEvaluator, + nsIDocument* aDocument) : mEvaluator(aEvaluator), + mDocument(aDocument) + { + } + virtual ~nsXPathDocumentTearoff() + { + } + + NS_DECL_ISUPPORTS_INHERITED + + NS_FORWARD_NSIDOMXPATHEVALUATOR(mEvaluator->) + +private: + nsCOMPtr mEvaluator; + nsIDocument* mDocument; +}; + +NS_INTERFACE_MAP_BEGIN(nsXPathDocumentTearoff) + NS_INTERFACE_MAP_ENTRY(nsIDOMXPathEvaluator) +NS_INTERFACE_MAP_END_AGGREGATED(mDocument) +NS_IMPL_ADDREF_USING_AGGREGATOR(nsXPathDocumentTearoff, mDocument) +NS_IMPL_RELEASE_USING_AGGREGATOR(nsXPathDocumentTearoff, mDocument) + + // ================================================================== // = // ================================================================== nsDocument::nsDocument() : mIsGoingAway(PR_FALSE), - mCSSLoader(nsnull) + mCSSLoader(nsnull), + mXPathDocument(nsnull) { NS_INIT_REFCNT(); @@ -478,6 +507,8 @@ nsDocument::nsDocument() : mIsGoingAway(PR_FALSE), nsDocument::~nsDocument() { + delete mXPathDocument; + // XXX Inform any remaining observers that we are going away. // Note that this currently contradicts the rule that all // observers must hold on to live references to the document. @@ -575,6 +606,9 @@ nsDocument::~nsDocument() } } +PRBool gCheckedForXPathDOM = PR_FALSE; +PRBool gHaveXPathDOM = PR_FALSE; + NS_INTERFACE_MAP_BEGIN(nsDocument) NS_INTERFACE_MAP_ENTRY(nsIDocument) NS_INTERFACE_MAP_ENTRY(nsIDOMDocument) @@ -592,6 +626,24 @@ NS_INTERFACE_MAP_BEGIN(nsDocument) NS_INTERFACE_MAP_ENTRY(nsIDOM3Node) NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDocument) + if (aIID.Equals(NS_GET_IID(nsIDOMXPathEvaluator)) && + (!gCheckedForXPathDOM || gHaveXPathDOM)) { + if (!mXPathDocument) { + nsCOMPtr evaluator; + nsresult rv; + evaluator = do_CreateInstance(NS_XPATH_EVALUATOR_CONTRACTID, &rv); + gCheckedForXPathDOM = PR_TRUE; + gHaveXPathDOM = (evaluator != nsnull); + if (rv == NS_ERROR_FACTORY_NOT_REGISTERED) { + return NS_ERROR_NO_INTERFACE; + } + NS_ENSURE_SUCCESS(rv, rv); + mXPathDocument = new nsXPathDocumentTearoff(evaluator, this); + NS_ENSURE_TRUE(mXPathDocument, NS_ERROR_OUT_OF_MEMORY); + } + foundInterface = mXPathDocument; + } + else NS_INTERFACE_MAP_END NS_IMPL_ADDREF(nsDocument) diff --git a/mozilla/content/base/src/nsDocument.h b/mozilla/content/base/src/nsDocument.h index 22b2e9d1b42..bc3aa3735e2 100644 --- a/mozilla/content/base/src/nsDocument.h +++ b/mozilla/content/base/src/nsDocument.h @@ -68,12 +68,14 @@ #include "nsIURI.h" #include "nsScriptLoader.h" #include "nsICSSLoader.h" +#include "nsIDOMXPathEvaluator.h" class nsIEventListenerManager; class nsDOMStyleSheetList; class nsIOutputStream; class nsDocument; class nsIDTD; +class nsXPathDocumentTearoff; #if 0 @@ -630,6 +632,8 @@ private: // These are not implemented and not supported. nsDocument(const nsDocument& aOther); nsDocument& operator=(const nsDocument& aOther); + + nsXPathDocumentTearoff* mXPathDocument; }; diff --git a/mozilla/content/base/src/nsGenericElement.cpp b/mozilla/content/base/src/nsGenericElement.cpp index 89d652f6549..df4473e6ec7 100644 --- a/mozilla/content/base/src/nsGenericElement.cpp +++ b/mozilla/content/base/src/nsGenericElement.cpp @@ -100,6 +100,7 @@ // baseURI #include "nsIXMLDocument.h" +#include "nsIDOMXPathEvaluator.h" #ifdef DEBUG_waterson @@ -828,6 +829,9 @@ nsGenericElement::SetPrefix(const nsAString& aPrefix) return NS_OK; } +extern PRBool gCheckedForXPathDOM; +extern PRBool gHaveXPathDOM; + nsresult nsGenericElement::InternalIsSupported(const nsAString& aFeature, const nsAString& aVersion, @@ -858,6 +862,16 @@ nsGenericElement::InternalIsSupported(const nsAString& aFeature, if (aVersion.IsEmpty() || aVersion.Equals(NS_LITERAL_STRING("2.0"))) { *aReturn = PR_TRUE; } + } else if ((!gCheckedForXPathDOM || gHaveXPathDOM) && + feature.Equals(NS_LITERAL_STRING("XPath"), nsCaseInsensitiveStringComparator()) && + (aVersion.IsEmpty() || aVersion.Equals(NS_LITERAL_STRING("3.0")))) { + if (!gCheckedForXPathDOM) { + nsCOMPtr evaluator; + evaluator = do_CreateInstance(NS_XPATH_EVALUATOR_CONTRACTID); + gHaveXPathDOM = (evaluator != nsnull); + gCheckedForXPathDOM = PR_TRUE; + } + *aReturn = gHaveXPathDOM; } return NS_OK; diff --git a/mozilla/dom/public/nsDOMCID.h b/mozilla/dom/public/nsDOMCID.h index 898985b86b2..0bd2009176b 100644 --- a/mozilla/dom/public/nsDOMCID.h +++ b/mozilla/dom/public/nsDOMCID.h @@ -60,4 +60,6 @@ #define NS_CRYPTO_CONTRACTID "@mozilla.org/security/crypto;1" #define NS_PKCS11_CONTRACTID "@mozilla.org/security/pkcs11;1" +#define NS_XPATH_EVALUATOR_CONTRACTID "@mozilla.org/dom/xpath-evaluator;1" + #endif /* nsDOMCID_h__ */ diff --git a/mozilla/dom/src/base/nsDOMClassInfo.cpp b/mozilla/dom/src/base/nsDOMClassInfo.cpp index b5abfd895dc..022ec8b2cd4 100644 --- a/mozilla/dom/src/base/nsDOMClassInfo.cpp +++ b/mozilla/dom/src/base/nsDOMClassInfo.cpp @@ -42,6 +42,7 @@ #include "nsCRT.h" #include "nsIServiceManager.h" #include "nsICategoryManager.h" +#include "nsIComponentRegistrar.h" #include "nsISupportsPrimitives.h" #include "nsIXPConnect.h" #include "nsIJSContextStack.h" @@ -267,6 +268,7 @@ #include "nsITreeSelection.h" #include "nsITreeContentView.h" #include "nsITreeView.h" +#include "nsIDOMXPathEvaluator.h" #ifdef MOZ_SVG #include "nsIDOMSVGAnimatedLength.h" @@ -1162,6 +1164,10 @@ nsDOMClassInfo::RegisterExternalClasses() DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget) \ DOM_CLASSINFO_MAP_ENTRY(nsIDOM3Node) +#define DOM_CLASSINFO_MAP_END_WITH_XPATH \ + xpathEvaluatorIID, \ + DOM_CLASSINFO_MAP_END + nsresult nsDOMClassInfo::Init() { @@ -1171,6 +1177,18 @@ nsDOMClassInfo::Init() "BAD! You'll need to adjust the size of PtrBits to the size " "of a pointer on your platform."); + nsCOMPtr cr; + NS_GetComponentRegistrar(getter_AddRefs(cr)); + const nsIID* xpathEvaluatorIID = nsnull; + if (cr) { + PRBool haveXPathDOM; + cr->IsContractIDRegistered(NS_XPATH_EVALUATOR_CONTRACTID, + &haveXPathDOM); + if (haveXPathDOM) { + xpathEvaluatorIID = &NS_GET_IID(nsIDOMXPathEvaluator); + } + } + DOM_CLASSINFO_MAP_BEGIN(Window, nsIDOMWindow) DOM_CLASSINFO_MAP_ENTRY(nsIDOMWindow) DOM_CLASSINFO_MAP_ENTRY(nsIDOMJSWindow) @@ -1231,7 +1249,7 @@ nsDOMClassInfo::Init() DOM_CLASSINFO_MAP_ENTRY(nsIDOMDocumentTraversal) DOM_CLASSINFO_MAP_ENTRY(nsIDOMDocumentXBL) DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget) - DOM_CLASSINFO_MAP_END + DOM_CLASSINFO_MAP_END_WITH_XPATH DOM_CLASSINFO_MAP_BEGIN(DocumentType, nsIDOMDocumentType) DOM_CLASSINFO_MAP_ENTRY(nsIDOMDocumentType) @@ -1333,7 +1351,7 @@ nsDOMClassInfo::Init() DOM_CLASSINFO_MAP_ENTRY(nsIDOMDocumentTraversal) DOM_CLASSINFO_MAP_ENTRY(nsIDOMDocumentXBL) DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget) - DOM_CLASSINFO_MAP_END + DOM_CLASSINFO_MAP_END_WITH_XPATH DOM_CLASSINFO_MAP_BEGIN(HTMLCollection, nsIDOMHTMLCollection) DOM_CLASSINFO_MAP_ENTRY(nsIDOMNodeList) @@ -1840,7 +1858,7 @@ nsDOMClassInfo::Init() DOM_CLASSINFO_MAP_ENTRY(nsIDOMDocumentXBL) DOM_CLASSINFO_MAP_ENTRY(nsIDOMDocumentStyle) DOM_CLASSINFO_MAP_ENTRY(nsIDOMDocumentRange) - DOM_CLASSINFO_MAP_END + DOM_CLASSINFO_MAP_END_WITH_XPATH DOM_CLASSINFO_MAP_BEGIN(SVGSVGElement, nsIDOMSVGSVGElement) DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget) diff --git a/mozilla/extensions/transformiix/build/XSLTProcessorModule.cpp b/mozilla/extensions/transformiix/build/XSLTProcessorModule.cpp index 38a9bdcce80..ee2cfdd63c3 100755 --- a/mozilla/extensions/transformiix/build/XSLTProcessorModule.cpp +++ b/mozilla/extensions/transformiix/build/XSLTProcessorModule.cpp @@ -232,7 +232,7 @@ static const nsModuleComponentInfo gComponents[] = { RegisterTransformiix }, { "XPathEvaluator", TRANSFORMIIX_XPATH_EVALUATOR_CID, - TRANSFORMIIX_XPATH_EVALUATOR_CONTRACTID, + NS_XPATH_EVALUATOR_CONTRACTID, nsXPathEvaluatorConstructor }, { "Transformiix Synchronous Loader", TRANSFORMIIX_SYNCLOADER_CID, diff --git a/mozilla/extensions/transformiix/source/xpath/nsXPathEvaluator.h b/mozilla/extensions/transformiix/source/xpath/nsXPathEvaluator.h index 4d183d428aa..f6e8e44e72e 100644 --- a/mozilla/extensions/transformiix/source/xpath/nsXPathEvaluator.h +++ b/mozilla/extensions/transformiix/source/xpath/nsXPathEvaluator.h @@ -66,7 +66,4 @@ private: #define TRANSFORMIIX_XPATH_EVALUATOR_CID \ { 0xd0a75e02, 0xb5e7, 0x11d5, { 0xa7, 0xf2, 0xdf, 0x10, 0x9f, 0xb8, 0xa1, 0xfc } } -#define TRANSFORMIIX_XPATH_EVALUATOR_CONTRACTID \ -"@mozilla.org/transformiix/xpath-evaluator;1" - #endif