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_exclgroup.h" 8 9 #include <vector> 10 11 #include "fxjs/cfxjse_engine.h" 12 #include "fxjs/cfxjse_value.h" 13 #include "fxjs/js_resources.h" 14 #include "xfa/fxfa/cxfa_eventparam.h" 15 #include "xfa/fxfa/cxfa_ffnotify.h" 16 #include "xfa/fxfa/fxfa.h" 17 #include "xfa/fxfa/parser/cxfa_document.h" 18 #include "xfa/fxfa/parser/cxfa_exclgroup.h" 19 20 const CJX_MethodSpec CJX_ExclGroup::MethodSpecs[] = { 21 {"execCalculate", execCalculate_static}, 22 {"execEvent", execEvent_static}, 23 {"execInitialize", execInitialize_static}, 24 {"execValidate", execValidate_static}, 25 {"selectedMember", selectedMember_static}}; 26 27 CJX_ExclGroup::CJX_ExclGroup(CXFA_ExclGroup* group) : CJX_Node(group) { 28 DefineMethods(MethodSpecs, FX_ArraySize(MethodSpecs)); 29 } 30 31 CJX_ExclGroup::~CJX_ExclGroup() {} 32 33 CJS_Return CJX_ExclGroup::execEvent( 34 CJS_V8* runtime, 35 const std::vector<v8::Local<v8::Value>>& params) { 36 if (params.size() != 1) 37 return CJS_Return(JSGetStringFromID(JSMessage::kParamError)); 38 39 execSingleEventByName(runtime->ToWideString(params[0]).AsStringView(), 40 XFA_Element::ExclGroup); 41 return CJS_Return(true); 42 } 43 44 CJS_Return CJX_ExclGroup::execInitialize( 45 CJS_V8* runtime, 46 const std::vector<v8::Local<v8::Value>>& params) { 47 if (!params.empty()) 48 return CJS_Return(JSGetStringFromID(JSMessage::kParamError)); 49 50 CXFA_FFNotify* pNotify = GetDocument()->GetNotify(); 51 if (pNotify) 52 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize); 53 return CJS_Return(true); 54 } 55 56 CJS_Return CJX_ExclGroup::execCalculate( 57 CJS_V8* runtime, 58 const std::vector<v8::Local<v8::Value>>& params) { 59 if (!params.empty()) 60 return CJS_Return(JSGetStringFromID(JSMessage::kParamError)); 61 62 CXFA_FFNotify* pNotify = GetDocument()->GetNotify(); 63 if (pNotify) 64 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate); 65 return CJS_Return(true); 66 } 67 68 CJS_Return CJX_ExclGroup::execValidate( 69 CJS_V8* runtime, 70 const std::vector<v8::Local<v8::Value>>& params) { 71 if (!params.empty()) 72 return CJS_Return(JSGetStringFromID(JSMessage::kParamError)); 73 74 CXFA_FFNotify* notify = GetDocument()->GetNotify(); 75 if (!notify) 76 return CJS_Return(runtime->NewBoolean(false)); 77 78 int32_t iRet = notify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Validate); 79 return CJS_Return(runtime->NewBoolean(iRet != XFA_EVENTERROR_Error)); 80 } 81 82 CJS_Return CJX_ExclGroup::selectedMember( 83 CJS_V8* runtime, 84 const std::vector<v8::Local<v8::Value>>& params) { 85 if (!params.empty()) 86 return CJS_Return(JSGetStringFromID(JSMessage::kParamError)); 87 88 CXFA_WidgetAcc* pWidgetAcc = ToNode(GetXFAObject())->GetWidgetAcc(); 89 if (!pWidgetAcc) 90 return CJS_Return(runtime->NewNull()); 91 92 CXFA_Node* pReturnNode = nullptr; 93 if (params.empty()) { 94 pReturnNode = pWidgetAcc->GetSelectedMember(); 95 } else { 96 pReturnNode = pWidgetAcc->SetSelectedMember( 97 runtime->ToWideString(params[0]).AsStringView(), true); 98 } 99 if (!pReturnNode) 100 return CJS_Return(runtime->NewNull()); 101 102 CFXJSE_Value* value = 103 GetDocument()->GetScriptContext()->GetJSValueFromMap(pReturnNode); 104 if (!value) 105 return CJS_Return(runtime->NewNull()); 106 107 return CJS_Return(value->DirectGetValue().Get(runtime->GetIsolate())); 108 } 109 110 void CJX_ExclGroup::defaultValue(CFXJSE_Value* pValue, 111 bool bSetting, 112 XFA_Attribute eAttribute) { 113 CXFA_WidgetAcc* pWidgetAcc = GetXFANode()->GetWidgetAcc(); 114 if (!pWidgetAcc) 115 return; 116 117 if (bSetting) { 118 pWidgetAcc->SetSelectedMemberByValue(pValue->ToWideString().AsStringView(), 119 true, true, true); 120 return; 121 } 122 123 WideString wsValue = GetContent(true); 124 XFA_VERSION curVersion = GetDocument()->GetCurVersionMode(); 125 if (wsValue.IsEmpty() && curVersion >= XFA_VERSION_300) { 126 pValue->SetNull(); 127 return; 128 } 129 pValue->SetString(wsValue.UTF8Encode().AsStringView()); 130 } 131 132 void CJX_ExclGroup::rawValue(CFXJSE_Value* pValue, 133 bool bSetting, 134 XFA_Attribute eAttribute) { 135 defaultValue(pValue, bSetting, eAttribute); 136 } 137 138 void CJX_ExclGroup::transient(CFXJSE_Value* pValue, 139 bool bSetting, 140 XFA_Attribute eAttribute) {} 141 142 void CJX_ExclGroup::access(CFXJSE_Value* pValue, 143 bool bSetting, 144 XFA_Attribute eAttribute) { 145 Script_Attribute_String(pValue, bSetting, eAttribute); 146 } 147 148 void CJX_ExclGroup::accessKey(CFXJSE_Value* pValue, 149 bool bSetting, 150 XFA_Attribute eAttribute) { 151 Script_Attribute_String(pValue, bSetting, eAttribute); 152 } 153 154 void CJX_ExclGroup::anchorType(CFXJSE_Value* pValue, 155 bool bSetting, 156 XFA_Attribute eAttribute) { 157 Script_Attribute_String(pValue, bSetting, eAttribute); 158 } 159 160 void CJX_ExclGroup::borderColor(CFXJSE_Value* pValue, 161 bool bSetting, 162 XFA_Attribute eAttribute) { 163 Script_Som_BorderColor(pValue, bSetting, eAttribute); 164 } 165 166 void CJX_ExclGroup::borderWidth(CFXJSE_Value* pValue, 167 bool bSetting, 168 XFA_Attribute eAttribute) { 169 Script_Som_BorderWidth(pValue, bSetting, eAttribute); 170 } 171 172 void CJX_ExclGroup::colSpan(CFXJSE_Value* pValue, 173 bool bSetting, 174 XFA_Attribute eAttribute) { 175 Script_Attribute_String(pValue, bSetting, eAttribute); 176 } 177 178 void CJX_ExclGroup::fillColor(CFXJSE_Value* pValue, 179 bool bSetting, 180 XFA_Attribute eAttribute) { 181 Script_Som_FillColor(pValue, bSetting, eAttribute); 182 } 183 184 void CJX_ExclGroup::h(CFXJSE_Value* pValue, 185 bool bSetting, 186 XFA_Attribute eAttribute) { 187 Script_Attribute_String(pValue, bSetting, eAttribute); 188 } 189 190 void CJX_ExclGroup::hAlign(CFXJSE_Value* pValue, 191 bool bSetting, 192 XFA_Attribute eAttribute) { 193 Script_Attribute_String(pValue, bSetting, eAttribute); 194 } 195 196 void CJX_ExclGroup::layout(CFXJSE_Value* pValue, 197 bool bSetting, 198 XFA_Attribute eAttribute) { 199 Script_Attribute_String(pValue, bSetting, eAttribute); 200 } 201 202 void CJX_ExclGroup::mandatory(CFXJSE_Value* pValue, 203 bool bSetting, 204 XFA_Attribute eAttribute) { 205 Script_Som_Mandatory(pValue, bSetting, eAttribute); 206 } 207 208 void CJX_ExclGroup::mandatoryMessage(CFXJSE_Value* pValue, 209 bool bSetting, 210 XFA_Attribute eAttribute) { 211 Script_Som_MandatoryMessage(pValue, bSetting, eAttribute); 212 } 213 214 void CJX_ExclGroup::maxH(CFXJSE_Value* pValue, 215 bool bSetting, 216 XFA_Attribute eAttribute) { 217 Script_Attribute_String(pValue, bSetting, eAttribute); 218 } 219 220 void CJX_ExclGroup::maxW(CFXJSE_Value* pValue, 221 bool bSetting, 222 XFA_Attribute eAttribute) { 223 Script_Attribute_String(pValue, bSetting, eAttribute); 224 } 225 226 void CJX_ExclGroup::minH(CFXJSE_Value* pValue, 227 bool bSetting, 228 XFA_Attribute eAttribute) { 229 Script_Attribute_String(pValue, bSetting, eAttribute); 230 } 231 232 void CJX_ExclGroup::minW(CFXJSE_Value* pValue, 233 bool bSetting, 234 XFA_Attribute eAttribute) { 235 Script_Attribute_String(pValue, bSetting, eAttribute); 236 } 237 238 void CJX_ExclGroup::presence(CFXJSE_Value* pValue, 239 bool bSetting, 240 XFA_Attribute eAttribute) { 241 Script_Attribute_String(pValue, bSetting, eAttribute); 242 } 243 244 void CJX_ExclGroup::relevant(CFXJSE_Value* pValue, 245 bool bSetting, 246 XFA_Attribute eAttribute) { 247 Script_Attribute_String(pValue, bSetting, eAttribute); 248 } 249 250 void CJX_ExclGroup::use(CFXJSE_Value* pValue, 251 bool bSetting, 252 XFA_Attribute eAttribute) { 253 Script_Attribute_String(pValue, bSetting, eAttribute); 254 } 255 256 void CJX_ExclGroup::usehref(CFXJSE_Value* pValue, 257 bool bSetting, 258 XFA_Attribute eAttribute) { 259 Script_Attribute_String(pValue, bSetting, eAttribute); 260 } 261 262 void CJX_ExclGroup::validationMessage(CFXJSE_Value* pValue, 263 bool bSetting, 264 XFA_Attribute eAttribute) { 265 Script_Som_ValidationMessage(pValue, bSetting, eAttribute); 266 } 267 268 void CJX_ExclGroup::vAlign(CFXJSE_Value* pValue, 269 bool bSetting, 270 XFA_Attribute eAttribute) { 271 Script_Attribute_String(pValue, bSetting, eAttribute); 272 } 273 274 void CJX_ExclGroup::w(CFXJSE_Value* pValue, 275 bool bSetting, 276 XFA_Attribute eAttribute) { 277 Script_Attribute_String(pValue, bSetting, eAttribute); 278 } 279 280 void CJX_ExclGroup::x(CFXJSE_Value* pValue, 281 bool bSetting, 282 XFA_Attribute eAttribute) { 283 Script_Attribute_String(pValue, bSetting, eAttribute); 284 } 285 286 void CJX_ExclGroup::y(CFXJSE_Value* pValue, 287 bool bSetting, 288 XFA_Attribute eAttribute) { 289 Script_Attribute_String(pValue, bSetting, eAttribute); 290 } 291