blob: ff1d4fb7b79ef9d527e99b7f7c46e97a89e59802 [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,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 int nStart,
230 int& nSkip,
231 int nMaxStep) {
232 int nRet = 0;
233 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500234 for (int 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,
252 int nStart,
253 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 CFX_WideString swRet;
255 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500256 for (int 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
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 int nSkip = 0;
282 int nLen = value.GetLength();
283 int nIndex = 0;
284 int i = 0;
285 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
369 int i = 0;
370 int j = 0;
371
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': {
395 int oldj = j;
396 int nSkip = 0;
397 int remaining = format.GetLength() - i - 1;
398
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
631 int i = 0;
632 while (i < format.GetLength()) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400633 wchar_t c = format[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 int remaining = format.GetLength() - i - 1;
635 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
809 if (iDec2 < strValue.GetLength()) {
810 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;
986 if (pEvent->SelEnd() < wstrValue.GetLength())
Ryan Harrisone7a99de2017-07-28 14:07:04 -0400987 wpostfix = wstrValue.Right(wstrValue.GetLength() - pEvent->SelEnd());
thestigcf03f8e2016-05-09 12:36:18 -0700988 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700989 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990}
991
992// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -0800993bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700994 const std::vector<CJS_Value>& params,
995 CJS_Value& vRet,
996 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700999 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001000 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001002
1003 CJS_EventHandler* pEvent =
1004 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001006 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007
1008 CFX_WideString& Value = pEvent->Value();
1009 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1010 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001011 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012
tsepezb4694242016-08-15 16:44:55 -07001013 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014 if (iDec < 0)
1015 iDec = -iDec;
1016
tsepezb4694242016-08-15 16:44:55 -07001017 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 if (iSepStyle < 0 || iSepStyle > 3)
1019 iSepStyle = 0;
1020
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001021 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001022 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023 dValue *= 100;
1024 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001025 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026
1027 int iDec2;
1028 int iNegative = 0;
1029 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1030 if (strValue.IsEmpty()) {
1031 dValue = 0;
1032 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1033 }
1034
1035 if (iDec2 < 0) {
1036 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1037 strValue = "0" + strValue;
1038 }
1039 iDec2 = 0;
1040 }
1041 int iMax = strValue.GetLength();
1042 if (iDec2 > iMax) {
1043 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1044 strValue += "0";
1045 }
1046 iMax = iDec2 + 1;
1047 }
dsinclair64376be2016-03-31 20:03:24 -07001048
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 // for processing seperator style
1050 if (iDec2 < iMax) {
1051 if (iSepStyle == 0 || iSepStyle == 1) {
1052 strValue.Insert(iDec2, '.');
1053 iMax++;
1054 } else if (iSepStyle == 2 || iSepStyle == 3) {
1055 strValue.Insert(iDec2, ',');
1056 iMax++;
1057 }
1058
1059 if (iDec2 == 0)
1060 strValue.Insert(iDec2, '0');
1061 }
1062 if (iSepStyle == 0 || iSepStyle == 2) {
1063 char cSeperator;
1064 if (iSepStyle == 0)
1065 cSeperator = ',';
1066 else
1067 cSeperator = '.';
1068
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001069 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070 strValue.Insert(iDecPositive, cSeperator);
1071 iMax++;
1072 }
1073 }
dsinclair64376be2016-03-31 20:03:24 -07001074
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 // negative mark
1076 if (iNegative)
1077 strValue = "-" + strValue;
1078 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001079 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080#endif
tsepez4cf55152016-11-02 14:37:54 -07001081 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082}
1083// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001084bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001085 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001086 const std::vector<CJS_Value>& params,
1087 CJS_Value& vRet,
1088 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001089 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001090}
1091
1092// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001093bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001094 const std::vector<CJS_Value>& params,
1095 CJS_Value& vRet,
1096 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001097 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001098 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001099 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001101
1102 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1103 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001105 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106
1107 CFX_WideString& val = pEvent->Value();
1108 CFX_WideString strValue = val;
1109 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001110 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111
tsepezb4694242016-08-15 16:44:55 -07001112 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113 double dDate = 0.0f;
1114
Ryan Harrison12db7512017-08-23 10:39:35 -04001115 if (strValue.Contains(L"GMT")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116 // for GMT format time
1117 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1118 dDate = MakeInterDate(strValue);
1119 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001120 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001121 }
1122
Tom Sepezdf950b82017-08-04 11:33:49 -07001123 if (std::isnan(dDate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001125 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001127 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001128 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001129 }
1130
1131 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001132 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133}
1134
tsepez745611b2016-04-12 16:46:34 -07001135double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001136 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001137 CFX_WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001138 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001140 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 sTemp = L"";
1142 continue;
1143 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001144 sTemp += c;
1145 }
Tom Sepezab277682016-02-17 10:07:21 -08001146 wsArray.push_back(sTemp);
1147 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 return 0;
1149
Tom Sepez4246b002016-01-20 11:48:29 -08001150 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 sTemp = wsArray[1];
1152 if (sTemp.Compare(L"Jan") == 0)
1153 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001154 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001156 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001158 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001159 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001160 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001162 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001164 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001166 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001168 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001170 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001172 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001174 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 nMonth = 12;
1176
tsepez4c3debb2016-04-08 12:20:38 -07001177 int nDay = FX_atof(wsArray[2].AsStringC());
1178 int nHour = FX_atof(wsArray[3].AsStringC());
1179 int nMin = FX_atof(wsArray[4].AsStringC());
1180 int nSec = FX_atof(wsArray[5].AsStringC());
1181 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1183 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -07001184 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001185 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186
1187 return dRet;
1188}
1189
1190// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001191bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001192 const std::vector<CJS_Value>& params,
1193 CJS_Value& vRet,
1194 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195 if (params.size() != 1) {
1196 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001197 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198 }
1199
Tom Sepezb1670b52017-02-16 17:01:00 -08001200 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1201 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 if (pEvent->WillCommit()) {
1203 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001204 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001205
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 CFX_WideString strValue = pEvent->Value();
1207 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001208 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001209
tsepezb4694242016-08-15 16:44:55 -07001210 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001211 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001212 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Tom Sepezdf950b82017-08-04 11:33:49 -07001213 if (bWrongFormat || std::isnan(dRet)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001215 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001217 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001218 pEvent->Rc() = false;
1219 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001220 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001221 }
tsepez4cf55152016-11-02 14:37:54 -07001222 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001223}
1224
Tom Sepezb1670b52017-02-16 17:01:00 -08001225bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001226 const std::vector<CJS_Value>& params,
1227 CJS_Value& vRet,
1228 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001230 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001231 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001233
tsepezb4694242016-08-15 16:44:55 -07001234 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001235 const wchar_t* cFormats[] = {L"m/d",
1236 L"m/d/yy",
1237 L"mm/dd/yy",
1238 L"mm/yy",
1239 L"d-mmm",
1240 L"d-mmm-yy",
1241 L"dd-mmm-yy",
1242 L"yy-mm-dd",
1243 L"mmm-yy",
1244 L"mmmm-yy",
1245 L"mmm d, yyyy",
1246 L"mmmm d, yyyy",
1247 L"m/d/yy h:MM tt",
1248 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001249
Lei Zhanga0f67242015-08-17 15:39:30 -07001250 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1251 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001252
Lei Zhang945fdb72015-11-11 10:18:16 -08001253 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001254 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1255 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001257
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001259bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001260 const std::vector<CJS_Value>& params,
1261 CJS_Value& vRet,
1262 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001264 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001265 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 }
1267
tsepezb4694242016-08-15 16:44:55 -07001268 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001269 const wchar_t* cFormats[] = {L"m/d",
1270 L"m/d/yy",
1271 L"mm/dd/yy",
1272 L"mm/yy",
1273 L"d-mmm",
1274 L"d-mmm-yy",
1275 L"dd-mmm-yy",
1276 L"yy-mm-dd",
1277 L"mmm-yy",
1278 L"mmmm-yy",
1279 L"mmm d, yyyy",
1280 L"mmmm d, yyyy",
1281 L"m/d/yy h:MM tt",
1282 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283
Lei Zhanga0f67242015-08-17 15:39:30 -07001284 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1285 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001286
Lei Zhang945fdb72015-11-11 10:18:16 -08001287 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001288 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1289 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001290}
1291
1292// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001293bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001294 const std::vector<CJS_Value>& params,
1295 CJS_Value& vRet,
1296 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001298 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001299 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 }
1301
tsepezb4694242016-08-15 16:44:55 -07001302 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001303 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1304 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305
Lei Zhanga0f67242015-08-17 15:39:30 -07001306 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1307 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001308
Lei Zhang945fdb72015-11-11 10:18:16 -08001309 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001310 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1311 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312}
1313
Tom Sepezb1670b52017-02-16 17:01:00 -08001314bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001315 const std::vector<CJS_Value>& params,
1316 CJS_Value& vRet,
1317 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001319 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001320 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321 }
1322
tsepezb4694242016-08-15 16:44:55 -07001323 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001324 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1325 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326
Lei Zhanga0f67242015-08-17 15:39:30 -07001327 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1328 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001329
Lei Zhang945fdb72015-11-11 10:18:16 -08001330 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001331 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1332 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001333}
1334
Tom Sepezb1670b52017-02-16 17:01:00 -08001335bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001336 const std::vector<CJS_Value>& params,
1337 CJS_Value& vRet,
1338 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001339 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001340}
1341
Tom Sepezb1670b52017-02-16 17:01:00 -08001342bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001343 const std::vector<CJS_Value>& params,
1344 CJS_Value& vRet,
1345 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001346 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001347}
1348
1349// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001350bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001351 const std::vector<CJS_Value>& params,
1352 CJS_Value& vRet,
1353 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001355 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001356 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357 }
1358
Tom Sepezb1670b52017-02-16 17:01:00 -08001359 CJS_EventHandler* pEvent =
1360 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001362 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363
tsepez4f1f41f2016-03-28 14:13:16 -07001364 CFX_WideString wsSource = pEvent->Value();
1365 CFX_WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001366 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001367 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001368 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001370 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001371 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001373 case 2:
1374 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1375 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376 else
tsepez4f1f41f2016-03-28 14:13:16 -07001377 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001379 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001380 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 break;
1382 }
1383
tsepez4f1f41f2016-03-28 14:13:16 -07001384 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001385 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001386}
1387
1388// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001389bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001390 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001391 const std::vector<CJS_Value>& params,
1392 CJS_Value& vRet,
1393 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001395 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001396 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 }
1398
Tom Sepezb1670b52017-02-16 17:01:00 -08001399 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1400 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001401 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001402 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403
tsepezcd5dc852016-09-08 11:23:24 -07001404 CFX_WideString& valEvent = pEvent->Value();
tsepezb4694242016-08-15 16:44:55 -07001405 CFX_WideString wstrMask = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001407 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001410 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001411 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001412
1413 FX_STRSIZE iIndexMask = 0;
1414 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1415 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001416 break;
1417 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001418
thestigcf03f8e2016-05-09 12:36:18 -07001419 if (iIndexMask != wstrMask.GetLength() ||
1420 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001421 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001422 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001423 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424 }
tsepez4cf55152016-11-02 14:37:54 -07001425 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001426 }
1427
1428 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001429 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001430 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431
thestigcf03f8e2016-05-09 12:36:18 -07001432 CFX_WideString wChange = wideChange;
1433 FX_STRSIZE iIndexMask = pEvent->SelStart();
1434 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1435 pEvent->SelStart() - pEvent->SelEnd();
1436 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001437 AlertIfPossible(pContext,
1438 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001439 pEvent->Rc() = false;
1440 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441 }
1442
thestigcf03f8e2016-05-09 12:36:18 -07001443 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001444 AlertIfPossible(pContext,
1445 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001446 pEvent->Rc() = false;
1447 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448 }
1449
thestigcf03f8e2016-05-09 12:36:18 -07001450 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1451 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001452 AlertIfPossible(pContext,
1453 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001454 pEvent->Rc() = false;
1455 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001456 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001457 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001458 if (!isReservedMaskChar(wMask))
1459 wChange.SetAt(i, wMask);
1460
1461 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001462 pEvent->Rc() = false;
1463 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001464 }
1465 iIndexMask++;
1466 }
thestigcf03f8e2016-05-09 12:36:18 -07001467 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001468 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001469}
1470
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001471// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001472bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001473 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001474 const std::vector<CJS_Value>& params,
1475 CJS_Value& vRet,
1476 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001478 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001479 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001481
Tom Sepezb1670b52017-02-16 17:01:00 -08001482 CJS_EventHandler* pEvent =
1483 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001484 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001485 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001486
thestigcf03f8e2016-05-09 12:36:18 -07001487 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001488 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001489 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 cFormat = "99999";
1491 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001492 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001493 cFormat = "999999999";
1494 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001495 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001496 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001497 cFormat = "9999999999";
1498 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 cFormat = "9999999";
1500 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001501 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502 cFormat = "999999999";
1503 break;
1504 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001505
Lei Zhang945fdb72015-11-11 10:18:16 -08001506 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001507 params2.push_back(CJS_Value(pRuntime, cFormat));
1508 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001509}
1510
Tom Sepezb1670b52017-02-16 17:01:00 -08001511bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001512 const std::vector<CJS_Value>& params,
1513 CJS_Value& vRet,
1514 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001516 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001517 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001519
Tom Sepezb1670b52017-02-16 17:01:00 -08001520 CJS_EventHandler* pEventHandler =
1521 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001522
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001523 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001524 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001525 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001526
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001528 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001529 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001530 }
1531
1532 CFX_WideString prefix, postfix;
1533
1534 if (pEventHandler->SelStart() >= 0)
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001535 prefix = swValue.Left(pEventHandler->SelStart());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001536 else
1537 prefix = L"";
1538
1539 if (pEventHandler->SelEnd() >= 0 &&
1540 pEventHandler->SelEnd() <= swValue.GetLength())
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001541 postfix = swValue.Right(swValue.GetLength() - pEventHandler->SelEnd());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542 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);
Tom Sepezdf950b82017-08-04 11:33:49 -07001562 if (std::isnan(dDate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001563 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);
Ryan Harrison75c65212017-08-16 13:52:15 -04001764 if (str.GetLength() > 0 && (str[0] == L'.' || str[0] == L','))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001765 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}