blob: 0173bb3ff5b7b4a56badf5190d58cc22b7c034c7 [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
dsinclair1727aee2016-09-29 13:12:56 -070012#include "core/fpdfdoc/cpdf_interform.h"
dsinclaira52ab742016-09-29 13:59:29 -070013#include "core/fxcrt/fx_ext.h"
dsinclair735606d2016-10-05 15:47:02 -070014#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070015#include "fpdfsdk/cpdfsdk_interform.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040016#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040017#include "fpdfsdk/javascript/JS_Define.h"
18#include "fpdfsdk/javascript/JS_EventHandler.h"
19#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040020#include "fpdfsdk/javascript/JS_Value.h"
dsinclair64376be2016-03-31 20:03:24 -070021#include "fpdfsdk/javascript/cjs_context.h"
22#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040023#include "fpdfsdk/javascript/color.h"
24#include "fpdfsdk/javascript/resource.h"
25#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
29BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format)
31JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke)
32JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format)
33JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke)
34JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx)
35JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx)
36JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format)
37JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke)
38JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx)
39JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx)
40JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format)
41JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke)
42JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format)
43JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke)
44JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx)
45JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple)
46JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
47JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
48JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
49JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
50JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
51JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052END_JS_STATIC_GLOBAL_FUN()
53
54IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
55
tsepez745611b2016-04-12 16:46:34 -070056namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
tsepez745611b2016-04-12 16:46:34 -070058const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
59 L"May", L"Jun", L"Jul", L"Aug",
60 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061
tsepez745611b2016-04-12 16:46:34 -070062const FX_WCHAR* const fullmonths[] = {L"January", L"February", L"March",
63 L"April", L"May", L"June",
64 L"July", L"August", L"September",
65 L"October", L"November", L"December"};
66
67CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
68 CFX_ByteString result(pStr);
69 result.TrimLeft(' ');
70 result.TrimRight(' ');
71 return result;
72}
73
74CFX_WideString StrTrim(const CFX_WideString& pStr) {
75 CFX_WideString result(pStr);
76 result.TrimLeft(' ');
77 result.TrimRight(' ');
78 return result;
79}
80
tsepeze1e7bd02016-08-08 13:03:16 -070081void AlertIfPossible(CJS_Context* pContext, const FX_WCHAR* swMsg) {
dsinclair8779fa82016-10-12 12:05:44 -070082 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
83 if (pFormFillEnv)
84 pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3);
tsepeze1e7bd02016-08-08 13:03:16 -070085}
86
tsepez745611b2016-04-12 16:46:34 -070087} // namespace
88
89bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -050090 CFX_WideString sTrim = StrTrim(str);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 const FX_WCHAR* pTrim = sTrim.c_str();
92 const FX_WCHAR* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -070093 bool bDot = false;
94 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -070097 while ((c = *p) != L'\0') {
98 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -0700100 return false;
101 bDot = true;
102 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -0700104 return false;
105 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700107 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 p++;
110 c = *p;
Wei Li614d20a2016-03-15 13:55:12 -0700111 if (c == L'+' || c == L'-') {
112 bKXJS = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 } else {
Wei Li614d20a2016-03-15 13:55:12 -0700114 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800116 } else if (!FXSYS_iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700117 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 }
119 p++;
120 }
121
Wei Li614d20a2016-03-15 13:55:12 -0700122 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123}
124
Wei Li614d20a2016-03-15 13:55:12 -0700125bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 switch (c_Mask) {
127 case L'9':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800128 return FXSYS_iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800130 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800132 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700134 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 default:
136 return (c_Change == c_Mask);
137 }
138}
139
Wei Li614d20a2016-03-15 13:55:12 -0700140bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
142}
143
144double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
145 double dValue1,
146 double dValue2) {
147 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
148 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
149 return dValue1 + dValue2;
150 }
151 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
152 return dValue1 * dValue2;
153 }
154 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800155 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 }
157 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800158 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 }
160 return dValue1;
161}
162
Tom Sepez67fd5df2015-10-08 12:24:19 -0700163CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 CJS_Value val) {
tsepeze5aff742016-08-08 09:49:42 -0700165 CJS_Array StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 if (val.IsArrayObject()) {
tsepezb4694242016-08-15 16:44:55 -0700167 val.ConvertToArray(pRuntime, StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700168 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 }
tsepezb4694242016-08-15 16:44:55 -0700170 CFX_WideString wsStr = val.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700172 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173
174 int ch = ',';
175 int nIndex = 0;
176
177 while (*p) {
178 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800179 if (!pTemp) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700180 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700181 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700182 CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 }
Lei Zhang997de612015-11-04 18:17:53 -0800185
186 char* pSub = new char[pTemp - p + 1];
187 strncpy(pSub, p, pTemp - p);
188 *(pSub + (pTemp - p)) = '\0';
189
tsepezb4c9f3f2016-04-13 15:41:21 -0700190 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700191 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700192 CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800193 delete[] pSub;
194
195 nIndex++;
196 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 }
198 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500201int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 int nStart,
203 int& nSkip,
204 int nMaxStep) {
205 int nRet = 0;
206 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500207 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 if (i - nStart > 10)
209 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500211 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800212 if (!FXSYS_iswdigit(c))
213 break;
214
Dan Sinclair1c915372016-03-03 17:12:58 -0500215 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800216 nSkip = i - nStart + 1;
217 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 break;
219 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222}
223
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500224CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
225 int nStart,
226 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 CFX_WideString swRet;
228 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500229 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
230 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800231 if (!FXSYS_iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800233
234 swRet += c;
235 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800242 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 int nYear = JS_GetYearFromTime(dt);
246 int nMonth = JS_GetMonthFromTime(dt) + 1;
247 int nDay = JS_GetDayFromTime(dt);
248 int nHour = JS_GetHourFromTime(dt);
249 int nMin = JS_GetMinFromTime(dt);
250 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 int nSkip = 0;
255 int nLen = value.GetLength();
256 int nIndex = 0;
257 int i = 0;
258 while (i < nLen) {
259 if (nIndex > 2)
260 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 FX_WCHAR c = value.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800263 if (FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
265 i += nSkip;
266 } else {
267 i++;
268 }
269 }
270
271 if (nIndex == 2) {
272 // case2: month/day
273 // case3: day/month
274 if ((number[0] >= 1 && number[0] <= 12) &&
275 (number[1] >= 1 && number[1] <= 31)) {
276 nMonth = number[0];
277 nDay = number[1];
278 } else if ((number[0] >= 1 && number[0] <= 31) &&
279 (number[1] >= 1 && number[1] <= 12)) {
280 nDay = number[0];
281 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700282 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283
Lei Zhang9559b7a2015-12-21 11:12:20 -0800284 if (bWrongFormat)
285 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 } else if (nIndex == 3) {
287 // case1: year/month/day
288 // case2: month/day/year
289 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700290
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
292 (number[2] >= 1 && number[2] <= 31)) {
293 nYear = number[0];
294 nMonth = number[1];
295 nDay = number[2];
296 } else if ((number[0] >= 1 && number[0] <= 12) &&
297 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
298 nMonth = number[0];
299 nDay = number[1];
300 nYear = number[2];
301 } else if ((number[0] >= 1 && number[0] <= 31) &&
302 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
303 nDay = number[0];
304 nMonth = number[1];
305 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700306 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307
Lei Zhang9559b7a2015-12-21 11:12:20 -0800308 if (bWrongFormat)
309 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800311 if (bWrongFormat)
312 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 return dt;
314 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 CFX_WideString swTemp;
317 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700318 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319}
320
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
322 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800323 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700325
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 if (format.IsEmpty() || value.IsEmpty())
327 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700328
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 int nYear = JS_GetYearFromTime(dt);
330 int nMonth = JS_GetMonthFromTime(dt) + 1;
331 int nDay = JS_GetDayFromTime(dt);
332 int nHour = JS_GetHourFromTime(dt);
333 int nMin = JS_GetMinFromTime(dt);
334 int nSec = JS_GetSecFromTime(dt);
335
336 int nYearSub = 99; // nYear - 2000;
337
tsepez4cf55152016-11-02 14:37:54 -0700338 bool bPm = false;
339 bool bExit = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800340 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341
342 int i = 0;
343 int j = 0;
344
345 while (i < format.GetLength()) {
346 if (bExit)
347 break;
348
349 FX_WCHAR c = format.GetAt(i);
350 switch (c) {
351 case ':':
352 case '.':
353 case '-':
354 case '\\':
355 case '/':
356 i++;
357 j++;
358 break;
359
360 case 'y':
361 case 'm':
362 case 'd':
363 case 'H':
364 case 'h':
365 case 'M':
366 case 's':
367 case 't': {
368 int oldj = j;
369 int nSkip = 0;
370 int remaining = format.GetLength() - i - 1;
371
372 if (remaining == 0 || format.GetAt(i + 1) != c) {
373 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700374 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 i++;
376 j++;
377 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700378 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 nMonth = ParseStringInteger(value, j, nSkip, 2);
380 i++;
381 j += nSkip;
382 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700383 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 nDay = ParseStringInteger(value, j, nSkip, 2);
385 i++;
386 j += nSkip;
387 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700388 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 nHour = ParseStringInteger(value, j, nSkip, 2);
390 i++;
391 j += nSkip;
392 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700393 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 nHour = ParseStringInteger(value, j, nSkip, 2);
395 i++;
396 j += nSkip;
397 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700398 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 nMin = ParseStringInteger(value, j, nSkip, 2);
400 i++;
401 j += nSkip;
402 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700403 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 nSec = ParseStringInteger(value, j, nSkip, 2);
405 i++;
406 j += nSkip;
407 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700408 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
410 i++;
411 j++;
412 break;
413 }
414 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
415 switch (c) {
416 case 'y':
417 nYear = ParseStringInteger(value, j, nSkip, 4);
418 i += 2;
419 j += nSkip;
420 break;
421 case 'm':
422 nMonth = ParseStringInteger(value, j, nSkip, 2);
423 i += 2;
424 j += nSkip;
425 break;
426 case 'd':
427 nDay = ParseStringInteger(value, j, nSkip, 2);
428 i += 2;
429 j += nSkip;
430 break;
431 case 'H':
432 nHour = ParseStringInteger(value, j, nSkip, 2);
433 i += 2;
434 j += nSkip;
435 break;
436 case 'h':
437 nHour = ParseStringInteger(value, j, nSkip, 2);
438 i += 2;
439 j += nSkip;
440 break;
441 case 'M':
442 nMin = ParseStringInteger(value, j, nSkip, 2);
443 i += 2;
444 j += nSkip;
445 break;
446 case 's':
447 nSec = ParseStringInteger(value, j, nSkip, 2);
448 i += 2;
449 j += nSkip;
450 break;
451 case 't':
452 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
453 value.GetAt(j + 1) == 'm');
454 i += 2;
455 j += 2;
456 break;
457 }
458 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
459 switch (c) {
460 case 'm': {
461 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
tsepez4cf55152016-11-02 14:37:54 -0700462 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 for (int m = 0; m < 12; m++) {
464 if (sMonth.CompareNoCase(months[m]) == 0) {
465 nMonth = m + 1;
466 i += 3;
467 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700468 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700470 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 }
472
473 if (!bFind) {
474 nMonth = ParseStringInteger(value, j, nSkip, 3);
475 i += 3;
476 j += nSkip;
477 }
478 } break;
479 case 'y':
480 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700481 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 i += 3;
483 j += 3;
484 break;
485 }
486 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
487 switch (c) {
488 case 'y':
489 nYear = ParseStringInteger(value, j, nSkip, 4);
490 j += nSkip;
491 i += 4;
492 break;
493 case 'm': {
tsepez4cf55152016-11-02 14:37:54 -0700494 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495
496 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
497 sMonth.MakeLower();
498
499 for (int m = 0; m < 12; m++) {
500 CFX_WideString sFullMonths = fullmonths[m];
501 sFullMonths.MakeLower();
502
503 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
504 nMonth = m + 1;
505 i += 4;
506 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700507 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 break;
509 }
510 }
511
512 if (!bFind) {
513 nMonth = ParseStringInteger(value, j, nSkip, 4);
514 i += 4;
515 j += nSkip;
516 }
517 } break;
518 default:
519 i += 4;
520 j += 4;
521 break;
522 }
523 } else {
524 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800525 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700526 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527 }
528 i++;
529 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700530 }
Tom Sepez85386422014-07-23 10:28:37 -0700531
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800533 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700534 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535 }
536 }
537
538 break;
539 default:
540 if (value.GetLength() <= j) {
tsepez4cf55152016-11-02 14:37:54 -0700541 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700542 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800543 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700544 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700545 }
546
547 i++;
548 j++;
549 break;
550 }
551 }
552
553 if (bPm)
554 nHour += 12;
555
556 if (nYear >= 0 && nYear <= nYearSub)
557 nYear += 2000;
558
559 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800560 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561
562 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800563 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564
565 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800566 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700567
568 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800569 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570
571 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800572 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573
574 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800575 if (bBadFormat) {
576 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577 } else {
578 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
579 JS_MakeTime(nHour, nMin, nSec, 0));
tsepez018935c2016-04-15 13:15:12 -0700580 if (JS_PortIsNan(dRet))
581 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 }
583
tsepez018935c2016-04-15 13:15:12 -0700584 if (JS_PortIsNan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800585 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586
Lei Zhang9559b7a2015-12-21 11:12:20 -0800587 if (bWrongFormat)
588 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700589
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590 return dRet;
591}
592
593CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
594 const CFX_WideString& format) {
595 CFX_WideString sRet = L"", sPart = L"";
596
597 int nYear = JS_GetYearFromTime(dDate);
598 int nMonth = JS_GetMonthFromTime(dDate) + 1;
599 int nDay = JS_GetDayFromTime(dDate);
600 int nHour = JS_GetHourFromTime(dDate);
601 int nMin = JS_GetMinFromTime(dDate);
602 int nSec = JS_GetSecFromTime(dDate);
603
604 int i = 0;
605 while (i < format.GetLength()) {
606 FX_WCHAR c = format.GetAt(i);
607 int remaining = format.GetLength() - i - 1;
608 sPart = L"";
609 switch (c) {
610 case 'y':
611 case 'm':
612 case 'd':
613 case 'H':
614 case 'h':
615 case 'M':
616 case 's':
617 case 't':
618 if (remaining == 0 || format.GetAt(i + 1) != c) {
619 switch (c) {
620 case 'y':
621 sPart += c;
622 break;
623 case 'm':
624 sPart.Format(L"%d", nMonth);
625 break;
626 case 'd':
627 sPart.Format(L"%d", nDay);
628 break;
629 case 'H':
630 sPart.Format(L"%d", nHour);
631 break;
632 case 'h':
633 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
634 break;
635 case 'M':
636 sPart.Format(L"%d", nMin);
637 break;
638 case 's':
639 sPart.Format(L"%d", nSec);
640 break;
641 case 't':
642 sPart += nHour > 12 ? 'p' : 'a';
643 break;
644 }
645 i++;
646 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
647 switch (c) {
648 case 'y':
649 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
650 break;
651 case 'm':
652 sPart.Format(L"%02d", nMonth);
653 break;
654 case 'd':
655 sPart.Format(L"%02d", nDay);
656 break;
657 case 'H':
658 sPart.Format(L"%02d", nHour);
659 break;
660 case 'h':
661 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
662 break;
663 case 'M':
664 sPart.Format(L"%02d", nMin);
665 break;
666 case 's':
667 sPart.Format(L"%02d", nSec);
668 break;
669 case 't':
670 sPart = nHour > 12 ? L"pm" : L"am";
671 break;
672 }
673 i += 2;
674 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
675 switch (c) {
676 case 'm':
677 i += 3;
678 if (nMonth > 0 && nMonth <= 12)
679 sPart += months[nMonth - 1];
680 break;
681 default:
682 i += 3;
683 sPart += c;
684 sPart += c;
685 sPart += c;
686 break;
687 }
688 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
689 switch (c) {
690 case 'y':
691 sPart.Format(L"%04d", nYear);
692 i += 4;
693 break;
694 case 'm':
695 i += 4;
696 if (nMonth > 0 && nMonth <= 12)
697 sPart += fullmonths[nMonth - 1];
698 break;
699 default:
700 i += 4;
701 sPart += c;
702 sPart += c;
703 sPart += c;
704 sPart += c;
705 break;
706 }
707 } else {
708 i++;
709 sPart += c;
710 }
711 break;
712 default:
713 i++;
714 sPart += c;
715 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700716 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700717
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718 sRet += sPart;
719 }
720
721 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700722}
723
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
725// bCurrencyPrepend)
tsepez4cf55152016-11-02 14:37:54 -0700726bool CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
727 const std::vector<CJS_Value>& params,
728 CJS_Value& vRet,
729 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700730#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731 if (params.size() != 6) {
tsepezcd5dc852016-09-08 11:23:24 -0700732 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700733 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700735
tsepezcd5dc852016-09-08 11:23:24 -0700736 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
737 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Tom Sepez67fd5df2015-10-08 12:24:19 -0700738 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700740 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700741
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742 CFX_WideString& Value = pEvent->Value();
743 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700744 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700745 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700746
tsepezb4694242016-08-15 16:44:55 -0700747 int iDec = params[0].ToInt(pRuntime);
748 int iSepStyle = params[1].ToInt(pRuntime);
749 int iNegStyle = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700750 // params[3] is iCurrStyle, it's not used.
tsepezb4694242016-08-15 16:44:55 -0700751 CFX_WideString wstrCurrency = params[4].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700752 bool bCurrencyPrepend = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700753
754 if (iDec < 0)
755 iDec = -iDec;
756
757 if (iSepStyle < 0 || iSepStyle > 3)
758 iSepStyle = 0;
759
760 if (iNegStyle < 0 || iNegStyle > 3)
761 iNegStyle = 0;
762
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763 // for processing decimal places
764 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700765 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700766 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700767 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700768
769 int iDec2;
770 int iNegative = 0;
771
772 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
773 if (strValue.IsEmpty()) {
774 dValue = 0;
775 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
776 if (strValue.IsEmpty()) {
777 strValue = "0";
778 iDec2 = 1;
779 }
780 }
781
782 if (iDec2 < 0) {
783 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
784 strValue = "0" + strValue;
785 }
786 iDec2 = 0;
787 }
788 int iMax = strValue.GetLength();
789 if (iDec2 > iMax) {
790 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
791 strValue += "0";
792 }
793 iMax = iDec2 + 1;
794 }
dsinclair64376be2016-03-31 20:03:24 -0700795
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796 // for processing seperator style
797 if (iDec2 < iMax) {
798 if (iSepStyle == 0 || iSepStyle == 1) {
799 strValue.Insert(iDec2, '.');
800 iMax++;
801 } else if (iSepStyle == 2 || iSepStyle == 3) {
802 strValue.Insert(iDec2, ',');
803 iMax++;
804 }
805
806 if (iDec2 == 0)
807 strValue.Insert(iDec2, '0');
808 }
809 if (iSepStyle == 0 || iSepStyle == 2) {
810 char cSeperator;
811 if (iSepStyle == 0)
812 cSeperator = ',';
813 else
814 cSeperator = '.';
815
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700816 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817 strValue.Insert(iDecPositive, cSeperator);
818 iMax++;
819 }
820 }
821
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822 // for processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700823 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700824
825 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700826 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827 else
thestigcf03f8e2016-05-09 12:36:18 -0700828 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830 // for processing negative style
831 if (iNegative) {
832 if (iNegStyle == 0) {
thestigcf03f8e2016-05-09 12:36:18 -0700833 Value = L"-" + Value;
834 } else if (iNegStyle == 2 || iNegStyle == 3) {
835 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836 }
837 if (iNegStyle == 1 || iNegStyle == 3) {
838 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700839 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700840 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700841 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700842 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700843 vColElm = CJS_Value(pRuntime, 1);
tsepezb4694242016-08-15 16:44:55 -0700844 arColor.SetElement(pRuntime, 1, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700845 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700846 arColor.SetElement(pRuntime, 2, vColElm);
847 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700848
Tom Sepez67fd5df2015-10-08 12:24:19 -0700849 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850 vProp.StartGetting();
851 vProp << arColor;
852 vProp.StartSetting();
853 fTarget->textColor(cc, vProp, sError); // red
854 }
855 }
856 } else {
857 if (iNegStyle == 1 || iNegStyle == 3) {
858 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700859 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700860 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700861 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700862 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700863 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700864 arColor.SetElement(pRuntime, 1, vColElm);
865 arColor.SetElement(pRuntime, 2, vColElm);
866 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867
Tom Sepez67fd5df2015-10-08 12:24:19 -0700868 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869 vProp.StartGetting();
870 fTarget->textColor(cc, vProp, sError);
871
tsepeze5aff742016-08-08 09:49:42 -0700872 CJS_Array aProp;
tsepezb4694242016-08-15 16:44:55 -0700873 vProp.GetJSValue()->ConvertToArray(pRuntime, aProp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874
875 CPWL_Color crProp;
876 CPWL_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700877 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
878 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879
880 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700881 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882 vProp2.StartGetting();
883 vProp2 << arColor;
884 vProp2.StartSetting();
885 fTarget->textColor(cc, vProp2, sError);
886 }
887 }
888 }
889 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890#endif
tsepez4cf55152016-11-02 14:37:54 -0700891 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700892}
893
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
895// bCurrencyPrepend)
tsepez4cf55152016-11-02 14:37:54 -0700896bool CJS_PublicMethods::AFNumber_Keystroke(IJS_Context* cc,
897 const std::vector<CJS_Value>& params,
898 CJS_Value& vRet,
899 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700900 CJS_Context* pContext = static_cast<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)
tsepez4cf55152016-11-02 14:37:54 -0700904 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700905
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700907 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())
tsepez4cf55152016-11-02 14:37:54 -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())) {
tsepez4cf55152016-11-02 14:37:54 -0700920 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700921 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700922 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700923 }
tsepez4cf55152016-11-02 14:37:54 -0700924 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700925 }
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) {
tsepez4cf55152016-11-02 14:37:54 -0700937 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
tsepezf3dc8c62016-08-10 06:29:29 -0700943 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -0700944 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700945 if (iSepStyle < 0 || iSepStyle > 3)
946 iSepStyle = 0;
947 const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700948
thestigcf03f8e2016-05-09 12:36:18 -0700949 bool bHasSep = wstrValue.Find(cSep) != -1;
950 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
951 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952 if (bHasSep) {
tsepez4cf55152016-11-02 14:37:54 -0700953 bool& bRc = pEvent->Rc();
954 bRc = false;
955 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700956 }
tsepez4cf55152016-11-02 14:37:54 -0700957 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 continue;
959 }
thestigcf03f8e2016-05-09 12:36:18 -0700960 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961 if (bHasSign) {
tsepez4cf55152016-11-02 14:37:54 -0700962 bool& bRc = pEvent->Rc();
963 bRc = false;
964 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800966 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700967 if (i != 0) {
tsepez4cf55152016-11-02 14:37:54 -0700968 bool& bRc = pEvent->Rc();
969 bRc = false;
970 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971 }
972 if (pEvent->SelStart() != 0) {
tsepez4cf55152016-11-02 14:37:54 -0700973 bool& bRc = pEvent->Rc();
974 bRc = false;
975 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976 }
tsepez4cf55152016-11-02 14:37:54 -0700977 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 continue;
979 }
980
thestigcf03f8e2016-05-09 12:36:18 -0700981 if (!FXSYS_iswdigit(wstrChange[i])) {
tsepez4cf55152016-11-02 14:37:54 -0700982 bool& bRc = pEvent->Rc();
983 bRc = false;
984 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985 }
986 }
987
thestigcf03f8e2016-05-09 12:36:18 -0700988 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
989 CFX_WideString wpostfix;
990 if (pEvent->SelEnd() < wstrValue.GetLength())
991 wpostfix = wstrValue.Mid(pEvent->SelEnd());
992 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700993 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700994}
995
996// function AFPercent_Format(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -0700997bool CJS_PublicMethods::AFPercent_Format(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_
tsepezcd5dc852016-09-08 11:23:24 -07001002 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001003 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005
1006 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001007 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001008 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 }
1010 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001011 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012
1013 CFX_WideString& Value = pEvent->Value();
1014 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1015 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001016 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017
tsepezb4694242016-08-15 16:44:55 -07001018 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 if (iDec < 0)
1020 iDec = -iDec;
1021
tsepezb4694242016-08-15 16:44:55 -07001022 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023 if (iSepStyle < 0 || iSepStyle > 3)
1024 iSepStyle = 0;
1025
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001027 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028 dValue *= 100;
1029 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001030 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031
1032 int iDec2;
1033 int iNegative = 0;
1034 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1035 if (strValue.IsEmpty()) {
1036 dValue = 0;
1037 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1038 }
1039
1040 if (iDec2 < 0) {
1041 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1042 strValue = "0" + strValue;
1043 }
1044 iDec2 = 0;
1045 }
1046 int iMax = strValue.GetLength();
1047 if (iDec2 > iMax) {
1048 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1049 strValue += "0";
1050 }
1051 iMax = iDec2 + 1;
1052 }
dsinclair64376be2016-03-31 20:03:24 -07001053
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054 // for processing seperator style
1055 if (iDec2 < iMax) {
1056 if (iSepStyle == 0 || iSepStyle == 1) {
1057 strValue.Insert(iDec2, '.');
1058 iMax++;
1059 } else if (iSepStyle == 2 || iSepStyle == 3) {
1060 strValue.Insert(iDec2, ',');
1061 iMax++;
1062 }
1063
1064 if (iDec2 == 0)
1065 strValue.Insert(iDec2, '0');
1066 }
1067 if (iSepStyle == 0 || iSepStyle == 2) {
1068 char cSeperator;
1069 if (iSepStyle == 0)
1070 cSeperator = ',';
1071 else
1072 cSeperator = '.';
1073
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001074 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 strValue.Insert(iDecPositive, cSeperator);
1076 iMax++;
1077 }
1078 }
dsinclair64376be2016-03-31 20:03:24 -07001079
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080 // negative mark
1081 if (iNegative)
1082 strValue = "-" + strValue;
1083 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001084 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085#endif
tsepez4cf55152016-11-02 14:37:54 -07001086 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087}
1088// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001089bool CJS_PublicMethods::AFPercent_Keystroke(
Lei Zhang945fdb72015-11-11 10:18:16 -08001090 IJS_Context* cc,
1091 const std::vector<CJS_Value>& params,
1092 CJS_Value& vRet,
1093 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 return AFNumber_Keystroke(cc, params, vRet, sError);
1095}
1096
1097// function AFDate_FormatEx(cFormat)
tsepez4cf55152016-11-02 14:37:54 -07001098bool CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
1099 const std::vector<CJS_Value>& params,
1100 CJS_Value& vRet,
1101 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -07001102 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001103 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105
1106 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001107 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001108 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001109 }
1110 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001111 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112
1113 CFX_WideString& val = pEvent->Value();
1114 CFX_WideString strValue = val;
1115 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001116 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117
tsepezb4694242016-08-15 16:44:55 -07001118 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001119 double dDate = 0.0f;
1120
1121 if (strValue.Find(L"GMT") != -1) {
1122 // for GMT format time
1123 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1124 dDate = MakeInterDate(strValue);
1125 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001126 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127 }
1128
1129 if (JS_PortIsNan(dDate)) {
1130 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001131 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001132 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001133 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001134 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 }
1136
1137 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001138 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139}
1140
tsepez745611b2016-04-12 16:46:34 -07001141double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001142 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001144 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145 FX_WCHAR c = strValue.GetAt(i);
1146 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001147 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 sTemp = L"";
1149 continue;
1150 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 sTemp += c;
1152 }
Tom Sepezab277682016-02-17 10:07:21 -08001153 wsArray.push_back(sTemp);
1154 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 return 0;
1156
Tom Sepez4246b002016-01-20 11:48:29 -08001157 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 sTemp = wsArray[1];
1159 if (sTemp.Compare(L"Jan") == 0)
1160 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001161 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001163 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001165 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001167 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001169 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001171 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001173 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001175 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001176 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001177 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001179 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001181 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182 nMonth = 12;
1183
tsepez4c3debb2016-04-08 12:20:38 -07001184 int nDay = FX_atof(wsArray[2].AsStringC());
1185 int nHour = FX_atof(wsArray[3].AsStringC());
1186 int nMin = FX_atof(wsArray[4].AsStringC());
1187 int nSec = FX_atof(wsArray[5].AsStringC());
1188 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1190 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001191 if (JS_PortIsNan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001192 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193
1194 return dRet;
1195}
1196
1197// AFDate_KeystrokeEx(cFormat)
tsepez4cf55152016-11-02 14:37:54 -07001198bool CJS_PublicMethods::AFDate_KeystrokeEx(IJS_Context* cc,
1199 const std::vector<CJS_Value>& params,
1200 CJS_Value& vRet,
1201 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 CJS_Context* pContext = (CJS_Context*)cc;
tsepezf3dc8c62016-08-10 06:29:29 -07001203 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001205
1206 if (params.size() != 1) {
1207 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001208 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209 }
1210
1211 if (pEvent->WillCommit()) {
1212 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001213 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214 CFX_WideString strValue = pEvent->Value();
1215 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001216 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001217
tsepezb4694242016-08-15 16:44:55 -07001218 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001219 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001220 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001221 if (bWrongFormat || JS_PortIsNan(dRet)) {
1222 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001223 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001224 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001225 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001226 pEvent->Rc() = false;
1227 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001228 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 }
tsepez4cf55152016-11-02 14:37:54 -07001230 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001231}
1232
tsepez4cf55152016-11-02 14:37:54 -07001233bool CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
1234 const std::vector<CJS_Value>& params,
1235 CJS_Value& vRet,
1236 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001238 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001239 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001240 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001241
tsepezf3dc8c62016-08-10 06:29:29 -07001242 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001243 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001244 const FX_WCHAR* cFormats[] = {L"m/d",
1245 L"m/d/yy",
1246 L"mm/dd/yy",
1247 L"mm/yy",
1248 L"d-mmm",
1249 L"d-mmm-yy",
1250 L"dd-mmm-yy",
1251 L"yy-mm-dd",
1252 L"mmm-yy",
1253 L"mmmm-yy",
1254 L"mmm d, yyyy",
1255 L"mmmm d, yyyy",
1256 L"m/d/yy h:MM tt",
1257 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001258
Lei Zhanga0f67242015-08-17 15:39:30 -07001259 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1260 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001261
Lei Zhang945fdb72015-11-11 10:18:16 -08001262 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001263 newParams.push_back(
1264 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 return AFDate_FormatEx(cc, newParams, vRet, sError);
1266}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001267
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268// AFDate_KeystrokeEx(cFormat)
tsepez4cf55152016-11-02 14:37:54 -07001269bool CJS_PublicMethods::AFDate_Keystroke(IJS_Context* cc,
1270 const std::vector<CJS_Value>& params,
1271 CJS_Value& vRet,
1272 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001273 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001274 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001275 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001276 }
1277
tsepezf3dc8c62016-08-10 06:29:29 -07001278 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001279 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280 const FX_WCHAR* cFormats[] = {L"m/d",
1281 L"m/d/yy",
1282 L"mm/dd/yy",
1283 L"mm/yy",
1284 L"d-mmm",
1285 L"d-mmm-yy",
1286 L"dd-mmm-yy",
1287 L"yy-mm-dd",
1288 L"mmm-yy",
1289 L"mmmm-yy",
1290 L"mmm d, yyyy",
1291 L"mmmm d, yyyy",
1292 L"m/d/yy h:MM tt",
1293 L"m/d/yy HH:MM"};
1294
Lei Zhanga0f67242015-08-17 15:39:30 -07001295 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1296 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297
Lei Zhang945fdb72015-11-11 10:18:16 -08001298 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001299 newParams.push_back(
1300 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1302}
1303
1304// function AFTime_Format(ptf)
tsepez4cf55152016-11-02 14:37:54 -07001305bool CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
1306 const std::vector<CJS_Value>& params,
1307 CJS_Value& vRet,
1308 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001310 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001311 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312 }
1313
tsepezf3dc8c62016-08-10 06:29:29 -07001314 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001315 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001316 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1317 L"h:MM:ss tt"};
1318
Lei Zhanga0f67242015-08-17 15:39:30 -07001319 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1320 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321
Lei Zhang945fdb72015-11-11 10:18:16 -08001322 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001323 newParams.push_back(
1324 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001325 return AFDate_FormatEx(cc, newParams, vRet, sError);
1326}
1327
tsepez4cf55152016-11-02 14:37:54 -07001328bool CJS_PublicMethods::AFTime_Keystroke(IJS_Context* cc,
1329 const std::vector<CJS_Value>& params,
1330 CJS_Value& vRet,
1331 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001332 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001333 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001334 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001335 }
1336
tsepezf3dc8c62016-08-10 06:29:29 -07001337 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001338 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339 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
tsepez4cf55152016-11-02 14:37:54 -07001351bool CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
1352 const std::vector<CJS_Value>& params,
1353 CJS_Value& vRet,
1354 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001355 return AFDate_FormatEx(cc, params, vRet, sError);
1356}
1357
tsepez4cf55152016-11-02 14:37:54 -07001358bool CJS_PublicMethods::AFTime_KeystrokeEx(IJS_Context* cc,
1359 const std::vector<CJS_Value>& params,
1360 CJS_Value& vRet,
1361 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1363}
1364
1365// function AFSpecial_Format(psf)
tsepez4cf55152016-11-02 14:37:54 -07001366bool CJS_PublicMethods::AFSpecial_Format(IJS_Context* cc,
1367 const std::vector<CJS_Value>& params,
1368 CJS_Value& vRet,
1369 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001370 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001371 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001372 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 }
1374
tsepezcd5dc852016-09-08 11:23:24 -07001375 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001378 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001379
tsepezf3dc8c62016-08-10 06:29:29 -07001380 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez4f1f41f2016-03-28 14:13:16 -07001381 CFX_WideString wsSource = pEvent->Value();
1382 CFX_WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001383 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001384 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001385 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001386 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001387 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001388 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001389 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001390 case 2:
1391 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1392 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001393 else
tsepez4f1f41f2016-03-28 14:13:16 -07001394 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001395 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001396 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001397 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001398 break;
1399 }
1400
tsepez4f1f41f2016-03-28 14:13:16 -07001401 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001402 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403}
1404
1405// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001406bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Lei Zhang945fdb72015-11-11 10:18:16 -08001407 IJS_Context* cc,
1408 const std::vector<CJS_Value>& params,
1409 CJS_Value& vRet,
1410 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001411 CJS_Context* pContext = (CJS_Context*)cc;
tsepezf3dc8c62016-08-10 06:29:29 -07001412 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001413 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1414
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001415 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001416 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001417 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001418 }
1419
1420 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001421 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422
tsepezcd5dc852016-09-08 11:23:24 -07001423 CFX_WideString& valEvent = pEvent->Value();
tsepezb4694242016-08-15 16:44:55 -07001424 CFX_WideString wstrMask = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001426 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001428 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001429 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001430 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001431
1432 FX_STRSIZE iIndexMask = 0;
1433 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1434 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001435 break;
1436 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001437
thestigcf03f8e2016-05-09 12:36:18 -07001438 if (iIndexMask != wstrMask.GetLength() ||
1439 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001440 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001441 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001442 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443 }
tsepez4cf55152016-11-02 14:37:54 -07001444 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001445 }
1446
1447 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001448 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001449 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001450
thestigcf03f8e2016-05-09 12:36:18 -07001451 CFX_WideString wChange = wideChange;
1452 FX_STRSIZE iIndexMask = pEvent->SelStart();
1453 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1454 pEvent->SelStart() - pEvent->SelEnd();
1455 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001456 AlertIfPossible(pContext,
1457 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001458 pEvent->Rc() = false;
1459 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001460 }
1461
thestigcf03f8e2016-05-09 12:36:18 -07001462 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001463 AlertIfPossible(pContext,
1464 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001465 pEvent->Rc() = false;
1466 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001467 }
1468
thestigcf03f8e2016-05-09 12:36:18 -07001469 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1470 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001471 AlertIfPossible(pContext,
1472 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001473 pEvent->Rc() = false;
1474 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475 }
thestigcf03f8e2016-05-09 12:36:18 -07001476 FX_WCHAR wMask = wstrMask[iIndexMask];
1477 if (!isReservedMaskChar(wMask))
1478 wChange.SetAt(i, wMask);
1479
1480 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001481 pEvent->Rc() = false;
1482 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001483 }
1484 iIndexMask++;
1485 }
thestigcf03f8e2016-05-09 12:36:18 -07001486 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001487 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001488}
1489
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001491bool CJS_PublicMethods::AFSpecial_Keystroke(
Lei Zhang945fdb72015-11-11 10:18:16 -08001492 IJS_Context* cc,
1493 const std::vector<CJS_Value>& params,
1494 CJS_Value& vRet,
1495 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001497 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001498 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001500
tsepezcd5dc852016-09-08 11:23:24 -07001501 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001502 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001504 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001505
thestigcf03f8e2016-05-09 12:36:18 -07001506 const char* cFormat = "";
tsepezf3dc8c62016-08-10 06:29:29 -07001507 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001508 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001509 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001510 cFormat = "99999";
1511 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001512 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001513 cFormat = "999999999";
1514 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001515 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001516 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517 cFormat = "9999999999";
1518 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001519 cFormat = "9999999";
1520 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001521 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 cFormat = "999999999";
1523 break;
1524 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001525
Lei Zhang945fdb72015-11-11 10:18:16 -08001526 std::vector<CJS_Value> params2;
thestigcf03f8e2016-05-09 12:36:18 -07001527 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001528 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001529}
1530
tsepez4cf55152016-11-02 14:37:54 -07001531bool CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
1532 const std::vector<CJS_Value>& params,
1533 CJS_Value& vRet,
1534 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001535 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001536 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001537 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001539
tsepezcd5dc852016-09-08 11:23:24 -07001540 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1541 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1542 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
1543
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001545 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001546 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001547
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001548 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001549 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001550 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001551 }
1552
1553 CFX_WideString prefix, postfix;
1554
1555 if (pEventHandler->SelStart() >= 0)
1556 prefix = swValue.Mid(0, pEventHandler->SelStart());
1557 else
1558 prefix = L"";
1559
1560 if (pEventHandler->SelEnd() >= 0 &&
1561 pEventHandler->SelEnd() <= swValue.GetLength())
1562 postfix = swValue.Mid(pEventHandler->SelEnd(),
1563 swValue.GetLength() - pEventHandler->SelEnd());
1564 else
1565 postfix = L"";
1566
tsepezf3dc8c62016-08-10 06:29:29 -07001567 vRet =
1568 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001569 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001570}
1571
tsepez4cf55152016-11-02 14:37:54 -07001572bool CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
1573 const std::vector<CJS_Value>& params,
1574 CJS_Value& vRet,
1575 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001576 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001577 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001578 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579 }
1580
tsepezcd5dc852016-09-08 11:23:24 -07001581 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001582 CFX_WideString sValue = params[0].ToCFXWideString(pRuntime);
1583 CFX_WideString sFormat = params[1].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001584
Lei Zhang9559b7a2015-12-21 11:12:20 -08001585 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001586
1587 if (JS_PortIsNan(dDate)) {
1588 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001589 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001590 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001591 AlertIfPossible((CJS_Context*)cc, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001592 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001593 }
1594
tsepezf3dc8c62016-08-10 06:29:29 -07001595 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001596 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001597}
1598
tsepez4cf55152016-11-02 14:37:54 -07001599bool CJS_PublicMethods::AFSimple(IJS_Context* cc,
1600 const std::vector<CJS_Value>& params,
1601 CJS_Value& vRet,
1602 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001603 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001604 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001605 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001606 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001607
tsepezcd5dc852016-09-08 11:23:24 -07001608 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001609 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
1610 params[0].ToCFXWideString(pRuntime).c_str(),
1611 params[1].ToDouble(pRuntime),
1612 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001613
tsepez4cf55152016-11-02 14:37:54 -07001614 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001615}
1616
tsepez4cf55152016-11-02 14:37:54 -07001617bool CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
1618 const std::vector<CJS_Value>& params,
1619 CJS_Value& vRet,
1620 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001621 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001622 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001623 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001624 }
tsepezf3dc8c62016-08-10 06:29:29 -07001625
tsepezcd5dc852016-09-08 11:23:24 -07001626 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001627 CFX_WideString ws = params[0].ToCFXWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001628 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001629 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001630 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001631 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001632 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001633 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001634}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001635
tsepez4cf55152016-11-02 14:37:54 -07001636bool CJS_PublicMethods::AFSimple_Calculate(IJS_Context* cc,
1637 const std::vector<CJS_Value>& params,
1638 CJS_Value& vRet,
1639 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001640 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001641 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001642 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001643 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001644
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001646 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001647 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001648 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001649 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001650
tsepezcd5dc852016-09-08 11:23:24 -07001651 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1652 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
dsinclair4526faf2016-10-11 10:54:49 -07001653 CPDFSDK_InterForm* pReaderInterForm =
dsinclair7cbe68e2016-10-12 11:56:23 -07001654 pContext->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001655 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001656
tsepezb4694242016-08-15 16:44:55 -07001657 CFX_WideString sFunction = params[0].ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001658 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001659
Tom Sepez67fd5df2015-10-08 12:24:19 -07001660 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001661 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001662
tsepezb4694242016-08-15 16:44:55 -07001663 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001664 CJS_Value jsValue(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001665 FieldNameArray.GetElement(pRuntime, i, jsValue);
1666 CFX_WideString wsFieldName = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001667
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1669 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1670 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001671 switch (pFormField->GetFieldType()) {
1672 case FIELDTYPE_TEXTFIELD:
1673 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001674 CFX_WideString trimmed = pFormField->GetValue();
1675 trimmed.TrimRight();
1676 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001677 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001678 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001679 case FIELDTYPE_PUSHBUTTON: {
1680 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001681 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001682 case FIELDTYPE_CHECKBOX:
1683 case FIELDTYPE_RADIOBUTTON: {
1684 dTemp = 0.0;
1685 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1686 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1687 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001688 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1689 trimmed.TrimRight();
1690 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001691 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001692 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001693 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001694 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001695 }
Tom Sepez4246b002016-01-20 11:48:29 -08001696 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001697 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001698 if (pFormField->CountSelectedItems() <= 1) {
1699 CFX_WideString trimmed = pFormField->GetValue();
1700 trimmed.TrimRight();
1701 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001702 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001703 }
1704 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001705 default:
1706 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001707 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001708
1709 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1710 wcscmp(sFunction.c_str(), L"MAX") == 0))
1711 dValue = dTemp;
1712
1713 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1714
1715 nFieldsCount++;
1716 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001717 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001718 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001719
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001720 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1721 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001722
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1724 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001725 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001726 if (pContext->GetEventHandler()->m_pValue)
tsepezb4694242016-08-15 16:44:55 -07001727 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001728
tsepez4cf55152016-11-02 14:37:54 -07001729 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001730}
1731
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001732/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001733** within the specified range. */
1734
tsepez4cf55152016-11-02 14:37:54 -07001735bool CJS_PublicMethods::AFRange_Validate(IJS_Context* cc,
1736 const std::vector<CJS_Value>& params,
1737 CJS_Value& vRet,
1738 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001739 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001740 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001741 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001742 }
tsepezcd5dc852016-09-08 11:23:24 -07001743 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1744 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1745 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001746 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001747 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001748
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001749 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001750 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001751
tsepezb4c9f3f2016-04-13 15:41:21 -07001752 double dEentValue =
1753 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001754 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001755 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001756 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001757 double dLessThan = params[3].ToDouble(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 CFX_WideString swMsg;
1759
1760 if (bGreaterThan && bLessThan) {
1761 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001762 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001763 params[1].ToCFXWideString(pRuntime).c_str(),
1764 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001765 } else if (bGreaterThan) {
1766 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001767 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001768 params[1].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001769 } else if (bLessThan) {
1770 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001771 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001772 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001773 }
1774
1775 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001776 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001777 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001778 }
tsepez4cf55152016-11-02 14:37:54 -07001779 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001780}
1781
tsepez4cf55152016-11-02 14:37:54 -07001782bool CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
1783 const std::vector<CJS_Value>& params,
1784 CJS_Value& vRet,
1785 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001786 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001787 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001788 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001789 }
1790
tsepezcd5dc852016-09-08 11:23:24 -07001791 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001792 CFX_WideString str = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001793 CFX_WideString sPart;
tsepezcd5dc852016-09-08 11:23:24 -07001794 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001795
1796 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1797 str = L"0" + str;
1798
1799 int nIndex = 0;
1800 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1801 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001802 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001803 sPart += wc;
1804 } else {
1805 if (sPart.GetLength() > 0) {
tsepezb4694242016-08-15 16:44:55 -07001806 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001807 sPart = L"";
1808 nIndex++;
1809 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001810 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001811 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001812
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001813 if (sPart.GetLength() > 0) {
tsepezb4694242016-08-15 16:44:55 -07001814 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001815 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001816
tsepezb4694242016-08-15 16:44:55 -07001817 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001818 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001819 else
tsepezf3dc8c62016-08-10 06:29:29 -07001820 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001821
tsepez4cf55152016-11-02 14:37:54 -07001822 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001823}