blob: 970bbd2c750ee8b3e919438ff79692f7b0553eaf [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>
Tom Sepezdf950b82017-08-04 11:33:49 -070010#include <cmath>
Lei Zhange247ec42017-04-20 21:41:36 -070011#include <cwctype>
npm49c59282016-11-15 15:14:04 -080012#include <iomanip>
dsinclair992ecf72016-12-14 05:45:57 -080013#include <limits>
npm49c59282016-11-15 15:14:04 -080014#include <sstream>
15#include <string>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050016#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080017
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpdf_interform.h"
Dan Sinclaircfb19442017-04-20 13:13:04 -040019#include "core/fxcrt/fx_extension.h"
dsinclair735606d2016-10-05 15:47:02 -070020#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070021#include "fpdfsdk/cpdfsdk_interform.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040022#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040023#include "fpdfsdk/javascript/JS_Define.h"
24#include "fpdfsdk/javascript/JS_EventHandler.h"
25#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040026#include "fpdfsdk/javascript/JS_Value.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080027#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070028#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040029#include "fpdfsdk/javascript/color.h"
30#include "fpdfsdk/javascript/resource.h"
31#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034
Tom Sepez04557b82017-02-16 09:43:10 -080035JSMethodSpec CJS_PublicMethods::GlobalFunctionSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -080036 {"AFNumber_Format", AFNumber_Format_static},
37 {"AFNumber_Keystroke", AFNumber_Keystroke_static},
38 {"AFPercent_Format", AFPercent_Format_static},
39 {"AFPercent_Keystroke", AFPercent_Keystroke_static},
40 {"AFDate_FormatEx", AFDate_FormatEx_static},
41 {"AFDate_KeystrokeEx", AFDate_KeystrokeEx_static},
42 {"AFDate_Format", AFDate_Format_static},
43 {"AFDate_Keystroke", AFDate_Keystroke_static},
44 {"AFTime_FormatEx", AFTime_FormatEx_static},
45 {"AFTime_KeystrokeEx", AFTime_KeystrokeEx_static},
46 {"AFTime_Format", AFTime_Format_static},
47 {"AFTime_Keystroke", AFTime_Keystroke_static},
48 {"AFSpecial_Format", AFSpecial_Format_static},
49 {"AFSpecial_Keystroke", AFSpecial_Keystroke_static},
50 {"AFSpecial_KeystrokeEx", AFSpecial_KeystrokeEx_static},
51 {"AFSimple", AFSimple_static},
52 {"AFMakeNumber", AFMakeNumber_static},
53 {"AFSimple_Calculate", AFSimple_Calculate_static},
54 {"AFRange_Validate", AFRange_Validate_static},
55 {"AFMergeChange", AFMergeChange_static},
56 {"AFParseDateEx", AFParseDateEx_static},
57 {"AFExtractNums", AFExtractNums_static},
Tom Sepez04557b82017-02-16 09:43:10 -080058 {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059
60IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
61
tsepez745611b2016-04-12 16:46:34 -070062namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063
Dan Sinclair812e96c2017-03-13 16:43:37 -040064const wchar_t* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
65 L"May", L"Jun", L"Jul", L"Aug",
66 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Dan Sinclair812e96c2017-03-13 16:43:37 -040068const wchar_t* const fullmonths[] = {L"January", L"February", L"March",
69 L"April", L"May", L"June",
70 L"July", L"August", L"September",
71 L"October", L"November", L"December"};
tsepez745611b2016-04-12 16:46:34 -070072
73CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
74 CFX_ByteString result(pStr);
75 result.TrimLeft(' ');
76 result.TrimRight(' ');
77 return result;
78}
79
80CFX_WideString StrTrim(const CFX_WideString& pStr) {
81 CFX_WideString result(pStr);
82 result.TrimLeft(' ');
83 result.TrimRight(' ');
84 return result;
85}
86
Dan Sinclair812e96c2017-03-13 16:43:37 -040087void AlertIfPossible(CJS_EventContext* pContext, const wchar_t* swMsg) {
dsinclair8779fa82016-10-12 12:05:44 -070088 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
89 if (pFormFillEnv)
90 pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3);
tsepeze1e7bd02016-08-08 13:03:16 -070091}
92
npm49c59282016-11-15 15:14:04 -080093#if _FX_OS_ != _FX_ANDROID_
94CFX_ByteString CalculateString(double dValue,
95 int iDec,
96 int* iDec2,
97 bool* bNegative) {
98 *bNegative = dValue < 0;
99 if (*bNegative)
100 dValue = -dValue;
dsinclair992ecf72016-12-14 05:45:57 -0800101
102 // Make sure the number of precision characters will fit.
103 if (iDec > std::numeric_limits<double>::digits10)
104 iDec = std::numeric_limits<double>::digits10;
105
npm49c59282016-11-15 15:14:04 -0800106 std::stringstream ss;
107 ss << std::fixed << std::setprecision(iDec) << dValue;
108 std::string stringValue = ss.str();
109 size_t iDecimalPos = stringValue.find(".");
110 *iDec2 = iDecimalPos == std::string::npos ? stringValue.size()
111 : static_cast<int>(iDecimalPos);
112 return CFX_ByteString(stringValue.c_str());
113}
114#endif
115
tsepez745611b2016-04-12 16:46:34 -0700116} // namespace
117
118bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500119 CFX_WideString sTrim = StrTrim(str);
Dan Sinclair812e96c2017-03-13 16:43:37 -0400120 const wchar_t* pTrim = sTrim.c_str();
121 const wchar_t* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -0700122 bool bDot = false;
123 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -0700126 while ((c = *p) != L'\0') {
127 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -0700129 return false;
130 bDot = true;
131 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -0700133 return false;
134 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700136 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 p++;
139 c = *p;
Lei Zhange247ec42017-04-20 21:41:36 -0700140 if (c != L'+' && c != L'-')
Wei Li614d20a2016-03-15 13:55:12 -0700141 return false;
Lei Zhange247ec42017-04-20 21:41:36 -0700142 bKXJS = true;
143 } else if (!std::iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700144 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 }
146 p++;
147 }
148
Wei Li614d20a2016-03-15 13:55:12 -0700149 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150}
151
Wei Li614d20a2016-03-15 13:55:12 -0700152bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 switch (c_Mask) {
154 case L'9':
Lei Zhange247ec42017-04-20 21:41:36 -0700155 return !!std::iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800157 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800159 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700161 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 default:
163 return (c_Change == c_Mask);
164 }
165}
166
Wei Li614d20a2016-03-15 13:55:12 -0700167bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
169}
170
Dan Sinclair812e96c2017-03-13 16:43:37 -0400171double CJS_PublicMethods::AF_Simple(const wchar_t* sFuction,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 double dValue1,
173 double dValue2) {
174 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
175 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
176 return dValue1 + dValue2;
177 }
178 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
179 return dValue1 * dValue2;
180 }
181 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800182 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 }
184 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800185 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 }
187 return dValue1;
188}
189
Tom Sepez67fd5df2015-10-08 12:24:19 -0700190CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 CJS_Value val) {
tsepeze5aff742016-08-08 09:49:42 -0700192 CJS_Array StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 if (val.IsArrayObject()) {
tsepezb4694242016-08-15 16:44:55 -0700194 val.ConvertToArray(pRuntime, StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700195 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 }
tsepezb4694242016-08-15 16:44:55 -0700197 CFX_WideString wsStr = val.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700199 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200
201 int ch = ',';
202 int nIndex = 0;
203
204 while (*p) {
205 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800206 if (!pTemp) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700207 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700208 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700209 CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 }
Lei Zhang997de612015-11-04 18:17:53 -0800212
213 char* pSub = new char[pTemp - p + 1];
214 strncpy(pSub, p, pTemp - p);
215 *(pSub + (pTemp - p)) = '\0';
216
tsepezb4c9f3f2016-04-13 15:41:21 -0700217 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700218 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700219 CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800220 delete[] pSub;
221
222 nIndex++;
223 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 }
225 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226}
227
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500228int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Ryan Harrison75584142017-09-01 13:30:19 -0400229 FX_STRSIZE nStart,
230 FX_STRSIZE& nSkip,
231 FX_STRSIZE nMaxStep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 int nRet = 0;
233 nSkip = 0;
Ryan Harrison75584142017-09-01 13:30:19 -0400234 for (FX_STRSIZE i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 if (i - nStart > 10)
236 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400238 wchar_t c = str[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700239 if (!std::iswdigit(c))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800240 break;
241
Lei Zhange8c1d412017-05-04 12:13:55 -0700242 nRet = nRet * 10 + FXSYS_DecimalCharToInt(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800243 nSkip = i - nStart + 1;
244 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 break;
246 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249}
250
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500251CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
Ryan Harrison75584142017-09-01 13:30:19 -0400252 FX_STRSIZE nStart,
253 FX_STRSIZE& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 CFX_WideString swRet;
255 nSkip = 0;
Ryan Harrison75584142017-09-01 13:30:19 -0400256 for (FX_STRSIZE i = nStart, sz = str.GetLength(); i < sz; i++) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400257 wchar_t c = str[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700258 if (!std::iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800260
261 swRet += c;
262 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266}
267
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800269 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 int nYear = JS_GetYearFromTime(dt);
273 int nMonth = JS_GetMonthFromTime(dt) + 1;
274 int nDay = JS_GetDayFromTime(dt);
275 int nHour = JS_GetHourFromTime(dt);
276 int nMin = JS_GetMinFromTime(dt);
277 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
Ryan Harrison75584142017-09-01 13:30:19 -0400281 FX_STRSIZE nSkip = 0;
282 FX_STRSIZE nLen = value.GetLength();
283 FX_STRSIZE nIndex = 0;
284 FX_STRSIZE i = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 while (i < nLen) {
286 if (nIndex > 2)
287 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400289 wchar_t c = value[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700290 if (std::iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
292 i += nSkip;
293 } else {
294 i++;
295 }
296 }
297
298 if (nIndex == 2) {
299 // case2: month/day
300 // case3: day/month
301 if ((number[0] >= 1 && number[0] <= 12) &&
302 (number[1] >= 1 && number[1] <= 31)) {
303 nMonth = number[0];
304 nDay = number[1];
305 } else if ((number[0] >= 1 && number[0] <= 31) &&
306 (number[1] >= 1 && number[1] <= 12)) {
307 nDay = number[0];
308 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700309 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310
Lei Zhang9559b7a2015-12-21 11:12:20 -0800311 if (bWrongFormat)
312 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 } else if (nIndex == 3) {
314 // case1: year/month/day
315 // case2: month/day/year
316 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
319 (number[2] >= 1 && number[2] <= 31)) {
320 nYear = number[0];
321 nMonth = number[1];
322 nDay = number[2];
323 } else if ((number[0] >= 1 && number[0] <= 12) &&
324 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
325 nMonth = number[0];
326 nDay = number[1];
327 nYear = number[2];
328 } else if ((number[0] >= 1 && number[0] <= 31) &&
329 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
330 nDay = number[0];
331 nMonth = number[1];
332 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700333 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334
Lei Zhang9559b7a2015-12-21 11:12:20 -0800335 if (bWrongFormat)
336 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800338 if (bWrongFormat)
339 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 return dt;
341 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 CFX_WideString swTemp;
344 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700345 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346}
347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
349 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800350 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 if (format.IsEmpty() || value.IsEmpty())
354 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 int nYear = JS_GetYearFromTime(dt);
357 int nMonth = JS_GetMonthFromTime(dt) + 1;
358 int nDay = JS_GetDayFromTime(dt);
359 int nHour = JS_GetHourFromTime(dt);
360 int nMin = JS_GetMinFromTime(dt);
361 int nSec = JS_GetSecFromTime(dt);
362
363 int nYearSub = 99; // nYear - 2000;
364
tsepez4cf55152016-11-02 14:37:54 -0700365 bool bPm = false;
366 bool bExit = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800367 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368
Ryan Harrisonaa3a9cd2017-08-29 16:39:44 -0400369 FX_STRSIZE i = 0;
Ryan Harrison75584142017-09-01 13:30:19 -0400370 FX_STRSIZE j = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371
372 while (i < format.GetLength()) {
373 if (bExit)
374 break;
375
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400376 wchar_t c = format[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 switch (c) {
378 case ':':
379 case '.':
380 case '-':
381 case '\\':
382 case '/':
383 i++;
384 j++;
385 break;
386
387 case 'y':
388 case 'm':
389 case 'd':
390 case 'H':
391 case 'h':
392 case 'M':
393 case 's':
394 case 't': {
Ryan Harrison75584142017-09-01 13:30:19 -0400395 FX_STRSIZE oldj = j;
396 FX_STRSIZE nSkip = 0;
Ryan Harrisonaa3a9cd2017-08-29 16:39:44 -0400397 FX_STRSIZE remaining = format.GetLength() - i - 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400399 if (remaining == 0 || format[i + 1] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700401 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 i++;
403 j++;
404 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700405 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 nMonth = ParseStringInteger(value, j, nSkip, 2);
407 i++;
408 j += nSkip;
409 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700410 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 nDay = ParseStringInteger(value, j, nSkip, 2);
412 i++;
413 j += nSkip;
414 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700415 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 nHour = ParseStringInteger(value, j, nSkip, 2);
417 i++;
418 j += nSkip;
419 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700420 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 nHour = ParseStringInteger(value, j, nSkip, 2);
422 i++;
423 j += nSkip;
424 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700425 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 nMin = ParseStringInteger(value, j, nSkip, 2);
427 i++;
428 j += nSkip;
429 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700430 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 nSec = ParseStringInteger(value, j, nSkip, 2);
432 i++;
433 j += nSkip;
434 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700435 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400436 bPm = (j < value.GetLength() && value[j] == 'p');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 i++;
438 j++;
439 break;
440 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400441 } else if (remaining == 1 || format[i + 2] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 switch (c) {
443 case 'y':
444 nYear = ParseStringInteger(value, j, nSkip, 4);
445 i += 2;
446 j += nSkip;
447 break;
448 case 'm':
449 nMonth = ParseStringInteger(value, j, nSkip, 2);
450 i += 2;
451 j += nSkip;
452 break;
453 case 'd':
454 nDay = ParseStringInteger(value, j, nSkip, 2);
455 i += 2;
456 j += nSkip;
457 break;
458 case 'H':
459 nHour = ParseStringInteger(value, j, nSkip, 2);
460 i += 2;
461 j += nSkip;
462 break;
463 case 'h':
464 nHour = ParseStringInteger(value, j, nSkip, 2);
465 i += 2;
466 j += nSkip;
467 break;
468 case 'M':
469 nMin = ParseStringInteger(value, j, nSkip, 2);
470 i += 2;
471 j += nSkip;
472 break;
473 case 's':
474 nSec = ParseStringInteger(value, j, nSkip, 2);
475 i += 2;
476 j += nSkip;
477 break;
478 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400479 bPm = (j + 1 < value.GetLength() && value[j] == 'p' &&
480 value[j + 1] == 'm');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 i += 2;
482 j += 2;
483 break;
484 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400485 } else if (remaining == 2 || format[i + 3] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 switch (c) {
487 case 'm': {
488 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
tsepez4cf55152016-11-02 14:37:54 -0700489 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 for (int m = 0; m < 12; m++) {
491 if (sMonth.CompareNoCase(months[m]) == 0) {
492 nMonth = m + 1;
493 i += 3;
494 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700495 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700497 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 }
499
500 if (!bFind) {
501 nMonth = ParseStringInteger(value, j, nSkip, 3);
502 i += 3;
503 j += nSkip;
504 }
505 } break;
506 case 'y':
507 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700508 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 i += 3;
510 j += 3;
511 break;
512 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400513 } else if (remaining == 3 || format[i + 4] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514 switch (c) {
515 case 'y':
516 nYear = ParseStringInteger(value, j, nSkip, 4);
517 j += nSkip;
518 i += 4;
519 break;
520 case 'm': {
tsepez4cf55152016-11-02 14:37:54 -0700521 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522
523 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
524 sMonth.MakeLower();
525
526 for (int m = 0; m < 12; m++) {
527 CFX_WideString sFullMonths = fullmonths[m];
528 sFullMonths.MakeLower();
529
Ryan Harrison12db7512017-08-23 10:39:35 -0400530 if (sFullMonths.Contains(sMonth.c_str())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700531 nMonth = m + 1;
532 i += 4;
533 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700534 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535 break;
536 }
537 }
538
539 if (!bFind) {
540 nMonth = ParseStringInteger(value, j, nSkip, 4);
541 i += 4;
542 j += nSkip;
543 }
544 } break;
545 default:
546 i += 4;
547 j += 4;
548 break;
549 }
550 } else {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400551 if (j >= value.GetLength() || format[i] != value[j]) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800552 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700553 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554 }
555 i++;
556 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700557 }
Tom Sepez85386422014-07-23 10:28:37 -0700558
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800560 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700561 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562 }
563 }
564
565 break;
566 default:
567 if (value.GetLength() <= j) {
tsepez4cf55152016-11-02 14:37:54 -0700568 bExit = true;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400569 } else if (format[i] != value[j]) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800570 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700571 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572 }
573
574 i++;
575 j++;
576 break;
577 }
578 }
579
580 if (bPm)
581 nHour += 12;
582
583 if (nYear >= 0 && nYear <= nYearSub)
584 nYear += 2000;
585
586 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800587 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588
589 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800590 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591
592 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800593 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594
595 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800596 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700597
598 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800599 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600
601 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800602 if (bBadFormat) {
603 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 } else {
605 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
606 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -0700607 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -0700608 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 }
610
Tom Sepezdf950b82017-08-04 11:33:49 -0700611 if (std::isnan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800612 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613
Lei Zhang9559b7a2015-12-21 11:12:20 -0800614 if (bWrongFormat)
615 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700616
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 return dRet;
618}
619
620CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
621 const CFX_WideString& format) {
622 CFX_WideString sRet = L"", sPart = L"";
623
624 int nYear = JS_GetYearFromTime(dDate);
625 int nMonth = JS_GetMonthFromTime(dDate) + 1;
626 int nDay = JS_GetDayFromTime(dDate);
627 int nHour = JS_GetHourFromTime(dDate);
628 int nMin = JS_GetMinFromTime(dDate);
629 int nSec = JS_GetSecFromTime(dDate);
630
Ryan Harrisonaa3a9cd2017-08-29 16:39:44 -0400631 FX_STRSIZE i = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632 while (i < format.GetLength()) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400633 wchar_t c = format[i];
Ryan Harrisonaa3a9cd2017-08-29 16:39:44 -0400634 FX_STRSIZE remaining = format.GetLength() - i - 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700635 sPart = L"";
636 switch (c) {
637 case 'y':
638 case 'm':
639 case 'd':
640 case 'H':
641 case 'h':
642 case 'M':
643 case 's':
644 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400645 if (remaining == 0 || format[i + 1] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646 switch (c) {
647 case 'y':
648 sPart += c;
649 break;
650 case 'm':
651 sPart.Format(L"%d", nMonth);
652 break;
653 case 'd':
654 sPart.Format(L"%d", nDay);
655 break;
656 case 'H':
657 sPart.Format(L"%d", nHour);
658 break;
659 case 'h':
660 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
661 break;
662 case 'M':
663 sPart.Format(L"%d", nMin);
664 break;
665 case 's':
666 sPart.Format(L"%d", nSec);
667 break;
668 case 't':
669 sPart += nHour > 12 ? 'p' : 'a';
670 break;
671 }
672 i++;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400673 } else if (remaining == 1 || format[i + 2] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700674 switch (c) {
675 case 'y':
676 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
677 break;
678 case 'm':
679 sPart.Format(L"%02d", nMonth);
680 break;
681 case 'd':
682 sPart.Format(L"%02d", nDay);
683 break;
684 case 'H':
685 sPart.Format(L"%02d", nHour);
686 break;
687 case 'h':
688 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
689 break;
690 case 'M':
691 sPart.Format(L"%02d", nMin);
692 break;
693 case 's':
694 sPart.Format(L"%02d", nSec);
695 break;
696 case 't':
697 sPart = nHour > 12 ? L"pm" : L"am";
698 break;
699 }
700 i += 2;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400701 } else if (remaining == 2 || format[i + 3] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700702 switch (c) {
703 case 'm':
704 i += 3;
705 if (nMonth > 0 && nMonth <= 12)
706 sPart += months[nMonth - 1];
707 break;
708 default:
709 i += 3;
710 sPart += c;
711 sPart += c;
712 sPart += c;
713 break;
714 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400715 } else if (remaining == 3 || format[i + 4] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700716 switch (c) {
717 case 'y':
718 sPart.Format(L"%04d", nYear);
719 i += 4;
720 break;
721 case 'm':
722 i += 4;
723 if (nMonth > 0 && nMonth <= 12)
724 sPart += fullmonths[nMonth - 1];
725 break;
726 default:
727 i += 4;
728 sPart += c;
729 sPart += c;
730 sPart += c;
731 sPart += c;
732 break;
733 }
734 } else {
735 i++;
736 sPart += c;
737 }
738 break;
739 default:
740 i++;
741 sPart += c;
742 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700743 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700744
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745 sRet += sPart;
746 }
747
748 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700749}
750
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
752// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800753bool CJS_PublicMethods::AFNumber_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700754 const std::vector<CJS_Value>& params,
755 CJS_Value& vRet,
756 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700757#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758 if (params.size() != 6) {
tsepezcd5dc852016-09-08 11:23:24 -0700759 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700760 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700761 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700762
Tom Sepezb1670b52017-02-16 17:01:00 -0800763 CJS_EventHandler* pEvent =
764 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700766 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700767
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700768 CFX_WideString& Value = pEvent->Value();
769 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700771 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700772
tsepezb4694242016-08-15 16:44:55 -0700773 int iDec = params[0].ToInt(pRuntime);
774 int iSepStyle = params[1].ToInt(pRuntime);
775 int iNegStyle = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776 // params[3] is iCurrStyle, it's not used.
tsepezb4694242016-08-15 16:44:55 -0700777 CFX_WideString wstrCurrency = params[4].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700778 bool bCurrencyPrepend = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700779
780 if (iDec < 0)
781 iDec = -iDec;
782
783 if (iSepStyle < 0 || iSepStyle > 3)
784 iSepStyle = 0;
785
786 if (iNegStyle < 0 || iNegStyle > 3)
787 iNegStyle = 0;
788
npm49c59282016-11-15 15:14:04 -0800789 // Processing decimal places
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700790 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700791 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700793 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794
npm49c59282016-11-15 15:14:04 -0800795 // Calculating number string
796 bool bNegative;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797 int iDec2;
npm49c59282016-11-15 15:14:04 -0800798 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700799 if (strValue.IsEmpty()) {
800 dValue = 0;
npm49c59282016-11-15 15:14:04 -0800801 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700802 if (strValue.IsEmpty()) {
803 strValue = "0";
804 iDec2 = 1;
805 }
806 }
807
npm49c59282016-11-15 15:14:04 -0800808 // Processing separator style
Ryan Harrison75584142017-09-01 13:30:19 -0400809 if (static_cast<FX_STRSIZE>(iDec2) < strValue.GetLength()) {
npm49c59282016-11-15 15:14:04 -0800810 if (iSepStyle == 2 || iSepStyle == 3)
811 strValue.Replace(".", ",");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700812
813 if (iDec2 == 0)
814 strValue.Insert(iDec2, '0');
815 }
816 if (iSepStyle == 0 || iSepStyle == 2) {
npm49c59282016-11-15 15:14:04 -0800817 char cSeparator;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 if (iSepStyle == 0)
npm49c59282016-11-15 15:14:04 -0800819 cSeparator = ',';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820 else
npm49c59282016-11-15 15:14:04 -0800821 cSeparator = '.';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822
npm49c59282016-11-15 15:14:04 -0800823 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3)
824 strValue.Insert(iDecPositive, cSeparator);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825 }
826
npm49c59282016-11-15 15:14:04 -0800827 // Processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700828 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829
830 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700831 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832 else
thestigcf03f8e2016-05-09 12:36:18 -0700833 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700834
npm49c59282016-11-15 15:14:04 -0800835 // Processing negative style
836 if (bNegative) {
837 if (iNegStyle == 0)
thestigcf03f8e2016-05-09 12:36:18 -0700838 Value = L"-" + Value;
npm49c59282016-11-15 15:14:04 -0800839 else if (iNegStyle == 2 || iNegStyle == 3)
thestigcf03f8e2016-05-09 12:36:18 -0700840 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700841 if (iNegStyle == 1 || iNegStyle == 3) {
842 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700843 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700844 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700845 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700846 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700847 vColElm = CJS_Value(pRuntime, 1);
tsepezb4694242016-08-15 16:44:55 -0700848 arColor.SetElement(pRuntime, 1, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700849 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700850 arColor.SetElement(pRuntime, 2, vColElm);
851 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852
Tom Sepez67fd5df2015-10-08 12:24:19 -0700853 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 vProp.StartGetting();
855 vProp << arColor;
856 vProp.StartSetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800857 fTarget->textColor(pRuntime, vProp, sError); // red
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858 }
859 }
860 } else {
861 if (iNegStyle == 1 || iNegStyle == 3) {
862 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700863 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700864 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700865 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700866 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700867 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700868 arColor.SetElement(pRuntime, 1, vColElm);
869 arColor.SetElement(pRuntime, 2, vColElm);
870 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871
Tom Sepez67fd5df2015-10-08 12:24:19 -0700872 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873 vProp.StartGetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800874 fTarget->textColor(pRuntime, vProp, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875
tsepeze5aff742016-08-08 09:49:42 -0700876 CJS_Array aProp;
tsepezb4694242016-08-15 16:44:55 -0700877 vProp.GetJSValue()->ConvertToArray(pRuntime, aProp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878
Dan Sinclair7f55a542017-07-13 14:17:10 -0400879 CFX_Color crProp;
880 CFX_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700881 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
882 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883
884 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700885 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700886 vProp2.StartGetting();
887 vProp2 << arColor;
888 vProp2.StartSetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800889 fTarget->textColor(pRuntime, vProp2, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890 }
891 }
892 }
893 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894#endif
tsepez4cf55152016-11-02 14:37:54 -0700895 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700896}
897
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
899// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800900bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700901 const std::vector<CJS_Value>& params,
902 CJS_Value& vRet,
903 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700904 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700905 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700906
Tom Sepezb1670b52017-02-16 17:01:00 -0800907 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
908 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700909 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700910 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700911
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912 CFX_WideString& val = pEvent->Value();
thestigcf03f8e2016-05-09 12:36:18 -0700913 CFX_WideString& wstrChange = pEvent->Change();
914 CFX_WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700915
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 if (pEvent->WillCommit()) {
ochanga0a3bc32016-05-12 15:22:48 -0700917 CFX_WideString swTemp = StrTrim(wstrValue);
918 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700919 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700920
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 swTemp.Replace(L",", L".");
922 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700923 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700924 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700925 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700926 }
tsepez4cf55152016-11-02 14:37:54 -0700927 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700928 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700929
thestigcf03f8e2016-05-09 12:36:18 -0700930 CFX_WideString wstrSelected;
931 if (pEvent->SelStart() != -1) {
932 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
933 pEvent->SelEnd() - pEvent->SelStart());
934 }
935
Ryan Harrison12db7512017-08-23 10:39:35 -0400936 bool bHasSign = wstrValue.Contains(L'-') && !wstrSelected.Contains(L'-');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 if (bHasSign) {
938 // can't insert "change" in front to sign postion.
939 if (pEvent->SelStart() == 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700940 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700941 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700942 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700944
tsepezb4694242016-08-15 16:44:55 -0700945 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700946 if (iSepStyle < 0 || iSepStyle > 3)
947 iSepStyle = 0;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400948 const wchar_t cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700949
Ryan Harrison12db7512017-08-23 10:39:35 -0400950 bool bHasSep = wstrValue.Contains(cSep);
thestigcf03f8e2016-05-09 12:36:18 -0700951 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
952 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 if (bHasSep) {
Lei Zhange247ec42017-04-20 21:41:36 -0700954 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700955 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) {
Lei Zhange247ec42017-04-20 21:41:36 -0700962 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700963 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800965 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700966 if (i != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700967 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700968 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700969 }
970 if (pEvent->SelStart() != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700971 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700972 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 }
tsepez4cf55152016-11-02 14:37:54 -0700974 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 continue;
976 }
977
Lei Zhange247ec42017-04-20 21:41:36 -0700978 if (!std::iswdigit(wstrChange[i])) {
979 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700980 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981 }
982 }
983
Ryan Harrisone7a99de2017-07-28 14:07:04 -0400984 CFX_WideString wprefix = wstrValue.Left(pEvent->SelStart());
thestigcf03f8e2016-05-09 12:36:18 -0700985 CFX_WideString wpostfix;
Ryan Harrison75584142017-09-01 13:30:19 -0400986 if (pEvent->SelEnd() >= 0 &&
987 static_cast<FX_STRSIZE>(pEvent->SelEnd()) < wstrValue.GetLength())
988 wpostfix = wstrValue.Right(wstrValue.GetLength() -
989 static_cast<FX_STRSIZE>(pEvent->SelEnd()));
thestigcf03f8e2016-05-09 12:36:18 -0700990 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700991 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992}
993
994// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -0800995bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700996 const std::vector<CJS_Value>& params,
997 CJS_Value& vRet,
998 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001001 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001002 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001004
1005 CJS_EventHandler* pEvent =
1006 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001008 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009
1010 CFX_WideString& Value = pEvent->Value();
1011 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1012 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001013 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014
tsepezb4694242016-08-15 16:44:55 -07001015 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016 if (iDec < 0)
1017 iDec = -iDec;
1018
tsepezb4694242016-08-15 16:44:55 -07001019 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 if (iSepStyle < 0 || iSepStyle > 3)
1021 iSepStyle = 0;
1022
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001024 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025 dValue *= 100;
1026 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001027 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028
1029 int iDec2;
1030 int iNegative = 0;
1031 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1032 if (strValue.IsEmpty()) {
1033 dValue = 0;
1034 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1035 }
1036
1037 if (iDec2 < 0) {
1038 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1039 strValue = "0" + strValue;
1040 }
1041 iDec2 = 0;
1042 }
1043 int iMax = strValue.GetLength();
1044 if (iDec2 > iMax) {
1045 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1046 strValue += "0";
1047 }
1048 iMax = iDec2 + 1;
1049 }
dsinclair64376be2016-03-31 20:03:24 -07001050
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 // for processing seperator style
1052 if (iDec2 < iMax) {
1053 if (iSepStyle == 0 || iSepStyle == 1) {
1054 strValue.Insert(iDec2, '.');
1055 iMax++;
1056 } else if (iSepStyle == 2 || iSepStyle == 3) {
1057 strValue.Insert(iDec2, ',');
1058 iMax++;
1059 }
1060
1061 if (iDec2 == 0)
1062 strValue.Insert(iDec2, '0');
1063 }
1064 if (iSepStyle == 0 || iSepStyle == 2) {
1065 char cSeperator;
1066 if (iSepStyle == 0)
1067 cSeperator = ',';
1068 else
1069 cSeperator = '.';
1070
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001071 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072 strValue.Insert(iDecPositive, cSeperator);
1073 iMax++;
1074 }
1075 }
dsinclair64376be2016-03-31 20:03:24 -07001076
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077 // negative mark
1078 if (iNegative)
1079 strValue = "-" + strValue;
1080 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001081 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082#endif
tsepez4cf55152016-11-02 14:37:54 -07001083 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084}
1085// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001086bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001087 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001088 const std::vector<CJS_Value>& params,
1089 CJS_Value& vRet,
1090 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001091 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092}
1093
1094// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001095bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001096 const std::vector<CJS_Value>& params,
1097 CJS_Value& vRet,
1098 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001100 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001101 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001103
1104 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1105 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001107 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001108
1109 CFX_WideString& val = pEvent->Value();
1110 CFX_WideString strValue = val;
1111 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001112 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113
tsepezb4694242016-08-15 16:44:55 -07001114 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001115 double dDate = 0.0f;
1116
Ryan Harrison12db7512017-08-23 10:39:35 -04001117 if (strValue.Contains(L"GMT")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 // for GMT format time
1119 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1120 dDate = MakeInterDate(strValue);
1121 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001122 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001123 }
1124
Tom Sepezdf950b82017-08-04 11:33:49 -07001125 if (std::isnan(dDate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001127 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001128 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001129 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001130 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001131 }
1132
1133 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001134 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135}
1136
tsepez745611b2016-04-12 16:46:34 -07001137double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001138 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 CFX_WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001140 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001142 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 sTemp = L"";
1144 continue;
1145 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146 sTemp += c;
1147 }
Tom Sepezab277682016-02-17 10:07:21 -08001148 wsArray.push_back(sTemp);
1149 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 return 0;
1151
Tom Sepez4246b002016-01-20 11:48:29 -08001152 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 sTemp = wsArray[1];
1154 if (sTemp.Compare(L"Jan") == 0)
1155 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001156 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001158 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001159 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001160 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001162 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001164 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001166 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001168 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001170 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001172 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001174 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001176 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177 nMonth = 12;
1178
tsepez4c3debb2016-04-08 12:20:38 -07001179 int nDay = FX_atof(wsArray[2].AsStringC());
1180 int nHour = FX_atof(wsArray[3].AsStringC());
1181 int nMin = FX_atof(wsArray[4].AsStringC());
1182 int nSec = FX_atof(wsArray[5].AsStringC());
1183 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1185 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -07001186 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001187 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001188
1189 return dRet;
1190}
1191
1192// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001193bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001194 const std::vector<CJS_Value>& params,
1195 CJS_Value& vRet,
1196 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197 if (params.size() != 1) {
1198 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001199 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200 }
1201
Tom Sepezb1670b52017-02-16 17:01:00 -08001202 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1203 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 if (pEvent->WillCommit()) {
1205 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001206 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001207
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001208 CFX_WideString strValue = pEvent->Value();
1209 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001210 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001211
tsepezb4694242016-08-15 16:44:55 -07001212 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001213 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001214 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Tom Sepezdf950b82017-08-04 11:33:49 -07001215 if (bWrongFormat || std::isnan(dRet)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001217 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001219 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001220 pEvent->Rc() = false;
1221 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001222 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001223 }
tsepez4cf55152016-11-02 14:37:54 -07001224 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001225}
1226
Tom Sepezb1670b52017-02-16 17:01:00 -08001227bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001228 const std::vector<CJS_Value>& params,
1229 CJS_Value& vRet,
1230 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001231 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001232 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001233 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001235
tsepezb4694242016-08-15 16:44:55 -07001236 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001237 const wchar_t* cFormats[] = {L"m/d",
1238 L"m/d/yy",
1239 L"mm/dd/yy",
1240 L"mm/yy",
1241 L"d-mmm",
1242 L"d-mmm-yy",
1243 L"dd-mmm-yy",
1244 L"yy-mm-dd",
1245 L"mmm-yy",
1246 L"mmmm-yy",
1247 L"mmm d, yyyy",
1248 L"mmmm d, yyyy",
1249 L"m/d/yy h:MM tt",
1250 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001251
Lei Zhanga0f67242015-08-17 15:39:30 -07001252 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1253 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001254
Lei Zhang945fdb72015-11-11 10:18:16 -08001255 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001256 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1257 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001259
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001261bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001262 const std::vector<CJS_Value>& params,
1263 CJS_Value& vRet,
1264 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001266 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001267 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268 }
1269
tsepezb4694242016-08-15 16:44:55 -07001270 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001271 const wchar_t* cFormats[] = {L"m/d",
1272 L"m/d/yy",
1273 L"mm/dd/yy",
1274 L"mm/yy",
1275 L"d-mmm",
1276 L"d-mmm-yy",
1277 L"dd-mmm-yy",
1278 L"yy-mm-dd",
1279 L"mmm-yy",
1280 L"mmmm-yy",
1281 L"mmm d, yyyy",
1282 L"mmmm d, yyyy",
1283 L"m/d/yy h:MM tt",
1284 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001285
Lei Zhanga0f67242015-08-17 15:39:30 -07001286 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1287 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288
Lei Zhang945fdb72015-11-11 10:18:16 -08001289 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001290 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1291 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292}
1293
1294// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001295bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001296 const std::vector<CJS_Value>& params,
1297 CJS_Value& vRet,
1298 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001300 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001301 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302 }
1303
tsepezb4694242016-08-15 16:44:55 -07001304 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001305 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1306 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001307
Lei Zhanga0f67242015-08-17 15:39:30 -07001308 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1309 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001310
Lei Zhang945fdb72015-11-11 10:18:16 -08001311 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001312 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1313 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314}
1315
Tom Sepezb1670b52017-02-16 17:01:00 -08001316bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001317 const std::vector<CJS_Value>& params,
1318 CJS_Value& vRet,
1319 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001321 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001322 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001323 }
1324
tsepezb4694242016-08-15 16:44:55 -07001325 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001326 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1327 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001328
Lei Zhanga0f67242015-08-17 15:39:30 -07001329 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1330 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001331
Lei Zhang945fdb72015-11-11 10:18:16 -08001332 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001333 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1334 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001335}
1336
Tom Sepezb1670b52017-02-16 17:01:00 -08001337bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001338 const std::vector<CJS_Value>& params,
1339 CJS_Value& vRet,
1340 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001341 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001342}
1343
Tom Sepezb1670b52017-02-16 17:01:00 -08001344bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001345 const std::vector<CJS_Value>& params,
1346 CJS_Value& vRet,
1347 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001348 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001349}
1350
1351// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001352bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001353 const std::vector<CJS_Value>& params,
1354 CJS_Value& vRet,
1355 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001356 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001357 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001358 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001359 }
1360
Tom Sepezb1670b52017-02-16 17:01:00 -08001361 CJS_EventHandler* pEvent =
1362 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001364 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001365
tsepez4f1f41f2016-03-28 14:13:16 -07001366 CFX_WideString wsSource = pEvent->Value();
1367 CFX_WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001368 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001369 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001370 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001371 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001372 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001373 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001375 case 2:
1376 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1377 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 else
tsepez4f1f41f2016-03-28 14:13:16 -07001379 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001381 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001382 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383 break;
1384 }
1385
tsepez4f1f41f2016-03-28 14:13:16 -07001386 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001387 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388}
1389
1390// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001391bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001392 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001393 const std::vector<CJS_Value>& params,
1394 CJS_Value& vRet,
1395 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001397 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001398 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001399 }
1400
Tom Sepezb1670b52017-02-16 17:01:00 -08001401 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1402 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001404 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405
tsepezcd5dc852016-09-08 11:23:24 -07001406 CFX_WideString& valEvent = pEvent->Value();
tsepezb4694242016-08-15 16:44:55 -07001407 CFX_WideString wstrMask = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001409 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001410
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001411 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001412 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001413 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001414
1415 FX_STRSIZE iIndexMask = 0;
1416 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1417 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001418 break;
1419 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001420
thestigcf03f8e2016-05-09 12:36:18 -07001421 if (iIndexMask != wstrMask.GetLength() ||
1422 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001423 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001424 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001425 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001426 }
tsepez4cf55152016-11-02 14:37:54 -07001427 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001428 }
1429
1430 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001431 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001432 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001433
thestigcf03f8e2016-05-09 12:36:18 -07001434 CFX_WideString wChange = wideChange;
1435 FX_STRSIZE iIndexMask = pEvent->SelStart();
1436 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1437 pEvent->SelStart() - pEvent->SelEnd();
1438 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001439 AlertIfPossible(pContext,
1440 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001441 pEvent->Rc() = false;
1442 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443 }
1444
thestigcf03f8e2016-05-09 12:36:18 -07001445 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001446 AlertIfPossible(pContext,
1447 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001448 pEvent->Rc() = false;
1449 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001450 }
1451
thestigcf03f8e2016-05-09 12:36:18 -07001452 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1453 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001454 AlertIfPossible(pContext,
1455 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001456 pEvent->Rc() = false;
1457 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001458 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001459 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001460 if (!isReservedMaskChar(wMask))
1461 wChange.SetAt(i, wMask);
1462
1463 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001464 pEvent->Rc() = false;
1465 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001466 }
1467 iIndexMask++;
1468 }
thestigcf03f8e2016-05-09 12:36:18 -07001469 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001470 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001471}
1472
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001473// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001474bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001475 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001476 const std::vector<CJS_Value>& params,
1477 CJS_Value& vRet,
1478 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001480 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001481 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001482 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001483
Tom Sepezb1670b52017-02-16 17:01:00 -08001484 CJS_EventHandler* pEvent =
1485 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001486 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001487 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001488
thestigcf03f8e2016-05-09 12:36:18 -07001489 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001490 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001491 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492 cFormat = "99999";
1493 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001494 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495 cFormat = "999999999";
1496 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001497 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001498 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 cFormat = "9999999999";
1500 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 cFormat = "9999999";
1502 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001503 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504 cFormat = "999999999";
1505 break;
1506 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001507
Lei Zhang945fdb72015-11-11 10:18:16 -08001508 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001509 params2.push_back(CJS_Value(pRuntime, cFormat));
1510 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001511}
1512
Tom Sepezb1670b52017-02-16 17:01:00 -08001513bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001514 const std::vector<CJS_Value>& params,
1515 CJS_Value& vRet,
1516 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001518 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001519 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001520 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001521
Tom Sepezb1670b52017-02-16 17:01:00 -08001522 CJS_EventHandler* pEventHandler =
1523 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001524
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001525 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001526 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001528
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001529 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001530 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001531 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001532 }
1533
1534 CFX_WideString prefix, postfix;
1535
1536 if (pEventHandler->SelStart() >= 0)
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001537 prefix = swValue.Left(pEventHandler->SelStart());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 else
1539 prefix = L"";
1540
1541 if (pEventHandler->SelEnd() >= 0 &&
Ryan Harrison75584142017-09-01 13:30:19 -04001542 static_cast<FX_STRSIZE>(pEventHandler->SelEnd()) <= swValue.GetLength())
1543 postfix = swValue.Right(swValue.GetLength() -
1544 static_cast<FX_STRSIZE>(pEventHandler->SelEnd()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001545 else
1546 postfix = L"";
1547
tsepezf3dc8c62016-08-10 06:29:29 -07001548 vRet =
1549 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001550 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001551}
1552
Tom Sepezb1670b52017-02-16 17:01:00 -08001553bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001554 const std::vector<CJS_Value>& params,
1555 CJS_Value& vRet,
1556 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001557 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001558 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001559 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001560 }
1561
tsepezb4694242016-08-15 16:44:55 -07001562 CFX_WideString sValue = params[0].ToCFXWideString(pRuntime);
1563 CFX_WideString sFormat = params[1].ToCFXWideString(pRuntime);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001564 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Tom Sepezdf950b82017-08-04 11:33:49 -07001565 if (std::isnan(dDate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001567 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001568 sFormat.c_str());
Tom Sepezb1670b52017-02-16 17:01:00 -08001569 AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001570 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001571 }
1572
tsepezf3dc8c62016-08-10 06:29:29 -07001573 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001574 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001575}
1576
Tom Sepezb1670b52017-02-16 17:01:00 -08001577bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001578 const std::vector<CJS_Value>& params,
1579 CJS_Value& vRet,
1580 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001581 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001582 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001583 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001584 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001585
tsepezb4694242016-08-15 16:44:55 -07001586 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
1587 params[0].ToCFXWideString(pRuntime).c_str(),
1588 params[1].ToDouble(pRuntime),
1589 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001590
tsepez4cf55152016-11-02 14:37:54 -07001591 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001592}
1593
Tom Sepezb1670b52017-02-16 17:01:00 -08001594bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001595 const std::vector<CJS_Value>& params,
1596 CJS_Value& vRet,
1597 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001598 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001599 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001600 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001601 }
tsepezf3dc8c62016-08-10 06:29:29 -07001602
tsepezb4694242016-08-15 16:44:55 -07001603 CFX_WideString ws = params[0].ToCFXWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001604 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001605 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001606 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001607 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001608 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001609 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001610}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001611
Tom Sepezb1670b52017-02-16 17:01:00 -08001612bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001613 const std::vector<CJS_Value>& params,
1614 CJS_Value& vRet,
1615 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001616 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001617 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001618 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001619 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001620
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001621 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001622 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001623 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001624 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001625 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001626
dsinclair4526faf2016-10-11 10:54:49 -07001627 CPDFSDK_InterForm* pReaderInterForm =
Tom Sepezb1670b52017-02-16 17:01:00 -08001628 pRuntime->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001629 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001630
tsepezb4694242016-08-15 16:44:55 -07001631 CFX_WideString sFunction = params[0].ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001632 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001633
Tom Sepez67fd5df2015-10-08 12:24:19 -07001634 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001635 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001636
tsepezb4694242016-08-15 16:44:55 -07001637 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001638 CJS_Value jsValue(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001639 FieldNameArray.GetElement(pRuntime, i, jsValue);
1640 CFX_WideString wsFieldName = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001641
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001642 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1643 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1644 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 switch (pFormField->GetFieldType()) {
1646 case FIELDTYPE_TEXTFIELD:
1647 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001648 CFX_WideString trimmed = pFormField->GetValue();
1649 trimmed.TrimRight();
1650 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001651 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001652 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001653 case FIELDTYPE_PUSHBUTTON: {
1654 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001655 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001656 case FIELDTYPE_CHECKBOX:
1657 case FIELDTYPE_RADIOBUTTON: {
1658 dTemp = 0.0;
1659 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1660 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1661 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001662 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1663 trimmed.TrimRight();
1664 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001665 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001666 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001667 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001669 }
Tom Sepez4246b002016-01-20 11:48:29 -08001670 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001671 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001672 if (pFormField->CountSelectedItems() <= 1) {
1673 CFX_WideString trimmed = pFormField->GetValue();
1674 trimmed.TrimRight();
1675 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001676 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001677 }
1678 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001679 default:
1680 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001681 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001682
1683 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1684 wcscmp(sFunction.c_str(), L"MAX") == 0))
1685 dValue = dTemp;
1686
1687 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1688
1689 nFieldsCount++;
1690 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001691 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001692 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001693
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001694 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1695 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001696
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001697 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1698 FXSYS_pow((double)10, (double)6);
Tom Sepezb1670b52017-02-16 17:01:00 -08001699
Tom Sepez67fd5df2015-10-08 12:24:19 -07001700 CJS_Value jsValue(pRuntime, dValue);
Tom Sepezb1670b52017-02-16 17:01:00 -08001701 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
foxit8b544ed2015-09-10 14:57:54 +08001702 if (pContext->GetEventHandler()->m_pValue)
tsepezb4694242016-08-15 16:44:55 -07001703 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001704
tsepez4cf55152016-11-02 14:37:54 -07001705 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001706}
1707
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001708/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001709** within the specified range. */
1710
Tom Sepezb1670b52017-02-16 17:01:00 -08001711bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001712 const std::vector<CJS_Value>& params,
1713 CJS_Value& vRet,
1714 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001715 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001716 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001717 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001718 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001719 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
tsepezcd5dc852016-09-08 11:23:24 -07001720 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001721 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001722 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001723
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001724 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001725 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001726
tsepezb4c9f3f2016-04-13 15:41:21 -07001727 double dEentValue =
1728 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001729 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001730 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001731 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001732 double dLessThan = params[3].ToDouble(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001733 CFX_WideString swMsg;
1734
1735 if (bGreaterThan && bLessThan) {
1736 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001737 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001738 params[1].ToCFXWideString(pRuntime).c_str(),
1739 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001740 } else if (bGreaterThan) {
1741 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001742 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001743 params[1].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001744 } else if (bLessThan) {
1745 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001746 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001747 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001748 }
1749
1750 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001751 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001752 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001753 }
tsepez4cf55152016-11-02 14:37:54 -07001754 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001755}
1756
Tom Sepezb1670b52017-02-16 17:01:00 -08001757bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001758 const std::vector<CJS_Value>& params,
1759 CJS_Value& vRet,
1760 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001761 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001762 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001763 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001764 }
1765
tsepezb4694242016-08-15 16:44:55 -07001766 CFX_WideString str = params[0].ToCFXWideString(pRuntime);
Ryan Harrison75c65212017-08-16 13:52:15 -04001767 if (str.GetLength() > 0 && (str[0] == L'.' || str[0] == L','))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001768 str = L"0" + str;
1769
Tom Sepezb1670b52017-02-16 17:01:00 -08001770 CFX_WideString sPart;
1771 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001772 int nIndex = 0;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001773 for (const auto& wc : str) {
Lei Zhange247ec42017-04-20 21:41:36 -07001774 if (std::iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001775 sPart += wc;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001776 } else if (sPart.GetLength() > 0) {
1777 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
1778 sPart = L"";
1779 nIndex++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001780 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 }
Tom Sepez3c3e2712017-04-17 15:38:19 -07001782 if (sPart.GetLength() > 0)
tsepezb4694242016-08-15 16:44:55 -07001783 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001784
tsepezb4694242016-08-15 16:44:55 -07001785 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001786 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001787 else
tsepezf3dc8c62016-08-10 06:29:29 -07001788 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001789
tsepez4cf55152016-11-02 14:37:54 -07001790 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001791}