7#include "fxjs/xfa/cjx_tree.h"
12#include "fxjs/js_resources.h"
13#include "fxjs/xfa/cfxjse_class.h"
14#include "fxjs/xfa/cfxjse_engine.h"
15#include "third_party/base/containers/span.h"
16#include "third_party/base/numerics/safe_conversions.h"
17#include "v8/include/cppgc/allocation.h"
18#include "v8/include/v8-object.h"
19#include "v8/include/v8-primitive.h"
20#include "xfa/fxfa/parser/cxfa_arraynodelist.h"
21#include "xfa/fxfa/parser/cxfa_attachnodelist.h"
22#include "xfa/fxfa/parser/cxfa_document.h"
23#include "xfa/fxfa/parser/cxfa_node.h"
24#include "xfa/fxfa/parser/cxfa_object.h"
27 {
"resolveNode", resolveNode_static},
28 {
"resolveNodes", resolveNodes_static}};
41 pdfium::span<v8::Local<v8::Value>> params) {
42 if (params.size() != 1)
45 WideString wsExpression = runtime->ToWideString(params[0]);
50 absl::optional<CFXJSE_Engine::ResolveResult> maybeResult =
51 runtime->ResolveObjects(
52 ToNode(pRefNode), wsExpression.AsStringView(),
53 Mask<XFA_ResolveFlag>{
54 XFA_ResolveFlag::kChildren, XFA_ResolveFlag::kAttributes,
55 XFA_ResolveFlag::kProperties, XFA_ResolveFlag::kParent,
56 XFA_ResolveFlag::kSiblings});
57 if (!maybeResult.has_value())
60 if (maybeResult.value().type == CFXJSE_Engine::ResolveResult::Type::kNodes) {
61 return CJS_Result::Success(runtime->GetOrCreateJSBindingFromMap(
62 maybeResult.value().objects.front().Get()));
65 if (!maybeResult.value().script_attribute.callback ||
66 maybeResult.value().script_attribute.eValueType !=
67 XFA_ScriptType::Object) {
71 v8::Local<v8::Value> pValue;
72 CJX_Object* jsObject = maybeResult.value().objects.front()->JSObject();
73 (*maybeResult.value().script_attribute.callback)(
74 runtime->GetIsolate(), jsObject, &pValue,
false,
75 maybeResult.value().script_attribute.attribute);
76 return CJS_Result::Success(pValue);
80 pdfium::span<v8::Local<v8::Value>> params) {
81 if (params.size() != 1)
89 XFA_ResolveFlag::kChildren, XFA_ResolveFlag::kAttributes,
90 XFA_ResolveFlag::kProperties, XFA_ResolveFlag::kParent,
91 XFA_ResolveFlag::kSiblings};
92 return CJS_Result::Success(ResolveNodeList(runtime->GetIsolate(),
93 runtime->ToWideString(params[0]),
94 kFlags, ToNode(refNode)));
98 v8::Local<v8::Value>* pValue,
106 XFA_ResolveFlag::kALL};
108 *pValue = ResolveNodeList(pIsolate, wsExpression, kFlags,
nullptr);
112 v8::Local<v8::Value>* pValue,
120 XFA_ResolveFlag::kALL};
121 WideString wsExpression =
123 *pValue = ResolveNodeList(pIsolate, wsExpression, kFlags,
nullptr);
127 v8::Local<v8::Value>* pValue,
131 WideString wsMessage = L"Unable to set ";
132 FXJSE_ThrowMessage(pIsolate, wsMessage
.ToUTF8().AsStringView());
137 auto* pNodeList = cppgc::MakeGarbageCollected<CXFA_AttachNodeList>(
138 pDoc->GetHeap()->GetAllocationHandle(), pDoc, GetXFANode());
142 *pValue = pNodeList->JSObject()->NewBoundV8Object(
147 v8::Local<v8::Value>* pValue,
156 *pValue = pParent ? GetDocument()
158 ->GetOrCreateJSBindingFromMap(pParent)
160 : fxv8::NewNullHelper(pIsolate).As<v8::Value>();
164 v8::Local<v8::Value>* pValue,
173 size_t iIndex = pNode ? pNode->GetIndexByName() : 0;
174 *pValue = fxv8::NewNumberHelper(pIsolate,
175 pdfium::base::checked_cast<int32_t>(iIndex));
178void CJX_Tree::classIndex(
v8::Isolate* pIsolate,
179 v8::Local<v8::Value>* pValue,
188 size_t iIndex = pNode ? pNode->GetIndexByClassName() : 0;
189 *pValue = fxv8::NewNumberHelper(pIsolate,
190 pdfium::base::checked_cast<int32_t>(iIndex));
193void CJX_Tree::somExpression(
v8::Isolate* pIsolate,
194 v8::Local<v8::Value>* pValue,
202 ByteString bsSOMExpression =
GetXFAObject()->GetSOMExpression().ToUTF8();
203 *pValue = fxv8::NewStringHelper(pIsolate, bsSOMExpression.AsStringView());
206v8::Local<v8::Value>
CJX_Tree::ResolveNodeList(
v8::Isolate* pIsolate,
207 WideString wsExpression,
214 auto* pNodeList = cppgc::MakeGarbageCollected<CXFA_ArrayNodeList>(
215 pDoc->GetHeap()->GetAllocationHandle(), pDoc);
219 absl::optional<CFXJSE_Engine::ResolveResult> maybeResult =
220 pScriptContext->ResolveObjects(refNode, wsExpression.AsStringView(),
223 if (maybeResult.has_value()) {
224 if (maybeResult.value().type ==
225 CFXJSE_Engine::ResolveResult::Type::kNodes) {
226 for (
auto& pObject : maybeResult.value().objects) {
227 if (pObject->IsNode())
228 pNodeList->Append(pObject->AsNode());
231 if (maybeResult.value().script_attribute.callback &&
232 maybeResult.value().script_attribute.eValueType ==
233 XFA_ScriptType::Object) {
234 for (
auto& pObject : maybeResult.value().objects) {
235 v8::Local<v8::Value> innerValue;
236 CJX_Object* jsObject = pObject->JSObject();
237 (*maybeResult.value().script_attribute.callback)(
238 pIsolate, jsObject, &innerValue,
false,
239 maybeResult.value().script_attribute.attribute);
241 CFXJSE_Engine::ToObject(pScriptContext->GetIsolate(), innerValue);
243 pNodeList->Append(obj->AsNode());
248 return pNodeList->JSObject()->NewBoundV8Object(
CXFA_Object * GetThisObject() const
CFXJSE_Class * GetJseNormalClass() const
friend class EventParamScope
static CJS_Result Success()
static CJS_Result Failure(JSMessage id)
void DefineMethods(pdfium::span< const CJX_MethodSpec > methods)
CXFA_Object * GetXFAObject() const
void ThrowInvalidPropertyException(v8::Isolate *pIsolate) const
CXFA_Document * GetDocument() const
virtual bool DynamicTypeIs(TypeTag eType) const
CXFA_Node * GetXFANode() const
WideString GetAttributeByEnum(XFA_Attribute attr) const
CJX_Object(CXFA_Object *obj)
CJX_Tree(CXFA_Object *obj)
bool DynamicTypeIs(TypeTag eType) const override
CFXJSE_Engine * GetScriptContext() const
CXFA_NodeOwner * GetNodeOwner()
void PersistList(CXFA_List *list)
XFA_Element GetElementType() const
ByteString ToUTF8() const
static WideString FromASCII(ByteStringView str)