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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "XPathExpression.h"
#include <utility>
#include "XPathResult.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/Text.h"
#include "mozilla/dom/XPathResultBinding.h"
#include "nsError.h"
#include "nsINode.h"
#include "txExpr.h"
#include "txExprResult.h"
#include "txIXPathContext.h"
#include "txURIUtils.h"
#include "txXPathTreeWalker.h"
namespace mozilla::dom {
class EvalContextImpl : public txIEvalContext {
public:
EvalContextImpl(const txXPathNode& aContextNode, uint32_t aContextPosition,
uint32_t aContextSize, txResultRecycler* aRecycler)
: mContextNode(aContextNode),
mContextPosition(aContextPosition),
mContextSize(aContextSize),
mLastError(NS_OK),
mRecycler(aRecycler) {}
nsresult getError() { return mLastError; }
TX_DECL_EVAL_CONTEXT;
private:
const txXPathNode& mContextNode;
uint32_t mContextPosition;
uint32_t mContextSize;
nsresult mLastError;
RefPtr<txResultRecycler> mRecycler;
};
XPathExpression::XPathExpression(UniquePtr<Expr>&& aExpression,
txResultRecycler* aRecycler,
Document* aDocument)
: mExpression(std::move(aExpression)),
mRecycler(aRecycler),
mDocument(do_GetWeakReference(aDocument)),
mCheckDocument(aDocument != nullptr) {}
XPathExpression::~XPathExpression() = default;
already_AddRefed<XPathResult> XPathExpression::EvaluateWithContext(
JSContext* aCx, nsINode& aContextNode, uint32_t aContextPosition,
uint32_t aContextSize, uint16_t aType, JS::Handle<JSObject*> aInResult,
ErrorResult& aRv) {
RefPtr<XPathResult> inResult;
if (aInResult) {
nsresult rv = UNWRAP_OBJECT(XPathResult, aInResult, inResult);
if (NS_FAILED(rv) && rv != NS_ERROR_XPC_BAD_CONVERT_JS) {
aRv.Throw(rv);
return nullptr;
}
}
return EvaluateWithContext(aContextNode, aContextPosition, aContextSize,
aType, inResult, aRv);
}
already_AddRefed<XPathResult> XPathExpression::EvaluateWithContext(
nsINode& aContextNode, uint32_t aContextPosition, uint32_t aContextSize,
uint16_t aType, XPathResult* aInResult, ErrorResult& aRv) {
if (aContextPosition > aContextSize) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
if (aType > XPathResult_Binding::FIRST_ORDERED_NODE_TYPE) {
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return nullptr;
}
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
!nsContentUtils::CanCallerAccess(&aContextNode)) {
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
return nullptr;
}
if (mCheckDocument) {
nsCOMPtr<Document> doc = do_QueryReferent(mDocument);
if (doc != aContextNode.OwnerDoc()) {
aRv.Throw(NS_ERROR_DOM_WRONG_DOCUMENT_ERR);
return nullptr;
}
}
uint16_t nodeType = aContextNode.NodeType();
if (nodeType == nsINode::TEXT_NODE ||
nodeType == nsINode::CDATA_SECTION_NODE) {
Text* textNode = aContextNode.GetAsText();
MOZ_ASSERT(textNode);
uint32_t textLength = textNode->Length();
if (textLength == 0) {
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return nullptr;
}
// XXX Need to get logical XPath text node for CDATASection
// and Text nodes.
} else if (nodeType != nsINode::DOCUMENT_NODE &&
nodeType != nsINode::ELEMENT_NODE &&
nodeType != nsINode::ATTRIBUTE_NODE &&
nodeType != nsINode::COMMENT_NODE &&
nodeType != nsINode::PROCESSING_INSTRUCTION_NODE) {
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return nullptr;
}
UniquePtr<txXPathNode> contextNode(
txXPathNativeNode::createXPathNode(&aContextNode));
if (!contextNode) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
EvalContextImpl eContext(*contextNode, aContextPosition, aContextSize,
mRecycler);
RefPtr<txAExprResult> exprResult;
aRv = mExpression->evaluate(&eContext, getter_AddRefs(exprResult));
if (aRv.Failed()) {
return nullptr;
}
uint16_t resultType = aType;
if (aType == XPathResult::ANY_TYPE) {
short exprResultType = exprResult->getResultType();
switch (exprResultType) {
case txAExprResult::NUMBER:
resultType = XPathResult::NUMBER_TYPE;
break;
case txAExprResult::STRING:
resultType = XPathResult::STRING_TYPE;
break;
case txAExprResult::BOOLEAN:
resultType = XPathResult::BOOLEAN_TYPE;
break;
case txAExprResult::NODESET:
resultType = XPathResult::UNORDERED_NODE_ITERATOR_TYPE;
break;
case txAExprResult::RESULT_TREE_FRAGMENT:
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
}
RefPtr<XPathResult> xpathResult = aInResult;
if (!xpathResult) {
xpathResult = new XPathResult(&aContextNode);
}
xpathResult->SetExprResult(exprResult, resultType, &aContextNode, aRv);
if (aRv.Failed()) {
return nullptr;
}
return xpathResult.forget();
}
/*
* Implementation of the txIEvalContext private to XPathExpression
* EvalContextImpl bases on only one context node and no variables
*/
nsresult EvalContextImpl::getVariable(int32_t aNamespace, nsAtom* aLName,
txAExprResult*& aResult) {
aResult = 0;
return NS_ERROR_INVALID_ARG;
}
nsresult EvalContextImpl::isStripSpaceAllowed(const txXPathNode& aNode,
bool& aAllowed) {
aAllowed = false;
return NS_OK;
}
void* EvalContextImpl::getPrivateContext() {
// we don't have a private context here.
return nullptr;
}
txResultRecycler* EvalContextImpl::recycler() { return mRecycler; }
void EvalContextImpl::receiveError(const nsAString& aMsg, nsresult aRes) {
mLastError = aRes;
// forward aMsg to console service?
}
const txXPathNode& EvalContextImpl::getContextNode() { return mContextNode; }
uint32_t EvalContextImpl::size() { return mContextSize; }
uint32_t EvalContextImpl::position() { return mContextPosition; }
} // namespace mozilla::dom
|