Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cjx_tree.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "fxjs/xfa/cjx_tree.h"
8
9#include <vector>
10
11#include "fxjs/fxv8.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"
25
26const CJX_MethodSpec CJX_Tree::MethodSpecs[] = {
27 {"resolveNode", resolveNode_static},
28 {"resolveNodes", resolveNodes_static}};
29
31 DefineMethods(MethodSpecs);
32}
33
34CJX_Tree::~CJX_Tree() = default;
35
36bool CJX_Tree::DynamicTypeIs(TypeTag eType) const {
37 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
38}
39
40CJS_Result CJX_Tree::resolveNode(CFXJSE_Engine* runtime,
41 pdfium::span<v8::Local<v8::Value>> params) {
42 if (params.size() != 1)
44
45 WideString wsExpression = runtime->ToWideString(params[0]);
46 CXFA_Object* pRefNode = GetXFAObject();
47 if (pRefNode->GetElementType() == XFA_Element::Xfa)
48 pRefNode = runtime->GetThisObject();
49
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())
58 return CJS_Result::Success(runtime->NewNull());
59
60 if (maybeResult.value().type == CFXJSE_Engine::ResolveResult::Type::kNodes) {
61 return CJS_Result::Success(runtime->GetOrCreateJSBindingFromMap(
62 maybeResult.value().objects.front().Get()));
63 }
64
65 if (!maybeResult.value().script_attribute.callback ||
66 maybeResult.value().script_attribute.eValueType !=
67 XFA_ScriptType::Object) {
68 return CJS_Result::Success(runtime->NewNull());
69 }
70
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);
77}
78
79CJS_Result CJX_Tree::resolveNodes(CFXJSE_Engine* runtime,
80 pdfium::span<v8::Local<v8::Value>> params) {
81 if (params.size() != 1)
83
84 CXFA_Object* refNode = GetXFAObject();
85 if (refNode->GetElementType() == XFA_Element::Xfa)
86 refNode = runtime->GetThisObject();
87
88 const Mask<XFA_ResolveFlag> kFlags = {
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)));
95}
96
97void CJX_Tree::all(v8::Isolate* pIsolate,
98 v8::Local<v8::Value>* pValue,
99 bool bSetting,
100 XFA_Attribute eAttribute) {
101 if (bSetting) {
103 return;
104 }
105 const Mask<XFA_ResolveFlag> kFlags = {XFA_ResolveFlag::kSiblings,
106 XFA_ResolveFlag::kALL};
107 WideString wsExpression = GetAttributeByEnum(XFA_Attribute::Name) + L"[*]";
108 *pValue = ResolveNodeList(pIsolate, wsExpression, kFlags, nullptr);
109}
110
111void CJX_Tree::classAll(v8::Isolate* pIsolate,
112 v8::Local<v8::Value>* pValue,
113 bool bSetting,
114 XFA_Attribute eAttribute) {
115 if (bSetting) {
117 return;
118 }
119 const Mask<XFA_ResolveFlag> kFlags = {XFA_ResolveFlag::kSiblings,
120 XFA_ResolveFlag::kALL};
121 WideString wsExpression =
122 L"#" + WideString::FromASCII(GetXFAObject()->GetClassName()) + L"[*]";
123 *pValue = ResolveNodeList(pIsolate, wsExpression, kFlags, nullptr);
124}
125
126void CJX_Tree::nodes(v8::Isolate* pIsolate,
127 v8::Local<v8::Value>* pValue,
128 bool bSetting,
129 XFA_Attribute eAttribute) {
130 if (bSetting) {
131 WideString wsMessage = L"Unable to set ";
132 FXJSE_ThrowMessage(pIsolate, wsMessage.ToUTF8().AsStringView());
133 return;
134 }
135
136 CXFA_Document* pDoc = GetDocument();
137 auto* pNodeList = cppgc::MakeGarbageCollected<CXFA_AttachNodeList>(
138 pDoc->GetHeap()->GetAllocationHandle(), pDoc, GetXFANode());
140
141 CFXJSE_Engine* pEngine = pDoc->GetScriptContext();
142 *pValue = pNodeList->JSObject()->NewBoundV8Object(
143 pIsolate, pEngine->GetJseNormalClass()->GetTemplate(pIsolate));
144}
145
146void CJX_Tree::parent(v8::Isolate* pIsolate,
147 v8::Local<v8::Value>* pValue,
148 bool bSetting,
149 XFA_Attribute eAttribute) {
150 if (bSetting) {
152 return;
153 }
154
155 CXFA_Node* pParent = GetXFANode()->GetParent();
156 *pValue = pParent ? GetDocument()
157 ->GetScriptContext()
158 ->GetOrCreateJSBindingFromMap(pParent)
159 .As<v8::Value>()
160 : fxv8::NewNullHelper(pIsolate).As<v8::Value>();
161}
162
163void CJX_Tree::index(v8::Isolate* pIsolate,
164 v8::Local<v8::Value>* pValue,
165 bool bSetting,
166 XFA_Attribute eAttribute) {
167 if (bSetting) {
169 return;
170 }
171
172 CXFA_Node* pNode = GetXFANode();
173 size_t iIndex = pNode ? pNode->GetIndexByName() : 0;
174 *pValue = fxv8::NewNumberHelper(pIsolate,
175 pdfium::base::checked_cast<int32_t>(iIndex));
176}
177
178void CJX_Tree::classIndex(v8::Isolate* pIsolate,
179 v8::Local<v8::Value>* pValue,
180 bool bSetting,
181 XFA_Attribute eAttribute) {
182 if (bSetting) {
184 return;
185 }
186
187 CXFA_Node* pNode = GetXFANode();
188 size_t iIndex = pNode ? pNode->GetIndexByClassName() : 0;
189 *pValue = fxv8::NewNumberHelper(pIsolate,
190 pdfium::base::checked_cast<int32_t>(iIndex));
191}
192
193void CJX_Tree::somExpression(v8::Isolate* pIsolate,
194 v8::Local<v8::Value>* pValue,
195 bool bSetting,
196 XFA_Attribute eAttribute) {
197 if (bSetting) {
199 return;
200 }
201
202 ByteString bsSOMExpression = GetXFAObject()->GetSOMExpression().ToUTF8();
203 *pValue = fxv8::NewStringHelper(pIsolate, bsSOMExpression.AsStringView());
204}
205
206v8::Local<v8::Value> CJX_Tree::ResolveNodeList(v8::Isolate* pIsolate,
207 WideString wsExpression,
208 Mask<XFA_ResolveFlag> dwFlag,
209 CXFA_Node* refNode) {
210 if (!refNode)
211 refNode = GetXFANode();
212
213 CXFA_Document* pDoc = GetDocument();
214 auto* pNodeList = cppgc::MakeGarbageCollected<CXFA_ArrayNodeList>(
215 pDoc->GetHeap()->GetAllocationHandle(), pDoc);
217
218 CFXJSE_Engine* pScriptContext = pDoc->GetScriptContext();
219 absl::optional<CFXJSE_Engine::ResolveResult> maybeResult =
220 pScriptContext->ResolveObjects(refNode, wsExpression.AsStringView(),
221 dwFlag);
222
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());
229 }
230 } else {
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);
240 CXFA_Object* obj =
241 CFXJSE_Engine::ToObject(pScriptContext->GetIsolate(), innerValue);
242 if (obj->IsNode())
243 pNodeList->Append(obj->AsNode());
244 }
245 }
246 }
247 }
248 return pNodeList->JSObject()->NewBoundV8Object(
249 pIsolate, pScriptContext->GetJseNormalClass()->GetTemplate(pIsolate));
250}
XFA_ResolveFlag
CXFA_Object * GetThisObject() const
CFXJSE_Class * GetJseNormalClass() const
friend class EventParamScope
static CJS_Result Success()
Definition cjs_result.h:27
static CJS_Result Failure(JSMessage id)
Definition cjs_result.h:34
void DefineMethods(pdfium::span< const CJX_MethodSpec > methods)
CXFA_Object * GetXFAObject() const
Definition cjx_object.h:113
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)
Definition cjx_tree.cpp:30
bool DynamicTypeIs(TypeTag eType) const override
Definition cjx_tree.cpp:36
~CJX_Tree() override
CFXJSE_Engine * GetScriptContext() const
CXFA_NodeOwner * GetNodeOwner()
void PersistList(CXFA_List *list)
XFA_Element GetElementType() const
Definition cxfa_object.h:91
ByteString ToUTF8() const
static WideString FromASCII(ByteStringView str)
XFA_Attribute
Definition fxfa_basic.h:67
XFA_Element
Definition fxfa_basic.h:75
JSMessage