1 // Copyright 2017 PDFium Authors. All rights reserved.
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_subform.h"
8
9 #include <vector>
10
11 #include "fxjs/cfxjse_value.h"
12 #include "fxjs/js_resources.h"
13 #include "xfa/fxfa/cxfa_eventparam.h"
14 #include "xfa/fxfa/cxfa_ffnotify.h"
15 #include "xfa/fxfa/fxfa.h"
16 #include "xfa/fxfa/parser/cxfa_delta.h"
17 #include "xfa/fxfa/parser/cxfa_document.h"
18
19 const CJX_MethodSpec CJX_Subform::MethodSpecs[] = {
20 {"execCalculate", execCalculate_static},
21 {"execEvent", execEvent_static},
22 {"execInitialize", execInitialize_static},
23 {"execValidate", execValidate_static}};
24
CJX_Subform(CXFA_Node * node)25 CJX_Subform::CJX_Subform(CXFA_Node* node) : CJX_Container(node) {
26 DefineMethods(MethodSpecs, FX_ArraySize(MethodSpecs));
27 }
28
~CJX_Subform()29 CJX_Subform::~CJX_Subform() {}
30
execEvent(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)31 CJS_Return CJX_Subform::execEvent(
32 CJS_V8* runtime,
33 const std::vector<v8::Local<v8::Value>>& params) {
34 if (params.size() != 1)
35 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
36
37 execSingleEventByName(runtime->ToWideString(params[0]).AsStringView(),
38 XFA_Element::Subform);
39 return CJS_Return(true);
40 }
41
execInitialize(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)42 CJS_Return CJX_Subform::execInitialize(
43 CJS_V8* runtime,
44 const std::vector<v8::Local<v8::Value>>& params) {
45 if (!params.empty())
46 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
47
48 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
49 if (pNotify)
50 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize);
51 return CJS_Return(true);
52 }
53
execCalculate(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)54 CJS_Return CJX_Subform::execCalculate(
55 CJS_V8* runtime,
56 const std::vector<v8::Local<v8::Value>>& params) {
57 if (!params.empty())
58 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
59
60 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
61 if (pNotify)
62 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate);
63 return CJS_Return(true);
64 }
65
execValidate(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)66 CJS_Return CJX_Subform::execValidate(
67 CJS_V8* runtime,
68 const std::vector<v8::Local<v8::Value>>& params) {
69 if (!params.empty())
70 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
71
72 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
73 if (!pNotify)
74 return CJS_Return(runtime->NewBoolean(false));
75
76 int32_t iRet =
77 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Validate);
78 return CJS_Return(runtime->NewBoolean(iRet != XFA_EVENTERROR_Error));
79 }
80
locale(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)81 void CJX_Subform::locale(CFXJSE_Value* pValue,
82 bool bSetting,
83 XFA_Attribute eAttribute) {
84 if (bSetting) {
85 SetCData(XFA_Attribute::Locale, pValue->ToWideString(), true, true);
86 return;
87 }
88
89 WideString wsLocaleName = GetXFANode()->GetLocaleName().value_or(L"");
90 pValue->SetString(wsLocaleName.UTF8Encode().AsStringView());
91 }
92
instanceIndex(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)93 void CJX_Subform::instanceIndex(CFXJSE_Value* pValue,
94 bool bSetting,
95 XFA_Attribute eAttribute) {
96 Script_Som_InstanceIndex(pValue, bSetting, eAttribute);
97 }
98
allowMacro(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)99 void CJX_Subform::allowMacro(CFXJSE_Value* pValue,
100 bool bSetting,
101 XFA_Attribute eAttribute) {
102 Script_Attribute_String(pValue, bSetting, eAttribute);
103 }
104
anchorType(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)105 void CJX_Subform::anchorType(CFXJSE_Value* pValue,
106 bool bSetting,
107 XFA_Attribute eAttribute) {
108 Script_Attribute_String(pValue, bSetting, eAttribute);
109 }
110
colSpan(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)111 void CJX_Subform::colSpan(CFXJSE_Value* pValue,
112 bool bSetting,
113 XFA_Attribute eAttribute) {
114 Script_Attribute_String(pValue, bSetting, eAttribute);
115 }
116
columnWidths(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)117 void CJX_Subform::columnWidths(CFXJSE_Value* pValue,
118 bool bSetting,
119 XFA_Attribute eAttribute) {
120 Script_Attribute_String(pValue, bSetting, eAttribute);
121 }
122
h(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)123 void CJX_Subform::h(CFXJSE_Value* pValue,
124 bool bSetting,
125 XFA_Attribute eAttribute) {
126 Script_Attribute_String(pValue, bSetting, eAttribute);
127 }
128
hAlign(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)129 void CJX_Subform::hAlign(CFXJSE_Value* pValue,
130 bool bSetting,
131 XFA_Attribute eAttribute) {
132 Script_Attribute_String(pValue, bSetting, eAttribute);
133 }
134
layout(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)135 void CJX_Subform::layout(CFXJSE_Value* pValue,
136 bool bSetting,
137 XFA_Attribute eAttribute) {
138 Script_Attribute_String(pValue, bSetting, eAttribute);
139 }
140
maxH(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)141 void CJX_Subform::maxH(CFXJSE_Value* pValue,
142 bool bSetting,
143 XFA_Attribute eAttribute) {
144 Script_Attribute_String(pValue, bSetting, eAttribute);
145 }
146
maxW(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)147 void CJX_Subform::maxW(CFXJSE_Value* pValue,
148 bool bSetting,
149 XFA_Attribute eAttribute) {
150 Script_Attribute_String(pValue, bSetting, eAttribute);
151 }
152
minH(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)153 void CJX_Subform::minH(CFXJSE_Value* pValue,
154 bool bSetting,
155 XFA_Attribute eAttribute) {
156 Script_Attribute_String(pValue, bSetting, eAttribute);
157 }
158
minW(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)159 void CJX_Subform::minW(CFXJSE_Value* pValue,
160 bool bSetting,
161 XFA_Attribute eAttribute) {
162 Script_Attribute_String(pValue, bSetting, eAttribute);
163 }
164
presence(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)165 void CJX_Subform::presence(CFXJSE_Value* pValue,
166 bool bSetting,
167 XFA_Attribute eAttribute) {
168 Script_Attribute_String(pValue, bSetting, eAttribute);
169 }
170
relevant(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)171 void CJX_Subform::relevant(CFXJSE_Value* pValue,
172 bool bSetting,
173 XFA_Attribute eAttribute) {
174 Script_Attribute_String(pValue, bSetting, eAttribute);
175 }
176
restoreState(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)177 void CJX_Subform::restoreState(CFXJSE_Value* pValue,
178 bool bSetting,
179 XFA_Attribute eAttribute) {
180 Script_Attribute_String(pValue, bSetting, eAttribute);
181 }
182
scope(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)183 void CJX_Subform::scope(CFXJSE_Value* pValue,
184 bool bSetting,
185 XFA_Attribute eAttribute) {
186 Script_Attribute_String(pValue, bSetting, eAttribute);
187 }
188
use(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)189 void CJX_Subform::use(CFXJSE_Value* pValue,
190 bool bSetting,
191 XFA_Attribute eAttribute) {
192 Script_Attribute_String(pValue, bSetting, eAttribute);
193 }
194
usehref(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)195 void CJX_Subform::usehref(CFXJSE_Value* pValue,
196 bool bSetting,
197 XFA_Attribute eAttribute) {
198 Script_Attribute_String(pValue, bSetting, eAttribute);
199 }
200
validationMessage(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)201 void CJX_Subform::validationMessage(CFXJSE_Value* pValue,
202 bool bSetting,
203 XFA_Attribute eAttribute) {
204 Script_Som_ValidationMessage(pValue, bSetting, eAttribute);
205 }
206
vAlign(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)207 void CJX_Subform::vAlign(CFXJSE_Value* pValue,
208 bool bSetting,
209 XFA_Attribute eAttribute) {
210 Script_Attribute_String(pValue, bSetting, eAttribute);
211 }
212
w(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)213 void CJX_Subform::w(CFXJSE_Value* pValue,
214 bool bSetting,
215 XFA_Attribute eAttribute) {
216 Script_Attribute_String(pValue, bSetting, eAttribute);
217 }
218
x(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)219 void CJX_Subform::x(CFXJSE_Value* pValue,
220 bool bSetting,
221 XFA_Attribute eAttribute) {
222 Script_Attribute_String(pValue, bSetting, eAttribute);
223 }
224
y(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)225 void CJX_Subform::y(CFXJSE_Value* pValue,
226 bool bSetting,
227 XFA_Attribute eAttribute) {
228 Script_Attribute_String(pValue, bSetting, eAttribute);
229 }
230