blob: 34819c75eb8b8121cd24255b289ad6f4ec49245d [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/PublicMethods.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080011
dsinclaircac704d2016-07-28 12:59:09 -070012#include "core/fpdfdoc/include/cpdf_interform.h"
Dan Sinclaira8a28e02016-03-23 15:41:39 -040013#include "core/fxcrt/include/fx_ext.h"
dsinclair64376be2016-03-31 20:03:24 -070014#include "fpdfsdk/include/fsdk_mgr.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040015#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040016#include "fpdfsdk/javascript/JS_Define.h"
17#include "fpdfsdk/javascript/JS_EventHandler.h"
18#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040019#include "fpdfsdk/javascript/JS_Value.h"
dsinclair64376be2016-03-31 20:03:24 -070020#include "fpdfsdk/javascript/cjs_context.h"
21#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040022#include "fpdfsdk/javascript/color.h"
23#include "fpdfsdk/javascript/resource.h"
24#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
28BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format)
30JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke)
31JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format)
32JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke)
33JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx)
34JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx)
35JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format)
36JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke)
37JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx)
38JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx)
39JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format)
40JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke)
41JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format)
42JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke)
43JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx)
44JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple)
45JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
46JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
47JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
48JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
49JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
50JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051END_JS_STATIC_GLOBAL_FUN()
52
53IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
54
tsepez745611b2016-04-12 16:46:34 -070055namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
tsepez745611b2016-04-12 16:46:34 -070057const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
58 L"May", L"Jun", L"Jul", L"Aug",
59 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060
tsepez745611b2016-04-12 16:46:34 -070061const FX_WCHAR* const fullmonths[] = {L"January", L"February", L"March",
62 L"April", L"May", L"June",
63 L"July", L"August", L"September",
64 L"October", L"November", L"December"};
65
66CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
67 CFX_ByteString result(pStr);
68 result.TrimLeft(' ');
69 result.TrimRight(' ');
70 return result;
71}
72
73CFX_WideString StrTrim(const CFX_WideString& pStr) {
74 CFX_WideString result(pStr);
75 result.TrimLeft(' ');
76 result.TrimRight(' ');
77 return result;
78}
79
80} // namespace
81
82bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -050083 CFX_WideString sTrim = StrTrim(str);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 const FX_WCHAR* pTrim = sTrim.c_str();
85 const FX_WCHAR* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -070086 bool bDot = false;
87 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -070090 while ((c = *p) != L'\0') {
91 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -070093 return false;
94 bDot = true;
95 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -070097 return false;
98 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700100 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 p++;
103 c = *p;
Wei Li614d20a2016-03-15 13:55:12 -0700104 if (c == L'+' || c == L'-') {
105 bKXJS = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 } else {
Wei Li614d20a2016-03-15 13:55:12 -0700107 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800109 } else if (!FXSYS_iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700110 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 }
112 p++;
113 }
114
Wei Li614d20a2016-03-15 13:55:12 -0700115 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116}
117
Wei Li614d20a2016-03-15 13:55:12 -0700118bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 switch (c_Mask) {
120 case L'9':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800121 return FXSYS_iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800123 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800125 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700127 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 default:
129 return (c_Change == c_Mask);
130 }
131}
132
Wei Li614d20a2016-03-15 13:55:12 -0700133bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
135}
136
137double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
138 double dValue1,
139 double dValue2) {
140 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
141 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
142 return dValue1 + dValue2;
143 }
144 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
145 return dValue1 * dValue2;
146 }
147 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800148 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 }
150 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800151 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 }
153 return dValue1;
154}
155
Tom Sepez67fd5df2015-10-08 12:24:19 -0700156CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 CJS_Value val) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700158 CJS_Array StrArray(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 if (val.IsArrayObject()) {
160 val.ConvertToArray(StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700161 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 }
163 CFX_WideString wsStr = val.ToCFXWideString();
164 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700165 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166
167 int ch = ',';
168 int nIndex = 0;
169
170 while (*p) {
171 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800172 if (!pTemp) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700173 StrArray.SetElement(
174 nIndex, CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 }
Lei Zhang997de612015-11-04 18:17:53 -0800177
178 char* pSub = new char[pTemp - p + 1];
179 strncpy(pSub, p, pTemp - p);
180 *(pSub + (pTemp - p)) = '\0';
181
tsepezb4c9f3f2016-04-13 15:41:21 -0700182 StrArray.SetElement(
183 nIndex, CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800184 delete[] pSub;
185
186 nIndex++;
187 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 }
189 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190}
191
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500192int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 int nStart,
194 int& nSkip,
195 int nMaxStep) {
196 int nRet = 0;
197 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500198 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 if (i - nStart > 10)
200 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500202 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800203 if (!FXSYS_iswdigit(c))
204 break;
205
Dan Sinclair1c915372016-03-03 17:12:58 -0500206 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800207 nSkip = i - nStart + 1;
208 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 break;
210 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213}
214
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500215CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
216 int nStart,
217 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 CFX_WideString swRet;
219 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500220 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
221 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800222 if (!FXSYS_iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800224
225 swRet += c;
226 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230}
231
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800233 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 int nYear = JS_GetYearFromTime(dt);
237 int nMonth = JS_GetMonthFromTime(dt) + 1;
238 int nDay = JS_GetDayFromTime(dt);
239 int nHour = JS_GetHourFromTime(dt);
240 int nMin = JS_GetMinFromTime(dt);
241 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 int nSkip = 0;
246 int nLen = value.GetLength();
247 int nIndex = 0;
248 int i = 0;
249 while (i < nLen) {
250 if (nIndex > 2)
251 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 FX_WCHAR c = value.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800254 if (FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
256 i += nSkip;
257 } else {
258 i++;
259 }
260 }
261
262 if (nIndex == 2) {
263 // case2: month/day
264 // case3: day/month
265 if ((number[0] >= 1 && number[0] <= 12) &&
266 (number[1] >= 1 && number[1] <= 31)) {
267 nMonth = number[0];
268 nDay = number[1];
269 } else if ((number[0] >= 1 && number[0] <= 31) &&
270 (number[1] >= 1 && number[1] <= 12)) {
271 nDay = number[0];
272 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700273 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274
Lei Zhang9559b7a2015-12-21 11:12:20 -0800275 if (bWrongFormat)
276 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 } else if (nIndex == 3) {
278 // case1: year/month/day
279 // case2: month/day/year
280 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700281
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
283 (number[2] >= 1 && number[2] <= 31)) {
284 nYear = number[0];
285 nMonth = number[1];
286 nDay = number[2];
287 } else if ((number[0] >= 1 && number[0] <= 12) &&
288 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
289 nMonth = number[0];
290 nDay = number[1];
291 nYear = number[2];
292 } else if ((number[0] >= 1 && number[0] <= 31) &&
293 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
294 nDay = number[0];
295 nMonth = number[1];
296 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700297 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298
Lei Zhang9559b7a2015-12-21 11:12:20 -0800299 if (bWrongFormat)
300 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800302 if (bWrongFormat)
303 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 return dt;
305 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 CFX_WideString swTemp;
308 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700309 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310}
311
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
313 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800314 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700316
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 if (format.IsEmpty() || value.IsEmpty())
318 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 int nYear = JS_GetYearFromTime(dt);
321 int nMonth = JS_GetMonthFromTime(dt) + 1;
322 int nDay = JS_GetDayFromTime(dt);
323 int nHour = JS_GetHourFromTime(dt);
324 int nMin = JS_GetMinFromTime(dt);
325 int nSec = JS_GetSecFromTime(dt);
326
327 int nYearSub = 99; // nYear - 2000;
328
329 FX_BOOL bPm = FALSE;
330 FX_BOOL bExit = FALSE;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800331 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332
333 int i = 0;
334 int j = 0;
335
336 while (i < format.GetLength()) {
337 if (bExit)
338 break;
339
340 FX_WCHAR c = format.GetAt(i);
341 switch (c) {
342 case ':':
343 case '.':
344 case '-':
345 case '\\':
346 case '/':
347 i++;
348 j++;
349 break;
350
351 case 'y':
352 case 'm':
353 case 'd':
354 case 'H':
355 case 'h':
356 case 'M':
357 case 's':
358 case 't': {
359 int oldj = j;
360 int nSkip = 0;
361 int remaining = format.GetLength() - i - 1;
362
363 if (remaining == 0 || format.GetAt(i + 1) != c) {
364 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700365 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 i++;
367 j++;
368 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700369 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 nMonth = ParseStringInteger(value, j, nSkip, 2);
371 i++;
372 j += nSkip;
373 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700374 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 nDay = ParseStringInteger(value, j, nSkip, 2);
376 i++;
377 j += nSkip;
378 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700379 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 nHour = ParseStringInteger(value, j, nSkip, 2);
381 i++;
382 j += nSkip;
383 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700384 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 nHour = ParseStringInteger(value, j, nSkip, 2);
386 i++;
387 j += nSkip;
388 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700389 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 nMin = ParseStringInteger(value, j, nSkip, 2);
391 i++;
392 j += nSkip;
393 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700394 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 nSec = ParseStringInteger(value, j, nSkip, 2);
396 i++;
397 j += nSkip;
398 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700399 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
401 i++;
402 j++;
403 break;
404 }
405 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
406 switch (c) {
407 case 'y':
408 nYear = ParseStringInteger(value, j, nSkip, 4);
409 i += 2;
410 j += nSkip;
411 break;
412 case 'm':
413 nMonth = ParseStringInteger(value, j, nSkip, 2);
414 i += 2;
415 j += nSkip;
416 break;
417 case 'd':
418 nDay = ParseStringInteger(value, j, nSkip, 2);
419 i += 2;
420 j += nSkip;
421 break;
422 case 'H':
423 nHour = ParseStringInteger(value, j, nSkip, 2);
424 i += 2;
425 j += nSkip;
426 break;
427 case 'h':
428 nHour = ParseStringInteger(value, j, nSkip, 2);
429 i += 2;
430 j += nSkip;
431 break;
432 case 'M':
433 nMin = ParseStringInteger(value, j, nSkip, 2);
434 i += 2;
435 j += nSkip;
436 break;
437 case 's':
438 nSec = ParseStringInteger(value, j, nSkip, 2);
439 i += 2;
440 j += nSkip;
441 break;
442 case 't':
443 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
444 value.GetAt(j + 1) == 'm');
445 i += 2;
446 j += 2;
447 break;
448 }
449 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
450 switch (c) {
451 case 'm': {
452 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
453 FX_BOOL bFind = FALSE;
454 for (int m = 0; m < 12; m++) {
455 if (sMonth.CompareNoCase(months[m]) == 0) {
456 nMonth = m + 1;
457 i += 3;
458 j += nSkip;
459 bFind = TRUE;
460 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700461 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 }
463
464 if (!bFind) {
465 nMonth = ParseStringInteger(value, j, nSkip, 3);
466 i += 3;
467 j += nSkip;
468 }
469 } break;
470 case 'y':
471 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700472 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 i += 3;
474 j += 3;
475 break;
476 }
477 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
478 switch (c) {
479 case 'y':
480 nYear = ParseStringInteger(value, j, nSkip, 4);
481 j += nSkip;
482 i += 4;
483 break;
484 case 'm': {
485 FX_BOOL bFind = FALSE;
486
487 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
488 sMonth.MakeLower();
489
490 for (int m = 0; m < 12; m++) {
491 CFX_WideString sFullMonths = fullmonths[m];
492 sFullMonths.MakeLower();
493
494 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
495 nMonth = m + 1;
496 i += 4;
497 j += nSkip;
498 bFind = TRUE;
499 break;
500 }
501 }
502
503 if (!bFind) {
504 nMonth = ParseStringInteger(value, j, nSkip, 4);
505 i += 4;
506 j += nSkip;
507 }
508 } break;
509 default:
510 i += 4;
511 j += 4;
512 break;
513 }
514 } else {
515 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800516 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517 bExit = TRUE;
518 }
519 i++;
520 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700521 }
Tom Sepez85386422014-07-23 10:28:37 -0700522
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800524 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525 bExit = TRUE;
526 }
527 }
528
529 break;
530 default:
531 if (value.GetLength() <= j) {
532 bExit = TRUE;
533 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800534 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535 bExit = TRUE;
536 }
537
538 i++;
539 j++;
540 break;
541 }
542 }
543
544 if (bPm)
545 nHour += 12;
546
547 if (nYear >= 0 && nYear <= nYearSub)
548 nYear += 2000;
549
550 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800551 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552
553 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800554 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555
556 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800557 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558
559 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800560 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561
562 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800563 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564
565 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800566 if (bBadFormat) {
567 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568 } else {
569 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
570 JS_MakeTime(nHour, nMin, nSec, 0));
tsepez018935c2016-04-15 13:15:12 -0700571 if (JS_PortIsNan(dRet))
572 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 }
574
tsepez018935c2016-04-15 13:15:12 -0700575 if (JS_PortIsNan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800576 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577
Lei Zhang9559b7a2015-12-21 11:12:20 -0800578 if (bWrongFormat)
579 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700580
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 return dRet;
582}
583
584CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
585 const CFX_WideString& format) {
586 CFX_WideString sRet = L"", sPart = L"";
587
588 int nYear = JS_GetYearFromTime(dDate);
589 int nMonth = JS_GetMonthFromTime(dDate) + 1;
590 int nDay = JS_GetDayFromTime(dDate);
591 int nHour = JS_GetHourFromTime(dDate);
592 int nMin = JS_GetMinFromTime(dDate);
593 int nSec = JS_GetSecFromTime(dDate);
594
595 int i = 0;
596 while (i < format.GetLength()) {
597 FX_WCHAR c = format.GetAt(i);
598 int remaining = format.GetLength() - i - 1;
599 sPart = L"";
600 switch (c) {
601 case 'y':
602 case 'm':
603 case 'd':
604 case 'H':
605 case 'h':
606 case 'M':
607 case 's':
608 case 't':
609 if (remaining == 0 || format.GetAt(i + 1) != c) {
610 switch (c) {
611 case 'y':
612 sPart += c;
613 break;
614 case 'm':
615 sPart.Format(L"%d", nMonth);
616 break;
617 case 'd':
618 sPart.Format(L"%d", nDay);
619 break;
620 case 'H':
621 sPart.Format(L"%d", nHour);
622 break;
623 case 'h':
624 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
625 break;
626 case 'M':
627 sPart.Format(L"%d", nMin);
628 break;
629 case 's':
630 sPart.Format(L"%d", nSec);
631 break;
632 case 't':
633 sPart += nHour > 12 ? 'p' : 'a';
634 break;
635 }
636 i++;
637 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
638 switch (c) {
639 case 'y':
640 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
641 break;
642 case 'm':
643 sPart.Format(L"%02d", nMonth);
644 break;
645 case 'd':
646 sPart.Format(L"%02d", nDay);
647 break;
648 case 'H':
649 sPart.Format(L"%02d", nHour);
650 break;
651 case 'h':
652 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
653 break;
654 case 'M':
655 sPart.Format(L"%02d", nMin);
656 break;
657 case 's':
658 sPart.Format(L"%02d", nSec);
659 break;
660 case 't':
661 sPart = nHour > 12 ? L"pm" : L"am";
662 break;
663 }
664 i += 2;
665 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
666 switch (c) {
667 case 'm':
668 i += 3;
669 if (nMonth > 0 && nMonth <= 12)
670 sPart += months[nMonth - 1];
671 break;
672 default:
673 i += 3;
674 sPart += c;
675 sPart += c;
676 sPart += c;
677 break;
678 }
679 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
680 switch (c) {
681 case 'y':
682 sPart.Format(L"%04d", nYear);
683 i += 4;
684 break;
685 case 'm':
686 i += 4;
687 if (nMonth > 0 && nMonth <= 12)
688 sPart += fullmonths[nMonth - 1];
689 break;
690 default:
691 i += 4;
692 sPart += c;
693 sPart += c;
694 sPart += c;
695 sPart += c;
696 break;
697 }
698 } else {
699 i++;
700 sPart += c;
701 }
702 break;
703 default:
704 i++;
705 sPart += c;
706 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700707 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700708
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700709 sRet += sPart;
710 }
711
712 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700713}
714
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
716// bCurrencyPrepend)
Tom Sepezba038bc2015-10-08 12:03:00 -0700717FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800718 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719 CJS_Value& vRet,
720 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700721#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700722 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723 if (params.size() != 6) {
724 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
725 return FALSE;
726 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700727
728 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
729 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730 if (!pEvent->m_pValue)
731 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700732
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700733 CFX_WideString& Value = pEvent->Value();
734 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735 if (strValue.IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700736 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737
738 int iDec = params[0].ToInt();
739 int iSepStyle = params[1].ToInt();
740 int iNegStyle = params[2].ToInt();
741 // params[3] is iCurrStyle, it's not used.
thestigcf03f8e2016-05-09 12:36:18 -0700742 CFX_WideString wstrCurrency = params[4].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743 FX_BOOL bCurrencyPrepend = params[5].ToBool();
744
745 if (iDec < 0)
746 iDec = -iDec;
747
748 if (iSepStyle < 0 || iSepStyle > 3)
749 iSepStyle = 0;
750
751 if (iNegStyle < 0 || iNegStyle > 3)
752 iNegStyle = 0;
753
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754 // for processing decimal places
755 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700756 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700758 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759
760 int iDec2;
761 int iNegative = 0;
762
763 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
764 if (strValue.IsEmpty()) {
765 dValue = 0;
766 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
767 if (strValue.IsEmpty()) {
768 strValue = "0";
769 iDec2 = 1;
770 }
771 }
772
773 if (iDec2 < 0) {
774 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
775 strValue = "0" + strValue;
776 }
777 iDec2 = 0;
778 }
779 int iMax = strValue.GetLength();
780 if (iDec2 > iMax) {
781 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
782 strValue += "0";
783 }
784 iMax = iDec2 + 1;
785 }
dsinclair64376be2016-03-31 20:03:24 -0700786
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700787 // for processing seperator style
788 if (iDec2 < iMax) {
789 if (iSepStyle == 0 || iSepStyle == 1) {
790 strValue.Insert(iDec2, '.');
791 iMax++;
792 } else if (iSepStyle == 2 || iSepStyle == 3) {
793 strValue.Insert(iDec2, ',');
794 iMax++;
795 }
796
797 if (iDec2 == 0)
798 strValue.Insert(iDec2, '0');
799 }
800 if (iSepStyle == 0 || iSepStyle == 2) {
801 char cSeperator;
802 if (iSepStyle == 0)
803 cSeperator = ',';
804 else
805 cSeperator = '.';
806
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700807 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700808 strValue.Insert(iDecPositive, cSeperator);
809 iMax++;
810 }
811 }
812
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700813 // for processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700814 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815
816 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700817 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 else
thestigcf03f8e2016-05-09 12:36:18 -0700819 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821 // for processing negative style
822 if (iNegative) {
823 if (iNegStyle == 0) {
thestigcf03f8e2016-05-09 12:36:18 -0700824 Value = L"-" + Value;
825 } else if (iNegStyle == 2 || iNegStyle == 3) {
826 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827 }
828 if (iNegStyle == 1 || iNegStyle == 3) {
829 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700830 CJS_Array arColor(pRuntime);
831 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832 vColElm = L"RGB";
833 arColor.SetElement(0, vColElm);
834 vColElm = 1;
835 arColor.SetElement(1, vColElm);
836 vColElm = 0;
837 arColor.SetElement(2, vColElm);
838
839 arColor.SetElement(3, vColElm);
840
Tom Sepez67fd5df2015-10-08 12:24:19 -0700841 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842 vProp.StartGetting();
843 vProp << arColor;
844 vProp.StartSetting();
845 fTarget->textColor(cc, vProp, sError); // red
846 }
847 }
848 } else {
849 if (iNegStyle == 1 || iNegStyle == 3) {
850 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700851 CJS_Array arColor(pRuntime);
852 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700853 vColElm = L"RGB";
854 arColor.SetElement(0, vColElm);
855 vColElm = 0;
856 arColor.SetElement(1, vColElm);
857 arColor.SetElement(2, vColElm);
858 arColor.SetElement(3, vColElm);
859
Tom Sepez67fd5df2015-10-08 12:24:19 -0700860 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700861 vProp.StartGetting();
862 fTarget->textColor(cc, vProp, sError);
863
Tom Sepez67fd5df2015-10-08 12:24:19 -0700864 CJS_Array aProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700865 vProp.ConvertToArray(aProp);
866
867 CPWL_Color crProp;
868 CPWL_Color crColor;
869 color::ConvertArrayToPWLColor(aProp, crProp);
870 color::ConvertArrayToPWLColor(arColor, crColor);
871
872 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700873 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874 vProp2.StartGetting();
875 vProp2 << arColor;
876 vProp2.StartSetting();
877 fTarget->textColor(cc, vProp2, sError);
878 }
879 }
880 }
881 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882#endif
883 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700884}
885
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700886// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
887// bCurrencyPrepend)
Lei Zhang945fdb72015-11-11 10:18:16 -0800888FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(
889 IJS_Context* cc,
890 const std::vector<CJS_Value>& params,
891 CJS_Value& vRet,
892 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700895
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896 if (params.size() < 2)
897 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700898
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 if (!pEvent->m_pValue)
900 return FALSE;
thestigcf03f8e2016-05-09 12:36:18 -0700901
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902 CFX_WideString& val = pEvent->Value();
thestigcf03f8e2016-05-09 12:36:18 -0700903 CFX_WideString& wstrChange = pEvent->Change();
904 CFX_WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700905
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906 if (pEvent->WillCommit()) {
ochanga0a3bc32016-05-12 15:22:48 -0700907 CFX_WideString swTemp = StrTrim(wstrValue);
908 if (swTemp.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700909 return TRUE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700910
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911 swTemp.Replace(L",", L".");
912 if (!IsNumber(swTemp.c_str())) {
913 pEvent->Rc() = FALSE;
914 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
915 Alert(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700916 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 return TRUE; // it happens after the last keystroke and before validating,
918 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700919
thestigcf03f8e2016-05-09 12:36:18 -0700920 CFX_WideString wstrSelected;
921 if (pEvent->SelStart() != -1) {
922 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
923 pEvent->SelEnd() - pEvent->SelStart());
924 }
925
926 bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927 if (bHasSign) {
928 // can't insert "change" in front to sign postion.
929 if (pEvent->SelStart() == 0) {
930 FX_BOOL& bRc = pEvent->Rc();
931 bRc = FALSE;
932 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700933 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700935
thestigcf03f8e2016-05-09 12:36:18 -0700936 int iSepStyle = params[1].ToInt();
937 if (iSepStyle < 0 || iSepStyle > 3)
938 iSepStyle = 0;
939 const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700940
thestigcf03f8e2016-05-09 12:36:18 -0700941 bool bHasSep = wstrValue.Find(cSep) != -1;
942 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
943 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 if (bHasSep) {
945 FX_BOOL& bRc = pEvent->Rc();
946 bRc = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700947 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948 }
949 bHasSep = TRUE;
950 continue;
951 }
thestigcf03f8e2016-05-09 12:36:18 -0700952 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 if (bHasSign) {
954 FX_BOOL& bRc = pEvent->Rc();
955 bRc = FALSE;
956 return TRUE;
957 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800958 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700959 if (i != 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 FX_BOOL& bRc = pEvent->Rc();
961 bRc = FALSE;
962 return TRUE;
963 }
964 if (pEvent->SelStart() != 0) {
965 FX_BOOL& bRc = pEvent->Rc();
966 bRc = FALSE;
967 return TRUE;
968 }
969 bHasSign = TRUE;
970 continue;
971 }
972
thestigcf03f8e2016-05-09 12:36:18 -0700973 if (!FXSYS_iswdigit(wstrChange[i])) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974 FX_BOOL& bRc = pEvent->Rc();
975 bRc = FALSE;
976 return TRUE;
977 }
978 }
979
thestigcf03f8e2016-05-09 12:36:18 -0700980 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
981 CFX_WideString wpostfix;
982 if (pEvent->SelEnd() < wstrValue.GetLength())
983 wpostfix = wstrValue.Mid(pEvent->SelEnd());
984 val = wprefix + wstrChange + wpostfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985 return TRUE;
986}
987
988// function AFPercent_Format(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -0800989FX_BOOL CJS_PublicMethods::AFPercent_Format(
990 IJS_Context* cc,
991 const std::vector<CJS_Value>& params,
992 CJS_Value& vRet,
993 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700994#if _FX_OS_ != _FX_ANDROID_
995 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997
998 if (params.size() != 2) {
999 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1000 return FALSE;
1001 }
1002 if (!pEvent->m_pValue)
1003 return FALSE;
1004
1005 CFX_WideString& Value = pEvent->Value();
1006 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1007 if (strValue.IsEmpty())
1008 return TRUE;
1009
1010 int iDec = params[0].ToInt();
1011 if (iDec < 0)
1012 iDec = -iDec;
1013
1014 int iSepStyle = params[1].ToInt();
1015 if (iSepStyle < 0 || iSepStyle > 3)
1016 iSepStyle = 0;
1017
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001019 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 dValue *= 100;
1021 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001022 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023
1024 int iDec2;
1025 int iNegative = 0;
1026 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1027 if (strValue.IsEmpty()) {
1028 dValue = 0;
1029 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1030 }
1031
1032 if (iDec2 < 0) {
1033 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1034 strValue = "0" + strValue;
1035 }
1036 iDec2 = 0;
1037 }
1038 int iMax = strValue.GetLength();
1039 if (iDec2 > iMax) {
1040 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1041 strValue += "0";
1042 }
1043 iMax = iDec2 + 1;
1044 }
dsinclair64376be2016-03-31 20:03:24 -07001045
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 // for processing seperator style
1047 if (iDec2 < iMax) {
1048 if (iSepStyle == 0 || iSepStyle == 1) {
1049 strValue.Insert(iDec2, '.');
1050 iMax++;
1051 } else if (iSepStyle == 2 || iSepStyle == 3) {
1052 strValue.Insert(iDec2, ',');
1053 iMax++;
1054 }
1055
1056 if (iDec2 == 0)
1057 strValue.Insert(iDec2, '0');
1058 }
1059 if (iSepStyle == 0 || iSepStyle == 2) {
1060 char cSeperator;
1061 if (iSepStyle == 0)
1062 cSeperator = ',';
1063 else
1064 cSeperator = '.';
1065
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001066 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 strValue.Insert(iDecPositive, cSeperator);
1068 iMax++;
1069 }
1070 }
dsinclair64376be2016-03-31 20:03:24 -07001071
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072 // negative mark
1073 if (iNegative)
1074 strValue = "-" + strValue;
1075 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001076 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077#endif
1078 return TRUE;
1079}
1080// AFPercent_Keystroke(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001081FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(
1082 IJS_Context* cc,
1083 const std::vector<CJS_Value>& params,
1084 CJS_Value& vRet,
1085 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086 return AFNumber_Keystroke(cc, params, vRet, sError);
1087}
1088
1089// function AFDate_FormatEx(cFormat)
Tom Sepezba038bc2015-10-08 12:03:00 -07001090FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001091 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092 CJS_Value& vRet,
1093 CFX_WideString& sError) {
1094 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096
1097 if (params.size() != 1) {
1098 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1099 return FALSE;
1100 }
1101 if (!pEvent->m_pValue)
1102 return FALSE;
1103
1104 CFX_WideString& val = pEvent->Value();
1105 CFX_WideString strValue = val;
1106 if (strValue.IsEmpty())
1107 return TRUE;
1108
1109 CFX_WideString sFormat = params[0].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001110 double dDate = 0.0f;
1111
1112 if (strValue.Find(L"GMT") != -1) {
1113 // for GMT format time
1114 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1115 dDate = MakeInterDate(strValue);
1116 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001117 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 }
1119
1120 if (JS_PortIsNan(dDate)) {
1121 CFX_WideString swMsg;
1122 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1123 sFormat.c_str());
1124 Alert(pContext, swMsg.c_str());
1125 return FALSE;
1126 }
1127
1128 val = MakeFormatDate(dDate, sFormat);
1129 return TRUE;
1130}
1131
tsepez745611b2016-04-12 16:46:34 -07001132double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001133 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001135 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001136 FX_WCHAR c = strValue.GetAt(i);
1137 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001138 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 sTemp = L"";
1140 continue;
1141 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 sTemp += c;
1143 }
Tom Sepezab277682016-02-17 10:07:21 -08001144 wsArray.push_back(sTemp);
1145 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146 return 0;
1147
Tom Sepez4246b002016-01-20 11:48:29 -08001148 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 sTemp = wsArray[1];
1150 if (sTemp.Compare(L"Jan") == 0)
1151 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001152 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001154 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001156 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001158 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001159 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001160 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001162 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001164 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001166 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001168 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001170 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001172 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 nMonth = 12;
1174
tsepez4c3debb2016-04-08 12:20:38 -07001175 int nDay = FX_atof(wsArray[2].AsStringC());
1176 int nHour = FX_atof(wsArray[3].AsStringC());
1177 int nMin = FX_atof(wsArray[4].AsStringC());
1178 int nSec = FX_atof(wsArray[5].AsStringC());
1179 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1181 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001182 if (JS_PortIsNan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001183 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184
1185 return dRet;
1186}
1187
1188// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001189FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(
1190 IJS_Context* cc,
1191 const std::vector<CJS_Value>& params,
1192 CJS_Value& vRet,
1193 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001194 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196
1197 if (params.size() != 1) {
1198 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1199 return FALSE;
1200 }
1201
1202 if (pEvent->WillCommit()) {
1203 if (!pEvent->m_pValue)
1204 return FALSE;
1205 CFX_WideString strValue = pEvent->Value();
1206 if (strValue.IsEmpty())
1207 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001208
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001209 CFX_WideString sFormat = params[0].ToCFXWideString();
Lei Zhang9559b7a2015-12-21 11:12:20 -08001210 bool bWrongFormat = FALSE;
1211 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 if (bWrongFormat || JS_PortIsNan(dRet)) {
1213 CFX_WideString swMsg;
1214 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1215 sFormat.c_str());
1216 Alert(pContext, swMsg.c_str());
1217 pEvent->Rc() = FALSE;
1218 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001219 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 }
1221 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001222}
1223
Tom Sepezba038bc2015-10-08 12:03:00 -07001224FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001225 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226 CJS_Value& vRet,
1227 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001228 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001230 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1231 return FALSE;
1232 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001233
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234 int iIndex = params[0].ToInt();
1235 const FX_WCHAR* cFormats[] = {L"m/d",
1236 L"m/d/yy",
1237 L"mm/dd/yy",
1238 L"mm/yy",
1239 L"d-mmm",
1240 L"d-mmm-yy",
1241 L"dd-mmm-yy",
1242 L"yy-mm-dd",
1243 L"mmm-yy",
1244 L"mmmm-yy",
1245 L"mmm d, yyyy",
1246 L"mmmm d, yyyy",
1247 L"m/d/yy h:MM tt",
1248 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001249
Lei Zhanga0f67242015-08-17 15:39:30 -07001250 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1251 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001252
Lei Zhang945fdb72015-11-11 10:18:16 -08001253 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001254 newParams.push_back(
1255 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 return AFDate_FormatEx(cc, newParams, vRet, sError);
1257}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001258
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001259// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001260FX_BOOL CJS_PublicMethods::AFDate_Keystroke(
1261 IJS_Context* cc,
1262 const std::vector<CJS_Value>& params,
1263 CJS_Value& vRet,
1264 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001265 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1268 return FALSE;
1269 }
1270
1271 int iIndex = params[0].ToInt();
1272 const FX_WCHAR* cFormats[] = {L"m/d",
1273 L"m/d/yy",
1274 L"mm/dd/yy",
1275 L"mm/yy",
1276 L"d-mmm",
1277 L"d-mmm-yy",
1278 L"dd-mmm-yy",
1279 L"yy-mm-dd",
1280 L"mmm-yy",
1281 L"mmmm-yy",
1282 L"mmm d, yyyy",
1283 L"mmmm d, yyyy",
1284 L"m/d/yy h:MM tt",
1285 L"m/d/yy HH:MM"};
1286
Lei Zhanga0f67242015-08-17 15:39:30 -07001287 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1288 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289
Lei Zhang945fdb72015-11-11 10:18:16 -08001290 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001291 newParams.push_back(
1292 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001293 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1294}
1295
1296// function AFTime_Format(ptf)
Tom Sepezba038bc2015-10-08 12:03:00 -07001297FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001298 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299 CJS_Value& vRet,
1300 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001301 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1304 return FALSE;
1305 }
1306
1307 int iIndex = params[0].ToInt();
1308 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1309 L"h:MM:ss tt"};
1310
Lei Zhanga0f67242015-08-17 15:39:30 -07001311 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1312 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313
Lei Zhang945fdb72015-11-11 10:18:16 -08001314 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001315 newParams.push_back(
1316 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317 return AFDate_FormatEx(cc, newParams, vRet, sError);
1318}
1319
Lei Zhang945fdb72015-11-11 10:18:16 -08001320FX_BOOL CJS_PublicMethods::AFTime_Keystroke(
1321 IJS_Context* cc,
1322 const std::vector<CJS_Value>& params,
1323 CJS_Value& vRet,
1324 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001325 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1328 return FALSE;
1329 }
1330
1331 int iIndex = params[0].ToInt();
1332 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1333 L"h:MM:ss tt"};
1334
Lei Zhanga0f67242015-08-17 15:39:30 -07001335 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1336 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001337
Lei Zhang945fdb72015-11-11 10:18:16 -08001338 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001339 newParams.push_back(
1340 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001341 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1342}
1343
Tom Sepezba038bc2015-10-08 12:03:00 -07001344FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001345 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001346 CJS_Value& vRet,
1347 CFX_WideString& sError) {
1348 return AFDate_FormatEx(cc, params, vRet, sError);
1349}
1350
Lei Zhang945fdb72015-11-11 10:18:16 -08001351FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(
1352 IJS_Context* cc,
1353 const std::vector<CJS_Value>& params,
1354 CJS_Value& vRet,
1355 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001356 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1357}
1358
1359// function AFSpecial_Format(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001360FX_BOOL CJS_PublicMethods::AFSpecial_Format(
1361 IJS_Context* cc,
1362 const std::vector<CJS_Value>& params,
1363 CJS_Value& vRet,
1364 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001365 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001366
1367 if (params.size() != 1) {
1368 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1369 return FALSE;
1370 }
1371
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 if (!pEvent->m_pValue)
1374 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375
tsepez4f1f41f2016-03-28 14:13:16 -07001376 CFX_WideString wsSource = pEvent->Value();
1377 CFX_WideString wsFormat;
1378 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001379 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001380 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001382 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001383 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001384 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001385 case 2:
1386 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1387 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388 else
tsepez4f1f41f2016-03-28 14:13:16 -07001389 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001390 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001391 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001392 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001393 break;
1394 }
1395
tsepez4f1f41f2016-03-28 14:13:16 -07001396 pEvent->Value() = util::printx(wsFormat, wsSource);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 return TRUE;
1398}
1399
1400// function AFSpecial_KeystrokeEx(mask)
Lei Zhang945fdb72015-11-11 10:18:16 -08001401FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(
1402 IJS_Context* cc,
1403 const std::vector<CJS_Value>& params,
1404 CJS_Value& vRet,
1405 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1408
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409 if (params.size() < 1) {
1410 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1411 return FALSE;
1412 }
1413
1414 if (!pEvent->m_pValue)
1415 return FALSE;
1416 CFX_WideString& valEvent = pEvent->Value();
1417
1418 CFX_WideString wstrMask = params[0].ToCFXWideString();
1419 if (wstrMask.IsEmpty())
1420 return TRUE;
1421
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001423 if (valEvent.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424 return TRUE;
thestigcf03f8e2016-05-09 12:36:18 -07001425
1426 FX_STRSIZE iIndexMask = 0;
1427 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1428 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001429 break;
1430 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001431
thestigcf03f8e2016-05-09 12:36:18 -07001432 if (iIndexMask != wstrMask.GetLength() ||
1433 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001434 Alert(
1435 pContext,
1436 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1437 pEvent->Rc() = FALSE;
1438 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001439 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001440 }
1441
1442 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001443 if (wideChange.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001444 return TRUE;
1445
thestigcf03f8e2016-05-09 12:36:18 -07001446 CFX_WideString wChange = wideChange;
1447 FX_STRSIZE iIndexMask = pEvent->SelStart();
1448 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1449 pEvent->SelStart() - pEvent->SelEnd();
1450 if (combined_len > wstrMask.GetLength()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451 Alert(pContext,
1452 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1453 pEvent->Rc() = FALSE;
1454 return TRUE;
1455 }
1456
thestigcf03f8e2016-05-09 12:36:18 -07001457 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001458 Alert(pContext,
1459 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1460 pEvent->Rc() = FALSE;
1461 return TRUE;
1462 }
1463
thestigcf03f8e2016-05-09 12:36:18 -07001464 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1465 if (iIndexMask >= wstrMask.GetLength()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001466 Alert(pContext,
1467 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1468 pEvent->Rc() = FALSE;
1469 return TRUE;
1470 }
thestigcf03f8e2016-05-09 12:36:18 -07001471 FX_WCHAR wMask = wstrMask[iIndexMask];
1472 if (!isReservedMaskChar(wMask))
1473 wChange.SetAt(i, wMask);
1474
1475 if (!maskSatisfied(wChange[i], wMask)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001476 pEvent->Rc() = FALSE;
1477 return TRUE;
1478 }
1479 iIndexMask++;
1480 }
thestigcf03f8e2016-05-09 12:36:18 -07001481 wideChange = wChange;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001482 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001483}
1484
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485// function AFSpecial_Keystroke(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001486FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(
1487 IJS_Context* cc,
1488 const std::vector<CJS_Value>& params,
1489 CJS_Value& vRet,
1490 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492 if (params.size() != 1) {
1493 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1494 return FALSE;
1495 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001496
Tom Sepez67fd5df2015-10-08 12:24:19 -07001497 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498 if (!pEvent->m_pValue)
1499 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001500
thestigcf03f8e2016-05-09 12:36:18 -07001501 const char* cFormat = "";
1502 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001503 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504 cFormat = "99999";
1505 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001506 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001507 cFormat = "999999999";
1508 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001509 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001510 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511 cFormat = "9999999999";
1512 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001513 cFormat = "9999999";
1514 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001515 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516 cFormat = "999999999";
1517 break;
1518 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001519
Lei Zhang945fdb72015-11-11 10:18:16 -08001520 std::vector<CJS_Value> params2;
thestigcf03f8e2016-05-09 12:36:18 -07001521 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001523}
1524
Tom Sepezba038bc2015-10-08 12:03:00 -07001525FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001526 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 CJS_Value& vRet,
1528 CFX_WideString& sError) {
1529 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001530 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001531
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001532 if (params.size() != 1) {
1533 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1534 return FALSE;
1535 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001536
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001537 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001538 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001539 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001540
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001541 if (pEventHandler->WillCommit()) {
1542 vRet = swValue.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001543 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544 }
1545
1546 CFX_WideString prefix, postfix;
1547
1548 if (pEventHandler->SelStart() >= 0)
1549 prefix = swValue.Mid(0, pEventHandler->SelStart());
1550 else
1551 prefix = L"";
1552
1553 if (pEventHandler->SelEnd() >= 0 &&
1554 pEventHandler->SelEnd() <= swValue.GetLength())
1555 postfix = swValue.Mid(pEventHandler->SelEnd(),
1556 swValue.GetLength() - pEventHandler->SelEnd());
1557 else
1558 postfix = L"";
1559
1560 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1561
1562 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001563}
1564
Tom Sepezba038bc2015-10-08 12:03:00 -07001565FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001566 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001567 CJS_Value& vRet,
1568 CFX_WideString& sError) {
1569 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001570 ASSERT(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001571
1572 if (params.size() != 2) {
1573 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1574 return FALSE;
1575 }
1576
1577 CFX_WideString sValue = params[0].ToCFXWideString();
1578 CFX_WideString sFormat = params[1].ToCFXWideString();
1579
Lei Zhang9559b7a2015-12-21 11:12:20 -08001580 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001581
1582 if (JS_PortIsNan(dDate)) {
1583 CFX_WideString swMsg;
1584 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1585 sFormat.c_str());
1586 Alert((CJS_Context*)cc, swMsg.c_str());
1587 return FALSE;
1588 }
1589
1590 vRet = dDate;
1591 return TRUE;
1592}
1593
Tom Sepezba038bc2015-10-08 12:03:00 -07001594FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001595 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001596 CJS_Value& vRet,
1597 CFX_WideString& sError) {
1598 if (params.size() != 3) {
1599 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001600 ASSERT(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001601
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001602 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1603 return FALSE;
1604 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001605
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001606 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1607 params[1].ToDouble(), params[2].ToDouble());
1608 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001609}
1610
Tom Sepezba038bc2015-10-08 12:03:00 -07001611FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001612 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001613 CJS_Value& vRet,
1614 CFX_WideString& sError) {
Tom Sepez4246b002016-01-20 11:48:29 -08001615 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001616 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001617 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1618 return FALSE;
1619 }
Tom Sepez4246b002016-01-20 11:48:29 -08001620 CFX_WideString ws = params[0].ToCFXWideString();
1621 ws.Replace(L",", L".");
tsepezbd9748d2016-04-13 21:40:19 -07001622 vRet = ws.c_str();
Tom Sepez4246b002016-01-20 11:48:29 -08001623 vRet.MaybeCoerceToNumber();
1624 if (vRet.GetType() != CJS_Value::VT_number)
1625 vRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001626 return TRUE;
1627}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001628
Lei Zhang945fdb72015-11-11 10:18:16 -08001629FX_BOOL CJS_PublicMethods::AFSimple_Calculate(
1630 IJS_Context* cc,
1631 const std::vector<CJS_Value>& params,
1632 CJS_Value& vRet,
1633 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001634 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001635 if (params.size() != 2) {
1636 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1637 return FALSE;
1638 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001639
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001640 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001641 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001642 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1643 return FALSE;
1644 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001645
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001647 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001649
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001650 CFX_WideString sFunction = params[0].ToCFXWideString();
Tom Sepez67fd5df2015-10-08 12:24:19 -07001651 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001652
Tom Sepez67fd5df2015-10-08 12:24:19 -07001653 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1654 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001655 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001656
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001658 CJS_Value jsValue(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001659 FieldNameArray.GetElement(i, jsValue);
1660 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001661
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001662 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1663 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1664 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001665 switch (pFormField->GetFieldType()) {
1666 case FIELDTYPE_TEXTFIELD:
1667 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001668 CFX_WideString trimmed = pFormField->GetValue();
1669 trimmed.TrimRight();
1670 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001671 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001672 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001673 case FIELDTYPE_PUSHBUTTON: {
1674 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001675 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676 case FIELDTYPE_CHECKBOX:
1677 case FIELDTYPE_RADIOBUTTON: {
1678 dTemp = 0.0;
1679 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1680 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1681 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001682 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1683 trimmed.TrimRight();
1684 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001685 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001686 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001687 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001688 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001689 }
Tom Sepez4246b002016-01-20 11:48:29 -08001690 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001691 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001692 if (pFormField->CountSelectedItems() <= 1) {
1693 CFX_WideString trimmed = pFormField->GetValue();
1694 trimmed.TrimRight();
1695 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001696 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001697 }
1698 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001699 default:
1700 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001701 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001702
1703 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1704 wcscmp(sFunction.c_str(), L"MAX") == 0))
1705 dValue = dTemp;
1706
1707 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1708
1709 nFieldsCount++;
1710 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001711 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001712 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001713
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001714 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1715 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001716
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001717 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1718 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001719 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001720 if (pContext->GetEventHandler()->m_pValue)
1721 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001722
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001724}
1725
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001726/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001727** within the specified range. */
1728
Lei Zhang945fdb72015-11-11 10:18:16 -08001729FX_BOOL CJS_PublicMethods::AFRange_Validate(
1730 IJS_Context* cc,
1731 const std::vector<CJS_Value>& params,
1732 CJS_Value& vRet,
1733 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001734 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001735 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001736
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737 if (params.size() != 4) {
1738 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1739 return FALSE;
1740 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001741
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001742 if (!pEvent->m_pValue)
1743 return FALSE;
1744 if (pEvent->Value().IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001745 return TRUE;
tsepezb4c9f3f2016-04-13 15:41:21 -07001746 double dEentValue =
1747 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001748 FX_BOOL bGreaterThan = params[0].ToBool();
1749 double dGreaterThan = params[1].ToDouble();
1750 FX_BOOL bLessThan = params[2].ToBool();
1751 double dLessThan = params[3].ToDouble();
1752 CFX_WideString swMsg;
1753
1754 if (bGreaterThan && bLessThan) {
1755 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
1756 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
1757 params[1].ToCFXWideString().c_str(),
1758 params[3].ToCFXWideString().c_str());
1759 } else if (bGreaterThan) {
1760 if (dEentValue < dGreaterThan)
1761 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
1762 params[1].ToCFXWideString().c_str());
1763 } else if (bLessThan) {
1764 if (dEentValue > dLessThan)
1765 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
1766 params[3].ToCFXWideString().c_str());
1767 }
1768
1769 if (!swMsg.IsEmpty()) {
1770 Alert(pContext, swMsg.c_str());
1771 pEvent->Rc() = FALSE;
1772 }
1773 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001774}
1775
Tom Sepezba038bc2015-10-08 12:03:00 -07001776FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001777 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001778 CJS_Value& vRet,
1779 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001780 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 if (params.size() != 1) {
1782 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1783 return FALSE;
1784 }
1785
Tom Sepez67fd5df2015-10-08 12:24:19 -07001786 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1787 CJS_Array nums(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001788
1789 CFX_WideString str = params[0].ToCFXWideString();
1790 CFX_WideString sPart;
1791
1792 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1793 str = L"0" + str;
1794
1795 int nIndex = 0;
1796 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1797 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001798 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001799 sPart += wc;
1800 } else {
1801 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001802 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001803 sPart = L"";
1804 nIndex++;
1805 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001806 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001807 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001808
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001809 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001810 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001811 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001812
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001813 if (nums.GetLength() > 0)
1814 vRet = nums;
1815 else
1816 vRet.SetNull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001817
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001818 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001819}