1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
#include "mozilla/dom/XPathEvaluator.h"
#include "mozilla/Move.h"
#include "nsCOMPtr.h"
#include "nsIAtom.h"
#include "mozilla/dom/XPathExpression.h"
#include "XPathResult.h"
#include "nsContentCID.h"
#include "txExpr.h"
#include "txExprParser.h"
#include "nsError.h"
#include "txURIUtils.h"
#include "nsIDocument.h"
#include "nsIDOMDocument.h"
#include "nsDOMString.h"
#include "nsNameSpaceManager.h"
#include "nsContentUtils.h"
#include "txIXPathContext.h"
#include "mozilla/dom/XPathEvaluatorBinding.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/XPathNSResolverBinding.h"
extern nsresult
TX_ResolveFunctionCallXPCOM(const nsCString &aContractID, int32_t aNamespaceID,
nsIAtom *aName, nsISupports *aState,
FunctionCall **aFunction);
namespace mozilla {
namespace dom {
// txIParseContext implementation
class XPathEvaluatorParseContext : public txIParseContext
{
public:
XPathEvaluatorParseContext(XPathNSResolver* aResolver,
bool aIsCaseSensitive)
: mResolver(aResolver),
mResolverNode(nullptr),
mLastError(NS_OK),
mIsCaseSensitive(aIsCaseSensitive)
{
}
XPathEvaluatorParseContext(nsINode* aResolver,
bool aIsCaseSensitive)
: mResolver(nullptr),
mResolverNode(aResolver),
mLastError(NS_OK),
mIsCaseSensitive(aIsCaseSensitive)
{
}
nsresult getError()
{
return mLastError;
}
nsresult resolveNamespacePrefix(nsIAtom* aPrefix, int32_t& aID);
nsresult resolveFunctionCall(nsIAtom* aName, int32_t aID,
FunctionCall** aFunction);
bool caseInsensitiveNameTests();
void SetErrorOffset(uint32_t aOffset);
private:
XPathNSResolver* mResolver;
nsINode* mResolverNode;
nsresult mLastError;
bool mIsCaseSensitive;
};
NS_IMPL_ISUPPORTS(XPathEvaluator, nsIDOMXPathEvaluator)
XPathEvaluator::XPathEvaluator(nsIDocument* aDocument)
: mDocument(do_GetWeakReference(aDocument))
{
}
XPathEvaluator::~XPathEvaluator()
{
}
NS_IMETHODIMP
XPathEvaluator::Evaluate(const nsAString & aExpression,
nsIDOMNode *aContextNode,
nsIDOMNode *aResolver,
uint16_t aType,
nsISupports *aInResult,
nsISupports **aResult)
{
nsCOMPtr<nsINode> resolver = do_QueryInterface(aResolver);
ErrorResult rv;
nsAutoPtr<XPathExpression> expression(CreateExpression(aExpression,
resolver, rv));
if (rv.Failed()) {
return rv.StealNSResult();
}
nsCOMPtr<nsINode> node = do_QueryInterface(aContextNode);
if (!node) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIXPathResult> inResult = do_QueryInterface(aInResult);
RefPtr<XPathResult> result =
expression->Evaluate(*node, aType,
static_cast<XPathResult*>(inResult.get()), rv);
if (rv.Failed()) {
return rv.StealNSResult();
}
*aResult = ToSupports(result.forget().take());
return NS_OK;
}
XPathExpression*
XPathEvaluator::CreateExpression(const nsAString& aExpression,
XPathNSResolver* aResolver, ErrorResult& aRv)
{
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
XPathEvaluatorParseContext pContext(aResolver,
!(doc && doc->IsHTMLDocument()));
return CreateExpression(aExpression, &pContext, doc, aRv);
}
XPathExpression*
XPathEvaluator::CreateExpression(const nsAString& aExpression,
nsINode* aResolver, ErrorResult& aRv)
{
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
XPathEvaluatorParseContext pContext(aResolver,
!(doc && doc->IsHTMLDocument()));
return CreateExpression(aExpression, &pContext, doc, aRv);
}
XPathExpression*
XPathEvaluator::CreateExpression(const nsAString & aExpression,
txIParseContext* aContext,
nsIDocument* aDocument,
ErrorResult& aRv)
{
if (!mRecycler) {
mRecycler = new txResultRecycler;
}
nsAutoPtr<Expr> expression;
aRv = txExprParser::createExpr(PromiseFlatString(aExpression), aContext,
getter_Transfers(expression));
if (aRv.Failed()) {
if (!aRv.ErrorCodeIs(NS_ERROR_DOM_NAMESPACE_ERR)) {
aRv.SuppressException();
aRv.Throw(NS_ERROR_DOM_INVALID_EXPRESSION_ERR);
}
return nullptr;
}
return new XPathExpression(Move(expression), mRecycler, aDocument);
}
bool
XPathEvaluator::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto,
JS::MutableHandle<JSObject*> aReflector)
{
return dom::XPathEvaluatorBinding::Wrap(aCx, this, aGivenProto, aReflector);
}
/* static */
already_AddRefed<XPathEvaluator>
XPathEvaluator::Constructor(const GlobalObject& aGlobal,
ErrorResult& rv)
{
RefPtr<XPathEvaluator> newObj = new XPathEvaluator(nullptr);
return newObj.forget();
}
already_AddRefed<XPathResult>
XPathEvaluator::Evaluate(JSContext* aCx, const nsAString& aExpression,
nsINode& aContextNode, XPathNSResolver* aResolver,
uint16_t aType, JS::Handle<JSObject*> aResult,
ErrorResult& rv)
{
nsAutoPtr<XPathExpression> expression(CreateExpression(aExpression,
aResolver, rv));
if (rv.Failed()) {
return nullptr;
}
return expression->Evaluate(aCx, aContextNode, aType, aResult, rv);
}
/*
* Implementation of txIParseContext private to XPathEvaluator, based on a
* XPathNSResolver
*/
nsresult XPathEvaluatorParseContext::resolveNamespacePrefix
(nsIAtom* aPrefix, int32_t& aID)
{
aID = kNameSpaceID_Unknown;
if (!mResolver && !mResolverNode) {
return NS_ERROR_DOM_NAMESPACE_ERR;
}
nsAutoString prefix;
if (aPrefix) {
aPrefix->ToString(prefix);
}
nsVoidableString ns;
if (mResolver) {
ErrorResult rv;
mResolver->LookupNamespaceURI(prefix, ns, rv);
if (rv.Failed()) {
return rv.StealNSResult();
}
} else {
if (aPrefix == nsGkAtoms::xml) {
ns.AssignLiteral("http://www.w3.org/XML/1998/namespace");
} else {
mResolverNode->LookupNamespaceURI(prefix, ns);
}
}
if (DOMStringIsNull(ns)) {
return NS_ERROR_DOM_NAMESPACE_ERR;
}
if (ns.IsEmpty()) {
aID = kNameSpaceID_None;
return NS_OK;
}
// get the namespaceID for the URI
return nsContentUtils::NameSpaceManager()->RegisterNameSpace(ns, aID);
}
nsresult
XPathEvaluatorParseContext::resolveFunctionCall(nsIAtom* aName,
int32_t aID,
FunctionCall** aFn)
{
return NS_ERROR_XPATH_UNKNOWN_FUNCTION;
}
bool XPathEvaluatorParseContext::caseInsensitiveNameTests()
{
return !mIsCaseSensitive;
}
void
XPathEvaluatorParseContext::SetErrorOffset(uint32_t aOffset)
{
}
} // namespace dom
} // namespace mozilla
|