2005-11-02 10:35:22 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-11-02 10:35:22 +03:00
|
|
|
|
2013-07-04 19:39:24 +04:00
|
|
|
#include "mozilla/dom/XPathEvaluator.h"
|
2014-05-08 02:05:37 +04:00
|
|
|
#include "mozilla/Move.h"
|
2005-11-02 10:35:22 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2005-11-02 10:42:24 +03:00
|
|
|
#include "nsIAtom.h"
|
2005-11-02 10:35:22 +03:00
|
|
|
#include "nsXPathExpression.h"
|
|
|
|
#include "nsXPathNSResolver.h"
|
|
|
|
#include "nsXPathResult.h"
|
2005-11-02 10:37:50 +03:00
|
|
|
#include "nsContentCID.h"
|
2005-11-02 20:34:14 +03:00
|
|
|
#include "txExpr.h"
|
|
|
|
#include "txExprParser.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2005-11-02 10:38:12 +03:00
|
|
|
#include "txURIUtils.h"
|
2005-11-02 10:40:13 +03:00
|
|
|
#include "nsIDocument.h"
|
2005-11-02 10:39:54 +03:00
|
|
|
#include "nsIDOMDocument.h"
|
2005-11-02 10:41:59 +03:00
|
|
|
#include "nsDOMString.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2005-11-02 20:34:14 +03:00
|
|
|
#include "nsContentUtils.h"
|
2012-12-14 23:10:49 +04:00
|
|
|
#include "mozilla/dom/XPathEvaluatorBinding.h"
|
|
|
|
|
2013-08-24 01:11:30 +04:00
|
|
|
extern nsresult
|
|
|
|
TX_ResolveFunctionCallXPCOM(const nsCString &aContractID, int32_t aNamespaceID,
|
|
|
|
nsIAtom *aName, nsISupports *aState,
|
|
|
|
FunctionCall **aFunction);
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2005-11-02 10:35:22 +03:00
|
|
|
|
2005-11-18 16:39:43 +03:00
|
|
|
// txIParseContext implementation
|
2013-08-24 01:11:30 +04:00
|
|
|
class XPathEvaluatorParseContext : public txIParseContext
|
2005-11-18 16:39:43 +03:00
|
|
|
{
|
|
|
|
public:
|
2013-08-24 01:11:30 +04:00
|
|
|
XPathEvaluatorParseContext(nsIDOMXPathNSResolver* aResolver,
|
|
|
|
bool aIsCaseSensitive)
|
2012-08-10 22:01:16 +04:00
|
|
|
: mResolver(aResolver),
|
2005-11-18 16:39:43 +03:00
|
|
|
mLastError(NS_OK),
|
|
|
|
mIsCaseSensitive(aIsCaseSensitive)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult getError()
|
|
|
|
{
|
|
|
|
return mLastError;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult resolveNamespacePrefix(nsIAtom* aPrefix, int32_t& aID);
|
|
|
|
nsresult resolveFunctionCall(nsIAtom* aName, int32_t aID,
|
2006-11-17 00:42:25 +03:00
|
|
|
FunctionCall** aFunction);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool caseInsensitiveNameTests();
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetErrorOffset(uint32_t aOffset);
|
2005-11-18 16:39:43 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsIDOMXPathNSResolver* mResolver;
|
|
|
|
nsresult mLastError;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mIsCaseSensitive;
|
2005-11-18 16:39:43 +03:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(XPathEvaluator, nsIDOMXPathEvaluator)
|
2005-11-02 10:35:22 +03:00
|
|
|
|
2013-07-04 19:39:24 +04:00
|
|
|
XPathEvaluator::XPathEvaluator(nsIDocument* aDocument)
|
|
|
|
: mDocument(do_GetWeakReference(aDocument))
|
2005-11-02 10:35:22 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-24 01:11:30 +04:00
|
|
|
XPathEvaluator::CreateNSResolver(nsIDOMNode *aNodeResolver,
|
|
|
|
nsIDOMXPathNSResolver **aResult)
|
2005-11-02 10:35:22 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aNodeResolver);
|
2005-11-02 20:34:14 +03:00
|
|
|
if (!nsContentUtils::CanCallerAccess(aNodeResolver))
|
2005-11-02 10:38:12 +03:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2005-11-02 10:35:22 +03:00
|
|
|
*aResult = new nsXPathNSResolver(aNodeResolver);
|
|
|
|
NS_ENSURE_TRUE(*aResult, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-24 01:11:30 +04:00
|
|
|
XPathEvaluator::Evaluate(const nsAString & aExpression,
|
|
|
|
nsIDOMNode *aContextNode,
|
|
|
|
nsIDOMXPathNSResolver *aResolver,
|
|
|
|
uint16_t aType,
|
|
|
|
nsISupports *aInResult,
|
|
|
|
nsISupports **aResult)
|
2005-11-02 10:35:22 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMXPathExpression> expression;
|
|
|
|
nsresult rv = CreateExpression(aExpression, aResolver,
|
|
|
|
getter_AddRefs(expression));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return expression->Evaluate(aContextNode, aType, aInResult, aResult);
|
|
|
|
}
|
2005-11-02 10:37:50 +03:00
|
|
|
|
2005-11-02 10:39:52 +03:00
|
|
|
|
2006-07-13 18:21:53 +04:00
|
|
|
NS_IMETHODIMP
|
2013-08-24 01:11:30 +04:00
|
|
|
XPathEvaluator::CreateExpression(const nsAString & aExpression,
|
|
|
|
nsIDOMXPathNSResolver *aResolver,
|
|
|
|
nsIDOMXPathExpression **aResult)
|
2006-07-13 18:21:53 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (!mRecycler) {
|
|
|
|
nsRefPtr<txResultRecycler> recycler = new txResultRecycler;
|
|
|
|
NS_ENSURE_TRUE(recycler, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
rv = recycler->init();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mRecycler = recycler;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
|
2013-07-04 19:29:29 +04:00
|
|
|
XPathEvaluatorParseContext pContext(aResolver, !(doc && doc->IsHTML()));
|
2006-07-13 18:21:53 +04:00
|
|
|
|
|
|
|
nsAutoPtr<Expr> expression;
|
|
|
|
rv = txExprParser::createExpr(PromiseFlatString(aExpression), &pContext,
|
|
|
|
getter_Transfers(expression));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv == NS_ERROR_DOM_NAMESPACE_ERR) {
|
|
|
|
return NS_ERROR_DOM_NAMESPACE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_DOM_INVALID_EXPRESSION_ERR;
|
|
|
|
}
|
|
|
|
|
2006-08-10 17:05:05 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> document = do_QueryReferent(mDocument);
|
|
|
|
|
2014-05-08 02:05:37 +04:00
|
|
|
*aResult = new nsXPathExpression(Move(expression), mRecycler, document);
|
2006-07-13 18:21:53 +04:00
|
|
|
if (!*aResult) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-14 23:10:49 +04:00
|
|
|
JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
XPathEvaluator::WrapObject(JSContext* aCx)
|
2012-12-14 23:10:49 +04:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-09 02:27:17 +04:00
|
|
|
return dom::XPathEvaluatorBinding::Wrap(aCx, this);
|
2012-12-14 23:10:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2013-08-24 01:11:30 +04:00
|
|
|
already_AddRefed<XPathEvaluator>
|
|
|
|
XPathEvaluator::Constructor(const GlobalObject& aGlobal,
|
|
|
|
ErrorResult& rv)
|
2012-12-14 23:10:49 +04:00
|
|
|
{
|
2013-08-24 01:11:30 +04:00
|
|
|
nsRefPtr<XPathEvaluator> newObj = new XPathEvaluator(nullptr);
|
2012-12-14 23:10:49 +04:00
|
|
|
return newObj.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIDOMXPathExpression>
|
2013-08-24 01:11:30 +04:00
|
|
|
XPathEvaluator::CreateExpression(const nsAString& aExpression,
|
|
|
|
nsIDOMXPathNSResolver* aResolver,
|
|
|
|
ErrorResult& rv)
|
2012-12-14 23:10:49 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMXPathExpression> expr;
|
|
|
|
rv = CreateExpression(aExpression, aResolver, getter_AddRefs(expr));
|
|
|
|
return expr.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIDOMXPathNSResolver>
|
2013-08-24 01:11:30 +04:00
|
|
|
XPathEvaluator::CreateNSResolver(nsINode* aNodeResolver,
|
|
|
|
ErrorResult& rv)
|
2012-12-14 23:10:49 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> nodeResolver = do_QueryInterface(aNodeResolver);
|
|
|
|
nsCOMPtr<nsIDOMXPathNSResolver> res;
|
|
|
|
rv = CreateNSResolver(nodeResolver, getter_AddRefs(res));
|
|
|
|
return res.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsISupports>
|
2013-08-24 01:11:30 +04:00
|
|
|
XPathEvaluator::Evaluate(const nsAString& aExpression, nsINode* aContextNode,
|
|
|
|
nsIDOMXPathNSResolver* aResolver, uint16_t aType,
|
|
|
|
nsISupports* aResult, ErrorResult& rv)
|
2012-12-14 23:10:49 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> contextNode = do_QueryInterface(aContextNode);
|
|
|
|
nsCOMPtr<nsISupports> res;
|
|
|
|
rv = Evaluate(aExpression, contextNode, aResolver, aType,
|
|
|
|
aResult, getter_AddRefs(res));
|
|
|
|
return res.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-02 10:37:50 +03:00
|
|
|
/*
|
2013-08-24 01:11:30 +04:00
|
|
|
* Implementation of txIParseContext private to XPathEvaluator, based on a
|
2005-11-18 16:39:43 +03:00
|
|
|
* nsIDOMXPathNSResolver
|
2005-11-02 10:37:50 +03:00
|
|
|
*/
|
|
|
|
|
2013-08-24 01:11:30 +04:00
|
|
|
nsresult XPathEvaluatorParseContext::resolveNamespacePrefix
|
2012-08-22 19:56:38 +04:00
|
|
|
(nsIAtom* aPrefix, int32_t& aID)
|
2005-11-02 10:37:50 +03:00
|
|
|
{
|
2005-11-02 10:40:54 +03:00
|
|
|
aID = kNameSpaceID_Unknown;
|
|
|
|
|
|
|
|
if (!mResolver) {
|
|
|
|
return NS_ERROR_DOM_NAMESPACE_ERR;
|
|
|
|
}
|
|
|
|
|
2005-11-02 10:37:50 +03:00
|
|
|
nsAutoString prefix;
|
|
|
|
if (aPrefix) {
|
|
|
|
aPrefix->ToString(prefix);
|
|
|
|
}
|
2005-11-02 10:40:54 +03:00
|
|
|
|
2005-11-02 10:41:16 +03:00
|
|
|
nsVoidableString ns;
|
2005-11-02 10:40:54 +03:00
|
|
|
nsresult rv = mResolver->LookupNamespaceURI(prefix, ns);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-11-02 10:37:50 +03:00
|
|
|
|
2005-11-02 10:41:16 +03:00
|
|
|
if (DOMStringIsNull(ns)) {
|
|
|
|
return NS_ERROR_DOM_NAMESPACE_ERR;
|
|
|
|
}
|
|
|
|
|
2005-11-02 10:37:50 +03:00
|
|
|
if (ns.IsEmpty()) {
|
2005-11-02 10:40:54 +03:00
|
|
|
aID = kNameSpaceID_None;
|
|
|
|
|
2005-11-02 10:37:50 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the namespaceID for the URI
|
2005-11-02 20:34:14 +03:00
|
|
|
return nsContentUtils::NameSpaceManager()->RegisterNameSpace(ns, aID);
|
2005-11-02 10:37:50 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 10:41:11 +03:00
|
|
|
nsresult
|
2013-08-24 01:11:30 +04:00
|
|
|
XPathEvaluatorParseContext::resolveFunctionCall(nsIAtom* aName,
|
|
|
|
int32_t aID,
|
|
|
|
FunctionCall** aFn)
|
2005-11-02 10:37:50 +03:00
|
|
|
{
|
2013-07-04 19:29:29 +04:00
|
|
|
return NS_ERROR_XPATH_UNKNOWN_FUNCTION;
|
2005-11-02 10:37:50 +03:00
|
|
|
}
|
|
|
|
|
2013-08-24 01:11:30 +04:00
|
|
|
bool XPathEvaluatorParseContext::caseInsensitiveNameTests()
|
2005-11-02 10:39:52 +03:00
|
|
|
{
|
2005-11-02 10:40:13 +03:00
|
|
|
return !mIsCaseSensitive;
|
2005-11-02 10:39:52 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 10:41:11 +03:00
|
|
|
void
|
2013-08-24 01:11:30 +04:00
|
|
|
XPathEvaluatorParseContext::SetErrorOffset(uint32_t aOffset)
|
2005-11-02 10:37:50 +03:00
|
|
|
{
|
|
|
|
}
|
2013-08-24 01:11:30 +04:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|