From f4dd6c43ce0ce339d8a74dc4dca58b974fe06d30 Mon Sep 17 00:00:00 2001 From: "allan%beaufour.dk" Date: Thu, 2 Jun 2005 06:53:42 +0000 Subject: [PATCH] Support context size and position in ns(XForms)XPathExpression. Bug 292841, r=sicking, sr=peterv, a=mkaply --- dom/public/idl/xpath/Makefile.in | 1 + .../idl/xpath/nsIDOMNSXPathExpression.idl | 68 +++++++++++++++++++ .../build/XSLTProcessorModule.cpp | 2 + .../source/xpath/nsIXFormsXPathEvaluator.h | 22 +++--- .../source/xpath/nsXFormsXPathEvaluator.cpp | 9 ++- .../source/xpath/nsXPathExpression.cpp | 22 +++++- .../source/xpath/nsXPathExpression.h | 15 +++- 7 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 dom/public/idl/xpath/nsIDOMNSXPathExpression.idl diff --git a/dom/public/idl/xpath/Makefile.in b/dom/public/idl/xpath/Makefile.in index 5fb9f0bd53e8..1d6d243f67a4 100644 --- a/dom/public/idl/xpath/Makefile.in +++ b/dom/public/idl/xpath/Makefile.in @@ -52,6 +52,7 @@ XPIDLSRCS = \ nsIDOMXPathNamespace.idl \ nsIDOMXPathNSResolver.idl \ nsIDOMXPathResult.idl \ + nsIDOMNSXPathExpression.idl \ $(NULL) include $(topsrcdir)/config/rules.mk diff --git a/dom/public/idl/xpath/nsIDOMNSXPathExpression.idl b/dom/public/idl/xpath/nsIDOMNSXPathExpression.idl new file mode 100644 index 000000000000..ff7aed58ef12 --- /dev/null +++ b/dom/public/idl/xpath/nsIDOMNSXPathExpression.idl @@ -0,0 +1,68 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Novell, inc. + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Allan Beaufour (original author) + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "domstubs.idl" + +interface nsIDOMXPathResult; +interface XPathException; + +/** + * Interface for Mozilla specific XPathExpression functions. + */ +[scriptable, uuid(ce600ca8-e98a-4419-ad61-2f6d0cb0ecc8)] +interface nsIDOMNSXPathExpression : nsISupports +{ + /** + * Evaluate the expression with the given context. Similar to + * nsIDOMXPathExpression::evaluate(), except that this takes the context + * position and size too. + * + * @param contextNode The context node + * @param contextPosition The context position + * @param contextSize The context size + * @param type The needed result type + * @param result The result + */ + nsISupports evaluateWithContext(in nsIDOMNode contextNode, + in unsigned long contextPosition, + in unsigned long contextSize, + in unsigned short type, + in nsISupports result) + raises(XPathException, + DOMException); +}; diff --git a/extensions/transformiix/build/XSLTProcessorModule.cpp b/extensions/transformiix/build/XSLTProcessorModule.cpp index 7368bd425627..5b1278127026 100755 --- a/extensions/transformiix/build/XSLTProcessorModule.cpp +++ b/extensions/transformiix/build/XSLTProcessorModule.cpp @@ -38,6 +38,7 @@ #include "nsICategoryManager.h" #include "nsIDOMClassInfo.h" +#include "nsIDOMNSXPathExpression.h" #include "nsIDOMXPathExpression.h" #include "nsIDOMXPathNSResolver.h" #include "nsIDOMXPathResult.h" @@ -98,6 +99,7 @@ NS_DOMCI_EXTENSION(Transformiix) NS_DOMCI_EXTENSION_ENTRY_BEGIN(XPathExpression) NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIDOMXPathExpression) + NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIDOMNSXPathExpression) NS_DOMCI_EXTENSION_ENTRY_END(XPathExpression, nsIDOMXPathExpression, PR_TRUE, nsnull) diff --git a/extensions/transformiix/source/xpath/nsIXFormsXPathEvaluator.h b/extensions/transformiix/source/xpath/nsIXFormsXPathEvaluator.h index c80d41988f50..d6163bd6c886 100644 --- a/extensions/transformiix/source/xpath/nsIXFormsXPathEvaluator.h +++ b/extensions/transformiix/source/xpath/nsIXFormsXPathEvaluator.h @@ -47,22 +47,24 @@ #define NS_NO_VTABLE #endif class nsIDOMNode; /* forward declaration */ -class nsIDOMXPathExpression; /* forward declaration */ +class nsIDOMNSXPathExpression; /* forward declaration */ /* starting interface: nsIXFormsXPathEvaluator */ #define NS_XFORMS_XPATH_EVALUATOR_CONTRACTID "@mozilla.org/dom/xforms-xpath-evaluator;1" -/* a7e127c6-31ff-40b4-8780-15d6938b33d3 */ +/* 4cdd884f-f949-4d82-bb78-b8edd9f1420c */ #define TRANSFORMIIX_XFORMS_XPATH_EVALUATOR_CID \ -{ 0xa7e127c6, 0x31ff, 0x40b4, { 0x87, 0x80, 0x15, 0xd6, 0x93, 0x8b, 0x33, 0xd3 } } +{ 0x4cdd884f, 0xf949, 0x4d82, \ + {0xbb, 0x78, 0xb8, 0xed, 0xd9, 0xf1, 0x42, 0x0c} } -/* 60050a4a-4c99-4bad-8c47-3b9b96caf2b4 */ +/* 61e5a446-73f7-432e-a2d6-d94d4a51aed8 */ #define TRANSFORMIIX_XFORMS_XPATH_EVALUATOR_IID \ -{ 0x60050a4a, 0x4c99, 0x4bad, { 0x8c, 0x47, 0x3b, 0x9b, 0x96, 0xca, 0xf2, 0xb4 } } +{ 0x61e5a446, 0x73f7, 0x432e, \ + {0xa2, 0xd6, 0xd9, 0x4d, 0x4a, 0x51, 0xae, 0xd8} } /* Use this macro when declaring classes that implement this interface. */ #define NS_DECL_NSIXFORMXPATHEVALUATOR \ - NS_IMETHOD CreateExpression(const nsAString & aExpression, nsIDOMNode *aResolverNode,nsIDOMXPathExpression **aResult); \ - NS_IMETHOD Evaluate(const nsAString & aExpression, nsIDOMNode *aContextNode, nsIDOMNode *aResolverNode, PRUint16 aType, nsISupports *aInResult, nsISupports **aResult); + NS_IMETHOD CreateExpression(const nsAString & aExpression, nsIDOMNode *aResolverNode, nsIDOMNSXPathExpression **aResult); \ + NS_IMETHOD Evaluate(const nsAString & aExpression, nsIDOMNode *aContextNode, PRUint32 aContextPosition, PRUint32 aContextSize, nsIDOMNode *aResolverNode, PRUint16 aType, nsISupports *aInResult, nsISupports **aResult); /** * Private interface implemented by the nsXFormsXPathEvaluator in Transformiix @@ -79,18 +81,18 @@ class NS_NO_VTABLE nsIXFormsXPathEvaluator : public nsISupports { NS_DEFINE_STATIC_IID_ACCESSOR(TRANSFORMIIX_XFORMS_XPATH_EVALUATOR_IID) /** - * Function to create a nsIDOMXPathExpression from the provided expression + * Function to create a nsIDOMNSXPathExpression from the provided expression * string. aResolverNode is the xforms node that the expression is * associated with. */ - NS_IMETHOD CreateExpression(const nsAString & aExpression, nsIDOMNode *aResolverNode, nsIDOMXPathExpression **aResult) = 0; + NS_IMETHOD CreateExpression(const nsAString & aExpression, nsIDOMNode *aResolverNode, nsIDOMNSXPathExpression **aResult) = 0; /** * Function to evaluate the given expression. aResolverNode is the xforms * node that the expression is associated with. The other parameters are as * required by DOM's XPathEvaluator. */ - NS_IMETHOD Evaluate(const nsAString & aExpression, nsIDOMNode *aContextNode, nsIDOMNode *aResolverNode, PRUint16 aType, nsISupports *aInResult, nsISupports **aResult) = 0; + NS_IMETHOD Evaluate(const nsAString & aExpression, nsIDOMNode *aContextNode, PRUint32 aContextPosition, PRUint32 aContextSize, nsIDOMNode *aResolverNode, PRUint16 aType, nsISupports *aInResult, nsISupports **aResult) = 0; }; diff --git a/extensions/transformiix/source/xpath/nsXFormsXPathEvaluator.cpp b/extensions/transformiix/source/xpath/nsXFormsXPathEvaluator.cpp index c8c0d243426f..915bfaf6801f 100644 --- a/extensions/transformiix/source/xpath/nsXFormsXPathEvaluator.cpp +++ b/extensions/transformiix/source/xpath/nsXFormsXPathEvaluator.cpp @@ -78,7 +78,7 @@ nsXFormsXPathEvaluator::~nsXFormsXPathEvaluator() NS_IMETHODIMP nsXFormsXPathEvaluator::CreateExpression(const nsAString & aExpression, nsIDOMNode *aResolverNode, - nsIDOMXPathExpression **aResult) + nsIDOMNSXPathExpression **aResult) { nsresult rv = NS_OK; if (!mRecycler) { @@ -116,6 +116,8 @@ nsXFormsXPathEvaluator::CreateExpression(const nsAString & aExpression, NS_IMETHODIMP nsXFormsXPathEvaluator::Evaluate(const nsAString & aExpression, nsIDOMNode *aContextNode, + PRUint32 aPosition, + PRUint32 aSize, nsIDOMNode *aResolverNode, PRUint16 aType, nsISupports *aInResult, @@ -124,12 +126,13 @@ nsXFormsXPathEvaluator::Evaluate(const nsAString & aExpression, // XXX Need to check document of aContextNode if created by // QI'ing a document. - nsCOMPtr expression; + nsCOMPtr expression; nsresult rv = CreateExpression(aExpression, aResolverNode, getter_AddRefs(expression)); NS_ENSURE_SUCCESS(rv, rv); - return expression->Evaluate(aContextNode, aType, aInResult, aResult); + return expression->EvaluateWithContext(aContextNode, aPosition, aSize, + aType, aInResult, aResult); } diff --git a/extensions/transformiix/source/xpath/nsXPathExpression.cpp b/extensions/transformiix/source/xpath/nsXPathExpression.cpp index 817d978efe03..16454e6f4e93 100644 --- a/extensions/transformiix/source/xpath/nsXPathExpression.cpp +++ b/extensions/transformiix/source/xpath/nsXPathExpression.cpp @@ -53,6 +53,7 @@ NS_IMPL_ADDREF(nsXPathExpression) NS_IMPL_RELEASE(nsXPathExpression) NS_INTERFACE_MAP_BEGIN(nsXPathExpression) NS_INTERFACE_MAP_ENTRY(nsIDOMXPathExpression) + NS_INTERFACE_MAP_ENTRY(nsIDOMNSXPathExpression) NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMXPathExpression) NS_INTERFACE_MAP_ENTRY_EXTERNAL_DOM_CLASSINFO(XPathExpression) NS_INTERFACE_MAP_END @@ -73,9 +74,23 @@ nsXPathExpression::Evaluate(nsIDOMNode *aContextNode, PRUint16 aType, nsISupports *aInResult, nsISupports **aResult) +{ + return EvaluateWithContext(aContextNode, 1, 1, aType, aInResult, aResult); +} + +NS_IMETHODIMP +nsXPathExpression::EvaluateWithContext(nsIDOMNode *aContextNode, + PRUint32 aContextPosition, + PRUint32 aContextSize, + PRUint16 aType, + nsISupports *aInResult, + nsISupports **aResult) { NS_ENSURE_ARG(aContextNode); + if (aContextPosition > aContextSize) + return NS_ERROR_FAILURE; + if (!URIUtils::CanCallerAccess(aContextNode)) return NS_ERROR_DOM_SECURITY_ERR; @@ -116,7 +131,8 @@ nsXPathExpression::Evaluate(nsIDOMNode *aContextNode, return NS_ERROR_OUT_OF_MEMORY; } - EvalContextImpl eContext(*contextNode, mRecycler); + EvalContextImpl eContext(*contextNode, aContextPosition, aContextSize, + mRecycler); nsRefPtr exprResult; rv = mExpression->evaluate(&eContext, getter_AddRefs(exprResult)); NS_ENSURE_SUCCESS(rv, rv); @@ -200,10 +216,10 @@ const txXPathNode& nsXPathExpression::EvalContextImpl::getContextNode() PRUint32 nsXPathExpression::EvalContextImpl::size() { - return 1; + return mContextSize; } PRUint32 nsXPathExpression::EvalContextImpl::position() { - return 1; + return mContextPosition; } diff --git a/extensions/transformiix/source/xpath/nsXPathExpression.h b/extensions/transformiix/source/xpath/nsXPathExpression.h index 183d93eebc7e..8284b7d97598 100644 --- a/extensions/transformiix/source/xpath/nsXPathExpression.h +++ b/extensions/transformiix/source/xpath/nsXPathExpression.h @@ -40,6 +40,7 @@ #define nsXPathExpression_h__ #include "nsIDOMXPathExpression.h" +#include "nsIDOMNSXPathExpression.h" #include "txIXPathContext.h" #include "txResultRecycler.h" #include "nsAutoPtr.h" @@ -50,7 +51,8 @@ class txXPathNode; /** * A class for evaluating an XPath expression string */ -class nsXPathExpression : public nsIDOMXPathExpression +class nsXPathExpression : public nsIDOMXPathExpression, + public nsIDOMNSXPathExpression { public: nsXPathExpression(nsAutoPtr& aExpression, @@ -63,6 +65,9 @@ public: // nsIDOMXPathExpression interface NS_DECL_NSIDOMXPATHEXPRESSION + // nsIDOMNSXPathExpression interface + NS_DECL_NSIDOMNSXPATHEXPRESSION + private: nsAutoPtr mExpression; nsRefPtr mRecycler; @@ -70,8 +75,12 @@ private: class EvalContextImpl : public txIEvalContext { public: - EvalContextImpl(const txXPathNode& aContextNode, txResultRecycler* aRecycler) + EvalContextImpl(const txXPathNode& aContextNode, + PRUint32 aContextPosition, PRUint32 aContextSize, + txResultRecycler* aRecycler) : mContextNode(aContextNode), + mContextPosition(aContextPosition), + mContextSize(aContextSize), mLastError(NS_OK), mRecycler(aRecycler) { @@ -90,6 +99,8 @@ private: private: const txXPathNode& mContextNode; + PRUint32 mContextPosition; + PRUint32 mContextSize; nsresult mLastError; nsRefPtr mRecycler; };