blob: aa0efb1fa65d6d8b8e45b7c3de0e9f45460215a5 [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
tsepeze1e7bd02016-08-08 13:03:16 -070080void AlertIfPossible(CJS_Context* pContext, const FX_WCHAR* swMsg) {
81 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
82 if (pApp)
83 pApp->JS_appAlert(swMsg, nullptr, 0, 3);
84}
85
tsepez745611b2016-04-12 16:46:34 -070086} // namespace
87
88bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -050089 CFX_WideString sTrim = StrTrim(str);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 const FX_WCHAR* pTrim = sTrim.c_str();
91 const FX_WCHAR* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -070092 bool bDot = false;
93 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -070096 while ((c = *p) != L'\0') {
97 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -070099 return false;
100 bDot = true;
101 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -0700103 return false;
104 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700106 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 p++;
109 c = *p;
Wei Li614d20a2016-03-15 13:55:12 -0700110 if (c == L'+' || c == L'-') {
111 bKXJS = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 } else {
Wei Li614d20a2016-03-15 13:55:12 -0700113 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800115 } else if (!FXSYS_iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700116 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 }
118 p++;
119 }
120
Wei Li614d20a2016-03-15 13:55:12 -0700121 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122}
123
Wei Li614d20a2016-03-15 13:55:12 -0700124bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 switch (c_Mask) {
126 case L'9':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800127 return FXSYS_iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800129 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800131 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700133 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 default:
135 return (c_Change == c_Mask);
136 }
137}
138
Wei Li614d20a2016-03-15 13:55:12 -0700139bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
141}
142
143double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
144 double dValue1,
145 double dValue2) {
146 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
147 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
148 return dValue1 + dValue2;
149 }
150 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
151 return dValue1 * dValue2;
152 }
153 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800154 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 }
156 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800157 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 }
159 return dValue1;
160}
161
Tom Sepez67fd5df2015-10-08 12:24:19 -0700162CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 CJS_Value val) {
tsepeze5aff742016-08-08 09:49:42 -0700164 CJS_Array StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 if (val.IsArrayObject()) {
166 val.ConvertToArray(StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700167 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 }
169 CFX_WideString wsStr = val.ToCFXWideString();
170 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700171 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172
173 int ch = ',';
174 int nIndex = 0;
175
176 while (*p) {
177 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800178 if (!pTemp) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700179 StrArray.SetElement(
tsepeze5aff742016-08-08 09:49:42 -0700180 pRuntime->GetIsolate(), nIndex,
181 CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 }
Lei Zhang997de612015-11-04 18:17:53 -0800184
185 char* pSub = new char[pTemp - p + 1];
186 strncpy(pSub, p, pTemp - p);
187 *(pSub + (pTemp - p)) = '\0';
188
tsepezb4c9f3f2016-04-13 15:41:21 -0700189 StrArray.SetElement(
tsepeze5aff742016-08-08 09:49:42 -0700190 pRuntime->GetIsolate(), nIndex,
191 CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800192 delete[] pSub;
193
194 nIndex++;
195 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 }
197 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500200int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 int nStart,
202 int& nSkip,
203 int nMaxStep) {
204 int nRet = 0;
205 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500206 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 if (i - nStart > 10)
208 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500210 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800211 if (!FXSYS_iswdigit(c))
212 break;
213
Dan Sinclair1c915372016-03-03 17:12:58 -0500214 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800215 nSkip = i - nStart + 1;
216 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 break;
218 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500223CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
224 int nStart,
225 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 CFX_WideString swRet;
227 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500228 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
229 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800230 if (!FXSYS_iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800232
233 swRet += c;
234 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800241 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 int nYear = JS_GetYearFromTime(dt);
245 int nMonth = JS_GetMonthFromTime(dt) + 1;
246 int nDay = JS_GetDayFromTime(dt);
247 int nHour = JS_GetHourFromTime(dt);
248 int nMin = JS_GetMinFromTime(dt);
249 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 int nSkip = 0;
254 int nLen = value.GetLength();
255 int nIndex = 0;
256 int i = 0;
257 while (i < nLen) {
258 if (nIndex > 2)
259 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 FX_WCHAR c = value.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800262 if (FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
264 i += nSkip;
265 } else {
266 i++;
267 }
268 }
269
270 if (nIndex == 2) {
271 // case2: month/day
272 // case3: day/month
273 if ((number[0] >= 1 && number[0] <= 12) &&
274 (number[1] >= 1 && number[1] <= 31)) {
275 nMonth = number[0];
276 nDay = number[1];
277 } else if ((number[0] >= 1 && number[0] <= 31) &&
278 (number[1] >= 1 && number[1] <= 12)) {
279 nDay = number[0];
280 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700281 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Lei Zhang9559b7a2015-12-21 11:12:20 -0800283 if (bWrongFormat)
284 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 } else if (nIndex == 3) {
286 // case1: year/month/day
287 // case2: month/day/year
288 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700289
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
291 (number[2] >= 1 && number[2] <= 31)) {
292 nYear = number[0];
293 nMonth = number[1];
294 nDay = number[2];
295 } else if ((number[0] >= 1 && number[0] <= 12) &&
296 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
297 nMonth = number[0];
298 nDay = number[1];
299 nYear = number[2];
300 } else if ((number[0] >= 1 && number[0] <= 31) &&
301 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
302 nDay = number[0];
303 nMonth = number[1];
304 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700305 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306
Lei Zhang9559b7a2015-12-21 11:12:20 -0800307 if (bWrongFormat)
308 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800310 if (bWrongFormat)
311 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 return dt;
313 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700314
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 CFX_WideString swTemp;
316 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700317 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318}
319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
321 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800322 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 if (format.IsEmpty() || value.IsEmpty())
326 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700327
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 int nYear = JS_GetYearFromTime(dt);
329 int nMonth = JS_GetMonthFromTime(dt) + 1;
330 int nDay = JS_GetDayFromTime(dt);
331 int nHour = JS_GetHourFromTime(dt);
332 int nMin = JS_GetMinFromTime(dt);
333 int nSec = JS_GetSecFromTime(dt);
334
335 int nYearSub = 99; // nYear - 2000;
336
337 FX_BOOL bPm = FALSE;
338 FX_BOOL bExit = FALSE;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800339 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340
341 int i = 0;
342 int j = 0;
343
344 while (i < format.GetLength()) {
345 if (bExit)
346 break;
347
348 FX_WCHAR c = format.GetAt(i);
349 switch (c) {
350 case ':':
351 case '.':
352 case '-':
353 case '\\':
354 case '/':
355 i++;
356 j++;
357 break;
358
359 case 'y':
360 case 'm':
361 case 'd':
362 case 'H':
363 case 'h':
364 case 'M':
365 case 's':
366 case 't': {
367 int oldj = j;
368 int nSkip = 0;
369 int remaining = format.GetLength() - i - 1;
370
371 if (remaining == 0 || format.GetAt(i + 1) != c) {
372 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700373 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 i++;
375 j++;
376 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700377 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 nMonth = ParseStringInteger(value, j, nSkip, 2);
379 i++;
380 j += nSkip;
381 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700382 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 nDay = ParseStringInteger(value, j, nSkip, 2);
384 i++;
385 j += nSkip;
386 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700387 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 nHour = ParseStringInteger(value, j, nSkip, 2);
389 i++;
390 j += nSkip;
391 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700392 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 nHour = ParseStringInteger(value, j, nSkip, 2);
394 i++;
395 j += nSkip;
396 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700397 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 nMin = ParseStringInteger(value, j, nSkip, 2);
399 i++;
400 j += nSkip;
401 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700402 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 nSec = ParseStringInteger(value, j, nSkip, 2);
404 i++;
405 j += nSkip;
406 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700407 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
409 i++;
410 j++;
411 break;
412 }
413 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
414 switch (c) {
415 case 'y':
416 nYear = ParseStringInteger(value, j, nSkip, 4);
417 i += 2;
418 j += nSkip;
419 break;
420 case 'm':
421 nMonth = ParseStringInteger(value, j, nSkip, 2);
422 i += 2;
423 j += nSkip;
424 break;
425 case 'd':
426 nDay = ParseStringInteger(value, j, nSkip, 2);
427 i += 2;
428 j += nSkip;
429 break;
430 case 'H':
431 nHour = ParseStringInteger(value, j, nSkip, 2);
432 i += 2;
433 j += nSkip;
434 break;
435 case 'h':
436 nHour = ParseStringInteger(value, j, nSkip, 2);
437 i += 2;
438 j += nSkip;
439 break;
440 case 'M':
441 nMin = ParseStringInteger(value, j, nSkip, 2);
442 i += 2;
443 j += nSkip;
444 break;
445 case 's':
446 nSec = ParseStringInteger(value, j, nSkip, 2);
447 i += 2;
448 j += nSkip;
449 break;
450 case 't':
451 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
452 value.GetAt(j + 1) == 'm');
453 i += 2;
454 j += 2;
455 break;
456 }
457 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
458 switch (c) {
459 case 'm': {
460 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
461 FX_BOOL bFind = FALSE;
462 for (int m = 0; m < 12; m++) {
463 if (sMonth.CompareNoCase(months[m]) == 0) {
464 nMonth = m + 1;
465 i += 3;
466 j += nSkip;
467 bFind = TRUE;
468 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700469 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 }
471
472 if (!bFind) {
473 nMonth = ParseStringInteger(value, j, nSkip, 3);
474 i += 3;
475 j += nSkip;
476 }
477 } break;
478 case 'y':
479 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700480 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 i += 3;
482 j += 3;
483 break;
484 }
485 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
486 switch (c) {
487 case 'y':
488 nYear = ParseStringInteger(value, j, nSkip, 4);
489 j += nSkip;
490 i += 4;
491 break;
492 case 'm': {
493 FX_BOOL bFind = FALSE;
494
495 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
496 sMonth.MakeLower();
497
498 for (int m = 0; m < 12; m++) {
499 CFX_WideString sFullMonths = fullmonths[m];
500 sFullMonths.MakeLower();
501
502 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
503 nMonth = m + 1;
504 i += 4;
505 j += nSkip;
506 bFind = TRUE;
507 break;
508 }
509 }
510
511 if (!bFind) {
512 nMonth = ParseStringInteger(value, j, nSkip, 4);
513 i += 4;
514 j += nSkip;
515 }
516 } break;
517 default:
518 i += 4;
519 j += 4;
520 break;
521 }
522 } else {
523 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800524 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525 bExit = TRUE;
526 }
527 i++;
528 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700529 }
Tom Sepez85386422014-07-23 10:28:37 -0700530
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700531 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800532 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533 bExit = TRUE;
534 }
535 }
536
537 break;
538 default:
539 if (value.GetLength() <= j) {
540 bExit = TRUE;
541 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800542 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 bExit = TRUE;
544 }
545
546 i++;
547 j++;
548 break;
549 }
550 }
551
552 if (bPm)
553 nHour += 12;
554
555 if (nYear >= 0 && nYear <= nYearSub)
556 nYear += 2000;
557
558 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800559 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560
561 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800562 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563
564 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800565 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566
567 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800568 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569
570 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800571 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572
573 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800574 if (bBadFormat) {
575 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576 } else {
577 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
578 JS_MakeTime(nHour, nMin, nSec, 0));
tsepez018935c2016-04-15 13:15:12 -0700579 if (JS_PortIsNan(dRet))
580 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 }
582
tsepez018935c2016-04-15 13:15:12 -0700583 if (JS_PortIsNan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800584 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585
Lei Zhang9559b7a2015-12-21 11:12:20 -0800586 if (bWrongFormat)
587 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700588
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700589 return dRet;
590}
591
592CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
593 const CFX_WideString& format) {
594 CFX_WideString sRet = L"", sPart = L"";
595
596 int nYear = JS_GetYearFromTime(dDate);
597 int nMonth = JS_GetMonthFromTime(dDate) + 1;
598 int nDay = JS_GetDayFromTime(dDate);
599 int nHour = JS_GetHourFromTime(dDate);
600 int nMin = JS_GetMinFromTime(dDate);
601 int nSec = JS_GetSecFromTime(dDate);
602
603 int i = 0;
604 while (i < format.GetLength()) {
605 FX_WCHAR c = format.GetAt(i);
606 int remaining = format.GetLength() - i - 1;
607 sPart = L"";
608 switch (c) {
609 case 'y':
610 case 'm':
611 case 'd':
612 case 'H':
613 case 'h':
614 case 'M':
615 case 's':
616 case 't':
617 if (remaining == 0 || format.GetAt(i + 1) != c) {
618 switch (c) {
619 case 'y':
620 sPart += c;
621 break;
622 case 'm':
623 sPart.Format(L"%d", nMonth);
624 break;
625 case 'd':
626 sPart.Format(L"%d", nDay);
627 break;
628 case 'H':
629 sPart.Format(L"%d", nHour);
630 break;
631 case 'h':
632 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
633 break;
634 case 'M':
635 sPart.Format(L"%d", nMin);
636 break;
637 case 's':
638 sPart.Format(L"%d", nSec);
639 break;
640 case 't':
641 sPart += nHour > 12 ? 'p' : 'a';
642 break;
643 }
644 i++;
645 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
646 switch (c) {
647 case 'y':
648 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
649 break;
650 case 'm':
651 sPart.Format(L"%02d", nMonth);
652 break;
653 case 'd':
654 sPart.Format(L"%02d", nDay);
655 break;
656 case 'H':
657 sPart.Format(L"%02d", nHour);
658 break;
659 case 'h':
660 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
661 break;
662 case 'M':
663 sPart.Format(L"%02d", nMin);
664 break;
665 case 's':
666 sPart.Format(L"%02d", nSec);
667 break;
668 case 't':
669 sPart = nHour > 12 ? L"pm" : L"am";
670 break;
671 }
672 i += 2;
673 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
674 switch (c) {
675 case 'm':
676 i += 3;
677 if (nMonth > 0 && nMonth <= 12)
678 sPart += months[nMonth - 1];
679 break;
680 default:
681 i += 3;
682 sPart += c;
683 sPart += c;
684 sPart += c;
685 break;
686 }
687 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
688 switch (c) {
689 case 'y':
690 sPart.Format(L"%04d", nYear);
691 i += 4;
692 break;
693 case 'm':
694 i += 4;
695 if (nMonth > 0 && nMonth <= 12)
696 sPart += fullmonths[nMonth - 1];
697 break;
698 default:
699 i += 4;
700 sPart += c;
701 sPart += c;
702 sPart += c;
703 sPart += c;
704 break;
705 }
706 } else {
707 i++;
708 sPart += c;
709 }
710 break;
711 default:
712 i++;
713 sPart += c;
714 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700715 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700716
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717 sRet += sPart;
718 }
719
720 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700721}
722
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
724// bCurrencyPrepend)
Tom Sepezba038bc2015-10-08 12:03:00 -0700725FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800726 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727 CJS_Value& vRet,
728 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700729#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731 if (params.size() != 6) {
732 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
733 return FALSE;
734 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700735
736 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
737 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700738 if (!pEvent->m_pValue)
739 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700740
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741 CFX_WideString& Value = pEvent->Value();
742 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743 if (strValue.IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700744 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745
746 int iDec = params[0].ToInt();
747 int iSepStyle = params[1].ToInt();
748 int iNegStyle = params[2].ToInt();
749 // params[3] is iCurrStyle, it's not used.
thestigcf03f8e2016-05-09 12:36:18 -0700750 CFX_WideString wstrCurrency = params[4].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751 FX_BOOL bCurrencyPrepend = params[5].ToBool();
752
753 if (iDec < 0)
754 iDec = -iDec;
755
756 if (iSepStyle < 0 || iSepStyle > 3)
757 iSepStyle = 0;
758
759 if (iNegStyle < 0 || iNegStyle > 3)
760 iNegStyle = 0;
761
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762 // for processing decimal places
763 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700764 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700766 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767
768 int iDec2;
769 int iNegative = 0;
770
771 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
772 if (strValue.IsEmpty()) {
773 dValue = 0;
774 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
775 if (strValue.IsEmpty()) {
776 strValue = "0";
777 iDec2 = 1;
778 }
779 }
780
781 if (iDec2 < 0) {
782 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
783 strValue = "0" + strValue;
784 }
785 iDec2 = 0;
786 }
787 int iMax = strValue.GetLength();
788 if (iDec2 > iMax) {
789 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
790 strValue += "0";
791 }
792 iMax = iDec2 + 1;
793 }
dsinclair64376be2016-03-31 20:03:24 -0700794
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795 // for processing seperator style
796 if (iDec2 < iMax) {
797 if (iSepStyle == 0 || iSepStyle == 1) {
798 strValue.Insert(iDec2, '.');
799 iMax++;
800 } else if (iSepStyle == 2 || iSepStyle == 3) {
801 strValue.Insert(iDec2, ',');
802 iMax++;
803 }
804
805 if (iDec2 == 0)
806 strValue.Insert(iDec2, '0');
807 }
808 if (iSepStyle == 0 || iSepStyle == 2) {
809 char cSeperator;
810 if (iSepStyle == 0)
811 cSeperator = ',';
812 else
813 cSeperator = '.';
814
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700815 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 strValue.Insert(iDecPositive, cSeperator);
817 iMax++;
818 }
819 }
820
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821 // for processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700822 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823
824 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700825 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826 else
thestigcf03f8e2016-05-09 12:36:18 -0700827 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700828
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829 // for processing negative style
830 if (iNegative) {
831 if (iNegStyle == 0) {
thestigcf03f8e2016-05-09 12:36:18 -0700832 Value = L"-" + Value;
833 } else if (iNegStyle == 2 || iNegStyle == 3) {
834 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835 }
836 if (iNegStyle == 1 || iNegStyle == 3) {
837 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700838 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700839 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840 vColElm = L"RGB";
tsepeze5aff742016-08-08 09:49:42 -0700841 arColor.SetElement(pRuntime->GetIsolate(), 0, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842 vColElm = 1;
tsepeze5aff742016-08-08 09:49:42 -0700843 arColor.SetElement(pRuntime->GetIsolate(), 1, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700844 vColElm = 0;
tsepeze5aff742016-08-08 09:49:42 -0700845 arColor.SetElement(pRuntime->GetIsolate(), 2, vColElm);
846 arColor.SetElement(pRuntime->GetIsolate(), 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700847
Tom Sepez67fd5df2015-10-08 12:24:19 -0700848 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849 vProp.StartGetting();
850 vProp << arColor;
851 vProp.StartSetting();
852 fTarget->textColor(cc, vProp, sError); // red
853 }
854 }
855 } else {
856 if (iNegStyle == 1 || iNegStyle == 3) {
857 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700858 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700859 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860 vColElm = L"RGB";
tsepeze5aff742016-08-08 09:49:42 -0700861 arColor.SetElement(pRuntime->GetIsolate(), 0, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862 vColElm = 0;
tsepeze5aff742016-08-08 09:49:42 -0700863 arColor.SetElement(pRuntime->GetIsolate(), 1, vColElm);
864 arColor.SetElement(pRuntime->GetIsolate(), 2, vColElm);
865 arColor.SetElement(pRuntime->GetIsolate(), 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866
Tom Sepez67fd5df2015-10-08 12:24:19 -0700867 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700868 vProp.StartGetting();
869 fTarget->textColor(cc, vProp, sError);
870
tsepeze5aff742016-08-08 09:49:42 -0700871 CJS_Array aProp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872 vProp.ConvertToArray(aProp);
873
874 CPWL_Color crProp;
875 CPWL_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700876 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
877 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878
879 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700880 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 vProp2.StartGetting();
882 vProp2 << arColor;
883 vProp2.StartSetting();
884 fTarget->textColor(cc, vProp2, sError);
885 }
886 }
887 }
888 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889#endif
890 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700891}
892
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
894// bCurrencyPrepend)
Lei Zhang945fdb72015-11-11 10:18:16 -0800895FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(
896 IJS_Context* cc,
897 const std::vector<CJS_Value>& params,
898 CJS_Value& vRet,
899 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700901 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700902
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 if (params.size() < 2)
904 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700905
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906 if (!pEvent->m_pValue)
907 return FALSE;
thestigcf03f8e2016-05-09 12:36:18 -0700908
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700909 CFX_WideString& val = pEvent->Value();
thestigcf03f8e2016-05-09 12:36:18 -0700910 CFX_WideString& wstrChange = pEvent->Change();
911 CFX_WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700912
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700913 if (pEvent->WillCommit()) {
ochanga0a3bc32016-05-12 15:22:48 -0700914 CFX_WideString swTemp = StrTrim(wstrValue);
915 if (swTemp.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 return TRUE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700917
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 swTemp.Replace(L",", L".");
919 if (!IsNumber(swTemp.c_str())) {
920 pEvent->Rc() = FALSE;
921 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700922 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700923 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924 return TRUE; // it happens after the last keystroke and before validating,
925 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700926
thestigcf03f8e2016-05-09 12:36:18 -0700927 CFX_WideString wstrSelected;
928 if (pEvent->SelStart() != -1) {
929 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
930 pEvent->SelEnd() - pEvent->SelStart());
931 }
932
933 bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 if (bHasSign) {
935 // can't insert "change" in front to sign postion.
936 if (pEvent->SelStart() == 0) {
937 FX_BOOL& bRc = pEvent->Rc();
938 bRc = FALSE;
939 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700940 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700942
thestigcf03f8e2016-05-09 12:36:18 -0700943 int iSepStyle = params[1].ToInt();
944 if (iSepStyle < 0 || iSepStyle > 3)
945 iSepStyle = 0;
946 const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700947
thestigcf03f8e2016-05-09 12:36:18 -0700948 bool bHasSep = wstrValue.Find(cSep) != -1;
949 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
950 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 if (bHasSep) {
952 FX_BOOL& bRc = pEvent->Rc();
953 bRc = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700954 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955 }
956 bHasSep = TRUE;
957 continue;
958 }
thestigcf03f8e2016-05-09 12:36:18 -0700959 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 if (bHasSign) {
961 FX_BOOL& bRc = pEvent->Rc();
962 bRc = FALSE;
963 return TRUE;
964 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800965 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700966 if (i != 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967 FX_BOOL& bRc = pEvent->Rc();
968 bRc = FALSE;
969 return TRUE;
970 }
971 if (pEvent->SelStart() != 0) {
972 FX_BOOL& bRc = pEvent->Rc();
973 bRc = FALSE;
974 return TRUE;
975 }
976 bHasSign = TRUE;
977 continue;
978 }
979
thestigcf03f8e2016-05-09 12:36:18 -0700980 if (!FXSYS_iswdigit(wstrChange[i])) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981 FX_BOOL& bRc = pEvent->Rc();
982 bRc = FALSE;
983 return TRUE;
984 }
985 }
986
thestigcf03f8e2016-05-09 12:36:18 -0700987 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
988 CFX_WideString wpostfix;
989 if (pEvent->SelEnd() < wstrValue.GetLength())
990 wpostfix = wstrValue.Mid(pEvent->SelEnd());
991 val = wprefix + wstrChange + wpostfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992 return TRUE;
993}
994
995// function AFPercent_Format(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -0800996FX_BOOL CJS_PublicMethods::AFPercent_Format(
997 IJS_Context* cc,
998 const std::vector<CJS_Value>& params,
999 CJS_Value& vRet,
1000 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001#if _FX_OS_ != _FX_ANDROID_
1002 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004
1005 if (params.size() != 2) {
1006 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1007 return FALSE;
1008 }
1009 if (!pEvent->m_pValue)
1010 return FALSE;
1011
1012 CFX_WideString& Value = pEvent->Value();
1013 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1014 if (strValue.IsEmpty())
1015 return TRUE;
1016
1017 int iDec = params[0].ToInt();
1018 if (iDec < 0)
1019 iDec = -iDec;
1020
1021 int iSepStyle = params[1].ToInt();
1022 if (iSepStyle < 0 || iSepStyle > 3)
1023 iSepStyle = 0;
1024
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001026 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027 dValue *= 100;
1028 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001029 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030
1031 int iDec2;
1032 int iNegative = 0;
1033 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1034 if (strValue.IsEmpty()) {
1035 dValue = 0;
1036 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1037 }
1038
1039 if (iDec2 < 0) {
1040 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1041 strValue = "0" + strValue;
1042 }
1043 iDec2 = 0;
1044 }
1045 int iMax = strValue.GetLength();
1046 if (iDec2 > iMax) {
1047 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1048 strValue += "0";
1049 }
1050 iMax = iDec2 + 1;
1051 }
dsinclair64376be2016-03-31 20:03:24 -07001052
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 // for processing seperator style
1054 if (iDec2 < iMax) {
1055 if (iSepStyle == 0 || iSepStyle == 1) {
1056 strValue.Insert(iDec2, '.');
1057 iMax++;
1058 } else if (iSepStyle == 2 || iSepStyle == 3) {
1059 strValue.Insert(iDec2, ',');
1060 iMax++;
1061 }
1062
1063 if (iDec2 == 0)
1064 strValue.Insert(iDec2, '0');
1065 }
1066 if (iSepStyle == 0 || iSepStyle == 2) {
1067 char cSeperator;
1068 if (iSepStyle == 0)
1069 cSeperator = ',';
1070 else
1071 cSeperator = '.';
1072
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001073 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074 strValue.Insert(iDecPositive, cSeperator);
1075 iMax++;
1076 }
1077 }
dsinclair64376be2016-03-31 20:03:24 -07001078
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079 // negative mark
1080 if (iNegative)
1081 strValue = "-" + strValue;
1082 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001083 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084#endif
1085 return TRUE;
1086}
1087// AFPercent_Keystroke(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001088FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(
1089 IJS_Context* cc,
1090 const std::vector<CJS_Value>& params,
1091 CJS_Value& vRet,
1092 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 return AFNumber_Keystroke(cc, params, vRet, sError);
1094}
1095
1096// function AFDate_FormatEx(cFormat)
Tom Sepezba038bc2015-10-08 12:03:00 -07001097FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001098 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099 CJS_Value& vRet,
1100 CFX_WideString& sError) {
1101 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001103
1104 if (params.size() != 1) {
1105 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1106 return FALSE;
1107 }
1108 if (!pEvent->m_pValue)
1109 return FALSE;
1110
1111 CFX_WideString& val = pEvent->Value();
1112 CFX_WideString strValue = val;
1113 if (strValue.IsEmpty())
1114 return TRUE;
1115
1116 CFX_WideString sFormat = params[0].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117 double dDate = 0.0f;
1118
1119 if (strValue.Find(L"GMT") != -1) {
1120 // for GMT format time
1121 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1122 dDate = MakeInterDate(strValue);
1123 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001124 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001125 }
1126
1127 if (JS_PortIsNan(dDate)) {
1128 CFX_WideString swMsg;
1129 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1130 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001131 AlertIfPossible(pContext, swMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001132 return FALSE;
1133 }
1134
1135 val = MakeFormatDate(dDate, sFormat);
1136 return TRUE;
1137}
1138
tsepez745611b2016-04-12 16:46:34 -07001139double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001140 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001142 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 FX_WCHAR c = strValue.GetAt(i);
1144 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001145 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146 sTemp = L"";
1147 continue;
1148 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 sTemp += c;
1150 }
Tom Sepezab277682016-02-17 10:07:21 -08001151 wsArray.push_back(sTemp);
1152 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 return 0;
1154
Tom Sepez4246b002016-01-20 11:48:29 -08001155 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 sTemp = wsArray[1];
1157 if (sTemp.Compare(L"Jan") == 0)
1158 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001159 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001161 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001163 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001165 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001167 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001169 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001171 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001173 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001175 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001176 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001177 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001179 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180 nMonth = 12;
1181
tsepez4c3debb2016-04-08 12:20:38 -07001182 int nDay = FX_atof(wsArray[2].AsStringC());
1183 int nHour = FX_atof(wsArray[3].AsStringC());
1184 int nMin = FX_atof(wsArray[4].AsStringC());
1185 int nSec = FX_atof(wsArray[5].AsStringC());
1186 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001187 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1188 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001189 if (JS_PortIsNan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001190 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001191
1192 return dRet;
1193}
1194
1195// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001196FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(
1197 IJS_Context* cc,
1198 const std::vector<CJS_Value>& params,
1199 CJS_Value& vRet,
1200 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001203
1204 if (params.size() != 1) {
1205 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1206 return FALSE;
1207 }
1208
1209 if (pEvent->WillCommit()) {
1210 if (!pEvent->m_pValue)
1211 return FALSE;
1212 CFX_WideString strValue = pEvent->Value();
1213 if (strValue.IsEmpty())
1214 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001215
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001216 CFX_WideString sFormat = params[0].ToCFXWideString();
Lei Zhang9559b7a2015-12-21 11:12:20 -08001217 bool bWrongFormat = FALSE;
1218 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 if (bWrongFormat || JS_PortIsNan(dRet)) {
1220 CFX_WideString swMsg;
1221 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1222 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001223 AlertIfPossible(pContext, swMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001224 pEvent->Rc() = FALSE;
1225 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001226 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227 }
1228 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001229}
1230
Tom Sepezba038bc2015-10-08 12:03:00 -07001231FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001232 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233 CJS_Value& vRet,
1234 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001235 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001236 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1238 return FALSE;
1239 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001240
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001241 int iIndex = params[0].ToInt();
1242 const FX_WCHAR* cFormats[] = {L"m/d",
1243 L"m/d/yy",
1244 L"mm/dd/yy",
1245 L"mm/yy",
1246 L"d-mmm",
1247 L"d-mmm-yy",
1248 L"dd-mmm-yy",
1249 L"yy-mm-dd",
1250 L"mmm-yy",
1251 L"mmmm-yy",
1252 L"mmm d, yyyy",
1253 L"mmmm d, yyyy",
1254 L"m/d/yy h:MM tt",
1255 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001256
Lei Zhanga0f67242015-08-17 15:39:30 -07001257 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1258 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001259
Lei Zhang945fdb72015-11-11 10:18:16 -08001260 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001261 newParams.push_back(
1262 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263 return AFDate_FormatEx(cc, newParams, vRet, sError);
1264}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001265
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001267FX_BOOL CJS_PublicMethods::AFDate_Keystroke(
1268 IJS_Context* cc,
1269 const std::vector<CJS_Value>& params,
1270 CJS_Value& vRet,
1271 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001272 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001273 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001274 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1275 return FALSE;
1276 }
1277
1278 int iIndex = params[0].ToInt();
1279 const FX_WCHAR* cFormats[] = {L"m/d",
1280 L"m/d/yy",
1281 L"mm/dd/yy",
1282 L"mm/yy",
1283 L"d-mmm",
1284 L"d-mmm-yy",
1285 L"dd-mmm-yy",
1286 L"yy-mm-dd",
1287 L"mmm-yy",
1288 L"mmmm-yy",
1289 L"mmm d, yyyy",
1290 L"mmmm d, yyyy",
1291 L"m/d/yy h:MM tt",
1292 L"m/d/yy HH:MM"};
1293
Lei Zhanga0f67242015-08-17 15:39:30 -07001294 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1295 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296
Lei Zhang945fdb72015-11-11 10:18:16 -08001297 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001298 newParams.push_back(
1299 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1301}
1302
1303// function AFTime_Format(ptf)
Tom Sepezba038bc2015-10-08 12:03:00 -07001304FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001305 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001306 CJS_Value& vRet,
1307 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001308 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001310 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1311 return FALSE;
1312 }
1313
1314 int iIndex = params[0].ToInt();
1315 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1316 L"h:MM:ss tt"};
1317
Lei Zhanga0f67242015-08-17 15:39:30 -07001318 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1319 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320
Lei Zhang945fdb72015-11-11 10:18:16 -08001321 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001322 newParams.push_back(
1323 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324 return AFDate_FormatEx(cc, newParams, vRet, sError);
1325}
1326
Lei Zhang945fdb72015-11-11 10:18:16 -08001327FX_BOOL CJS_PublicMethods::AFTime_Keystroke(
1328 IJS_Context* cc,
1329 const std::vector<CJS_Value>& params,
1330 CJS_Value& vRet,
1331 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001332 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001333 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001334 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1335 return FALSE;
1336 }
1337
1338 int iIndex = params[0].ToInt();
1339 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1340 L"h:MM:ss tt"};
1341
Lei Zhanga0f67242015-08-17 15:39:30 -07001342 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1343 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001344
Lei Zhang945fdb72015-11-11 10:18:16 -08001345 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001346 newParams.push_back(
1347 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1349}
1350
Tom Sepezba038bc2015-10-08 12:03:00 -07001351FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001352 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001353 CJS_Value& vRet,
1354 CFX_WideString& sError) {
1355 return AFDate_FormatEx(cc, params, vRet, sError);
1356}
1357
Lei Zhang945fdb72015-11-11 10:18:16 -08001358FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(
1359 IJS_Context* cc,
1360 const std::vector<CJS_Value>& params,
1361 CJS_Value& vRet,
1362 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1364}
1365
1366// function AFSpecial_Format(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001367FX_BOOL CJS_PublicMethods::AFSpecial_Format(
1368 IJS_Context* cc,
1369 const std::vector<CJS_Value>& params,
1370 CJS_Value& vRet,
1371 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373
1374 if (params.size() != 1) {
1375 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1376 return FALSE;
1377 }
1378
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001379 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 if (!pEvent->m_pValue)
1381 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382
tsepez4f1f41f2016-03-28 14:13:16 -07001383 CFX_WideString wsSource = pEvent->Value();
1384 CFX_WideString wsFormat;
1385 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001386 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001387 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001389 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001390 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001391 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001392 case 2:
1393 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1394 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001395 else
tsepez4f1f41f2016-03-28 14:13:16 -07001396 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001398 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001399 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001400 break;
1401 }
1402
tsepez4f1f41f2016-03-28 14:13:16 -07001403 pEvent->Value() = util::printx(wsFormat, wsSource);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001404 return TRUE;
1405}
1406
1407// function AFSpecial_KeystrokeEx(mask)
Lei Zhang945fdb72015-11-11 10:18:16 -08001408FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(
1409 IJS_Context* cc,
1410 const std::vector<CJS_Value>& params,
1411 CJS_Value& vRet,
1412 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001413 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001414 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1415
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001416 if (params.size() < 1) {
1417 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1418 return FALSE;
1419 }
1420
1421 if (!pEvent->m_pValue)
1422 return FALSE;
1423 CFX_WideString& valEvent = pEvent->Value();
1424
1425 CFX_WideString wstrMask = params[0].ToCFXWideString();
1426 if (wstrMask.IsEmpty())
1427 return TRUE;
1428
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001429 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001430 if (valEvent.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431 return TRUE;
thestigcf03f8e2016-05-09 12:36:18 -07001432
1433 FX_STRSIZE iIndexMask = 0;
1434 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1435 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001436 break;
1437 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001438
thestigcf03f8e2016-05-09 12:36:18 -07001439 if (iIndexMask != wstrMask.GetLength() ||
1440 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001441 AlertIfPossible(
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001442 pContext,
1443 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1444 pEvent->Rc() = FALSE;
1445 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001446 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 }
1448
1449 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001450 if (wideChange.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451 return TRUE;
1452
thestigcf03f8e2016-05-09 12:36:18 -07001453 CFX_WideString wChange = wideChange;
1454 FX_STRSIZE iIndexMask = pEvent->SelStart();
1455 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1456 pEvent->SelStart() - pEvent->SelEnd();
1457 if (combined_len > wstrMask.GetLength()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001458 AlertIfPossible(
1459 pContext,
1460 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001461 pEvent->Rc() = FALSE;
1462 return TRUE;
1463 }
1464
thestigcf03f8e2016-05-09 12:36:18 -07001465 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001466 AlertIfPossible(
1467 pContext,
1468 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469 pEvent->Rc() = FALSE;
1470 return TRUE;
1471 }
1472
thestigcf03f8e2016-05-09 12:36:18 -07001473 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1474 if (iIndexMask >= wstrMask.GetLength()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001475 AlertIfPossible(
1476 pContext,
1477 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001478 pEvent->Rc() = FALSE;
1479 return TRUE;
1480 }
thestigcf03f8e2016-05-09 12:36:18 -07001481 FX_WCHAR wMask = wstrMask[iIndexMask];
1482 if (!isReservedMaskChar(wMask))
1483 wChange.SetAt(i, wMask);
1484
1485 if (!maskSatisfied(wChange[i], wMask)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001486 pEvent->Rc() = FALSE;
1487 return TRUE;
1488 }
1489 iIndexMask++;
1490 }
thestigcf03f8e2016-05-09 12:36:18 -07001491 wideChange = wChange;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001493}
1494
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495// function AFSpecial_Keystroke(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001496FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(
1497 IJS_Context* cc,
1498 const std::vector<CJS_Value>& params,
1499 CJS_Value& vRet,
1500 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502 if (params.size() != 1) {
1503 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1504 return FALSE;
1505 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001506
Tom Sepez67fd5df2015-10-08 12:24:19 -07001507 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001508 if (!pEvent->m_pValue)
1509 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001510
thestigcf03f8e2016-05-09 12:36:18 -07001511 const char* cFormat = "";
1512 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001513 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001514 cFormat = "99999";
1515 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001516 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517 cFormat = "999999999";
1518 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001519 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001520 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 cFormat = "9999999999";
1522 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001523 cFormat = "9999999";
1524 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001525 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001526 cFormat = "999999999";
1527 break;
1528 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001529
Lei Zhang945fdb72015-11-11 10:18:16 -08001530 std::vector<CJS_Value> params2;
thestigcf03f8e2016-05-09 12:36:18 -07001531 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001532 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001533}
1534
Tom Sepezba038bc2015-10-08 12:03:00 -07001535FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001536 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001537 CJS_Value& vRet,
1538 CFX_WideString& sError) {
1539 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001540 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001541
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542 if (params.size() != 1) {
1543 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1544 return FALSE;
1545 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001546
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001547 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001548 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001549 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001550
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001551 if (pEventHandler->WillCommit()) {
1552 vRet = swValue.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001553 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001554 }
1555
1556 CFX_WideString prefix, postfix;
1557
1558 if (pEventHandler->SelStart() >= 0)
1559 prefix = swValue.Mid(0, pEventHandler->SelStart());
1560 else
1561 prefix = L"";
1562
1563 if (pEventHandler->SelEnd() >= 0 &&
1564 pEventHandler->SelEnd() <= swValue.GetLength())
1565 postfix = swValue.Mid(pEventHandler->SelEnd(),
1566 swValue.GetLength() - pEventHandler->SelEnd());
1567 else
1568 postfix = L"";
1569
1570 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1571
1572 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001573}
1574
Tom Sepezba038bc2015-10-08 12:03:00 -07001575FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001576 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001577 CJS_Value& vRet,
1578 CFX_WideString& sError) {
1579 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001580 ASSERT(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001581
1582 if (params.size() != 2) {
1583 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1584 return FALSE;
1585 }
1586
1587 CFX_WideString sValue = params[0].ToCFXWideString();
1588 CFX_WideString sFormat = params[1].ToCFXWideString();
1589
Lei Zhang9559b7a2015-12-21 11:12:20 -08001590 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001591
1592 if (JS_PortIsNan(dDate)) {
1593 CFX_WideString swMsg;
1594 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1595 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001596 AlertIfPossible((CJS_Context*)cc, swMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001597 return FALSE;
1598 }
1599
1600 vRet = dDate;
1601 return TRUE;
1602}
1603
Tom Sepezba038bc2015-10-08 12:03:00 -07001604FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001605 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001606 CJS_Value& vRet,
1607 CFX_WideString& sError) {
1608 if (params.size() != 3) {
1609 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001610 ASSERT(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001611
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001612 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1613 return FALSE;
1614 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001615
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001616 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1617 params[1].ToDouble(), params[2].ToDouble());
1618 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001619}
1620
Tom Sepezba038bc2015-10-08 12:03:00 -07001621FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001622 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001623 CJS_Value& vRet,
1624 CFX_WideString& sError) {
Tom Sepez4246b002016-01-20 11:48:29 -08001625 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001626 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1628 return FALSE;
1629 }
Tom Sepez4246b002016-01-20 11:48:29 -08001630 CFX_WideString ws = params[0].ToCFXWideString();
1631 ws.Replace(L",", L".");
tsepezbd9748d2016-04-13 21:40:19 -07001632 vRet = ws.c_str();
Tom Sepez4246b002016-01-20 11:48:29 -08001633 vRet.MaybeCoerceToNumber();
1634 if (vRet.GetType() != CJS_Value::VT_number)
1635 vRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636 return TRUE;
1637}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001638
Lei Zhang945fdb72015-11-11 10:18:16 -08001639FX_BOOL CJS_PublicMethods::AFSimple_Calculate(
1640 IJS_Context* cc,
1641 const std::vector<CJS_Value>& params,
1642 CJS_Value& vRet,
1643 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001644 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 if (params.size() != 2) {
1646 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1647 return FALSE;
1648 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001649
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001650 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001651 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001652 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1653 return FALSE;
1654 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001655
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001656 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001658 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001659
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001660 CFX_WideString sFunction = params[0].ToCFXWideString();
Tom Sepez67fd5df2015-10-08 12:24:19 -07001661 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001662
Tom Sepez67fd5df2015-10-08 12:24:19 -07001663 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1664 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001665 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001666
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001667 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001668 CJS_Value jsValue(pRuntime);
tsepeze5aff742016-08-08 09:49:42 -07001669 FieldNameArray.GetElement(pRuntime->GetIsolate(), i, jsValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001670 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001671
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001672 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1673 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1674 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001675 switch (pFormField->GetFieldType()) {
1676 case FIELDTYPE_TEXTFIELD:
1677 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001678 CFX_WideString trimmed = pFormField->GetValue();
1679 trimmed.TrimRight();
1680 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001681 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001682 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001683 case FIELDTYPE_PUSHBUTTON: {
1684 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001685 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001686 case FIELDTYPE_CHECKBOX:
1687 case FIELDTYPE_RADIOBUTTON: {
1688 dTemp = 0.0;
1689 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1690 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1691 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001692 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1693 trimmed.TrimRight();
1694 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001695 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001696 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001697 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001698 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001699 }
Tom Sepez4246b002016-01-20 11:48:29 -08001700 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001701 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001702 if (pFormField->CountSelectedItems() <= 1) {
1703 CFX_WideString trimmed = pFormField->GetValue();
1704 trimmed.TrimRight();
1705 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001706 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001707 }
1708 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001709 default:
1710 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001711 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001712
1713 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1714 wcscmp(sFunction.c_str(), L"MAX") == 0))
1715 dValue = dTemp;
1716
1717 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1718
1719 nFieldsCount++;
1720 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001721 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001722 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001723
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001724 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1725 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001726
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001727 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1728 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001729 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001730 if (pContext->GetEventHandler()->m_pValue)
1731 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001732
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001733 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001734}
1735
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001736/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001737** within the specified range. */
1738
Lei Zhang945fdb72015-11-11 10:18:16 -08001739FX_BOOL CJS_PublicMethods::AFRange_Validate(
1740 IJS_Context* cc,
1741 const std::vector<CJS_Value>& params,
1742 CJS_Value& vRet,
1743 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001744 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001745 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001746
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001747 if (params.size() != 4) {
1748 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1749 return FALSE;
1750 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001751
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001752 if (!pEvent->m_pValue)
1753 return FALSE;
1754 if (pEvent->Value().IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001755 return TRUE;
tsepezb4c9f3f2016-04-13 15:41:21 -07001756 double dEentValue =
1757 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 FX_BOOL bGreaterThan = params[0].ToBool();
1759 double dGreaterThan = params[1].ToDouble();
1760 FX_BOOL bLessThan = params[2].ToBool();
1761 double dLessThan = params[3].ToDouble();
1762 CFX_WideString swMsg;
1763
1764 if (bGreaterThan && bLessThan) {
1765 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
1766 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
1767 params[1].ToCFXWideString().c_str(),
1768 params[3].ToCFXWideString().c_str());
1769 } else if (bGreaterThan) {
1770 if (dEentValue < dGreaterThan)
1771 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
1772 params[1].ToCFXWideString().c_str());
1773 } else if (bLessThan) {
1774 if (dEentValue > dLessThan)
1775 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
1776 params[3].ToCFXWideString().c_str());
1777 }
1778
1779 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001780 AlertIfPossible(pContext, swMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 pEvent->Rc() = FALSE;
1782 }
1783 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001784}
1785
Tom Sepezba038bc2015-10-08 12:03:00 -07001786FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001787 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001788 CJS_Value& vRet,
1789 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001790 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001791 if (params.size() != 1) {
1792 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1793 return FALSE;
1794 }
1795
Tom Sepez67fd5df2015-10-08 12:24:19 -07001796 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepeze5aff742016-08-08 09:49:42 -07001797 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001798
1799 CFX_WideString str = params[0].ToCFXWideString();
1800 CFX_WideString sPart;
1801
1802 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1803 str = L"0" + str;
1804
1805 int nIndex = 0;
1806 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1807 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001808 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001809 sPart += wc;
1810 } else {
1811 if (sPart.GetLength() > 0) {
tsepeze5aff742016-08-08 09:49:42 -07001812 nums.SetElement(pRuntime->GetIsolate(), nIndex,
1813 CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001814 sPart = L"";
1815 nIndex++;
1816 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001817 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001818 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001819
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001820 if (sPart.GetLength() > 0) {
tsepeze5aff742016-08-08 09:49:42 -07001821 nums.SetElement(pRuntime->GetIsolate(), nIndex,
1822 CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001823 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001824
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001825 if (nums.GetLength() > 0)
tsepeze5aff742016-08-08 09:49:42 -07001826 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001827 else
1828 vRet.SetNull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001829
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001830 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001831}