blob: a84c0935bc96db014cc348389cf0626276be5930 [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 <string>
11#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080012
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.
742 std::wstring wstrCurrency(params[4].ToCFXWideString().c_str());
743 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 std::wstring strValue2 = Value.c_str();
816
817 if (bCurrencyPrepend)
818 strValue2 = wstrCurrency + strValue2;
819 else
820 strValue2 = strValue2 + wstrCurrency;
821
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822 // for processing negative style
823 if (iNegative) {
824 if (iNegStyle == 0) {
825 strValue2.insert(0, L"-");
826 }
827 if (iNegStyle == 2 || iNegStyle == 3) {
828 strValue2.insert(0, L"(");
829 strValue2.insert(strValue2.length(), L")");
830 }
831 if (iNegStyle == 1 || iNegStyle == 3) {
832 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700833 CJS_Array arColor(pRuntime);
834 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835 vColElm = L"RGB";
836 arColor.SetElement(0, vColElm);
837 vColElm = 1;
838 arColor.SetElement(1, vColElm);
839 vColElm = 0;
840 arColor.SetElement(2, vColElm);
841
842 arColor.SetElement(3, vColElm);
843
Tom Sepez67fd5df2015-10-08 12:24:19 -0700844 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845 vProp.StartGetting();
846 vProp << arColor;
847 vProp.StartSetting();
848 fTarget->textColor(cc, vProp, sError); // red
849 }
850 }
851 } else {
852 if (iNegStyle == 1 || iNegStyle == 3) {
853 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700854 CJS_Array arColor(pRuntime);
855 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856 vColElm = L"RGB";
857 arColor.SetElement(0, vColElm);
858 vColElm = 0;
859 arColor.SetElement(1, vColElm);
860 arColor.SetElement(2, vColElm);
861 arColor.SetElement(3, vColElm);
862
Tom Sepez67fd5df2015-10-08 12:24:19 -0700863 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700864 vProp.StartGetting();
865 fTarget->textColor(cc, vProp, sError);
866
Tom Sepez67fd5df2015-10-08 12:24:19 -0700867 CJS_Array aProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700868 vProp.ConvertToArray(aProp);
869
870 CPWL_Color crProp;
871 CPWL_Color crColor;
872 color::ConvertArrayToPWLColor(aProp, crProp);
873 color::ConvertArrayToPWLColor(arColor, crColor);
874
875 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700876 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877 vProp2.StartGetting();
878 vProp2 << arColor;
879 vProp2.StartSetting();
880 fTarget->textColor(cc, vProp2, sError);
881 }
882 }
883 }
884 }
885 Value = strValue2.c_str();
886#endif
887 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700888}
889
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
891// bCurrencyPrepend)
Lei Zhang945fdb72015-11-11 10:18:16 -0800892FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(
893 IJS_Context* cc,
894 const std::vector<CJS_Value>& params,
895 CJS_Value& vRet,
896 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700899
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 if (params.size() < 2)
901 return FALSE;
902 int iSepStyle = params[1].ToInt();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700903
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700904 if (iSepStyle < 0 || iSepStyle > 3)
905 iSepStyle = 0;
906 if (!pEvent->m_pValue)
907 return FALSE;
908 CFX_WideString& val = pEvent->Value();
909 CFX_WideString& w_strChange = pEvent->Change();
910 CFX_WideString w_strValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700911
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912 if (pEvent->WillCommit()) {
913 CFX_WideString wstrChange = w_strChange;
tsepez745611b2016-04-12 16:46:34 -0700914 CFX_WideString wstrValue = StrTrim(w_strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 if (wstrValue.IsEmpty())
916 return TRUE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700917
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 CFX_WideString swTemp = wstrValue;
919 swTemp.Replace(L",", L".");
920 if (!IsNumber(swTemp.c_str())) {
921 pEvent->Rc() = FALSE;
922 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
923 Alert(pContext, sError.c_str());
924 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700925 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926 return TRUE; // it happens after the last keystroke and before validating,
927 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700928
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700929 std::wstring w_strValue2 = w_strValue.c_str();
930 std::wstring w_strChange2 = w_strChange.c_str();
931 std::wstring w_strSelected;
932 if (-1 != pEvent->SelStart())
933 w_strSelected = w_strValue2.substr(pEvent->SelStart(),
934 (pEvent->SelEnd() - pEvent->SelStart()));
Lei Zhangb9c31972015-08-11 14:09:35 -0700935 bool bHasSign = (w_strValue2.find('-') != std::wstring::npos) &&
936 (w_strSelected.find('-') == std::wstring::npos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 if (bHasSign) {
938 // can't insert "change" in front to sign postion.
939 if (pEvent->SelStart() == 0) {
940 FX_BOOL& bRc = pEvent->Rc();
941 bRc = FALSE;
942 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700943 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700945
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 char cSep = L'.';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700947
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948 switch (iSepStyle) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700949 case 0:
950 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 cSep = L'.';
952 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700953 case 2:
954 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955 cSep = L',';
956 break;
957 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700958
Lei Zhangb9c31972015-08-11 14:09:35 -0700959 bool bHasSep = (w_strValue2.find(cSep) != std::wstring::npos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 for (std::wstring::iterator it = w_strChange2.begin();
961 it != w_strChange2.end(); it++) {
962 if (*it == cSep) {
963 if (bHasSep) {
964 FX_BOOL& bRc = pEvent->Rc();
965 bRc = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700966 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967 }
968 bHasSep = TRUE;
969 continue;
970 }
971 if (*it == L'-') {
972 if (bHasSign) {
973 FX_BOOL& bRc = pEvent->Rc();
974 bRc = FALSE;
975 return TRUE;
976 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800977 // sign's position is not correct
978 if (it != w_strChange2.begin()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979 FX_BOOL& bRc = pEvent->Rc();
980 bRc = FALSE;
981 return TRUE;
982 }
983 if (pEvent->SelStart() != 0) {
984 FX_BOOL& bRc = pEvent->Rc();
985 bRc = FALSE;
986 return TRUE;
987 }
988 bHasSign = TRUE;
989 continue;
990 }
991
Lei Zhang9559b7a2015-12-21 11:12:20 -0800992 if (!FXSYS_iswdigit(*it)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993 FX_BOOL& bRc = pEvent->Rc();
994 bRc = FALSE;
995 return TRUE;
996 }
997 }
998
999 std::wstring w_prefix = w_strValue2.substr(0, pEvent->SelStart());
1000 std::wstring w_postfix;
1001 if (pEvent->SelEnd() < (int)w_strValue2.length())
1002 w_postfix = w_strValue2.substr(pEvent->SelEnd());
1003 w_strValue2 = w_prefix + w_strChange2 + w_postfix;
1004 w_strValue = w_strValue2.c_str();
1005 val = w_strValue;
1006 return TRUE;
1007}
1008
1009// function AFPercent_Format(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001010FX_BOOL CJS_PublicMethods::AFPercent_Format(
1011 IJS_Context* cc,
1012 const std::vector<CJS_Value>& params,
1013 CJS_Value& vRet,
1014 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015#if _FX_OS_ != _FX_ANDROID_
1016 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018
1019 if (params.size() != 2) {
1020 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1021 return FALSE;
1022 }
1023 if (!pEvent->m_pValue)
1024 return FALSE;
1025
1026 CFX_WideString& Value = pEvent->Value();
1027 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1028 if (strValue.IsEmpty())
1029 return TRUE;
1030
1031 int iDec = params[0].ToInt();
1032 if (iDec < 0)
1033 iDec = -iDec;
1034
1035 int iSepStyle = params[1].ToInt();
1036 if (iSepStyle < 0 || iSepStyle > 3)
1037 iSepStyle = 0;
1038
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001040 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041 dValue *= 100;
1042 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001043 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044
1045 int iDec2;
1046 int iNegative = 0;
1047 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1048 if (strValue.IsEmpty()) {
1049 dValue = 0;
1050 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1051 }
1052
1053 if (iDec2 < 0) {
1054 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1055 strValue = "0" + strValue;
1056 }
1057 iDec2 = 0;
1058 }
1059 int iMax = strValue.GetLength();
1060 if (iDec2 > iMax) {
1061 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1062 strValue += "0";
1063 }
1064 iMax = iDec2 + 1;
1065 }
dsinclair64376be2016-03-31 20:03:24 -07001066
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 // for processing seperator style
1068 if (iDec2 < iMax) {
1069 if (iSepStyle == 0 || iSepStyle == 1) {
1070 strValue.Insert(iDec2, '.');
1071 iMax++;
1072 } else if (iSepStyle == 2 || iSepStyle == 3) {
1073 strValue.Insert(iDec2, ',');
1074 iMax++;
1075 }
1076
1077 if (iDec2 == 0)
1078 strValue.Insert(iDec2, '0');
1079 }
1080 if (iSepStyle == 0 || iSepStyle == 2) {
1081 char cSeperator;
1082 if (iSepStyle == 0)
1083 cSeperator = ',';
1084 else
1085 cSeperator = '.';
1086
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001087 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 strValue.Insert(iDecPositive, cSeperator);
1089 iMax++;
1090 }
1091 }
dsinclair64376be2016-03-31 20:03:24 -07001092
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 // negative mark
1094 if (iNegative)
1095 strValue = "-" + strValue;
1096 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001097 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001098#endif
1099 return TRUE;
1100}
1101// AFPercent_Keystroke(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001102FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(
1103 IJS_Context* cc,
1104 const std::vector<CJS_Value>& params,
1105 CJS_Value& vRet,
1106 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107 return AFNumber_Keystroke(cc, params, vRet, sError);
1108}
1109
1110// function AFDate_FormatEx(cFormat)
Tom Sepezba038bc2015-10-08 12:03:00 -07001111FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001112 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113 CJS_Value& vRet,
1114 CFX_WideString& sError) {
1115 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117
1118 if (params.size() != 1) {
1119 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1120 return FALSE;
1121 }
1122 if (!pEvent->m_pValue)
1123 return FALSE;
1124
1125 CFX_WideString& val = pEvent->Value();
1126 CFX_WideString strValue = val;
1127 if (strValue.IsEmpty())
1128 return TRUE;
1129
1130 CFX_WideString sFormat = params[0].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001131 double dDate = 0.0f;
1132
1133 if (strValue.Find(L"GMT") != -1) {
1134 // for GMT format time
1135 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1136 dDate = MakeInterDate(strValue);
1137 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001138 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 }
1140
1141 if (JS_PortIsNan(dDate)) {
1142 CFX_WideString swMsg;
1143 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1144 sFormat.c_str());
1145 Alert(pContext, swMsg.c_str());
1146 return FALSE;
1147 }
1148
1149 val = MakeFormatDate(dDate, sFormat);
1150 return TRUE;
1151}
1152
tsepez745611b2016-04-12 16:46:34 -07001153double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001154 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001156 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 FX_WCHAR c = strValue.GetAt(i);
1158 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001159 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 sTemp = L"";
1161 continue;
1162 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 sTemp += c;
1164 }
Tom Sepezab277682016-02-17 10:07:21 -08001165 wsArray.push_back(sTemp);
1166 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 return 0;
1168
Tom Sepez4246b002016-01-20 11:48:29 -08001169 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 sTemp = wsArray[1];
1171 if (sTemp.Compare(L"Jan") == 0)
1172 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001173 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001175 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001176 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001177 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001179 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001181 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001183 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001185 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001187 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001188 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001189 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001191 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001192 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001193 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001194 nMonth = 12;
1195
tsepez4c3debb2016-04-08 12:20:38 -07001196 int nDay = FX_atof(wsArray[2].AsStringC());
1197 int nHour = FX_atof(wsArray[3].AsStringC());
1198 int nMin = FX_atof(wsArray[4].AsStringC());
1199 int nSec = FX_atof(wsArray[5].AsStringC());
1200 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1202 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001203 if (JS_PortIsNan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001204 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001205
1206 return dRet;
1207}
1208
1209// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001210FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(
1211 IJS_Context* cc,
1212 const std::vector<CJS_Value>& params,
1213 CJS_Value& vRet,
1214 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001215 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217
1218 if (params.size() != 1) {
1219 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1220 return FALSE;
1221 }
1222
1223 if (pEvent->WillCommit()) {
1224 if (!pEvent->m_pValue)
1225 return FALSE;
1226 CFX_WideString strValue = pEvent->Value();
1227 if (strValue.IsEmpty())
1228 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001229
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001230 CFX_WideString sFormat = params[0].ToCFXWideString();
Lei Zhang9559b7a2015-12-21 11:12:20 -08001231 bool bWrongFormat = FALSE;
1232 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233 if (bWrongFormat || JS_PortIsNan(dRet)) {
1234 CFX_WideString swMsg;
1235 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1236 sFormat.c_str());
1237 Alert(pContext, swMsg.c_str());
1238 pEvent->Rc() = FALSE;
1239 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001240 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001241 }
1242 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001243}
1244
Tom Sepezba038bc2015-10-08 12:03:00 -07001245FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001246 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001247 CJS_Value& vRet,
1248 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001249 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001250 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1252 return FALSE;
1253 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001254
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001255 int iIndex = params[0].ToInt();
1256 const FX_WCHAR* cFormats[] = {L"m/d",
1257 L"m/d/yy",
1258 L"mm/dd/yy",
1259 L"mm/yy",
1260 L"d-mmm",
1261 L"d-mmm-yy",
1262 L"dd-mmm-yy",
1263 L"yy-mm-dd",
1264 L"mmm-yy",
1265 L"mmmm-yy",
1266 L"mmm d, yyyy",
1267 L"mmmm d, yyyy",
1268 L"m/d/yy h:MM tt",
1269 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001270
Lei Zhanga0f67242015-08-17 15:39:30 -07001271 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1272 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001273
Lei Zhang945fdb72015-11-11 10:18:16 -08001274 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001275 newParams.push_back(
1276 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 return AFDate_FormatEx(cc, newParams, vRet, sError);
1278}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001279
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001281FX_BOOL CJS_PublicMethods::AFDate_Keystroke(
1282 IJS_Context* cc,
1283 const std::vector<CJS_Value>& params,
1284 CJS_Value& vRet,
1285 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001286 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1289 return FALSE;
1290 }
1291
1292 int iIndex = params[0].ToInt();
1293 const FX_WCHAR* cFormats[] = {L"m/d",
1294 L"m/d/yy",
1295 L"mm/dd/yy",
1296 L"mm/yy",
1297 L"d-mmm",
1298 L"d-mmm-yy",
1299 L"dd-mmm-yy",
1300 L"yy-mm-dd",
1301 L"mmm-yy",
1302 L"mmmm-yy",
1303 L"mmm d, yyyy",
1304 L"mmmm d, yyyy",
1305 L"m/d/yy h:MM tt",
1306 L"m/d/yy HH:MM"};
1307
Lei Zhanga0f67242015-08-17 15:39:30 -07001308 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1309 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001310
Lei Zhang945fdb72015-11-11 10:18:16 -08001311 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001312 newParams.push_back(
1313 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1315}
1316
1317// function AFTime_Format(ptf)
Tom Sepezba038bc2015-10-08 12:03:00 -07001318FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001319 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320 CJS_Value& vRet,
1321 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001322 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001323 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1325 return FALSE;
1326 }
1327
1328 int iIndex = params[0].ToInt();
1329 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1330 L"h:MM:ss tt"};
1331
Lei Zhanga0f67242015-08-17 15:39:30 -07001332 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1333 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001334
Lei Zhang945fdb72015-11-11 10:18:16 -08001335 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001336 newParams.push_back(
1337 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001338 return AFDate_FormatEx(cc, newParams, vRet, sError);
1339}
1340
Lei Zhang945fdb72015-11-11 10:18:16 -08001341FX_BOOL CJS_PublicMethods::AFTime_Keystroke(
1342 IJS_Context* cc,
1343 const std::vector<CJS_Value>& params,
1344 CJS_Value& vRet,
1345 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001346 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001347 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1349 return FALSE;
1350 }
1351
1352 int iIndex = params[0].ToInt();
1353 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1354 L"h:MM:ss tt"};
1355
Lei Zhanga0f67242015-08-17 15:39:30 -07001356 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1357 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358
Lei Zhang945fdb72015-11-11 10:18:16 -08001359 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001360 newParams.push_back(
1361 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1363}
1364
Tom Sepezba038bc2015-10-08 12:03:00 -07001365FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001366 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 CJS_Value& vRet,
1368 CFX_WideString& sError) {
1369 return AFDate_FormatEx(cc, params, vRet, sError);
1370}
1371
Lei Zhang945fdb72015-11-11 10:18:16 -08001372FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(
1373 IJS_Context* cc,
1374 const std::vector<CJS_Value>& params,
1375 CJS_Value& vRet,
1376 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1378}
1379
1380// function AFSpecial_Format(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001381FX_BOOL CJS_PublicMethods::AFSpecial_Format(
1382 IJS_Context* cc,
1383 const std::vector<CJS_Value>& params,
1384 CJS_Value& vRet,
1385 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001386 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387
1388 if (params.size() != 1) {
1389 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1390 return FALSE;
1391 }
1392
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001393 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 if (!pEvent->m_pValue)
1395 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396
tsepez4f1f41f2016-03-28 14:13:16 -07001397 CFX_WideString wsSource = pEvent->Value();
1398 CFX_WideString wsFormat;
1399 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001400 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001401 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001402 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001403 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001404 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001406 case 2:
1407 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1408 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409 else
tsepez4f1f41f2016-03-28 14:13:16 -07001410 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001411 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001412 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001413 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001414 break;
1415 }
1416
tsepez4f1f41f2016-03-28 14:13:16 -07001417 pEvent->Value() = util::printx(wsFormat, wsSource);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001418 return TRUE;
1419}
1420
1421// function AFSpecial_KeystrokeEx(mask)
Lei Zhang945fdb72015-11-11 10:18:16 -08001422FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(
1423 IJS_Context* cc,
1424 const std::vector<CJS_Value>& params,
1425 CJS_Value& vRet,
1426 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001428 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1429
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001430 if (params.size() < 1) {
1431 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1432 return FALSE;
1433 }
1434
1435 if (!pEvent->m_pValue)
1436 return FALSE;
1437 CFX_WideString& valEvent = pEvent->Value();
1438
1439 CFX_WideString wstrMask = params[0].ToCFXWideString();
1440 if (wstrMask.IsEmpty())
1441 return TRUE;
1442
Lei Zhanga0f67242015-08-17 15:39:30 -07001443 const size_t wstrMaskLen = wstrMask.GetLength();
1444 const std::wstring wstrValue = valEvent.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001445
1446 if (pEvent->WillCommit()) {
1447 if (wstrValue.empty())
1448 return TRUE;
Lei Zhanga0f67242015-08-17 15:39:30 -07001449 size_t iIndexMask = 0;
1450 for (const auto& w_Value : wstrValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451 if (!maskSatisfied(w_Value, wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001452 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001453 iIndexMask++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001454 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001455
Lei Zhanga0f67242015-08-17 15:39:30 -07001456 if (iIndexMask != wstrMaskLen ||
1457 (iIndexMask != wstrValue.size() && wstrMaskLen != 0)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001458 Alert(
1459 pContext,
1460 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1461 pEvent->Rc() = FALSE;
1462 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001463 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001464 }
1465
1466 CFX_WideString& wideChange = pEvent->Change();
1467 std::wstring wChange = wideChange.c_str();
1468 if (wChange.empty())
1469 return TRUE;
1470
Wei Li05d53f02016-03-29 16:42:53 -07001471 size_t iIndexMask = pEvent->SelStart();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472
Lei Zhanga0f67242015-08-17 15:39:30 -07001473 size_t combined_len = wstrValue.length() + wChange.length() -
1474 (pEvent->SelEnd() - pEvent->SelStart());
1475 if (combined_len > wstrMaskLen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001476 Alert(pContext,
1477 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1478 pEvent->Rc() = FALSE;
1479 return TRUE;
1480 }
1481
Lei Zhanga0f67242015-08-17 15:39:30 -07001482 if (iIndexMask >= wstrMaskLen && (!wChange.empty())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001483 Alert(pContext,
1484 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1485 pEvent->Rc() = FALSE;
1486 return TRUE;
1487 }
1488
1489 for (std::wstring::iterator it = wChange.begin(); it != wChange.end(); it++) {
Lei Zhanga0f67242015-08-17 15:39:30 -07001490 if (iIndexMask >= wstrMaskLen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491 Alert(pContext,
1492 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1493 pEvent->Rc() = FALSE;
1494 return TRUE;
1495 }
1496 wchar_t w_Mask = wstrMask[iIndexMask];
1497 if (!isReservedMaskChar(w_Mask)) {
1498 *it = w_Mask;
1499 }
1500 wchar_t w_Change = *it;
1501 if (!maskSatisfied(w_Change, w_Mask)) {
1502 pEvent->Rc() = FALSE;
1503 return TRUE;
1504 }
1505 iIndexMask++;
1506 }
1507
1508 wideChange = wChange.c_str();
1509 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001510}
1511
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512// function AFSpecial_Keystroke(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001513FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(
1514 IJS_Context* cc,
1515 const std::vector<CJS_Value>& params,
1516 CJS_Value& vRet,
1517 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001519 if (params.size() != 1) {
1520 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1521 return FALSE;
1522 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001523
Tom Sepez67fd5df2015-10-08 12:24:19 -07001524 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001525 if (!pEvent->m_pValue)
1526 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001527
1528 std::string cFormat;
1529 int iIndex = params[0].ToInt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001530 CFX_WideString& val = pEvent->Value();
1531 std::string strSrc = CFX_ByteString::FromUnicode(val).c_str();
1532 std::wstring wstrChange = pEvent->Change().c_str();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001533
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001534 switch (iIndex) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001535 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001536 cFormat = "99999";
1537 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001538 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001539 cFormat = "999999999";
1540 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001541 case 2:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542 if (strSrc.length() + wstrChange.length() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 cFormat = "9999999999";
1544 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001545 cFormat = "9999999";
1546 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001547 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001548 cFormat = "999999999";
1549 break;
1550 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001551
Lei Zhang945fdb72015-11-11 10:18:16 -08001552 std::vector<CJS_Value> params2;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001553 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001554 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001555}
1556
Tom Sepezba038bc2015-10-08 12:03:00 -07001557FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001558 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001559 CJS_Value& vRet,
1560 CFX_WideString& sError) {
1561 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001562 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001563
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001564 if (params.size() != 1) {
1565 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1566 return FALSE;
1567 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001568
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001569 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001570 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001571 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001572
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001573 if (pEventHandler->WillCommit()) {
1574 vRet = swValue.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001575 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001576 }
1577
1578 CFX_WideString prefix, postfix;
1579
1580 if (pEventHandler->SelStart() >= 0)
1581 prefix = swValue.Mid(0, pEventHandler->SelStart());
1582 else
1583 prefix = L"";
1584
1585 if (pEventHandler->SelEnd() >= 0 &&
1586 pEventHandler->SelEnd() <= swValue.GetLength())
1587 postfix = swValue.Mid(pEventHandler->SelEnd(),
1588 swValue.GetLength() - pEventHandler->SelEnd());
1589 else
1590 postfix = L"";
1591
1592 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1593
1594 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001595}
1596
Tom Sepezba038bc2015-10-08 12:03:00 -07001597FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001598 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001599 CJS_Value& vRet,
1600 CFX_WideString& sError) {
1601 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001602 ASSERT(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001603
1604 if (params.size() != 2) {
1605 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1606 return FALSE;
1607 }
1608
1609 CFX_WideString sValue = params[0].ToCFXWideString();
1610 CFX_WideString sFormat = params[1].ToCFXWideString();
1611
Lei Zhang9559b7a2015-12-21 11:12:20 -08001612 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001613
1614 if (JS_PortIsNan(dDate)) {
1615 CFX_WideString swMsg;
1616 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1617 sFormat.c_str());
1618 Alert((CJS_Context*)cc, swMsg.c_str());
1619 return FALSE;
1620 }
1621
1622 vRet = dDate;
1623 return TRUE;
1624}
1625
Tom Sepezba038bc2015-10-08 12:03:00 -07001626FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001627 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001628 CJS_Value& vRet,
1629 CFX_WideString& sError) {
1630 if (params.size() != 3) {
1631 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001632 ASSERT(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001633
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001634 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1635 return FALSE;
1636 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001637
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001638 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1639 params[1].ToDouble(), params[2].ToDouble());
1640 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001641}
1642
Tom Sepezba038bc2015-10-08 12:03:00 -07001643FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001644 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 CJS_Value& vRet,
1646 CFX_WideString& sError) {
Tom Sepez4246b002016-01-20 11:48:29 -08001647 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001649 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1650 return FALSE;
1651 }
Tom Sepez4246b002016-01-20 11:48:29 -08001652 CFX_WideString ws = params[0].ToCFXWideString();
1653 ws.Replace(L",", L".");
tsepezbd9748d2016-04-13 21:40:19 -07001654 vRet = ws.c_str();
Tom Sepez4246b002016-01-20 11:48:29 -08001655 vRet.MaybeCoerceToNumber();
1656 if (vRet.GetType() != CJS_Value::VT_number)
1657 vRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001658 return TRUE;
1659}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001660
Lei Zhang945fdb72015-11-11 10:18:16 -08001661FX_BOOL CJS_PublicMethods::AFSimple_Calculate(
1662 IJS_Context* cc,
1663 const std::vector<CJS_Value>& params,
1664 CJS_Value& vRet,
1665 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001666 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001667 if (params.size() != 2) {
1668 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1669 return FALSE;
1670 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001671
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001672 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001673 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001674 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1675 return FALSE;
1676 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001677
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001678 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001679 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001680 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001681
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001682 CFX_WideString sFunction = params[0].ToCFXWideString();
Tom Sepez67fd5df2015-10-08 12:24:19 -07001683 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001684
Tom Sepez67fd5df2015-10-08 12:24:19 -07001685 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1686 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001687 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001688
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001689 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001690 CJS_Value jsValue(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001691 FieldNameArray.GetElement(i, jsValue);
1692 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001693
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001694 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1695 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1696 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001697 switch (pFormField->GetFieldType()) {
1698 case FIELDTYPE_TEXTFIELD:
1699 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001700 CFX_WideString trimmed = pFormField->GetValue();
1701 trimmed.TrimRight();
1702 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001703 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001704 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001705 case FIELDTYPE_PUSHBUTTON: {
1706 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001707 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001708 case FIELDTYPE_CHECKBOX:
1709 case FIELDTYPE_RADIOBUTTON: {
1710 dTemp = 0.0;
1711 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1712 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1713 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001714 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1715 trimmed.TrimRight();
1716 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001717 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001718 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001719 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001720 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001721 }
Tom Sepez4246b002016-01-20 11:48:29 -08001722 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001724 if (pFormField->CountSelectedItems() <= 1) {
1725 CFX_WideString trimmed = pFormField->GetValue();
1726 trimmed.TrimRight();
1727 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001728 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001729 }
1730 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731 default:
1732 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001733 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001734
1735 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1736 wcscmp(sFunction.c_str(), L"MAX") == 0))
1737 dValue = dTemp;
1738
1739 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1740
1741 nFieldsCount++;
1742 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001743 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001744 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001745
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001746 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1747 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001748
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001749 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1750 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001751 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001752 if (pContext->GetEventHandler()->m_pValue)
1753 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001754
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001755 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001756}
1757
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001758/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001759** within the specified range. */
1760
Lei Zhang945fdb72015-11-11 10:18:16 -08001761FX_BOOL CJS_PublicMethods::AFRange_Validate(
1762 IJS_Context* cc,
1763 const std::vector<CJS_Value>& params,
1764 CJS_Value& vRet,
1765 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001766 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001767 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001768
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001769 if (params.size() != 4) {
1770 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1771 return FALSE;
1772 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001773
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001774 if (!pEvent->m_pValue)
1775 return FALSE;
1776 if (pEvent->Value().IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001777 return TRUE;
tsepezb4c9f3f2016-04-13 15:41:21 -07001778 double dEentValue =
1779 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001780 FX_BOOL bGreaterThan = params[0].ToBool();
1781 double dGreaterThan = params[1].ToDouble();
1782 FX_BOOL bLessThan = params[2].ToBool();
1783 double dLessThan = params[3].ToDouble();
1784 CFX_WideString swMsg;
1785
1786 if (bGreaterThan && bLessThan) {
1787 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
1788 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
1789 params[1].ToCFXWideString().c_str(),
1790 params[3].ToCFXWideString().c_str());
1791 } else if (bGreaterThan) {
1792 if (dEentValue < dGreaterThan)
1793 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
1794 params[1].ToCFXWideString().c_str());
1795 } else if (bLessThan) {
1796 if (dEentValue > dLessThan)
1797 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
1798 params[3].ToCFXWideString().c_str());
1799 }
1800
1801 if (!swMsg.IsEmpty()) {
1802 Alert(pContext, swMsg.c_str());
1803 pEvent->Rc() = FALSE;
1804 }
1805 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001806}
1807
Tom Sepezba038bc2015-10-08 12:03:00 -07001808FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001809 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001810 CJS_Value& vRet,
1811 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001813 if (params.size() != 1) {
1814 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1815 return FALSE;
1816 }
1817
Tom Sepez67fd5df2015-10-08 12:24:19 -07001818 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1819 CJS_Array nums(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001820
1821 CFX_WideString str = params[0].ToCFXWideString();
1822 CFX_WideString sPart;
1823
1824 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1825 str = L"0" + str;
1826
1827 int nIndex = 0;
1828 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1829 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001830 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001831 sPart += wc;
1832 } else {
1833 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001834 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001835 sPart = L"";
1836 nIndex++;
1837 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001838 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001839 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001840
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001841 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001842 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001843 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001844
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001845 if (nums.GetLength() > 0)
1846 vRet = nums;
1847 else
1848 vRet.SetNull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001849
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001850 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001851}