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_field.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_field.h"
8
9#include <vector>
10
11#include "fxjs/cfx_v8.h"
12#include "fxjs/fxv8.h"
13#include "fxjs/js_resources.h"
14#include "third_party/base/containers/span.h"
15#include "third_party/base/numerics/safe_conversions.h"
16#include "v8/include/v8-primitive.h"
17#include "xfa/fgas/crt/cfgas_decimal.h"
18#include "xfa/fxfa/cxfa_eventparam.h"
19#include "xfa/fxfa/cxfa_ffnotify.h"
20#include "xfa/fxfa/fxfa.h"
21#include "xfa/fxfa/parser/cxfa_document.h"
22#include "xfa/fxfa/parser/cxfa_field.h"
23#include "xfa/fxfa/parser/cxfa_value.h"
24
25const CJX_MethodSpec CJX_Field::MethodSpecs[] = {
26 {"addItem", addItem_static},
27 {"boundItem", boundItem_static},
28 {"clearItems", clearItems_static},
29 {"deleteItem", deleteItem_static},
30 {"execCalculate", execCalculate_static},
31 {"execEvent", execEvent_static},
32 {"execInitialize", execInitialize_static},
33 {"execValidate", execValidate_static},
34 {"getDisplayItem", getDisplayItem_static},
35 {"getItemState", getItemState_static},
36 {"getSaveItem", getSaveItem_static},
37 {"setItemState", setItemState_static}};
38
39CJX_Field::CJX_Field(CXFA_Field* field) : CJX_Container(field) {
40 DefineMethods(MethodSpecs);
41}
42
43CJX_Field::~CJX_Field() = default;
44
45bool CJX_Field::DynamicTypeIs(TypeTag eType) const {
46 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
47}
48
49CJS_Result CJX_Field::clearItems(CFXJSE_Engine* runtime,
50 pdfium::span<v8::Local<v8::Value>> params) {
51 CXFA_Node* node = GetXFANode();
52 if (node->IsWidgetReady())
53 node->DeleteItem(-1, true, false);
55}
56
57CJS_Result CJX_Field::execEvent(CFXJSE_Engine* runtime,
58 pdfium::span<v8::Local<v8::Value>> params) {
59 if (params.size() != 1)
61
62 WideString eventString = runtime->ToWideString(params[0]);
63 XFA_EventError iRet =
64 execSingleEventByName(eventString.AsStringView(), XFA_Element::Field);
65 if (!eventString.EqualsASCII("validate"))
67
69 runtime->NewBoolean(iRet != XFA_EventError::kError));
70}
71
72CJS_Result CJX_Field::execInitialize(
73 CFXJSE_Engine* runtime,
74 pdfium::span<v8::Local<v8::Value>> params) {
75 if (!params.empty())
77
78 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
79 if (pNotify) {
81 false);
82 }
84}
85
86CJS_Result CJX_Field::deleteItem(CFXJSE_Engine* runtime,
87 pdfium::span<v8::Local<v8::Value>> params) {
88 if (params.size() != 1)
90
91 CXFA_Node* node = GetXFANode();
92 if (!node->IsWidgetReady())
94
95 bool bValue = node->DeleteItem(runtime->ToInt32(params[0]), true, true);
96 return CJS_Result::Success(runtime->NewBoolean(bValue));
97}
98
99CJS_Result CJX_Field::getSaveItem(CFXJSE_Engine* runtime,
100 pdfium::span<v8::Local<v8::Value>> params) {
101 if (params.size() != 1)
103
104 int32_t iIndex = runtime->ToInt32(params[0]);
105 if (iIndex < 0)
106 return CJS_Result::Success(runtime->NewNull());
107
108 CXFA_Node* node = GetXFANode();
109 if (!node->IsWidgetReady())
110 return CJS_Result::Success(runtime->NewNull());
111
112 absl::optional<WideString> value = node->GetChoiceListItem(iIndex, true);
113 if (!value.has_value())
114 return CJS_Result::Success(runtime->NewNull());
115
116 return CJS_Result::Success(
117 runtime->NewString(value->ToUTF8().AsStringView()));
118}
119
120CJS_Result CJX_Field::boundItem(CFXJSE_Engine* runtime,
121 pdfium::span<v8::Local<v8::Value>> params) {
122 if (params.size() != 1)
124
125 CXFA_Node* node = GetXFANode();
126 if (!node->IsWidgetReady())
128
129 WideString value = runtime->ToWideString(params[0]);
130 WideString boundValue = node->GetItemValue(value.AsStringView());
132 runtime->NewString(boundValue.ToUTF8().AsStringView()));
133}
134
135CJS_Result CJX_Field::getItemState(CFXJSE_Engine* runtime,
136 pdfium::span<v8::Local<v8::Value>> params) {
137 if (params.size() != 1)
139
140 CXFA_Node* node = GetXFANode();
141 if (!node->IsWidgetReady())
143
144 int32_t state = node->GetItemState(runtime->ToInt32(params[0]));
145 return CJS_Result::Success(runtime->NewBoolean(state != 0));
146}
147
148CJS_Result CJX_Field::execCalculate(CFXJSE_Engine* runtime,
149 pdfium::span<v8::Local<v8::Value>> params) {
150 if (!params.empty())
152
153 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
154 if (pNotify) {
156 false);
157 }
159}
160
161CJS_Result CJX_Field::getDisplayItem(
162 CFXJSE_Engine* runtime,
163 pdfium::span<v8::Local<v8::Value>> params) {
164 if (params.size() != 1)
166
167 int32_t iIndex = runtime->ToInt32(params[0]);
168 if (iIndex < 0)
169 return CJS_Result::Success(runtime->NewNull());
170
171 CXFA_Node* node = GetXFANode();
172 if (!node->IsWidgetReady())
173 return CJS_Result::Success(runtime->NewNull());
174
175 absl::optional<WideString> value = node->GetChoiceListItem(iIndex, false);
176 if (!value.has_value())
177 return CJS_Result::Success(runtime->NewNull());
178
179 return CJS_Result::Success(
180 runtime->NewString(value->ToUTF8().AsStringView()));
181}
182
183CJS_Result CJX_Field::setItemState(CFXJSE_Engine* runtime,
184 pdfium::span<v8::Local<v8::Value>> params) {
185 if (params.size() != 2)
187
188 CXFA_Node* node = GetXFANode();
189 if (!node->IsWidgetReady())
191
192 int32_t iIndex = runtime->ToInt32(params[0]);
193 if (runtime->ToInt32(params[1]) != 0) {
194 node->SetItemState(iIndex, true, true, true);
196 }
197 if (node->GetItemState(iIndex))
198 node->SetItemState(iIndex, false, true, true);
199
201}
202
203CJS_Result CJX_Field::addItem(CFXJSE_Engine* runtime,
204 pdfium::span<v8::Local<v8::Value>> params) {
205 if (params.size() != 1 && params.size() != 2)
207
208 CXFA_Node* node = GetXFANode();
209 if (!node->IsWidgetReady())
211
212 WideString label;
213 if (params.size() >= 1)
214 label = runtime->ToWideString(params[0]);
215
216 WideString value;
217 if (params.size() >= 2)
218 value = runtime->ToWideString(params[1]);
219
220 node->InsertItem(label, value, true);
222}
223
224CJS_Result CJX_Field::execValidate(CFXJSE_Engine* runtime,
225 pdfium::span<v8::Local<v8::Value>> params) {
226 if (!params.empty())
228
229 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
230 if (!pNotify)
231 return CJS_Result::Success(runtime->NewBoolean(false));
232
236 runtime->NewBoolean(iRet != XFA_EventError::kError));
237}
238
239void CJX_Field::defaultValue(v8::Isolate* pIsolate,
240 v8::Local<v8::Value>* pValue,
241 bool bSetting,
242 XFA_Attribute eAttribute) {
243 CXFA_Node* xfaNode = GetXFANode();
244 if (!xfaNode->IsWidgetReady())
245 return;
246
247 if (bSetting) {
248 if (pValue) {
249 xfaNode->SetPreNull(xfaNode->IsNull());
250 xfaNode->SetIsNull(fxv8::IsNull(*pValue));
251 }
252
253 WideString wsNewText;
254 if (pValue && !(fxv8::IsNull(*pValue) || fxv8::IsUndefined(*pValue)))
255 wsNewText = fxv8::ReentrantToWideStringHelper(pIsolate, *pValue);
256 if (xfaNode->GetUIChildNode()->GetElementType() == XFA_Element::NumericEdit)
257 wsNewText = xfaNode->NumericLimit(wsNewText);
258
259 CXFA_Node* pContainerNode = xfaNode->GetContainerNode();
260 WideString wsFormatText(wsNewText);
261 if (pContainerNode)
262 wsFormatText = pContainerNode->GetFormatDataValue(wsNewText);
263
264 SetContent(wsNewText, wsFormatText, true, true, true);
265 return;
266 }
267
268 WideString content = GetContent(true);
269 if (content.IsEmpty()) {
270 *pValue = fxv8::NewNullHelper(pIsolate);
271 return;
272 }
273
274 CXFA_Node* formValue = xfaNode->GetFormValueIfExists();
275 CXFA_Node* pNode = formValue ? formValue->GetFirstChild() : nullptr;
276 if (pNode && pNode->GetElementType() == XFA_Element::Decimal) {
278 XFA_Element::NumericEdit &&
279 (pNode->JSObject()->GetInteger(XFA_Attribute::FracDigits) == -1)) {
280 *pValue =
281 fxv8::NewStringHelper(pIsolate, content.ToUTF8().AsStringView());
282 } else {
283 CFGAS_Decimal decimal(content.AsStringView());
284 *pValue = fxv8::NewNumberHelper(pIsolate, decimal.ToFloat());
285 }
286 } else if (pNode && pNode->GetElementType() == XFA_Element::Integer) {
287 *pValue = fxv8::NewNumberHelper(pIsolate, FXSYS_wtoi(content.c_str()));
288 } else if (pNode && pNode->GetElementType() == XFA_Element::Boolean) {
289 *pValue =
290 fxv8::NewBooleanHelper(pIsolate, FXSYS_wtoi(content.c_str()) != 0);
291 } else if (pNode && pNode->GetElementType() == XFA_Element::Float) {
292 CFGAS_Decimal decimal(content.AsStringView());
293 *pValue = fxv8::NewNumberHelper(pIsolate, decimal.ToFloat());
294 } else {
295 *pValue = fxv8::NewStringHelper(pIsolate, content.ToUTF8().AsStringView());
296 }
297}
298
299void CJX_Field::editValue(v8::Isolate* pIsolate,
300 v8::Local<v8::Value>* pValue,
301 bool bSetting,
302 XFA_Attribute eAttribute) {
303 CXFA_Node* node = GetXFANode();
304 if (!node->IsWidgetReady())
305 return;
306
307 if (bSetting) {
309 fxv8::ReentrantToWideStringHelper(pIsolate, *pValue));
310 return;
311 }
312 *pValue = fxv8::NewStringHelper(
313 pIsolate,
315}
316
317void CJX_Field::formatMessage(v8::Isolate* pIsolate,
318 v8::Local<v8::Value>* pValue,
319 bool bSetting,
320 XFA_Attribute eAttribute) {
321 ScriptSomMessage(pIsolate, pValue, bSetting, SOMMessageType::kFormatMessage);
322}
323
324void CJX_Field::formattedValue(v8::Isolate* pIsolate,
325 v8::Local<v8::Value>* pValue,
326 bool bSetting,
327 XFA_Attribute eAttribute) {
328 CXFA_Node* node = GetXFANode();
329 if (!node->IsWidgetReady())
330 return;
331
332 if (bSetting) {
334 fxv8::ReentrantToWideStringHelper(pIsolate, *pValue));
335 return;
336 }
337 *pValue = fxv8::NewStringHelper(
338 pIsolate,
340}
341
342void CJX_Field::length(v8::Isolate* pIsolate,
343 v8::Local<v8::Value>* pValue,
344 bool bSetting,
345 XFA_Attribute eAttribute) {
346 if (bSetting) {
348 return;
349 }
350
351 CXFA_Node* node = GetXFANode();
352 *pValue = fxv8::NewNumberHelper(
353 pIsolate, node->IsWidgetReady() ? pdfium::base::checked_cast<int>(
354 node->CountChoiceListItems(true))
355 : 0);
356}
357
358void CJX_Field::parentSubform(v8::Isolate* pIsolate,
359 v8::Local<v8::Value>* pValue,
360 bool bSetting,
361 XFA_Attribute eAttribute) {
362 if (bSetting) {
364 return;
365 }
366 *pValue = fxv8::NewNullHelper(pIsolate);
367}
368
369void CJX_Field::selectedIndex(v8::Isolate* pIsolate,
370 v8::Local<v8::Value>* pValue,
371 bool bSetting,
372 XFA_Attribute eAttribute) {
373 CXFA_Node* node = GetXFANode();
374 if (!node->IsWidgetReady())
375 return;
376
377 if (!bSetting) {
378 *pValue = fxv8::NewNumberHelper(pIsolate, node->GetSelectedItem(0));
379 return;
380 }
381
382 int32_t iIndex = fxv8::ReentrantToInt32Helper(pIsolate, *pValue);
383 if (iIndex == -1) {
385 return;
386 }
387
388 node->SetItemState(iIndex, true, true, true);
389}
390
391void CJX_Field::rawValue(v8::Isolate* pIsolate,
392 v8::Local<v8::Value>* pValue,
393 bool bSetting,
394 XFA_Attribute eAttribute) {
395 defaultValue(pIsolate, pValue, bSetting, eAttribute);
396}
float ToFloat() const
friend class EventParamScope
static CJS_Result Success()
Definition cjs_result.h:27
static CJS_Result Failure(JSMessage id)
Definition cjs_result.h:34
bool DynamicTypeIs(TypeTag eType) const override
bool DynamicTypeIs(TypeTag eType) const override
Definition cjx_field.cpp:45
~CJX_Field() override
XFA_EventError execSingleEventByName(WideStringView wsEventName, XFA_Element eType)
Definition cjx_node.cpp:468
void DefineMethods(pdfium::span< const CJX_MethodSpec > methods)
WideString GetContent(bool bScriptModify) const
void ThrowInvalidPropertyException(v8::Isolate *pIsolate) const
CXFA_Document * GetDocument() const
CXFA_Node * GetXFANode() const
void SetContent(const WideString &wsContent, const WideString &wsXMLValue, bool bNotify, bool bScriptModify, bool bSyncData)
XFA_EventError ExecEventByDeepFirst(CXFA_Node *pFormNode, XFA_EVENTTYPE eEventType, bool bIsFormReady, bool bRecursive)
CXFA_Value * GetFormValueIfExists() const
WideString GetValue(XFA_ValuePicture eValueType)
bool SetValue(XFA_ValuePicture eValueType, const WideString &wsValue)
int32_t GetSelectedItem(int32_t nIndex)
void SetItemState(int32_t nIndex, bool bSelected, bool bNotify, bool bScriptModify)
WideString NumericLimit(const WideString &wsValue)
void InsertItem(const WideString &wsLabel, const WideString &wsValue, bool bNotify)
WideString GetFormatDataValue(const WideString &wsValue)
bool DeleteItem(int32_t nIndex, bool bNotify, bool bScriptModify)
void SetPreNull(bool val)
Definition cxfa_node.h:322
bool GetItemState(int32_t nIndex)
void ClearAllSelections()
bool IsNull() const
Definition cxfa_node.h:323
CXFA_Node * GetContainerNode()
bool IsWidgetReady() const
Definition cxfa_node.h:327
CXFA_Node * GetUIChildNode()
WideString GetItemValue(WideStringView wsLabel)
void SetIsNull(bool val)
Definition cxfa_node.h:324
XFA_Element GetElementType() const
Definition cxfa_object.h:91
ByteString ToUTF8() const
WideString & operator=(WideString &&that) noexcept
bool IsEmpty() const
Definition widestring.h:118
const wchar_t * c_str() const
Definition widestring.h:81
WideString(const WideString &other)
bool EqualsASCII(ByteStringView that) const
Definition widestring.h:216
@ XFA_EVENT_Initialize
@ XFA_EVENT_Calculate
@ XFA_EVENT_Validate
XFA_ValuePicture
Definition cxfa_node.h:70
int32_t FXSYS_wtoi(const wchar_t *str)
XFA_EventError
Definition fxfa.h:54
XFA_Attribute
Definition fxfa_basic.h:67
XFA_Element
Definition fxfa_basic.h:75
JSMessage
Definition fxv8.h:22
bool IsUndefined(v8::Local< v8::Value > value)
Definition fxv8.cpp:19
bool IsNull(v8::Local< v8::Value > value)
Definition fxv8.cpp:23