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_exclgroup.h"
8
9 #include "core/fxcrt/span.h"
10 #include "fxjs/fxv8.h"
11 #include "fxjs/js_resources.h"
12 #include "fxjs/xfa/cfxjse_engine.h"
13 #include "v8/include/v8-object.h"
14 #include "v8/include/v8-primitive.h"
15 #include "xfa/fxfa/cxfa_eventparam.h"
16 #include "xfa/fxfa/cxfa_ffnotify.h"
17 #include "xfa/fxfa/fxfa.h"
18 #include "xfa/fxfa/parser/cxfa_document.h"
19 #include "xfa/fxfa/parser/cxfa_exclgroup.h"
20
21 const CJX_MethodSpec CJX_ExclGroup::MethodSpecs[] = {
22 {"execCalculate", execCalculate_static},
23 {"execEvent", execEvent_static},
24 {"execInitialize", execInitialize_static},
25 {"execValidate", execValidate_static},
26 {"selectedMember", selectedMember_static}};
27
CJX_ExclGroup(CXFA_ExclGroup * group)28 CJX_ExclGroup::CJX_ExclGroup(CXFA_ExclGroup* group) : CJX_Node(group) {
29 DefineMethods(MethodSpecs);
30 }
31
32 CJX_ExclGroup::~CJX_ExclGroup() = default;
33
DynamicTypeIs(TypeTag eType) const34 bool CJX_ExclGroup::DynamicTypeIs(TypeTag eType) const {
35 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
36 }
37
execEvent(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)38 CJS_Result CJX_ExclGroup::execEvent(CFXJSE_Engine* runtime,
39 pdfium::span<v8::Local<v8::Value>> params) {
40 if (params.size() != 1)
41 return CJS_Result::Failure(JSMessage::kParamError);
42
43 execSingleEventByName(runtime->ToWideString(params[0]).AsStringView(),
44 XFA_Element::ExclGroup);
45 return CJS_Result::Success();
46 }
47
execInitialize(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)48 CJS_Result CJX_ExclGroup::execInitialize(
49 CFXJSE_Engine* runtime,
50 pdfium::span<v8::Local<v8::Value>> params) {
51 if (!params.empty())
52 return CJS_Result::Failure(JSMessage::kParamError);
53
54 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
55 if (pNotify)
56 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize, false,
57 true);
58 return CJS_Result::Success();
59 }
60
execCalculate(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)61 CJS_Result CJX_ExclGroup::execCalculate(
62 CFXJSE_Engine* runtime,
63 pdfium::span<v8::Local<v8::Value>> params) {
64 if (!params.empty())
65 return CJS_Result::Failure(JSMessage::kParamError);
66
67 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
68 if (pNotify)
69 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false,
70 true);
71 return CJS_Result::Success();
72 }
73
execValidate(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)74 CJS_Result CJX_ExclGroup::execValidate(
75 CFXJSE_Engine* runtime,
76 pdfium::span<v8::Local<v8::Value>> params) {
77 if (!params.empty())
78 return CJS_Result::Failure(JSMessage::kParamError);
79
80 CXFA_FFNotify* notify = GetDocument()->GetNotify();
81 if (!notify)
82 return CJS_Result::Success(runtime->NewBoolean(false));
83
84 XFA_EventError iRet = notify->ExecEventByDeepFirst(
85 GetXFANode(), XFA_EVENT_Validate, false, true);
86 return CJS_Result::Success(
87 runtime->NewBoolean(iRet != XFA_EventError::kError));
88 }
89
selectedMember(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)90 CJS_Result CJX_ExclGroup::selectedMember(
91 CFXJSE_Engine* runtime,
92 pdfium::span<v8::Local<v8::Value>> params) {
93 if (!params.empty())
94 return CJS_Result::Failure(JSMessage::kParamError);
95
96 CXFA_Node* node = GetXFANode();
97 if (!node->IsWidgetReady())
98 return CJS_Result::Success(runtime->NewNull());
99
100 CXFA_Node* pReturnNode = nullptr;
101 if (params.empty()) {
102 pReturnNode = node->GetSelectedMember();
103 } else {
104 pReturnNode = node->SetSelectedMember(
105 runtime->ToWideString(params[0]).AsStringView());
106 }
107 if (!pReturnNode)
108 return CJS_Result::Success(runtime->NewNull());
109
110 return CJS_Result::Success(runtime->GetOrCreateJSBindingFromMap(pReturnNode));
111 }
112
defaultValue(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)113 void CJX_ExclGroup::defaultValue(v8::Isolate* pIsolate,
114 v8::Local<v8::Value>* pValue,
115 bool bSetting,
116 XFA_Attribute eAttribute) {
117 CXFA_Node* node = GetXFANode();
118 if (!node->IsWidgetReady())
119 return;
120
121 if (bSetting) {
122 node->SetSelectedMemberByValue(
123 fxv8::ReentrantToWideStringHelper(pIsolate, *pValue).AsStringView(),
124 true, true, true);
125 return;
126 }
127
128 WideString wsValue = GetContent(true);
129 XFA_VERSION curVersion = GetDocument()->GetCurVersionMode();
130 if (wsValue.IsEmpty() && curVersion >= XFA_VERSION_300) {
131 *pValue = fxv8::NewNullHelper(pIsolate);
132 return;
133 }
134 *pValue = fxv8::NewStringHelper(pIsolate, wsValue.ToUTF8().AsStringView());
135 }
136
rawValue(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)137 void CJX_ExclGroup::rawValue(v8::Isolate* pIsolate,
138 v8::Local<v8::Value>* pValue,
139 bool bSetting,
140 XFA_Attribute eAttribute) {
141 defaultValue(pIsolate, pValue, bSetting, eAttribute);
142 }
143
transient(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)144 void CJX_ExclGroup::transient(v8::Isolate* pIsolate,
145 v8::Local<v8::Value>* pValue,
146 bool bSetting,
147 XFA_Attribute eAttribute) {}
148
errorText(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)149 void CJX_ExclGroup::errorText(v8::Isolate* pIsolate,
150 v8::Local<v8::Value>* pValue,
151 bool bSetting,
152 XFA_Attribute eAttribute) {
153 if (bSetting)
154 ThrowInvalidPropertyException(pIsolate);
155 }
156