blob: 6af59bd278cf9a15a0f587b7d6e7a5aedb49077d [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/PublicMethods.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080011
dsinclaircac704d2016-07-28 12:59:09 -070012#include "core/fpdfdoc/include/cpdf_interform.h"
Dan Sinclaira8a28e02016-03-23 15:41:39 -040013#include "core/fxcrt/include/fx_ext.h"
dsinclair64376be2016-03-31 20:03:24 -070014#include "fpdfsdk/include/fsdk_mgr.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040015#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040016#include "fpdfsdk/javascript/JS_Define.h"
17#include "fpdfsdk/javascript/JS_EventHandler.h"
18#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040019#include "fpdfsdk/javascript/JS_Value.h"
dsinclair64376be2016-03-31 20:03:24 -070020#include "fpdfsdk/javascript/cjs_context.h"
21#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040022#include "fpdfsdk/javascript/color.h"
23#include "fpdfsdk/javascript/resource.h"
24#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
28BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format)
30JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke)
31JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format)
32JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke)
33JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx)
34JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx)
35JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format)
36JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke)
37JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx)
38JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx)
39JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format)
40JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke)
41JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format)
42JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke)
43JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx)
44JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple)
45JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
46JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
47JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
48JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
49JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
50JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051END_JS_STATIC_GLOBAL_FUN()
52
53IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
54
tsepez745611b2016-04-12 16:46:34 -070055namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
tsepez745611b2016-04-12 16:46:34 -070057const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
58 L"May", L"Jun", L"Jul", L"Aug",
59 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060
tsepez745611b2016-04-12 16:46:34 -070061const FX_WCHAR* const fullmonths[] = {L"January", L"February", L"March",
62 L"April", L"May", L"June",
63 L"July", L"August", L"September",
64 L"October", L"November", L"December"};
65
66CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
67 CFX_ByteString result(pStr);
68 result.TrimLeft(' ');
69 result.TrimRight(' ');
70 return result;
71}
72
73CFX_WideString StrTrim(const CFX_WideString& pStr) {
74 CFX_WideString result(pStr);
75 result.TrimLeft(' ');
76 result.TrimRight(' ');
77 return result;
78}
79
80} // namespace
81
82bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -050083 CFX_WideString sTrim = StrTrim(str);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 const FX_WCHAR* pTrim = sTrim.c_str();
85 const FX_WCHAR* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -070086 bool bDot = false;
87 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -070090 while ((c = *p) != L'\0') {
91 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -070093 return false;
94 bDot = true;
95 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -070097 return false;
98 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700100 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 p++;
103 c = *p;
Wei Li614d20a2016-03-15 13:55:12 -0700104 if (c == L'+' || c == L'-') {
105 bKXJS = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 } else {
Wei Li614d20a2016-03-15 13:55:12 -0700107 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800109 } else if (!FXSYS_iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700110 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 }
112 p++;
113 }
114
Wei Li614d20a2016-03-15 13:55:12 -0700115 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116}
117
Wei Li614d20a2016-03-15 13:55:12 -0700118bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 switch (c_Mask) {
120 case L'9':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800121 return FXSYS_iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800123 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800125 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700127 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 default:
129 return (c_Change == c_Mask);
130 }
131}
132
Wei Li614d20a2016-03-15 13:55:12 -0700133bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
135}
136
137double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
138 double dValue1,
139 double dValue2) {
140 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
141 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
142 return dValue1 + dValue2;
143 }
144 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
145 return dValue1 * dValue2;
146 }
147 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800148 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 }
150 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800151 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 }
153 return dValue1;
154}
155
Tom Sepez67fd5df2015-10-08 12:24:19 -0700156CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 CJS_Value val) {
tsepeze5aff742016-08-08 09:49:42 -0700158 CJS_Array StrArray;
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(
tsepeze5aff742016-08-08 09:49:42 -0700174 pRuntime->GetIsolate(), nIndex,
175 CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 }
Lei Zhang997de612015-11-04 18:17:53 -0800178
179 char* pSub = new char[pTemp - p + 1];
180 strncpy(pSub, p, pTemp - p);
181 *(pSub + (pTemp - p)) = '\0';
182
tsepezb4c9f3f2016-04-13 15:41:21 -0700183 StrArray.SetElement(
tsepeze5aff742016-08-08 09:49:42 -0700184 pRuntime->GetIsolate(), nIndex,
185 CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800186 delete[] pSub;
187
188 nIndex++;
189 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 }
191 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500194int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 int nStart,
196 int& nSkip,
197 int nMaxStep) {
198 int nRet = 0;
199 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500200 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 if (i - nStart > 10)
202 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500204 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800205 if (!FXSYS_iswdigit(c))
206 break;
207
Dan Sinclair1c915372016-03-03 17:12:58 -0500208 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800209 nSkip = i - nStart + 1;
210 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 break;
212 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700215}
216
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500217CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
218 int nStart,
219 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 CFX_WideString swRet;
221 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500222 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
223 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800224 if (!FXSYS_iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800226
227 swRet += c;
228 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232}
233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800235 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 int nYear = JS_GetYearFromTime(dt);
239 int nMonth = JS_GetMonthFromTime(dt) + 1;
240 int nDay = JS_GetDayFromTime(dt);
241 int nHour = JS_GetHourFromTime(dt);
242 int nMin = JS_GetMinFromTime(dt);
243 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 int nSkip = 0;
248 int nLen = value.GetLength();
249 int nIndex = 0;
250 int i = 0;
251 while (i < nLen) {
252 if (nIndex > 2)
253 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 FX_WCHAR c = value.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800256 if (FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
258 i += nSkip;
259 } else {
260 i++;
261 }
262 }
263
264 if (nIndex == 2) {
265 // case2: month/day
266 // case3: day/month
267 if ((number[0] >= 1 && number[0] <= 12) &&
268 (number[1] >= 1 && number[1] <= 31)) {
269 nMonth = number[0];
270 nDay = number[1];
271 } else if ((number[0] >= 1 && number[0] <= 31) &&
272 (number[1] >= 1 && number[1] <= 12)) {
273 nDay = number[0];
274 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700275 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276
Lei Zhang9559b7a2015-12-21 11:12:20 -0800277 if (bWrongFormat)
278 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 } else if (nIndex == 3) {
280 // case1: year/month/day
281 // case2: month/day/year
282 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700283
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
285 (number[2] >= 1 && number[2] <= 31)) {
286 nYear = number[0];
287 nMonth = number[1];
288 nDay = number[2];
289 } else if ((number[0] >= 1 && number[0] <= 12) &&
290 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
291 nMonth = number[0];
292 nDay = number[1];
293 nYear = number[2];
294 } else if ((number[0] >= 1 && number[0] <= 31) &&
295 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
296 nDay = number[0];
297 nMonth = number[1];
298 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700299 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300
Lei Zhang9559b7a2015-12-21 11:12:20 -0800301 if (bWrongFormat)
302 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800304 if (bWrongFormat)
305 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306 return dt;
307 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 CFX_WideString swTemp;
310 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700311 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700312}
313
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
315 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800316 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 if (format.IsEmpty() || value.IsEmpty())
320 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 int nYear = JS_GetYearFromTime(dt);
323 int nMonth = JS_GetMonthFromTime(dt) + 1;
324 int nDay = JS_GetDayFromTime(dt);
325 int nHour = JS_GetHourFromTime(dt);
326 int nMin = JS_GetMinFromTime(dt);
327 int nSec = JS_GetSecFromTime(dt);
328
329 int nYearSub = 99; // nYear - 2000;
330
331 FX_BOOL bPm = FALSE;
332 FX_BOOL bExit = FALSE;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800333 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334
335 int i = 0;
336 int j = 0;
337
338 while (i < format.GetLength()) {
339 if (bExit)
340 break;
341
342 FX_WCHAR c = format.GetAt(i);
343 switch (c) {
344 case ':':
345 case '.':
346 case '-':
347 case '\\':
348 case '/':
349 i++;
350 j++;
351 break;
352
353 case 'y':
354 case 'm':
355 case 'd':
356 case 'H':
357 case 'h':
358 case 'M':
359 case 's':
360 case 't': {
361 int oldj = j;
362 int nSkip = 0;
363 int remaining = format.GetLength() - i - 1;
364
365 if (remaining == 0 || format.GetAt(i + 1) != c) {
366 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700367 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 i++;
369 j++;
370 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700371 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 nMonth = ParseStringInteger(value, j, nSkip, 2);
373 i++;
374 j += nSkip;
375 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700376 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 nDay = ParseStringInteger(value, j, nSkip, 2);
378 i++;
379 j += nSkip;
380 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700381 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 nHour = ParseStringInteger(value, j, nSkip, 2);
383 i++;
384 j += nSkip;
385 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700386 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 nHour = ParseStringInteger(value, j, nSkip, 2);
388 i++;
389 j += nSkip;
390 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700391 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 nMin = ParseStringInteger(value, j, nSkip, 2);
393 i++;
394 j += nSkip;
395 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700396 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 nSec = ParseStringInteger(value, j, nSkip, 2);
398 i++;
399 j += nSkip;
400 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700401 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
403 i++;
404 j++;
405 break;
406 }
407 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
408 switch (c) {
409 case 'y':
410 nYear = ParseStringInteger(value, j, nSkip, 4);
411 i += 2;
412 j += nSkip;
413 break;
414 case 'm':
415 nMonth = ParseStringInteger(value, j, nSkip, 2);
416 i += 2;
417 j += nSkip;
418 break;
419 case 'd':
420 nDay = ParseStringInteger(value, j, nSkip, 2);
421 i += 2;
422 j += nSkip;
423 break;
424 case 'H':
425 nHour = ParseStringInteger(value, j, nSkip, 2);
426 i += 2;
427 j += nSkip;
428 break;
429 case 'h':
430 nHour = ParseStringInteger(value, j, nSkip, 2);
431 i += 2;
432 j += nSkip;
433 break;
434 case 'M':
435 nMin = ParseStringInteger(value, j, nSkip, 2);
436 i += 2;
437 j += nSkip;
438 break;
439 case 's':
440 nSec = ParseStringInteger(value, j, nSkip, 2);
441 i += 2;
442 j += nSkip;
443 break;
444 case 't':
445 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
446 value.GetAt(j + 1) == 'm');
447 i += 2;
448 j += 2;
449 break;
450 }
451 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
452 switch (c) {
453 case 'm': {
454 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
455 FX_BOOL bFind = FALSE;
456 for (int m = 0; m < 12; m++) {
457 if (sMonth.CompareNoCase(months[m]) == 0) {
458 nMonth = m + 1;
459 i += 3;
460 j += nSkip;
461 bFind = TRUE;
462 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700463 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 }
465
466 if (!bFind) {
467 nMonth = ParseStringInteger(value, j, nSkip, 3);
468 i += 3;
469 j += nSkip;
470 }
471 } break;
472 case 'y':
473 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700474 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 i += 3;
476 j += 3;
477 break;
478 }
479 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
480 switch (c) {
481 case 'y':
482 nYear = ParseStringInteger(value, j, nSkip, 4);
483 j += nSkip;
484 i += 4;
485 break;
486 case 'm': {
487 FX_BOOL bFind = FALSE;
488
489 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
490 sMonth.MakeLower();
491
492 for (int m = 0; m < 12; m++) {
493 CFX_WideString sFullMonths = fullmonths[m];
494 sFullMonths.MakeLower();
495
496 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
497 nMonth = m + 1;
498 i += 4;
499 j += nSkip;
500 bFind = TRUE;
501 break;
502 }
503 }
504
505 if (!bFind) {
506 nMonth = ParseStringInteger(value, j, nSkip, 4);
507 i += 4;
508 j += nSkip;
509 }
510 } break;
511 default:
512 i += 4;
513 j += 4;
514 break;
515 }
516 } else {
517 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800518 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519 bExit = TRUE;
520 }
521 i++;
522 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700523 }
Tom Sepez85386422014-07-23 10:28:37 -0700524
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800526 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527 bExit = TRUE;
528 }
529 }
530
531 break;
532 default:
533 if (value.GetLength() <= j) {
534 bExit = TRUE;
535 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800536 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537 bExit = TRUE;
538 }
539
540 i++;
541 j++;
542 break;
543 }
544 }
545
546 if (bPm)
547 nHour += 12;
548
549 if (nYear >= 0 && nYear <= nYearSub)
550 nYear += 2000;
551
552 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800553 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554
555 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800556 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557
558 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800559 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560
561 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800562 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563
564 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800565 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566
567 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800568 if (bBadFormat) {
569 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570 } else {
571 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
572 JS_MakeTime(nHour, nMin, nSec, 0));
tsepez018935c2016-04-15 13:15:12 -0700573 if (JS_PortIsNan(dRet))
574 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 }
576
tsepez018935c2016-04-15 13:15:12 -0700577 if (JS_PortIsNan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800578 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579
Lei Zhang9559b7a2015-12-21 11:12:20 -0800580 if (bWrongFormat)
581 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700582
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 return dRet;
584}
585
586CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
587 const CFX_WideString& format) {
588 CFX_WideString sRet = L"", sPart = L"";
589
590 int nYear = JS_GetYearFromTime(dDate);
591 int nMonth = JS_GetMonthFromTime(dDate) + 1;
592 int nDay = JS_GetDayFromTime(dDate);
593 int nHour = JS_GetHourFromTime(dDate);
594 int nMin = JS_GetMinFromTime(dDate);
595 int nSec = JS_GetSecFromTime(dDate);
596
597 int i = 0;
598 while (i < format.GetLength()) {
599 FX_WCHAR c = format.GetAt(i);
600 int remaining = format.GetLength() - i - 1;
601 sPart = L"";
602 switch (c) {
603 case 'y':
604 case 'm':
605 case 'd':
606 case 'H':
607 case 'h':
608 case 'M':
609 case 's':
610 case 't':
611 if (remaining == 0 || format.GetAt(i + 1) != c) {
612 switch (c) {
613 case 'y':
614 sPart += c;
615 break;
616 case 'm':
617 sPart.Format(L"%d", nMonth);
618 break;
619 case 'd':
620 sPart.Format(L"%d", nDay);
621 break;
622 case 'H':
623 sPart.Format(L"%d", nHour);
624 break;
625 case 'h':
626 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
627 break;
628 case 'M':
629 sPart.Format(L"%d", nMin);
630 break;
631 case 's':
632 sPart.Format(L"%d", nSec);
633 break;
634 case 't':
635 sPart += nHour > 12 ? 'p' : 'a';
636 break;
637 }
638 i++;
639 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
640 switch (c) {
641 case 'y':
642 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
643 break;
644 case 'm':
645 sPart.Format(L"%02d", nMonth);
646 break;
647 case 'd':
648 sPart.Format(L"%02d", nDay);
649 break;
650 case 'H':
651 sPart.Format(L"%02d", nHour);
652 break;
653 case 'h':
654 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
655 break;
656 case 'M':
657 sPart.Format(L"%02d", nMin);
658 break;
659 case 's':
660 sPart.Format(L"%02d", nSec);
661 break;
662 case 't':
663 sPart = nHour > 12 ? L"pm" : L"am";
664 break;
665 }
666 i += 2;
667 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
668 switch (c) {
669 case 'm':
670 i += 3;
671 if (nMonth > 0 && nMonth <= 12)
672 sPart += months[nMonth - 1];
673 break;
674 default:
675 i += 3;
676 sPart += c;
677 sPart += c;
678 sPart += c;
679 break;
680 }
681 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
682 switch (c) {
683 case 'y':
684 sPart.Format(L"%04d", nYear);
685 i += 4;
686 break;
687 case 'm':
688 i += 4;
689 if (nMonth > 0 && nMonth <= 12)
690 sPart += fullmonths[nMonth - 1];
691 break;
692 default:
693 i += 4;
694 sPart += c;
695 sPart += c;
696 sPart += c;
697 sPart += c;
698 break;
699 }
700 } else {
701 i++;
702 sPart += c;
703 }
704 break;
705 default:
706 i++;
707 sPart += c;
708 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700709 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700710
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711 sRet += sPart;
712 }
713
714 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700715}
716
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
718// bCurrencyPrepend)
Tom Sepezba038bc2015-10-08 12:03:00 -0700719FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800720 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721 CJS_Value& vRet,
722 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700723#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725 if (params.size() != 6) {
726 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
727 return FALSE;
728 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700729
730 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
731 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732 if (!pEvent->m_pValue)
733 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700734
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735 CFX_WideString& Value = pEvent->Value();
736 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737 if (strValue.IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700738 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739
740 int iDec = params[0].ToInt();
741 int iSepStyle = params[1].ToInt();
742 int iNegStyle = params[2].ToInt();
743 // params[3] is iCurrStyle, it's not used.
thestigcf03f8e2016-05-09 12:36:18 -0700744 CFX_WideString wstrCurrency = params[4].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745 FX_BOOL bCurrencyPrepend = params[5].ToBool();
746
747 if (iDec < 0)
748 iDec = -iDec;
749
750 if (iSepStyle < 0 || iSepStyle > 3)
751 iSepStyle = 0;
752
753 if (iNegStyle < 0 || iNegStyle > 3)
754 iNegStyle = 0;
755
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756 // for processing decimal places
757 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700758 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700760 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700761
762 int iDec2;
763 int iNegative = 0;
764
765 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
766 if (strValue.IsEmpty()) {
767 dValue = 0;
768 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
769 if (strValue.IsEmpty()) {
770 strValue = "0";
771 iDec2 = 1;
772 }
773 }
774
775 if (iDec2 < 0) {
776 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
777 strValue = "0" + strValue;
778 }
779 iDec2 = 0;
780 }
781 int iMax = strValue.GetLength();
782 if (iDec2 > iMax) {
783 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
784 strValue += "0";
785 }
786 iMax = iDec2 + 1;
787 }
dsinclair64376be2016-03-31 20:03:24 -0700788
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700789 // for processing seperator style
790 if (iDec2 < iMax) {
791 if (iSepStyle == 0 || iSepStyle == 1) {
792 strValue.Insert(iDec2, '.');
793 iMax++;
794 } else if (iSepStyle == 2 || iSepStyle == 3) {
795 strValue.Insert(iDec2, ',');
796 iMax++;
797 }
798
799 if (iDec2 == 0)
800 strValue.Insert(iDec2, '0');
801 }
802 if (iSepStyle == 0 || iSepStyle == 2) {
803 char cSeperator;
804 if (iSepStyle == 0)
805 cSeperator = ',';
806 else
807 cSeperator = '.';
808
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700809 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700810 strValue.Insert(iDecPositive, cSeperator);
811 iMax++;
812 }
813 }
814
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815 // for processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700816 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817
818 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700819 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820 else
thestigcf03f8e2016-05-09 12:36:18 -0700821 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823 // for processing negative style
824 if (iNegative) {
825 if (iNegStyle == 0) {
thestigcf03f8e2016-05-09 12:36:18 -0700826 Value = L"-" + Value;
827 } else if (iNegStyle == 2 || iNegStyle == 3) {
828 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829 }
830 if (iNegStyle == 1 || iNegStyle == 3) {
831 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700832 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700833 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700834 vColElm = L"RGB";
tsepeze5aff742016-08-08 09:49:42 -0700835 arColor.SetElement(pRuntime->GetIsolate(), 0, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836 vColElm = 1;
tsepeze5aff742016-08-08 09:49:42 -0700837 arColor.SetElement(pRuntime->GetIsolate(), 1, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700838 vColElm = 0;
tsepeze5aff742016-08-08 09:49:42 -0700839 arColor.SetElement(pRuntime->GetIsolate(), 2, vColElm);
840 arColor.SetElement(pRuntime->GetIsolate(), 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700841
Tom Sepez67fd5df2015-10-08 12:24:19 -0700842 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700843 vProp.StartGetting();
844 vProp << arColor;
845 vProp.StartSetting();
846 fTarget->textColor(cc, vProp, sError); // red
847 }
848 }
849 } else {
850 if (iNegStyle == 1 || iNegStyle == 3) {
851 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700852 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700853 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 vColElm = L"RGB";
tsepeze5aff742016-08-08 09:49:42 -0700855 arColor.SetElement(pRuntime->GetIsolate(), 0, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856 vColElm = 0;
tsepeze5aff742016-08-08 09:49:42 -0700857 arColor.SetElement(pRuntime->GetIsolate(), 1, vColElm);
858 arColor.SetElement(pRuntime->GetIsolate(), 2, vColElm);
859 arColor.SetElement(pRuntime->GetIsolate(), 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860
Tom Sepez67fd5df2015-10-08 12:24:19 -0700861 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862 vProp.StartGetting();
863 fTarget->textColor(cc, vProp, sError);
864
tsepeze5aff742016-08-08 09:49:42 -0700865 CJS_Array aProp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866 vProp.ConvertToArray(aProp);
867
868 CPWL_Color crProp;
869 CPWL_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700870 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
871 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872
873 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700874 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875 vProp2.StartGetting();
876 vProp2 << arColor;
877 vProp2.StartSetting();
878 fTarget->textColor(cc, vProp2, sError);
879 }
880 }
881 }
882 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883#endif
884 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700885}
886
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
888// bCurrencyPrepend)
Lei Zhang945fdb72015-11-11 10:18:16 -0800889FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(
890 IJS_Context* cc,
891 const std::vector<CJS_Value>& params,
892 CJS_Value& vRet,
893 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700896
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897 if (params.size() < 2)
898 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700899
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 if (!pEvent->m_pValue)
901 return FALSE;
thestigcf03f8e2016-05-09 12:36:18 -0700902
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 CFX_WideString& val = pEvent->Value();
thestigcf03f8e2016-05-09 12:36:18 -0700904 CFX_WideString& wstrChange = pEvent->Change();
905 CFX_WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700906
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 if (pEvent->WillCommit()) {
ochanga0a3bc32016-05-12 15:22:48 -0700908 CFX_WideString swTemp = StrTrim(wstrValue);
909 if (swTemp.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910 return TRUE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700911
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912 swTemp.Replace(L",", L".");
913 if (!IsNumber(swTemp.c_str())) {
914 pEvent->Rc() = FALSE;
915 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
916 Alert(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700917 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 return TRUE; // it happens after the last keystroke and before validating,
919 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700920
thestigcf03f8e2016-05-09 12:36:18 -0700921 CFX_WideString wstrSelected;
922 if (pEvent->SelStart() != -1) {
923 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
924 pEvent->SelEnd() - pEvent->SelStart());
925 }
926
927 bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700928 if (bHasSign) {
929 // can't insert "change" in front to sign postion.
930 if (pEvent->SelStart() == 0) {
931 FX_BOOL& bRc = pEvent->Rc();
932 bRc = FALSE;
933 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700934 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700936
thestigcf03f8e2016-05-09 12:36:18 -0700937 int iSepStyle = params[1].ToInt();
938 if (iSepStyle < 0 || iSepStyle > 3)
939 iSepStyle = 0;
940 const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700941
thestigcf03f8e2016-05-09 12:36:18 -0700942 bool bHasSep = wstrValue.Find(cSep) != -1;
943 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
944 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 if (bHasSep) {
946 FX_BOOL& bRc = pEvent->Rc();
947 bRc = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700948 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949 }
950 bHasSep = TRUE;
951 continue;
952 }
thestigcf03f8e2016-05-09 12:36:18 -0700953 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700954 if (bHasSign) {
955 FX_BOOL& bRc = pEvent->Rc();
956 bRc = FALSE;
957 return TRUE;
958 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800959 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700960 if (i != 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961 FX_BOOL& bRc = pEvent->Rc();
962 bRc = FALSE;
963 return TRUE;
964 }
965 if (pEvent->SelStart() != 0) {
966 FX_BOOL& bRc = pEvent->Rc();
967 bRc = FALSE;
968 return TRUE;
969 }
970 bHasSign = TRUE;
971 continue;
972 }
973
thestigcf03f8e2016-05-09 12:36:18 -0700974 if (!FXSYS_iswdigit(wstrChange[i])) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 FX_BOOL& bRc = pEvent->Rc();
976 bRc = FALSE;
977 return TRUE;
978 }
979 }
980
thestigcf03f8e2016-05-09 12:36:18 -0700981 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
982 CFX_WideString wpostfix;
983 if (pEvent->SelEnd() < wstrValue.GetLength())
984 wpostfix = wstrValue.Mid(pEvent->SelEnd());
985 val = wprefix + wstrChange + wpostfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 return TRUE;
987}
988
989// function AFPercent_Format(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -0800990FX_BOOL CJS_PublicMethods::AFPercent_Format(
991 IJS_Context* cc,
992 const std::vector<CJS_Value>& params,
993 CJS_Value& vRet,
994 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995#if _FX_OS_ != _FX_ANDROID_
996 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998
999 if (params.size() != 2) {
1000 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1001 return FALSE;
1002 }
1003 if (!pEvent->m_pValue)
1004 return FALSE;
1005
1006 CFX_WideString& Value = pEvent->Value();
1007 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1008 if (strValue.IsEmpty())
1009 return TRUE;
1010
1011 int iDec = params[0].ToInt();
1012 if (iDec < 0)
1013 iDec = -iDec;
1014
1015 int iSepStyle = params[1].ToInt();
1016 if (iSepStyle < 0 || iSepStyle > 3)
1017 iSepStyle = 0;
1018
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001020 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001021 dValue *= 100;
1022 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001023 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024
1025 int iDec2;
1026 int iNegative = 0;
1027 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1028 if (strValue.IsEmpty()) {
1029 dValue = 0;
1030 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1031 }
1032
1033 if (iDec2 < 0) {
1034 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1035 strValue = "0" + strValue;
1036 }
1037 iDec2 = 0;
1038 }
1039 int iMax = strValue.GetLength();
1040 if (iDec2 > iMax) {
1041 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1042 strValue += "0";
1043 }
1044 iMax = iDec2 + 1;
1045 }
dsinclair64376be2016-03-31 20:03:24 -07001046
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047 // for processing seperator style
1048 if (iDec2 < iMax) {
1049 if (iSepStyle == 0 || iSepStyle == 1) {
1050 strValue.Insert(iDec2, '.');
1051 iMax++;
1052 } else if (iSepStyle == 2 || iSepStyle == 3) {
1053 strValue.Insert(iDec2, ',');
1054 iMax++;
1055 }
1056
1057 if (iDec2 == 0)
1058 strValue.Insert(iDec2, '0');
1059 }
1060 if (iSepStyle == 0 || iSepStyle == 2) {
1061 char cSeperator;
1062 if (iSepStyle == 0)
1063 cSeperator = ',';
1064 else
1065 cSeperator = '.';
1066
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001067 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068 strValue.Insert(iDecPositive, cSeperator);
1069 iMax++;
1070 }
1071 }
dsinclair64376be2016-03-31 20:03:24 -07001072
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001073 // negative mark
1074 if (iNegative)
1075 strValue = "-" + strValue;
1076 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001077 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078#endif
1079 return TRUE;
1080}
1081// AFPercent_Keystroke(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001082FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(
1083 IJS_Context* cc,
1084 const std::vector<CJS_Value>& params,
1085 CJS_Value& vRet,
1086 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087 return AFNumber_Keystroke(cc, params, vRet, sError);
1088}
1089
1090// function AFDate_FormatEx(cFormat)
Tom Sepezba038bc2015-10-08 12:03:00 -07001091FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001092 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 CJS_Value& vRet,
1094 CFX_WideString& sError) {
1095 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001097
1098 if (params.size() != 1) {
1099 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1100 return FALSE;
1101 }
1102 if (!pEvent->m_pValue)
1103 return FALSE;
1104
1105 CFX_WideString& val = pEvent->Value();
1106 CFX_WideString strValue = val;
1107 if (strValue.IsEmpty())
1108 return TRUE;
1109
1110 CFX_WideString sFormat = params[0].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111 double dDate = 0.0f;
1112
1113 if (strValue.Find(L"GMT") != -1) {
1114 // for GMT format time
1115 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1116 dDate = MakeInterDate(strValue);
1117 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001118 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001119 }
1120
1121 if (JS_PortIsNan(dDate)) {
1122 CFX_WideString swMsg;
1123 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1124 sFormat.c_str());
1125 Alert(pContext, swMsg.c_str());
1126 return FALSE;
1127 }
1128
1129 val = MakeFormatDate(dDate, sFormat);
1130 return TRUE;
1131}
1132
tsepez745611b2016-04-12 16:46:34 -07001133double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001134 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001136 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001137 FX_WCHAR c = strValue.GetAt(i);
1138 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001139 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001140 sTemp = L"";
1141 continue;
1142 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 sTemp += c;
1144 }
Tom Sepezab277682016-02-17 10:07:21 -08001145 wsArray.push_back(sTemp);
1146 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001147 return 0;
1148
Tom Sepez4246b002016-01-20 11:48:29 -08001149 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 sTemp = wsArray[1];
1151 if (sTemp.Compare(L"Jan") == 0)
1152 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001153 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001155 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001157 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001159 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001161 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001163 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001165 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001167 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001169 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001171 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001173 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174 nMonth = 12;
1175
tsepez4c3debb2016-04-08 12:20:38 -07001176 int nDay = FX_atof(wsArray[2].AsStringC());
1177 int nHour = FX_atof(wsArray[3].AsStringC());
1178 int nMin = FX_atof(wsArray[4].AsStringC());
1179 int nSec = FX_atof(wsArray[5].AsStringC());
1180 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1182 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001183 if (JS_PortIsNan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001184 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001185
1186 return dRet;
1187}
1188
1189// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001190FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(
1191 IJS_Context* cc,
1192 const std::vector<CJS_Value>& params,
1193 CJS_Value& vRet,
1194 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197
1198 if (params.size() != 1) {
1199 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1200 return FALSE;
1201 }
1202
1203 if (pEvent->WillCommit()) {
1204 if (!pEvent->m_pValue)
1205 return FALSE;
1206 CFX_WideString strValue = pEvent->Value();
1207 if (strValue.IsEmpty())
1208 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001209
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001210 CFX_WideString sFormat = params[0].ToCFXWideString();
Lei Zhang9559b7a2015-12-21 11:12:20 -08001211 bool bWrongFormat = FALSE;
1212 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213 if (bWrongFormat || JS_PortIsNan(dRet)) {
1214 CFX_WideString swMsg;
1215 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1216 sFormat.c_str());
1217 Alert(pContext, swMsg.c_str());
1218 pEvent->Rc() = FALSE;
1219 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001220 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001221 }
1222 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001223}
1224
Tom Sepezba038bc2015-10-08 12:03:00 -07001225FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001226 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227 CJS_Value& vRet,
1228 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001229 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001230 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001231 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1232 return FALSE;
1233 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001234
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235 int iIndex = params[0].ToInt();
1236 const FX_WCHAR* cFormats[] = {L"m/d",
1237 L"m/d/yy",
1238 L"mm/dd/yy",
1239 L"mm/yy",
1240 L"d-mmm",
1241 L"d-mmm-yy",
1242 L"dd-mmm-yy",
1243 L"yy-mm-dd",
1244 L"mmm-yy",
1245 L"mmmm-yy",
1246 L"mmm d, yyyy",
1247 L"mmmm d, yyyy",
1248 L"m/d/yy h:MM tt",
1249 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001250
Lei Zhanga0f67242015-08-17 15:39:30 -07001251 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1252 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001253
Lei Zhang945fdb72015-11-11 10:18:16 -08001254 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001255 newParams.push_back(
1256 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001257 return AFDate_FormatEx(cc, newParams, vRet, sError);
1258}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001259
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001261FX_BOOL CJS_PublicMethods::AFDate_Keystroke(
1262 IJS_Context* cc,
1263 const std::vector<CJS_Value>& params,
1264 CJS_Value& vRet,
1265 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001266 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1269 return FALSE;
1270 }
1271
1272 int iIndex = params[0].ToInt();
1273 const FX_WCHAR* cFormats[] = {L"m/d",
1274 L"m/d/yy",
1275 L"mm/dd/yy",
1276 L"mm/yy",
1277 L"d-mmm",
1278 L"d-mmm-yy",
1279 L"dd-mmm-yy",
1280 L"yy-mm-dd",
1281 L"mmm-yy",
1282 L"mmmm-yy",
1283 L"mmm d, yyyy",
1284 L"mmmm d, yyyy",
1285 L"m/d/yy h:MM tt",
1286 L"m/d/yy HH:MM"};
1287
Lei Zhanga0f67242015-08-17 15:39:30 -07001288 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1289 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001290
Lei Zhang945fdb72015-11-11 10:18:16 -08001291 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001292 newParams.push_back(
1293 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001294 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1295}
1296
1297// function AFTime_Format(ptf)
Tom Sepezba038bc2015-10-08 12:03:00 -07001298FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001299 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 CJS_Value& vRet,
1301 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001302 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001304 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1305 return FALSE;
1306 }
1307
1308 int iIndex = params[0].ToInt();
1309 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1310 L"h:MM:ss tt"};
1311
Lei Zhanga0f67242015-08-17 15:39:30 -07001312 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1313 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314
Lei Zhang945fdb72015-11-11 10:18:16 -08001315 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001316 newParams.push_back(
1317 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318 return AFDate_FormatEx(cc, newParams, vRet, sError);
1319}
1320
Lei Zhang945fdb72015-11-11 10:18:16 -08001321FX_BOOL CJS_PublicMethods::AFTime_Keystroke(
1322 IJS_Context* cc,
1323 const std::vector<CJS_Value>& params,
1324 CJS_Value& vRet,
1325 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001326 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001328 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1329 return FALSE;
1330 }
1331
1332 int iIndex = params[0].ToInt();
1333 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1334 L"h:MM:ss tt"};
1335
Lei Zhanga0f67242015-08-17 15:39:30 -07001336 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1337 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001338
Lei Zhang945fdb72015-11-11 10:18:16 -08001339 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001340 newParams.push_back(
1341 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001342 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1343}
1344
Tom Sepezba038bc2015-10-08 12:03:00 -07001345FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001346 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001347 CJS_Value& vRet,
1348 CFX_WideString& sError) {
1349 return AFDate_FormatEx(cc, params, vRet, sError);
1350}
1351
Lei Zhang945fdb72015-11-11 10:18:16 -08001352FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(
1353 IJS_Context* cc,
1354 const std::vector<CJS_Value>& params,
1355 CJS_Value& vRet,
1356 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1358}
1359
1360// function AFSpecial_Format(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001361FX_BOOL CJS_PublicMethods::AFSpecial_Format(
1362 IJS_Context* cc,
1363 const std::vector<CJS_Value>& params,
1364 CJS_Value& vRet,
1365 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001366 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367
1368 if (params.size() != 1) {
1369 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1370 return FALSE;
1371 }
1372
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374 if (!pEvent->m_pValue)
1375 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376
tsepez4f1f41f2016-03-28 14:13:16 -07001377 CFX_WideString wsSource = pEvent->Value();
1378 CFX_WideString wsFormat;
1379 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001380 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001381 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001383 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001384 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001385 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001386 case 2:
1387 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1388 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001389 else
tsepez4f1f41f2016-03-28 14:13:16 -07001390 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001391 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001392 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001393 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 break;
1395 }
1396
tsepez4f1f41f2016-03-28 14:13:16 -07001397 pEvent->Value() = util::printx(wsFormat, wsSource);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001398 return TRUE;
1399}
1400
1401// function AFSpecial_KeystrokeEx(mask)
Lei Zhang945fdb72015-11-11 10:18:16 -08001402FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(
1403 IJS_Context* cc,
1404 const std::vector<CJS_Value>& params,
1405 CJS_Value& vRet,
1406 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1409
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001410 if (params.size() < 1) {
1411 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1412 return FALSE;
1413 }
1414
1415 if (!pEvent->m_pValue)
1416 return FALSE;
1417 CFX_WideString& valEvent = pEvent->Value();
1418
1419 CFX_WideString wstrMask = params[0].ToCFXWideString();
1420 if (wstrMask.IsEmpty())
1421 return TRUE;
1422
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001424 if (valEvent.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 return TRUE;
thestigcf03f8e2016-05-09 12:36:18 -07001426
1427 FX_STRSIZE iIndexMask = 0;
1428 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1429 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001430 break;
1431 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001432
thestigcf03f8e2016-05-09 12:36:18 -07001433 if (iIndexMask != wstrMask.GetLength() ||
1434 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001435 Alert(
1436 pContext,
1437 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1438 pEvent->Rc() = FALSE;
1439 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001440 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441 }
1442
1443 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001444 if (wideChange.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001445 return TRUE;
1446
thestigcf03f8e2016-05-09 12:36:18 -07001447 CFX_WideString wChange = wideChange;
1448 FX_STRSIZE iIndexMask = pEvent->SelStart();
1449 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1450 pEvent->SelStart() - pEvent->SelEnd();
1451 if (combined_len > wstrMask.GetLength()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001452 Alert(pContext,
1453 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1454 pEvent->Rc() = FALSE;
1455 return TRUE;
1456 }
1457
thestigcf03f8e2016-05-09 12:36:18 -07001458 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001459 Alert(pContext,
1460 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1461 pEvent->Rc() = FALSE;
1462 return TRUE;
1463 }
1464
thestigcf03f8e2016-05-09 12:36:18 -07001465 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1466 if (iIndexMask >= wstrMask.GetLength()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001467 Alert(pContext,
1468 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1469 pEvent->Rc() = FALSE;
1470 return TRUE;
1471 }
thestigcf03f8e2016-05-09 12:36:18 -07001472 FX_WCHAR wMask = wstrMask[iIndexMask];
1473 if (!isReservedMaskChar(wMask))
1474 wChange.SetAt(i, wMask);
1475
1476 if (!maskSatisfied(wChange[i], wMask)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477 pEvent->Rc() = FALSE;
1478 return TRUE;
1479 }
1480 iIndexMask++;
1481 }
thestigcf03f8e2016-05-09 12:36:18 -07001482 wideChange = wChange;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001483 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001484}
1485
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001486// function AFSpecial_Keystroke(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001487FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(
1488 IJS_Context* cc,
1489 const std::vector<CJS_Value>& params,
1490 CJS_Value& vRet,
1491 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001493 if (params.size() != 1) {
1494 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1495 return FALSE;
1496 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001497
Tom Sepez67fd5df2015-10-08 12:24:19 -07001498 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 if (!pEvent->m_pValue)
1500 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001501
thestigcf03f8e2016-05-09 12:36:18 -07001502 const char* cFormat = "";
1503 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001504 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505 cFormat = "99999";
1506 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001507 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001508 cFormat = "999999999";
1509 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001510 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001511 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512 cFormat = "9999999999";
1513 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001514 cFormat = "9999999";
1515 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001516 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517 cFormat = "999999999";
1518 break;
1519 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001520
Lei Zhang945fdb72015-11-11 10:18:16 -08001521 std::vector<CJS_Value> params2;
thestigcf03f8e2016-05-09 12:36:18 -07001522 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001523 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001524}
1525
Tom Sepezba038bc2015-10-08 12:03:00 -07001526FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001527 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001528 CJS_Value& vRet,
1529 CFX_WideString& sError) {
1530 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001531 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001532
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001533 if (params.size() != 1) {
1534 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1535 return FALSE;
1536 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001537
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001539 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001540 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001541
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542 if (pEventHandler->WillCommit()) {
1543 vRet = swValue.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001544 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001545 }
1546
1547 CFX_WideString prefix, postfix;
1548
1549 if (pEventHandler->SelStart() >= 0)
1550 prefix = swValue.Mid(0, pEventHandler->SelStart());
1551 else
1552 prefix = L"";
1553
1554 if (pEventHandler->SelEnd() >= 0 &&
1555 pEventHandler->SelEnd() <= swValue.GetLength())
1556 postfix = swValue.Mid(pEventHandler->SelEnd(),
1557 swValue.GetLength() - pEventHandler->SelEnd());
1558 else
1559 postfix = L"";
1560
1561 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1562
1563 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001564}
1565
Tom Sepezba038bc2015-10-08 12:03:00 -07001566FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001567 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001568 CJS_Value& vRet,
1569 CFX_WideString& sError) {
1570 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001571 ASSERT(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001572
1573 if (params.size() != 2) {
1574 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1575 return FALSE;
1576 }
1577
1578 CFX_WideString sValue = params[0].ToCFXWideString();
1579 CFX_WideString sFormat = params[1].ToCFXWideString();
1580
Lei Zhang9559b7a2015-12-21 11:12:20 -08001581 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001582
1583 if (JS_PortIsNan(dDate)) {
1584 CFX_WideString swMsg;
1585 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1586 sFormat.c_str());
1587 Alert((CJS_Context*)cc, swMsg.c_str());
1588 return FALSE;
1589 }
1590
1591 vRet = dDate;
1592 return TRUE;
1593}
1594
Tom Sepezba038bc2015-10-08 12:03:00 -07001595FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001596 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001597 CJS_Value& vRet,
1598 CFX_WideString& sError) {
1599 if (params.size() != 3) {
1600 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001601 ASSERT(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001602
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001603 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1604 return FALSE;
1605 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001606
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001607 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1608 params[1].ToDouble(), params[2].ToDouble());
1609 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001610}
1611
Tom Sepezba038bc2015-10-08 12:03:00 -07001612FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001613 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001614 CJS_Value& vRet,
1615 CFX_WideString& sError) {
Tom Sepez4246b002016-01-20 11:48:29 -08001616 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001617 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001618 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1619 return FALSE;
1620 }
Tom Sepez4246b002016-01-20 11:48:29 -08001621 CFX_WideString ws = params[0].ToCFXWideString();
1622 ws.Replace(L",", L".");
tsepezbd9748d2016-04-13 21:40:19 -07001623 vRet = ws.c_str();
Tom Sepez4246b002016-01-20 11:48:29 -08001624 vRet.MaybeCoerceToNumber();
1625 if (vRet.GetType() != CJS_Value::VT_number)
1626 vRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627 return TRUE;
1628}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001629
Lei Zhang945fdb72015-11-11 10:18:16 -08001630FX_BOOL CJS_PublicMethods::AFSimple_Calculate(
1631 IJS_Context* cc,
1632 const std::vector<CJS_Value>& params,
1633 CJS_Value& vRet,
1634 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001635 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636 if (params.size() != 2) {
1637 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1638 return FALSE;
1639 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001640
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001641 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001642 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001643 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1644 return FALSE;
1645 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001646
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001647 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001649 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001650
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001651 CFX_WideString sFunction = params[0].ToCFXWideString();
Tom Sepez67fd5df2015-10-08 12:24:19 -07001652 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001653
Tom Sepez67fd5df2015-10-08 12:24:19 -07001654 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1655 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001656 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001657
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001658 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001659 CJS_Value jsValue(pRuntime);
tsepeze5aff742016-08-08 09:49:42 -07001660 FieldNameArray.GetElement(pRuntime->GetIsolate(), i, jsValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001661 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001662
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001663 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1664 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1665 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001666 switch (pFormField->GetFieldType()) {
1667 case FIELDTYPE_TEXTFIELD:
1668 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001669 CFX_WideString trimmed = pFormField->GetValue();
1670 trimmed.TrimRight();
1671 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001672 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001673 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001674 case FIELDTYPE_PUSHBUTTON: {
1675 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001676 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001677 case FIELDTYPE_CHECKBOX:
1678 case FIELDTYPE_RADIOBUTTON: {
1679 dTemp = 0.0;
1680 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1681 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1682 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001683 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1684 trimmed.TrimRight();
1685 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001686 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001687 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001688 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001689 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001690 }
Tom Sepez4246b002016-01-20 11:48:29 -08001691 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001692 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001693 if (pFormField->CountSelectedItems() <= 1) {
1694 CFX_WideString trimmed = pFormField->GetValue();
1695 trimmed.TrimRight();
1696 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001697 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001698 }
1699 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001700 default:
1701 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001702 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703
1704 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1705 wcscmp(sFunction.c_str(), L"MAX") == 0))
1706 dValue = dTemp;
1707
1708 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1709
1710 nFieldsCount++;
1711 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001712 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001714
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001715 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1716 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001717
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001718 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1719 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001720 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001721 if (pContext->GetEventHandler()->m_pValue)
1722 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001723
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001724 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001725}
1726
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001727/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001728** within the specified range. */
1729
Lei Zhang945fdb72015-11-11 10:18:16 -08001730FX_BOOL CJS_PublicMethods::AFRange_Validate(
1731 IJS_Context* cc,
1732 const std::vector<CJS_Value>& params,
1733 CJS_Value& vRet,
1734 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001735 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001736 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001737
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001738 if (params.size() != 4) {
1739 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1740 return FALSE;
1741 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001742
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001743 if (!pEvent->m_pValue)
1744 return FALSE;
1745 if (pEvent->Value().IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001746 return TRUE;
tsepezb4c9f3f2016-04-13 15:41:21 -07001747 double dEentValue =
1748 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001749 FX_BOOL bGreaterThan = params[0].ToBool();
1750 double dGreaterThan = params[1].ToDouble();
1751 FX_BOOL bLessThan = params[2].ToBool();
1752 double dLessThan = params[3].ToDouble();
1753 CFX_WideString swMsg;
1754
1755 if (bGreaterThan && bLessThan) {
1756 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
1757 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
1758 params[1].ToCFXWideString().c_str(),
1759 params[3].ToCFXWideString().c_str());
1760 } else if (bGreaterThan) {
1761 if (dEentValue < dGreaterThan)
1762 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
1763 params[1].ToCFXWideString().c_str());
1764 } else if (bLessThan) {
1765 if (dEentValue > dLessThan)
1766 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
1767 params[3].ToCFXWideString().c_str());
1768 }
1769
1770 if (!swMsg.IsEmpty()) {
1771 Alert(pContext, swMsg.c_str());
1772 pEvent->Rc() = FALSE;
1773 }
1774 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001775}
1776
Tom Sepezba038bc2015-10-08 12:03:00 -07001777FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001778 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001779 CJS_Value& vRet,
1780 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001782 if (params.size() != 1) {
1783 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1784 return FALSE;
1785 }
1786
Tom Sepez67fd5df2015-10-08 12:24:19 -07001787 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepeze5aff742016-08-08 09:49:42 -07001788 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001789
1790 CFX_WideString str = params[0].ToCFXWideString();
1791 CFX_WideString sPart;
1792
1793 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1794 str = L"0" + str;
1795
1796 int nIndex = 0;
1797 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1798 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001799 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001800 sPart += wc;
1801 } else {
1802 if (sPart.GetLength() > 0) {
tsepeze5aff742016-08-08 09:49:42 -07001803 nums.SetElement(pRuntime->GetIsolate(), nIndex,
1804 CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001805 sPart = L"";
1806 nIndex++;
1807 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001808 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001809 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001810
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001811 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 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001815
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001816 if (nums.GetLength() > 0)
tsepeze5aff742016-08-08 09:49:42 -07001817 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001818 else
1819 vRet.SetNull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001820
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001821 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001822}