blob: 759fd91a4d19bd38d418e01277d7ffee032a608b [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>
Lei Zhange247ec42017-04-20 21:41:36 -070010#include <cwctype>
npm49c59282016-11-15 15:14:04 -080011#include <iomanip>
dsinclair992ecf72016-12-14 05:45:57 -080012#include <limits>
npm49c59282016-11-15 15:14:04 -080013#include <sstream>
14#include <string>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050015#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080016
dsinclair1727aee2016-09-29 13:12:56 -070017#include "core/fpdfdoc/cpdf_interform.h"
Dan Sinclaircfb19442017-04-20 13:13:04 -040018#include "core/fxcrt/fx_extension.h"
dsinclair735606d2016-10-05 15:47:02 -070019#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/cpdfsdk_interform.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040021#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040022#include "fpdfsdk/javascript/JS_Define.h"
23#include "fpdfsdk/javascript/JS_EventHandler.h"
24#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040025#include "fpdfsdk/javascript/JS_Value.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080026#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070027#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040028#include "fpdfsdk/javascript/color.h"
29#include "fpdfsdk/javascript/resource.h"
30#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
Tom Sepez04557b82017-02-16 09:43:10 -080034JSMethodSpec CJS_PublicMethods::GlobalFunctionSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -080035 {"AFNumber_Format", AFNumber_Format_static},
36 {"AFNumber_Keystroke", AFNumber_Keystroke_static},
37 {"AFPercent_Format", AFPercent_Format_static},
38 {"AFPercent_Keystroke", AFPercent_Keystroke_static},
39 {"AFDate_FormatEx", AFDate_FormatEx_static},
40 {"AFDate_KeystrokeEx", AFDate_KeystrokeEx_static},
41 {"AFDate_Format", AFDate_Format_static},
42 {"AFDate_Keystroke", AFDate_Keystroke_static},
43 {"AFTime_FormatEx", AFTime_FormatEx_static},
44 {"AFTime_KeystrokeEx", AFTime_KeystrokeEx_static},
45 {"AFTime_Format", AFTime_Format_static},
46 {"AFTime_Keystroke", AFTime_Keystroke_static},
47 {"AFSpecial_Format", AFSpecial_Format_static},
48 {"AFSpecial_Keystroke", AFSpecial_Keystroke_static},
49 {"AFSpecial_KeystrokeEx", AFSpecial_KeystrokeEx_static},
50 {"AFSimple", AFSimple_static},
51 {"AFMakeNumber", AFMakeNumber_static},
52 {"AFSimple_Calculate", AFSimple_Calculate_static},
53 {"AFRange_Validate", AFRange_Validate_static},
54 {"AFMergeChange", AFMergeChange_static},
55 {"AFParseDateEx", AFParseDateEx_static},
56 {"AFExtractNums", AFExtractNums_static},
Tom Sepez04557b82017-02-16 09:43:10 -080057 {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058
59IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
60
tsepez745611b2016-04-12 16:46:34 -070061namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062
Dan Sinclair812e96c2017-03-13 16:43:37 -040063const wchar_t* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
64 L"May", L"Jun", L"Jul", L"Aug",
65 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
Dan Sinclair812e96c2017-03-13 16:43:37 -040067const wchar_t* const fullmonths[] = {L"January", L"February", L"March",
68 L"April", L"May", L"June",
69 L"July", L"August", L"September",
70 L"October", L"November", L"December"};
tsepez745611b2016-04-12 16:46:34 -070071
72CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
73 CFX_ByteString result(pStr);
74 result.TrimLeft(' ');
75 result.TrimRight(' ');
76 return result;
77}
78
79CFX_WideString StrTrim(const CFX_WideString& pStr) {
80 CFX_WideString result(pStr);
81 result.TrimLeft(' ');
82 result.TrimRight(' ');
83 return result;
84}
85
Dan Sinclair812e96c2017-03-13 16:43:37 -040086void AlertIfPossible(CJS_EventContext* pContext, const wchar_t* swMsg) {
dsinclair8779fa82016-10-12 12:05:44 -070087 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
88 if (pFormFillEnv)
89 pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3);
tsepeze1e7bd02016-08-08 13:03:16 -070090}
91
npm49c59282016-11-15 15:14:04 -080092#if _FX_OS_ != _FX_ANDROID_
93CFX_ByteString CalculateString(double dValue,
94 int iDec,
95 int* iDec2,
96 bool* bNegative) {
97 *bNegative = dValue < 0;
98 if (*bNegative)
99 dValue = -dValue;
dsinclair992ecf72016-12-14 05:45:57 -0800100
101 // Make sure the number of precision characters will fit.
102 if (iDec > std::numeric_limits<double>::digits10)
103 iDec = std::numeric_limits<double>::digits10;
104
npm49c59282016-11-15 15:14:04 -0800105 std::stringstream ss;
106 ss << std::fixed << std::setprecision(iDec) << dValue;
107 std::string stringValue = ss.str();
108 size_t iDecimalPos = stringValue.find(".");
109 *iDec2 = iDecimalPos == std::string::npos ? stringValue.size()
110 : static_cast<int>(iDecimalPos);
111 return CFX_ByteString(stringValue.c_str());
112}
113#endif
114
tsepez745611b2016-04-12 16:46:34 -0700115} // namespace
116
117bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500118 CFX_WideString sTrim = StrTrim(str);
Dan Sinclair812e96c2017-03-13 16:43:37 -0400119 const wchar_t* pTrim = sTrim.c_str();
120 const wchar_t* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -0700121 bool bDot = false;
122 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -0700125 while ((c = *p) != L'\0') {
126 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -0700128 return false;
129 bDot = true;
130 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -0700132 return false;
133 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700135 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 p++;
138 c = *p;
Lei Zhange247ec42017-04-20 21:41:36 -0700139 if (c != L'+' && c != L'-')
Wei Li614d20a2016-03-15 13:55:12 -0700140 return false;
Lei Zhange247ec42017-04-20 21:41:36 -0700141 bKXJS = true;
142 } else if (!std::iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700143 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 }
145 p++;
146 }
147
Wei Li614d20a2016-03-15 13:55:12 -0700148 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149}
150
Wei Li614d20a2016-03-15 13:55:12 -0700151bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 switch (c_Mask) {
153 case L'9':
Lei Zhange247ec42017-04-20 21:41:36 -0700154 return !!std::iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800156 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800158 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700160 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 default:
162 return (c_Change == c_Mask);
163 }
164}
165
Wei Li614d20a2016-03-15 13:55:12 -0700166bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
168}
169
Dan Sinclair812e96c2017-03-13 16:43:37 -0400170double CJS_PublicMethods::AF_Simple(const wchar_t* sFuction,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 double dValue1,
172 double dValue2) {
173 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
174 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
175 return dValue1 + dValue2;
176 }
177 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
178 return dValue1 * dValue2;
179 }
180 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800181 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 }
183 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800184 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 }
186 return dValue1;
187}
188
Tom Sepez67fd5df2015-10-08 12:24:19 -0700189CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 CJS_Value val) {
tsepeze5aff742016-08-08 09:49:42 -0700191 CJS_Array StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 if (val.IsArrayObject()) {
tsepezb4694242016-08-15 16:44:55 -0700193 val.ConvertToArray(pRuntime, StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700194 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 }
tsepezb4694242016-08-15 16:44:55 -0700196 CFX_WideString wsStr = val.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700198 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199
200 int ch = ',';
201 int nIndex = 0;
202
203 while (*p) {
204 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800205 if (!pTemp) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700206 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700207 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700208 CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 }
Lei Zhang997de612015-11-04 18:17:53 -0800211
212 char* pSub = new char[pTemp - p + 1];
213 strncpy(pSub, p, pTemp - p);
214 *(pSub + (pTemp - p)) = '\0';
215
tsepezb4c9f3f2016-04-13 15:41:21 -0700216 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700217 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700218 CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800219 delete[] pSub;
220
221 nIndex++;
222 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 }
224 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225}
226
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500227int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 int nStart,
229 int& nSkip,
230 int nMaxStep) {
231 int nRet = 0;
232 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500233 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 if (i - nStart > 10)
235 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236
Dan Sinclair812e96c2017-03-13 16:43:37 -0400237 wchar_t c = str.GetAt(i);
Lei Zhange247ec42017-04-20 21:41:36 -0700238 if (!std::iswdigit(c))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800239 break;
240
Dan Sinclair1c915372016-03-03 17:12:58 -0500241 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800242 nSkip = i - nStart + 1;
243 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 break;
245 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248}
249
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500250CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
251 int nStart,
252 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 CFX_WideString swRet;
254 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500255 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Dan Sinclair812e96c2017-03-13 16:43:37 -0400256 wchar_t c = str.GetAt(i);
Lei Zhange247ec42017-04-20 21:41:36 -0700257 if (!std::iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800259
260 swRet += c;
261 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265}
266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800268 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 int nYear = JS_GetYearFromTime(dt);
272 int nMonth = JS_GetMonthFromTime(dt) + 1;
273 int nDay = JS_GetDayFromTime(dt);
274 int nHour = JS_GetHourFromTime(dt);
275 int nMin = JS_GetMinFromTime(dt);
276 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 int nSkip = 0;
281 int nLen = value.GetLength();
282 int nIndex = 0;
283 int i = 0;
284 while (i < nLen) {
285 if (nIndex > 2)
286 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287
Dan Sinclair812e96c2017-03-13 16:43:37 -0400288 wchar_t c = value.GetAt(i);
Lei Zhange247ec42017-04-20 21:41:36 -0700289 if (std::iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
291 i += nSkip;
292 } else {
293 i++;
294 }
295 }
296
297 if (nIndex == 2) {
298 // case2: month/day
299 // case3: day/month
300 if ((number[0] >= 1 && number[0] <= 12) &&
301 (number[1] >= 1 && number[1] <= 31)) {
302 nMonth = number[0];
303 nDay = number[1];
304 } else if ((number[0] >= 1 && number[0] <= 31) &&
305 (number[1] >= 1 && number[1] <= 12)) {
306 nDay = number[0];
307 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700308 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309
Lei Zhang9559b7a2015-12-21 11:12:20 -0800310 if (bWrongFormat)
311 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 } else if (nIndex == 3) {
313 // case1: year/month/day
314 // case2: month/day/year
315 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700316
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
318 (number[2] >= 1 && number[2] <= 31)) {
319 nYear = number[0];
320 nMonth = number[1];
321 nDay = number[2];
322 } else if ((number[0] >= 1 && number[0] <= 12) &&
323 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
324 nMonth = number[0];
325 nDay = number[1];
326 nYear = number[2];
327 } else if ((number[0] >= 1 && number[0] <= 31) &&
328 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
329 nDay = number[0];
330 nMonth = number[1];
331 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700332 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333
Lei Zhang9559b7a2015-12-21 11:12:20 -0800334 if (bWrongFormat)
335 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800337 if (bWrongFormat)
338 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339 return dt;
340 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700341
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 CFX_WideString swTemp;
343 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700344 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700345}
346
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
348 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800349 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 if (format.IsEmpty() || value.IsEmpty())
353 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700354
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 int nYear = JS_GetYearFromTime(dt);
356 int nMonth = JS_GetMonthFromTime(dt) + 1;
357 int nDay = JS_GetDayFromTime(dt);
358 int nHour = JS_GetHourFromTime(dt);
359 int nMin = JS_GetMinFromTime(dt);
360 int nSec = JS_GetSecFromTime(dt);
361
362 int nYearSub = 99; // nYear - 2000;
363
tsepez4cf55152016-11-02 14:37:54 -0700364 bool bPm = false;
365 bool bExit = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800366 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367
368 int i = 0;
369 int j = 0;
370
371 while (i < format.GetLength()) {
372 if (bExit)
373 break;
374
Dan Sinclair812e96c2017-03-13 16:43:37 -0400375 wchar_t c = format.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 switch (c) {
377 case ':':
378 case '.':
379 case '-':
380 case '\\':
381 case '/':
382 i++;
383 j++;
384 break;
385
386 case 'y':
387 case 'm':
388 case 'd':
389 case 'H':
390 case 'h':
391 case 'M':
392 case 's':
393 case 't': {
394 int oldj = j;
395 int nSkip = 0;
396 int remaining = format.GetLength() - i - 1;
397
398 if (remaining == 0 || format.GetAt(i + 1) != c) {
399 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700400 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 i++;
402 j++;
403 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700404 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 nMonth = ParseStringInteger(value, j, nSkip, 2);
406 i++;
407 j += nSkip;
408 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700409 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 nDay = ParseStringInteger(value, j, nSkip, 2);
411 i++;
412 j += nSkip;
413 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700414 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 nHour = ParseStringInteger(value, j, nSkip, 2);
416 i++;
417 j += nSkip;
418 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700419 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 nHour = ParseStringInteger(value, j, nSkip, 2);
421 i++;
422 j += nSkip;
423 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700424 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 nMin = ParseStringInteger(value, j, nSkip, 2);
426 i++;
427 j += nSkip;
428 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700429 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 nSec = ParseStringInteger(value, j, nSkip, 2);
431 i++;
432 j += nSkip;
433 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700434 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
436 i++;
437 j++;
438 break;
439 }
440 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
441 switch (c) {
442 case 'y':
443 nYear = ParseStringInteger(value, j, nSkip, 4);
444 i += 2;
445 j += nSkip;
446 break;
447 case 'm':
448 nMonth = ParseStringInteger(value, j, nSkip, 2);
449 i += 2;
450 j += nSkip;
451 break;
452 case 'd':
453 nDay = ParseStringInteger(value, j, nSkip, 2);
454 i += 2;
455 j += nSkip;
456 break;
457 case 'H':
458 nHour = ParseStringInteger(value, j, nSkip, 2);
459 i += 2;
460 j += nSkip;
461 break;
462 case 'h':
463 nHour = ParseStringInteger(value, j, nSkip, 2);
464 i += 2;
465 j += nSkip;
466 break;
467 case 'M':
468 nMin = ParseStringInteger(value, j, nSkip, 2);
469 i += 2;
470 j += nSkip;
471 break;
472 case 's':
473 nSec = ParseStringInteger(value, j, nSkip, 2);
474 i += 2;
475 j += nSkip;
476 break;
477 case 't':
478 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
479 value.GetAt(j + 1) == 'm');
480 i += 2;
481 j += 2;
482 break;
483 }
484 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
485 switch (c) {
486 case 'm': {
487 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
tsepez4cf55152016-11-02 14:37:54 -0700488 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489 for (int m = 0; m < 12; m++) {
490 if (sMonth.CompareNoCase(months[m]) == 0) {
491 nMonth = m + 1;
492 i += 3;
493 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700494 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700496 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 }
498
499 if (!bFind) {
500 nMonth = ParseStringInteger(value, j, nSkip, 3);
501 i += 3;
502 j += nSkip;
503 }
504 } break;
505 case 'y':
506 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700507 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 i += 3;
509 j += 3;
510 break;
511 }
512 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
513 switch (c) {
514 case 'y':
515 nYear = ParseStringInteger(value, j, nSkip, 4);
516 j += nSkip;
517 i += 4;
518 break;
519 case 'm': {
tsepez4cf55152016-11-02 14:37:54 -0700520 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521
522 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
523 sMonth.MakeLower();
524
525 for (int m = 0; m < 12; m++) {
526 CFX_WideString sFullMonths = fullmonths[m];
527 sFullMonths.MakeLower();
528
529 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
530 nMonth = m + 1;
531 i += 4;
532 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700533 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534 break;
535 }
536 }
537
538 if (!bFind) {
539 nMonth = ParseStringInteger(value, j, nSkip, 4);
540 i += 4;
541 j += nSkip;
542 }
543 } break;
544 default:
545 i += 4;
546 j += 4;
547 break;
548 }
549 } else {
550 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800551 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700552 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 }
554 i++;
555 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700556 }
Tom Sepez85386422014-07-23 10:28:37 -0700557
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800559 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700560 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 }
562 }
563
564 break;
565 default:
566 if (value.GetLength() <= j) {
tsepez4cf55152016-11-02 14:37:54 -0700567 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800569 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700570 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571 }
572
573 i++;
574 j++;
575 break;
576 }
577 }
578
579 if (bPm)
580 nHour += 12;
581
582 if (nYear >= 0 && nYear <= nYearSub)
583 nYear += 2000;
584
585 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800586 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700587
588 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800589 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590
591 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800592 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700593
594 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800595 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596
597 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800598 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599
600 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800601 if (bBadFormat) {
602 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 } else {
604 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
605 JS_MakeTime(nHour, nMin, nSec, 0));
tsepez018935c2016-04-15 13:15:12 -0700606 if (JS_PortIsNan(dRet))
607 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 }
609
tsepez018935c2016-04-15 13:15:12 -0700610 if (JS_PortIsNan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800611 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700612
Lei Zhang9559b7a2015-12-21 11:12:20 -0800613 if (bWrongFormat)
614 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700615
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 return dRet;
617}
618
619CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
620 const CFX_WideString& format) {
621 CFX_WideString sRet = L"", sPart = L"";
622
623 int nYear = JS_GetYearFromTime(dDate);
624 int nMonth = JS_GetMonthFromTime(dDate) + 1;
625 int nDay = JS_GetDayFromTime(dDate);
626 int nHour = JS_GetHourFromTime(dDate);
627 int nMin = JS_GetMinFromTime(dDate);
628 int nSec = JS_GetSecFromTime(dDate);
629
630 int i = 0;
631 while (i < format.GetLength()) {
Dan Sinclair812e96c2017-03-13 16:43:37 -0400632 wchar_t c = format.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700633 int remaining = format.GetLength() - i - 1;
634 sPart = L"";
635 switch (c) {
636 case 'y':
637 case 'm':
638 case 'd':
639 case 'H':
640 case 'h':
641 case 'M':
642 case 's':
643 case 't':
644 if (remaining == 0 || format.GetAt(i + 1) != c) {
645 switch (c) {
646 case 'y':
647 sPart += c;
648 break;
649 case 'm':
650 sPart.Format(L"%d", nMonth);
651 break;
652 case 'd':
653 sPart.Format(L"%d", nDay);
654 break;
655 case 'H':
656 sPart.Format(L"%d", nHour);
657 break;
658 case 'h':
659 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
660 break;
661 case 'M':
662 sPart.Format(L"%d", nMin);
663 break;
664 case 's':
665 sPart.Format(L"%d", nSec);
666 break;
667 case 't':
668 sPart += nHour > 12 ? 'p' : 'a';
669 break;
670 }
671 i++;
672 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
673 switch (c) {
674 case 'y':
675 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
676 break;
677 case 'm':
678 sPart.Format(L"%02d", nMonth);
679 break;
680 case 'd':
681 sPart.Format(L"%02d", nDay);
682 break;
683 case 'H':
684 sPart.Format(L"%02d", nHour);
685 break;
686 case 'h':
687 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
688 break;
689 case 'M':
690 sPart.Format(L"%02d", nMin);
691 break;
692 case 's':
693 sPart.Format(L"%02d", nSec);
694 break;
695 case 't':
696 sPart = nHour > 12 ? L"pm" : L"am";
697 break;
698 }
699 i += 2;
700 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
701 switch (c) {
702 case 'm':
703 i += 3;
704 if (nMonth > 0 && nMonth <= 12)
705 sPart += months[nMonth - 1];
706 break;
707 default:
708 i += 3;
709 sPart += c;
710 sPart += c;
711 sPart += c;
712 break;
713 }
714 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
715 switch (c) {
716 case 'y':
717 sPart.Format(L"%04d", nYear);
718 i += 4;
719 break;
720 case 'm':
721 i += 4;
722 if (nMonth > 0 && nMonth <= 12)
723 sPart += fullmonths[nMonth - 1];
724 break;
725 default:
726 i += 4;
727 sPart += c;
728 sPart += c;
729 sPart += c;
730 sPart += c;
731 break;
732 }
733 } else {
734 i++;
735 sPart += c;
736 }
737 break;
738 default:
739 i++;
740 sPart += c;
741 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700742 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700743
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700744 sRet += sPart;
745 }
746
747 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700748}
749
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700750// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
751// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800752bool CJS_PublicMethods::AFNumber_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700753 const std::vector<CJS_Value>& params,
754 CJS_Value& vRet,
755 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700756#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 if (params.size() != 6) {
tsepezcd5dc852016-09-08 11:23:24 -0700758 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700759 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700760 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700761
Tom Sepezb1670b52017-02-16 17:01:00 -0800762 CJS_EventHandler* pEvent =
763 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700764 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700765 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700766
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767 CFX_WideString& Value = pEvent->Value();
768 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700769 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700770 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771
tsepezb4694242016-08-15 16:44:55 -0700772 int iDec = params[0].ToInt(pRuntime);
773 int iSepStyle = params[1].ToInt(pRuntime);
774 int iNegStyle = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700775 // params[3] is iCurrStyle, it's not used.
tsepezb4694242016-08-15 16:44:55 -0700776 CFX_WideString wstrCurrency = params[4].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700777 bool bCurrencyPrepend = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700778
779 if (iDec < 0)
780 iDec = -iDec;
781
782 if (iSepStyle < 0 || iSepStyle > 3)
783 iSepStyle = 0;
784
785 if (iNegStyle < 0 || iNegStyle > 3)
786 iNegStyle = 0;
787
npm49c59282016-11-15 15:14:04 -0800788 // Processing decimal places
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700789 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700790 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700791 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700792 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700793
npm49c59282016-11-15 15:14:04 -0800794 // Calculating number string
795 bool bNegative;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796 int iDec2;
npm49c59282016-11-15 15:14:04 -0800797 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700798 if (strValue.IsEmpty()) {
799 dValue = 0;
npm49c59282016-11-15 15:14:04 -0800800 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700801 if (strValue.IsEmpty()) {
802 strValue = "0";
803 iDec2 = 1;
804 }
805 }
806
npm49c59282016-11-15 15:14:04 -0800807 // Processing separator style
808 if (iDec2 < strValue.GetLength()) {
809 if (iSepStyle == 2 || iSepStyle == 3)
810 strValue.Replace(".", ",");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700811
812 if (iDec2 == 0)
813 strValue.Insert(iDec2, '0');
814 }
815 if (iSepStyle == 0 || iSepStyle == 2) {
npm49c59282016-11-15 15:14:04 -0800816 char cSeparator;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817 if (iSepStyle == 0)
npm49c59282016-11-15 15:14:04 -0800818 cSeparator = ',';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700819 else
npm49c59282016-11-15 15:14:04 -0800820 cSeparator = '.';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821
npm49c59282016-11-15 15:14:04 -0800822 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3)
823 strValue.Insert(iDecPositive, cSeparator);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700824 }
825
npm49c59282016-11-15 15:14:04 -0800826 // Processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700827 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700828
829 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700830 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831 else
thestigcf03f8e2016-05-09 12:36:18 -0700832 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700833
npm49c59282016-11-15 15:14:04 -0800834 // Processing negative style
835 if (bNegative) {
836 if (iNegStyle == 0)
thestigcf03f8e2016-05-09 12:36:18 -0700837 Value = L"-" + Value;
npm49c59282016-11-15 15:14:04 -0800838 else if (iNegStyle == 2 || iNegStyle == 3)
thestigcf03f8e2016-05-09 12:36:18 -0700839 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840 if (iNegStyle == 1 || iNegStyle == 3) {
841 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700842 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700843 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700844 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700845 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700846 vColElm = CJS_Value(pRuntime, 1);
tsepezb4694242016-08-15 16:44:55 -0700847 arColor.SetElement(pRuntime, 1, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700848 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700849 arColor.SetElement(pRuntime, 2, vColElm);
850 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851
Tom Sepez67fd5df2015-10-08 12:24:19 -0700852 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700853 vProp.StartGetting();
854 vProp << arColor;
855 vProp.StartSetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800856 fTarget->textColor(pRuntime, vProp, sError); // red
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857 }
858 }
859 } else {
860 if (iNegStyle == 1 || iNegStyle == 3) {
861 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700862 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700863 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700864 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700865 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700866 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700867 arColor.SetElement(pRuntime, 1, vColElm);
868 arColor.SetElement(pRuntime, 2, vColElm);
869 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700870
Tom Sepez67fd5df2015-10-08 12:24:19 -0700871 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872 vProp.StartGetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800873 fTarget->textColor(pRuntime, vProp, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874
tsepeze5aff742016-08-08 09:49:42 -0700875 CJS_Array aProp;
tsepezb4694242016-08-15 16:44:55 -0700876 vProp.GetJSValue()->ConvertToArray(pRuntime, aProp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877
878 CPWL_Color crProp;
879 CPWL_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700880 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
881 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882
883 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700884 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 vProp2.StartGetting();
886 vProp2 << arColor;
887 vProp2.StartSetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800888 fTarget->textColor(pRuntime, vProp2, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 }
890 }
891 }
892 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893#endif
tsepez4cf55152016-11-02 14:37:54 -0700894 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700895}
896
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
898// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800899bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700900 const std::vector<CJS_Value>& params,
901 CJS_Value& vRet,
902 CFX_WideString& sError) {
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
Tom Sepezb1670b52017-02-16 17:01:00 -0800906 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
907 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700909 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700910
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911 CFX_WideString& val = pEvent->Value();
thestigcf03f8e2016-05-09 12:36:18 -0700912 CFX_WideString& wstrChange = pEvent->Change();
913 CFX_WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700914
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 if (pEvent->WillCommit()) {
ochanga0a3bc32016-05-12 15:22:48 -0700916 CFX_WideString swTemp = StrTrim(wstrValue);
917 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700918 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700919
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700920 swTemp.Replace(L",", L".");
921 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700922 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700923 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700924 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700925 }
tsepez4cf55152016-11-02 14:37:54 -0700926 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700928
thestigcf03f8e2016-05-09 12:36:18 -0700929 CFX_WideString wstrSelected;
930 if (pEvent->SelStart() != -1) {
931 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
932 pEvent->SelEnd() - pEvent->SelStart());
933 }
934
935 bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936 if (bHasSign) {
937 // can't insert "change" in front to sign postion.
938 if (pEvent->SelStart() == 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700939 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700940 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700941 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700943
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;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400947 const wchar_t 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) {
Lei Zhange247ec42017-04-20 21:41:36 -0700953 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700954 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955 }
tsepez4cf55152016-11-02 14:37:54 -0700956 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 continue;
958 }
thestigcf03f8e2016-05-09 12:36:18 -0700959 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 if (bHasSign) {
Lei Zhange247ec42017-04-20 21:41:36 -0700961 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700962 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800964 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700965 if (i != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700966 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700967 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 }
969 if (pEvent->SelStart() != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700970 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700971 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972 }
tsepez4cf55152016-11-02 14:37:54 -0700973 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974 continue;
975 }
976
Lei Zhange247ec42017-04-20 21:41:36 -0700977 if (!std::iswdigit(wstrChange[i])) {
978 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700979 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 }
981 }
982
thestigcf03f8e2016-05-09 12:36:18 -0700983 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
984 CFX_WideString wpostfix;
985 if (pEvent->SelEnd() < wstrValue.GetLength())
986 wpostfix = wstrValue.Mid(pEvent->SelEnd());
987 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700988 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700989}
990
991// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -0800992bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700993 const std::vector<CJS_Value>& params,
994 CJS_Value& vRet,
995 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700998 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700999 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001001
1002 CJS_EventHandler* pEvent =
1003 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001005 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006
1007 CFX_WideString& Value = pEvent->Value();
1008 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1009 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001010 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011
tsepezb4694242016-08-15 16:44:55 -07001012 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013 if (iDec < 0)
1014 iDec = -iDec;
1015
tsepezb4694242016-08-15 16:44:55 -07001016 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017 if (iSepStyle < 0 || iSepStyle > 3)
1018 iSepStyle = 0;
1019
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001021 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 dValue *= 100;
1023 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001024 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025
1026 int iDec2;
1027 int iNegative = 0;
1028 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1029 if (strValue.IsEmpty()) {
1030 dValue = 0;
1031 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1032 }
1033
1034 if (iDec2 < 0) {
1035 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1036 strValue = "0" + strValue;
1037 }
1038 iDec2 = 0;
1039 }
1040 int iMax = strValue.GetLength();
1041 if (iDec2 > iMax) {
1042 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1043 strValue += "0";
1044 }
1045 iMax = iDec2 + 1;
1046 }
dsinclair64376be2016-03-31 20:03:24 -07001047
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001048 // for processing seperator style
1049 if (iDec2 < iMax) {
1050 if (iSepStyle == 0 || iSepStyle == 1) {
1051 strValue.Insert(iDec2, '.');
1052 iMax++;
1053 } else if (iSepStyle == 2 || iSepStyle == 3) {
1054 strValue.Insert(iDec2, ',');
1055 iMax++;
1056 }
1057
1058 if (iDec2 == 0)
1059 strValue.Insert(iDec2, '0');
1060 }
1061 if (iSepStyle == 0 || iSepStyle == 2) {
1062 char cSeperator;
1063 if (iSepStyle == 0)
1064 cSeperator = ',';
1065 else
1066 cSeperator = '.';
1067
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001068 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 strValue.Insert(iDecPositive, cSeperator);
1070 iMax++;
1071 }
1072 }
dsinclair64376be2016-03-31 20:03:24 -07001073
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074 // negative mark
1075 if (iNegative)
1076 strValue = "-" + strValue;
1077 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001078 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079#endif
tsepez4cf55152016-11-02 14:37:54 -07001080 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081}
1082// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001083bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001084 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001085 const std::vector<CJS_Value>& params,
1086 CJS_Value& vRet,
1087 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001088 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001089}
1090
1091// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001092bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001093 const std::vector<CJS_Value>& params,
1094 CJS_Value& vRet,
1095 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001097 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001098 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001100
1101 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1102 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001103 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001104 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105
1106 CFX_WideString& val = pEvent->Value();
1107 CFX_WideString strValue = val;
1108 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001109 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001110
tsepezb4694242016-08-15 16:44:55 -07001111 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112 double dDate = 0.0f;
1113
1114 if (strValue.Find(L"GMT") != -1) {
1115 // for GMT format time
1116 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1117 dDate = MakeInterDate(strValue);
1118 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001119 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120 }
1121
1122 if (JS_PortIsNan(dDate)) {
1123 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001124 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001125 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001126 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001127 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001128 }
1129
1130 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001131 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001132}
1133
tsepez745611b2016-04-12 16:46:34 -07001134double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001135 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001136 CFX_WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001137 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138 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)
Tom Sepezb1670b52017-02-16 17:01:00 -08001190bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001191 const std::vector<CJS_Value>& params,
1192 CJS_Value& vRet,
1193 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001194 if (params.size() != 1) {
1195 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001196 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197 }
1198
Tom Sepezb1670b52017-02-16 17:01:00 -08001199 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1200 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 if (pEvent->WillCommit()) {
1202 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001203 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001204
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001205 CFX_WideString strValue = pEvent->Value();
1206 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001207 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001208
tsepezb4694242016-08-15 16:44:55 -07001209 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001210 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001211 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 if (bWrongFormat || JS_PortIsNan(dRet)) {
1213 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001214 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001215 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001216 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001217 pEvent->Rc() = false;
1218 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001219 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 }
tsepez4cf55152016-11-02 14:37:54 -07001221 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001222}
1223
Tom Sepezb1670b52017-02-16 17:01:00 -08001224bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001225 const std::vector<CJS_Value>& params,
1226 CJS_Value& vRet,
1227 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001228 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001229 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001230 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001231 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001232
tsepezb4694242016-08-15 16:44:55 -07001233 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001234 const wchar_t* cFormats[] = {L"m/d",
1235 L"m/d/yy",
1236 L"mm/dd/yy",
1237 L"mm/yy",
1238 L"d-mmm",
1239 L"d-mmm-yy",
1240 L"dd-mmm-yy",
1241 L"yy-mm-dd",
1242 L"mmm-yy",
1243 L"mmmm-yy",
1244 L"mmm d, yyyy",
1245 L"mmmm d, yyyy",
1246 L"m/d/yy h:MM tt",
1247 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001248
Lei Zhanga0f67242015-08-17 15:39:30 -07001249 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1250 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001251
Lei Zhang945fdb72015-11-11 10:18:16 -08001252 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001253 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1254 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001255}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001256
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001257// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001258bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001259 const std::vector<CJS_Value>& params,
1260 CJS_Value& vRet,
1261 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001262 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001263 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001264 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 }
1266
tsepezb4694242016-08-15 16:44:55 -07001267 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001268 const wchar_t* cFormats[] = {L"m/d",
1269 L"m/d/yy",
1270 L"mm/dd/yy",
1271 L"mm/yy",
1272 L"d-mmm",
1273 L"d-mmm-yy",
1274 L"dd-mmm-yy",
1275 L"yy-mm-dd",
1276 L"mmm-yy",
1277 L"mmmm-yy",
1278 L"mmm d, yyyy",
1279 L"mmmm d, yyyy",
1280 L"m/d/yy h:MM tt",
1281 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001282
Lei Zhanga0f67242015-08-17 15:39:30 -07001283 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1284 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001285
Lei Zhang945fdb72015-11-11 10:18:16 -08001286 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001287 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1288 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289}
1290
1291// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001292bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001293 const std::vector<CJS_Value>& params,
1294 CJS_Value& vRet,
1295 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001297 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001298 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299 }
1300
tsepezb4694242016-08-15 16:44:55 -07001301 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001302 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1303 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001304
Lei Zhanga0f67242015-08-17 15:39:30 -07001305 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1306 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001307
Lei Zhang945fdb72015-11-11 10:18:16 -08001308 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001309 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1310 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311}
1312
Tom Sepezb1670b52017-02-16 17:01:00 -08001313bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001314 const std::vector<CJS_Value>& params,
1315 CJS_Value& vRet,
1316 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001318 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001319 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320 }
1321
tsepezb4694242016-08-15 16:44:55 -07001322 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001323 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1324 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001325
Lei Zhanga0f67242015-08-17 15:39:30 -07001326 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1327 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001328
Lei Zhang945fdb72015-11-11 10:18:16 -08001329 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001330 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1331 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001332}
1333
Tom Sepezb1670b52017-02-16 17:01:00 -08001334bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001335 const std::vector<CJS_Value>& params,
1336 CJS_Value& vRet,
1337 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001338 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339}
1340
Tom Sepezb1670b52017-02-16 17:01:00 -08001341bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001342 const std::vector<CJS_Value>& params,
1343 CJS_Value& vRet,
1344 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001345 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001346}
1347
1348// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001349bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001350 const std::vector<CJS_Value>& params,
1351 CJS_Value& vRet,
1352 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001353 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001354 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001355 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001356 }
1357
Tom Sepezb1670b52017-02-16 17:01:00 -08001358 CJS_EventHandler* pEvent =
1359 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001361 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362
tsepez4f1f41f2016-03-28 14:13:16 -07001363 CFX_WideString wsSource = pEvent->Value();
1364 CFX_WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001365 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001366 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001367 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001368 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001369 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001370 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001371 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001372 case 2:
1373 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1374 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 else
tsepez4f1f41f2016-03-28 14:13:16 -07001376 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001378 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001379 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 break;
1381 }
1382
tsepez4f1f41f2016-03-28 14:13:16 -07001383 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001384 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001385}
1386
1387// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001388bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001389 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001390 const std::vector<CJS_Value>& params,
1391 CJS_Value& vRet,
1392 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001393 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001394 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001395 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396 }
1397
Tom Sepezb1670b52017-02-16 17:01:00 -08001398 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1399 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001400 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001401 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001402
tsepezcd5dc852016-09-08 11:23:24 -07001403 CFX_WideString& valEvent = pEvent->Value();
tsepezb4694242016-08-15 16:44:55 -07001404 CFX_WideString wstrMask = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001406 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001409 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001410 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001411
1412 FX_STRSIZE iIndexMask = 0;
1413 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1414 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001415 break;
1416 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001417
thestigcf03f8e2016-05-09 12:36:18 -07001418 if (iIndexMask != wstrMask.GetLength() ||
1419 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001420 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001421 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001422 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 }
tsepez4cf55152016-11-02 14:37:54 -07001424 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 }
1426
1427 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001428 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001429 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001430
thestigcf03f8e2016-05-09 12:36:18 -07001431 CFX_WideString wChange = wideChange;
1432 FX_STRSIZE iIndexMask = pEvent->SelStart();
1433 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1434 pEvent->SelStart() - pEvent->SelEnd();
1435 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001436 AlertIfPossible(pContext,
1437 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001438 pEvent->Rc() = false;
1439 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001440 }
1441
thestigcf03f8e2016-05-09 12:36:18 -07001442 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001443 AlertIfPossible(pContext,
1444 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001445 pEvent->Rc() = false;
1446 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 }
1448
thestigcf03f8e2016-05-09 12:36:18 -07001449 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1450 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001451 AlertIfPossible(pContext,
1452 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001453 pEvent->Rc() = false;
1454 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001455 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001456 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001457 if (!isReservedMaskChar(wMask))
1458 wChange.SetAt(i, wMask);
1459
1460 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001461 pEvent->Rc() = false;
1462 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001463 }
1464 iIndexMask++;
1465 }
thestigcf03f8e2016-05-09 12:36:18 -07001466 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001467 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001468}
1469
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001470// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001471bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001472 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001473 const std::vector<CJS_Value>& params,
1474 CJS_Value& vRet,
1475 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001476 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001477 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001478 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001480
Tom Sepezb1670b52017-02-16 17:01:00 -08001481 CJS_EventHandler* pEvent =
1482 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001483 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001484 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001485
thestigcf03f8e2016-05-09 12:36:18 -07001486 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001487 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001488 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489 cFormat = "99999";
1490 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001491 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492 cFormat = "999999999";
1493 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001494 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001495 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496 cFormat = "9999999999";
1497 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498 cFormat = "9999999";
1499 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001500 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 cFormat = "999999999";
1502 break;
1503 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001504
Lei Zhang945fdb72015-11-11 10:18:16 -08001505 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001506 params2.push_back(CJS_Value(pRuntime, cFormat));
1507 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001508}
1509
Tom Sepezb1670b52017-02-16 17:01:00 -08001510bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001511 const std::vector<CJS_Value>& params,
1512 CJS_Value& vRet,
1513 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001514 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001515 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001516 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001518
Tom Sepezb1670b52017-02-16 17:01:00 -08001519 CJS_EventHandler* pEventHandler =
1520 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001521
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001523 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001524 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001525
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001526 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001527 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001528 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001529 }
1530
1531 CFX_WideString prefix, postfix;
1532
1533 if (pEventHandler->SelStart() >= 0)
1534 prefix = swValue.Mid(0, pEventHandler->SelStart());
1535 else
1536 prefix = L"";
1537
1538 if (pEventHandler->SelEnd() >= 0 &&
1539 pEventHandler->SelEnd() <= swValue.GetLength())
1540 postfix = swValue.Mid(pEventHandler->SelEnd(),
1541 swValue.GetLength() - pEventHandler->SelEnd());
1542 else
1543 postfix = L"";
1544
tsepezf3dc8c62016-08-10 06:29:29 -07001545 vRet =
1546 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001547 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001548}
1549
Tom Sepezb1670b52017-02-16 17:01:00 -08001550bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001551 const std::vector<CJS_Value>& params,
1552 CJS_Value& vRet,
1553 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001554 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001555 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001556 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001557 }
1558
tsepezb4694242016-08-15 16:44:55 -07001559 CFX_WideString sValue = params[0].ToCFXWideString(pRuntime);
1560 CFX_WideString sFormat = params[1].ToCFXWideString(pRuntime);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001561 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001562 if (JS_PortIsNan(dDate)) {
1563 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001564 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001565 sFormat.c_str());
Tom Sepezb1670b52017-02-16 17:01:00 -08001566 AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001567 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001568 }
1569
tsepezf3dc8c62016-08-10 06:29:29 -07001570 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001571 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001572}
1573
Tom Sepezb1670b52017-02-16 17:01:00 -08001574bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001575 const std::vector<CJS_Value>& params,
1576 CJS_Value& vRet,
1577 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001578 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001579 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001580 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001581 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001582
tsepezb4694242016-08-15 16:44:55 -07001583 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
1584 params[0].ToCFXWideString(pRuntime).c_str(),
1585 params[1].ToDouble(pRuntime),
1586 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001587
tsepez4cf55152016-11-02 14:37:54 -07001588 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001589}
1590
Tom Sepezb1670b52017-02-16 17:01:00 -08001591bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001592 const std::vector<CJS_Value>& params,
1593 CJS_Value& vRet,
1594 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001595 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001596 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001597 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001598 }
tsepezf3dc8c62016-08-10 06:29:29 -07001599
tsepezb4694242016-08-15 16:44:55 -07001600 CFX_WideString ws = params[0].ToCFXWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001601 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001602 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001603 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001604 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001605 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001606 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001607}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001608
Tom Sepezb1670b52017-02-16 17:01:00 -08001609bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001610 const std::vector<CJS_Value>& params,
1611 CJS_Value& vRet,
1612 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001613 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001614 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001615 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001616 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001617
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001618 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001619 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001620 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001621 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001622 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001623
dsinclair4526faf2016-10-11 10:54:49 -07001624 CPDFSDK_InterForm* pReaderInterForm =
Tom Sepezb1670b52017-02-16 17:01:00 -08001625 pRuntime->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001626 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001627
tsepezb4694242016-08-15 16:44:55 -07001628 CFX_WideString sFunction = params[0].ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001629 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001630
Tom Sepez67fd5df2015-10-08 12:24:19 -07001631 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001632 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001633
tsepezb4694242016-08-15 16:44:55 -07001634 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001635 CJS_Value jsValue(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001636 FieldNameArray.GetElement(pRuntime, i, jsValue);
1637 CFX_WideString wsFieldName = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001638
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001639 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1640 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1641 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001642 switch (pFormField->GetFieldType()) {
1643 case FIELDTYPE_TEXTFIELD:
1644 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001645 CFX_WideString trimmed = pFormField->GetValue();
1646 trimmed.TrimRight();
1647 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001648 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001649 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001650 case FIELDTYPE_PUSHBUTTON: {
1651 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001652 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001653 case FIELDTYPE_CHECKBOX:
1654 case FIELDTYPE_RADIOBUTTON: {
1655 dTemp = 0.0;
1656 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1657 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1658 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001659 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1660 trimmed.TrimRight();
1661 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001662 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001663 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001664 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001665 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001666 }
Tom Sepez4246b002016-01-20 11:48:29 -08001667 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001669 if (pFormField->CountSelectedItems() <= 1) {
1670 CFX_WideString trimmed = pFormField->GetValue();
1671 trimmed.TrimRight();
1672 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001673 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001674 }
1675 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676 default:
1677 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001678 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001679
1680 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1681 wcscmp(sFunction.c_str(), L"MAX") == 0))
1682 dValue = dTemp;
1683
1684 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1685
1686 nFieldsCount++;
1687 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001688 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001689 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001690
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001691 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1692 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001693
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001694 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1695 FXSYS_pow((double)10, (double)6);
Tom Sepezb1670b52017-02-16 17:01:00 -08001696
Tom Sepez67fd5df2015-10-08 12:24:19 -07001697 CJS_Value jsValue(pRuntime, dValue);
Tom Sepezb1670b52017-02-16 17:01:00 -08001698 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
foxit8b544ed2015-09-10 14:57:54 +08001699 if (pContext->GetEventHandler()->m_pValue)
tsepezb4694242016-08-15 16:44:55 -07001700 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001701
tsepez4cf55152016-11-02 14:37:54 -07001702 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001703}
1704
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001705/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001706** within the specified range. */
1707
Tom Sepezb1670b52017-02-16 17:01:00 -08001708bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001709 const std::vector<CJS_Value>& params,
1710 CJS_Value& vRet,
1711 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001712 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001713 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001714 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001715 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001716 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
tsepezcd5dc852016-09-08 11:23:24 -07001717 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001718 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001719 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001720
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001721 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001722 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001723
tsepezb4c9f3f2016-04-13 15:41:21 -07001724 double dEentValue =
1725 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001726 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001727 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001728 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001729 double dLessThan = params[3].ToDouble(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001730 CFX_WideString swMsg;
1731
1732 if (bGreaterThan && bLessThan) {
1733 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001734 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001735 params[1].ToCFXWideString(pRuntime).c_str(),
1736 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737 } else if (bGreaterThan) {
1738 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001739 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001740 params[1].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001741 } else if (bLessThan) {
1742 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001743 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001744 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001745 }
1746
1747 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001748 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001749 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001750 }
tsepez4cf55152016-11-02 14:37:54 -07001751 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001752}
1753
Tom Sepezb1670b52017-02-16 17:01:00 -08001754bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001755 const std::vector<CJS_Value>& params,
1756 CJS_Value& vRet,
1757 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001759 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001760 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001761 }
1762
tsepezb4694242016-08-15 16:44:55 -07001763 CFX_WideString str = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001764 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1765 str = L"0" + str;
1766
Tom Sepezb1670b52017-02-16 17:01:00 -08001767 CFX_WideString sPart;
1768 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001769 int nIndex = 0;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001770 for (const auto& wc : str) {
Lei Zhange247ec42017-04-20 21:41:36 -07001771 if (std::iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001772 sPart += wc;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001773 } else if (sPart.GetLength() > 0) {
1774 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
1775 sPart = L"";
1776 nIndex++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001777 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001778 }
Tom Sepez3c3e2712017-04-17 15:38:19 -07001779 if (sPart.GetLength() > 0)
tsepezb4694242016-08-15 16:44:55 -07001780 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001781
tsepezb4694242016-08-15 16:44:55 -07001782 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001783 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001784 else
tsepezf3dc8c62016-08-10 06:29:29 -07001785 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001786
tsepez4cf55152016-11-02 14:37:54 -07001787 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001788}