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