blob: 2996316d797a96cfdb86c2070faff9c7aafaa85f [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 Harrisonda129ab2017-08-01 15:16:59 -0400530 if (sFullMonths.Find(sMonth.c_str(), 0) != FX_STRNPOS) {
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 Harrisonda129ab2017-08-01 15:16:59 -0400936 bool bHasSign = wstrValue.Find(L'-') != FX_STRNPOS &&
937 wstrSelected.Find(L'-') == FX_STRNPOS;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938 if (bHasSign) {
939 // can't insert "change" in front to sign postion.
940 if (pEvent->SelStart() == 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700941 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700942 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700943 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700945
tsepezb4694242016-08-15 16:44:55 -0700946 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700947 if (iSepStyle < 0 || iSepStyle > 3)
948 iSepStyle = 0;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400949 const wchar_t cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700950
Ryan Harrisonda129ab2017-08-01 15:16:59 -0400951 bool bHasSep = wstrValue.Find(cSep) != FX_STRNPOS;
thestigcf03f8e2016-05-09 12:36:18 -0700952 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
953 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700954 if (bHasSep) {
Lei Zhange247ec42017-04-20 21:41:36 -0700955 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700956 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 }
tsepez4cf55152016-11-02 14:37:54 -0700958 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 continue;
960 }
thestigcf03f8e2016-05-09 12:36:18 -0700961 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700962 if (bHasSign) {
Lei Zhange247ec42017-04-20 21:41:36 -0700963 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700964 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800966 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700967 if (i != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700968 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700969 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 }
971 if (pEvent->SelStart() != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700972 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700973 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974 }
tsepez4cf55152016-11-02 14:37:54 -0700975 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976 continue;
977 }
978
Lei Zhange247ec42017-04-20 21:41:36 -0700979 if (!std::iswdigit(wstrChange[i])) {
980 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700981 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700982 }
983 }
984
Ryan Harrisone7a99de2017-07-28 14:07:04 -0400985 CFX_WideString wprefix = wstrValue.Left(pEvent->SelStart());
thestigcf03f8e2016-05-09 12:36:18 -0700986 CFX_WideString wpostfix;
987 if (pEvent->SelEnd() < wstrValue.GetLength())
Ryan Harrisone7a99de2017-07-28 14:07:04 -0400988 wpostfix = wstrValue.Right(wstrValue.GetLength() - pEvent->SelEnd());
thestigcf03f8e2016-05-09 12:36:18 -0700989 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700990 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700991}
992
993// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -0800994bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700995 const std::vector<CJS_Value>& params,
996 CJS_Value& vRet,
997 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001000 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001001 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001002 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001003
1004 CJS_EventHandler* pEvent =
1005 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001007 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008
1009 CFX_WideString& Value = pEvent->Value();
1010 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1011 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001012 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013
tsepezb4694242016-08-15 16:44:55 -07001014 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015 if (iDec < 0)
1016 iDec = -iDec;
1017
tsepezb4694242016-08-15 16:44:55 -07001018 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 if (iSepStyle < 0 || iSepStyle > 3)
1020 iSepStyle = 0;
1021
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001023 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 dValue *= 100;
1025 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001026 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027
1028 int iDec2;
1029 int iNegative = 0;
1030 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1031 if (strValue.IsEmpty()) {
1032 dValue = 0;
1033 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1034 }
1035
1036 if (iDec2 < 0) {
1037 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1038 strValue = "0" + strValue;
1039 }
1040 iDec2 = 0;
1041 }
1042 int iMax = strValue.GetLength();
1043 if (iDec2 > iMax) {
1044 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1045 strValue += "0";
1046 }
1047 iMax = iDec2 + 1;
1048 }
dsinclair64376be2016-03-31 20:03:24 -07001049
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001050 // for processing seperator style
1051 if (iDec2 < iMax) {
1052 if (iSepStyle == 0 || iSepStyle == 1) {
1053 strValue.Insert(iDec2, '.');
1054 iMax++;
1055 } else if (iSepStyle == 2 || iSepStyle == 3) {
1056 strValue.Insert(iDec2, ',');
1057 iMax++;
1058 }
1059
1060 if (iDec2 == 0)
1061 strValue.Insert(iDec2, '0');
1062 }
1063 if (iSepStyle == 0 || iSepStyle == 2) {
1064 char cSeperator;
1065 if (iSepStyle == 0)
1066 cSeperator = ',';
1067 else
1068 cSeperator = '.';
1069
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001070 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071 strValue.Insert(iDecPositive, cSeperator);
1072 iMax++;
1073 }
1074 }
dsinclair64376be2016-03-31 20:03:24 -07001075
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076 // negative mark
1077 if (iNegative)
1078 strValue = "-" + strValue;
1079 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001080 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081#endif
tsepez4cf55152016-11-02 14:37:54 -07001082 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001083}
1084// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001085bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001086 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001087 const std::vector<CJS_Value>& params,
1088 CJS_Value& vRet,
1089 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001090 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001091}
1092
1093// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001094bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001095 const std::vector<CJS_Value>& params,
1096 CJS_Value& vRet,
1097 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001098 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001099 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001100 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001102
1103 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1104 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001106 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107
1108 CFX_WideString& val = pEvent->Value();
1109 CFX_WideString strValue = val;
1110 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001111 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112
tsepezb4694242016-08-15 16:44:55 -07001113 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001114 double dDate = 0.0f;
1115
Ryan Harrisonda129ab2017-08-01 15:16:59 -04001116 if (strValue.Find(L"GMT") != FX_STRNPOS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117 // for GMT format time
1118 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1119 dDate = MakeInterDate(strValue);
1120 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001121 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001122 }
1123
Tom Sepezdf950b82017-08-04 11:33:49 -07001124 if (std::isnan(dDate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001125 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001126 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001128 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001129 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001130 }
1131
1132 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001133 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134}
1135
tsepez745611b2016-04-12 16:46:34 -07001136double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001137 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138 CFX_WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001139 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001140 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001141 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 sTemp = L"";
1143 continue;
1144 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145 sTemp += c;
1146 }
Tom Sepezab277682016-02-17 10:07:21 -08001147 wsArray.push_back(sTemp);
1148 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 return 0;
1150
Tom Sepez4246b002016-01-20 11:48:29 -08001151 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001152 sTemp = wsArray[1];
1153 if (sTemp.Compare(L"Jan") == 0)
1154 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001155 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001157 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001159 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001161 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001163 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001165 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001167 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001169 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001171 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001173 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001175 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001176 nMonth = 12;
1177
tsepez4c3debb2016-04-08 12:20:38 -07001178 int nDay = FX_atof(wsArray[2].AsStringC());
1179 int nHour = FX_atof(wsArray[3].AsStringC());
1180 int nMin = FX_atof(wsArray[4].AsStringC());
1181 int nSec = FX_atof(wsArray[5].AsStringC());
1182 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1184 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -07001185 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001186 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001187
1188 return dRet;
1189}
1190
1191// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001192bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001193 const std::vector<CJS_Value>& params,
1194 CJS_Value& vRet,
1195 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196 if (params.size() != 1) {
1197 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001198 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199 }
1200
Tom Sepezb1670b52017-02-16 17:01:00 -08001201 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1202 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001203 if (pEvent->WillCommit()) {
1204 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001205 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001206
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 CFX_WideString strValue = pEvent->Value();
1208 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001209 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001210
tsepezb4694242016-08-15 16:44:55 -07001211 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001212 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001213 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Tom Sepezdf950b82017-08-04 11:33:49 -07001214 if (bWrongFormat || std::isnan(dRet)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001215 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001216 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001218 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001219 pEvent->Rc() = false;
1220 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001221 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 }
tsepez4cf55152016-11-02 14:37:54 -07001223 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001224}
1225
Tom Sepezb1670b52017-02-16 17:01:00 -08001226bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001227 const std::vector<CJS_Value>& params,
1228 CJS_Value& vRet,
1229 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001230 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001231 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001232 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001234
tsepezb4694242016-08-15 16:44:55 -07001235 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001236 const wchar_t* cFormats[] = {L"m/d",
1237 L"m/d/yy",
1238 L"mm/dd/yy",
1239 L"mm/yy",
1240 L"d-mmm",
1241 L"d-mmm-yy",
1242 L"dd-mmm-yy",
1243 L"yy-mm-dd",
1244 L"mmm-yy",
1245 L"mmmm-yy",
1246 L"mmm d, yyyy",
1247 L"mmmm d, yyyy",
1248 L"m/d/yy h:MM tt",
1249 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001250
Lei Zhanga0f67242015-08-17 15:39:30 -07001251 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1252 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001253
Lei Zhang945fdb72015-11-11 10:18:16 -08001254 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001255 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1256 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001257}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001258
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001259// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001260bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001261 const std::vector<CJS_Value>& params,
1262 CJS_Value& vRet,
1263 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001265 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001266 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 }
1268
tsepezb4694242016-08-15 16:44:55 -07001269 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001270 const wchar_t* cFormats[] = {L"m/d",
1271 L"m/d/yy",
1272 L"mm/dd/yy",
1273 L"mm/yy",
1274 L"d-mmm",
1275 L"d-mmm-yy",
1276 L"dd-mmm-yy",
1277 L"yy-mm-dd",
1278 L"mmm-yy",
1279 L"mmmm-yy",
1280 L"mmm d, yyyy",
1281 L"mmmm d, yyyy",
1282 L"m/d/yy h:MM tt",
1283 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001284
Lei Zhanga0f67242015-08-17 15:39:30 -07001285 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1286 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287
Lei Zhang945fdb72015-11-11 10:18:16 -08001288 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001289 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1290 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291}
1292
1293// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001294bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001295 const std::vector<CJS_Value>& params,
1296 CJS_Value& vRet,
1297 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001298 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001299 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001300 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301 }
1302
tsepezb4694242016-08-15 16:44:55 -07001303 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001304 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1305 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001306
Lei Zhanga0f67242015-08-17 15:39:30 -07001307 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1308 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309
Lei Zhang945fdb72015-11-11 10:18:16 -08001310 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001311 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1312 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313}
1314
Tom Sepezb1670b52017-02-16 17:01:00 -08001315bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001316 const std::vector<CJS_Value>& params,
1317 CJS_Value& vRet,
1318 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001319 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001320 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001321 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001322 }
1323
tsepezb4694242016-08-15 16:44:55 -07001324 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001325 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1326 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327
Lei Zhanga0f67242015-08-17 15:39:30 -07001328 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1329 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001330
Lei Zhang945fdb72015-11-11 10:18:16 -08001331 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001332 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1333 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001334}
1335
Tom Sepezb1670b52017-02-16 17:01:00 -08001336bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001337 const std::vector<CJS_Value>& params,
1338 CJS_Value& vRet,
1339 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001340 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001341}
1342
Tom Sepezb1670b52017-02-16 17:01:00 -08001343bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001344 const std::vector<CJS_Value>& params,
1345 CJS_Value& vRet,
1346 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001347 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348}
1349
1350// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001351bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001352 const std::vector<CJS_Value>& params,
1353 CJS_Value& vRet,
1354 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001355 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001356 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001357 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358 }
1359
Tom Sepezb1670b52017-02-16 17:01:00 -08001360 CJS_EventHandler* pEvent =
1361 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001363 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001364
tsepez4f1f41f2016-03-28 14:13:16 -07001365 CFX_WideString wsSource = pEvent->Value();
1366 CFX_WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001367 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001368 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001369 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001370 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001371 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001372 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001374 case 2:
1375 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1376 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377 else
tsepez4f1f41f2016-03-28 14:13:16 -07001378 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001379 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001380 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001381 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382 break;
1383 }
1384
tsepez4f1f41f2016-03-28 14:13:16 -07001385 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001386 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387}
1388
1389// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001390bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001391 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001392 const std::vector<CJS_Value>& params,
1393 CJS_Value& vRet,
1394 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001395 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001396 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001397 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001398 }
1399
Tom Sepezb1670b52017-02-16 17:01:00 -08001400 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1401 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001402 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001403 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001404
tsepezcd5dc852016-09-08 11:23:24 -07001405 CFX_WideString& valEvent = pEvent->Value();
tsepezb4694242016-08-15 16:44:55 -07001406 CFX_WideString wstrMask = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001408 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001410 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001411 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001412 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001413
1414 FX_STRSIZE iIndexMask = 0;
1415 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1416 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001417 break;
1418 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001419
thestigcf03f8e2016-05-09 12:36:18 -07001420 if (iIndexMask != wstrMask.GetLength() ||
1421 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001422 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001423 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001424 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 }
tsepez4cf55152016-11-02 14:37:54 -07001426 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427 }
1428
1429 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001430 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001431 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432
thestigcf03f8e2016-05-09 12:36:18 -07001433 CFX_WideString wChange = wideChange;
1434 FX_STRSIZE iIndexMask = pEvent->SelStart();
1435 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1436 pEvent->SelStart() - pEvent->SelEnd();
1437 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001438 AlertIfPossible(pContext,
1439 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001440 pEvent->Rc() = false;
1441 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001442 }
1443
thestigcf03f8e2016-05-09 12:36:18 -07001444 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001445 AlertIfPossible(pContext,
1446 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001447 pEvent->Rc() = false;
1448 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001449 }
1450
thestigcf03f8e2016-05-09 12:36:18 -07001451 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1452 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001453 AlertIfPossible(pContext,
1454 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001455 pEvent->Rc() = false;
1456 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001458 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001459 if (!isReservedMaskChar(wMask))
1460 wChange.SetAt(i, wMask);
1461
1462 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001463 pEvent->Rc() = false;
1464 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001465 }
1466 iIndexMask++;
1467 }
thestigcf03f8e2016-05-09 12:36:18 -07001468 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001469 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001470}
1471
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001473bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001474 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001475 const std::vector<CJS_Value>& params,
1476 CJS_Value& vRet,
1477 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001478 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001479 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001480 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001481 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001482
Tom Sepezb1670b52017-02-16 17:01:00 -08001483 CJS_EventHandler* pEvent =
1484 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001486 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001487
thestigcf03f8e2016-05-09 12:36:18 -07001488 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001489 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001490 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491 cFormat = "99999";
1492 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001493 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001494 cFormat = "999999999";
1495 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001496 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001497 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498 cFormat = "9999999999";
1499 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001500 cFormat = "9999999";
1501 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001502 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503 cFormat = "999999999";
1504 break;
1505 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001506
Lei Zhang945fdb72015-11-11 10:18:16 -08001507 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001508 params2.push_back(CJS_Value(pRuntime, cFormat));
1509 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001510}
1511
Tom Sepezb1670b52017-02-16 17:01:00 -08001512bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001513 const std::vector<CJS_Value>& params,
1514 CJS_Value& vRet,
1515 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001517 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001518 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001519 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001520
Tom Sepezb1670b52017-02-16 17:01:00 -08001521 CJS_EventHandler* pEventHandler =
1522 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001523
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001524 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001525 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001526 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001527
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001528 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001529 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001530 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001531 }
1532
1533 CFX_WideString prefix, postfix;
1534
1535 if (pEventHandler->SelStart() >= 0)
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001536 prefix = swValue.Left(pEventHandler->SelStart());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001537 else
1538 prefix = L"";
1539
1540 if (pEventHandler->SelEnd() >= 0 &&
1541 pEventHandler->SelEnd() <= swValue.GetLength())
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001542 postfix = swValue.Right(swValue.GetLength() - pEventHandler->SelEnd());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 else
1544 postfix = L"";
1545
tsepezf3dc8c62016-08-10 06:29:29 -07001546 vRet =
1547 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001548 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001549}
1550
Tom Sepezb1670b52017-02-16 17:01:00 -08001551bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001552 const std::vector<CJS_Value>& params,
1553 CJS_Value& vRet,
1554 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001555 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001556 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001557 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001558 }
1559
tsepezb4694242016-08-15 16:44:55 -07001560 CFX_WideString sValue = params[0].ToCFXWideString(pRuntime);
1561 CFX_WideString sFormat = params[1].ToCFXWideString(pRuntime);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001562 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Tom Sepezdf950b82017-08-04 11:33:49 -07001563 if (std::isnan(dDate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001564 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001565 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566 sFormat.c_str());
Tom Sepezb1670b52017-02-16 17:01:00 -08001567 AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001568 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001569 }
1570
tsepezf3dc8c62016-08-10 06:29:29 -07001571 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001572 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001573}
1574
Tom Sepezb1670b52017-02-16 17:01:00 -08001575bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001576 const std::vector<CJS_Value>& params,
1577 CJS_Value& vRet,
1578 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001579 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001580 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001581 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001582 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001583
tsepezb4694242016-08-15 16:44:55 -07001584 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
1585 params[0].ToCFXWideString(pRuntime).c_str(),
1586 params[1].ToDouble(pRuntime),
1587 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001588
tsepez4cf55152016-11-02 14:37:54 -07001589 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001590}
1591
Tom Sepezb1670b52017-02-16 17:01:00 -08001592bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001593 const std::vector<CJS_Value>& params,
1594 CJS_Value& vRet,
1595 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001596 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001597 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001598 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001599 }
tsepezf3dc8c62016-08-10 06:29:29 -07001600
tsepezb4694242016-08-15 16:44:55 -07001601 CFX_WideString ws = params[0].ToCFXWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001602 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001603 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001604 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001605 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001606 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001607 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001608}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001609
Tom Sepezb1670b52017-02-16 17:01:00 -08001610bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001611 const std::vector<CJS_Value>& params,
1612 CJS_Value& vRet,
1613 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001614 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001615 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001616 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001617 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001618
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001619 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001620 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001621 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001622 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001623 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001624
dsinclair4526faf2016-10-11 10:54:49 -07001625 CPDFSDK_InterForm* pReaderInterForm =
Tom Sepezb1670b52017-02-16 17:01:00 -08001626 pRuntime->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001628
tsepezb4694242016-08-15 16:44:55 -07001629 CFX_WideString sFunction = params[0].ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001630 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001631
Tom Sepez67fd5df2015-10-08 12:24:19 -07001632 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001633 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001634
tsepezb4694242016-08-15 16:44:55 -07001635 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001636 CJS_Value jsValue(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001637 FieldNameArray.GetElement(pRuntime, i, jsValue);
1638 CFX_WideString wsFieldName = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001639
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001640 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1641 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1642 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001643 switch (pFormField->GetFieldType()) {
1644 case FIELDTYPE_TEXTFIELD:
1645 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001646 CFX_WideString trimmed = pFormField->GetValue();
1647 trimmed.TrimRight();
1648 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001649 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001650 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001651 case FIELDTYPE_PUSHBUTTON: {
1652 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001653 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001654 case FIELDTYPE_CHECKBOX:
1655 case FIELDTYPE_RADIOBUTTON: {
1656 dTemp = 0.0;
1657 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1658 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1659 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001660 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1661 trimmed.TrimRight();
1662 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001663 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001665 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001666 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001667 }
Tom Sepez4246b002016-01-20 11:48:29 -08001668 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001669 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001670 if (pFormField->CountSelectedItems() <= 1) {
1671 CFX_WideString trimmed = pFormField->GetValue();
1672 trimmed.TrimRight();
1673 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001674 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001675 }
1676 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001677 default:
1678 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001679 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001680
1681 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1682 wcscmp(sFunction.c_str(), L"MAX") == 0))
1683 dValue = dTemp;
1684
1685 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1686
1687 nFieldsCount++;
1688 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001689 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001690 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001691
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001692 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1693 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001694
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001695 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1696 FXSYS_pow((double)10, (double)6);
Tom Sepezb1670b52017-02-16 17:01:00 -08001697
Tom Sepez67fd5df2015-10-08 12:24:19 -07001698 CJS_Value jsValue(pRuntime, dValue);
Tom Sepezb1670b52017-02-16 17:01:00 -08001699 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
foxit8b544ed2015-09-10 14:57:54 +08001700 if (pContext->GetEventHandler()->m_pValue)
tsepezb4694242016-08-15 16:44:55 -07001701 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001702
tsepez4cf55152016-11-02 14:37:54 -07001703 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001704}
1705
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001706/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001707** within the specified range. */
1708
Tom Sepezb1670b52017-02-16 17:01:00 -08001709bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001710 const std::vector<CJS_Value>& params,
1711 CJS_Value& vRet,
1712 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001714 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001715 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001716 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001717 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
tsepezcd5dc852016-09-08 11:23:24 -07001718 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001719 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001720 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001721
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001722 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001723 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001724
tsepezb4c9f3f2016-04-13 15:41:21 -07001725 double dEentValue =
1726 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001727 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001728 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001729 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001730 double dLessThan = params[3].ToDouble(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731 CFX_WideString swMsg;
1732
1733 if (bGreaterThan && bLessThan) {
1734 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001735 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001736 params[1].ToCFXWideString(pRuntime).c_str(),
1737 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001738 } else if (bGreaterThan) {
1739 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001740 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001741 params[1].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001742 } else if (bLessThan) {
1743 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001744 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001745 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001746 }
1747
1748 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001749 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001750 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001751 }
tsepez4cf55152016-11-02 14:37:54 -07001752 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001753}
1754
Tom Sepezb1670b52017-02-16 17:01:00 -08001755bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001756 const std::vector<CJS_Value>& params,
1757 CJS_Value& vRet,
1758 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001759 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001760 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001761 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001762 }
1763
tsepezb4694242016-08-15 16:44:55 -07001764 CFX_WideString str = params[0].ToCFXWideString(pRuntime);
Ryan Harrison8a1758b2017-08-15 10:37:59 -04001765 if (str[0] == L'.' || str[0] == L',')
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001766 str = L"0" + str;
1767
Tom Sepezb1670b52017-02-16 17:01:00 -08001768 CFX_WideString sPart;
1769 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001770 int nIndex = 0;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001771 for (const auto& wc : str) {
Lei Zhange247ec42017-04-20 21:41:36 -07001772 if (std::iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001773 sPart += wc;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001774 } else if (sPart.GetLength() > 0) {
1775 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
1776 sPart = L"";
1777 nIndex++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001778 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001779 }
Tom Sepez3c3e2712017-04-17 15:38:19 -07001780 if (sPart.GetLength() > 0)
tsepezb4694242016-08-15 16:44:55 -07001781 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001782
tsepezb4694242016-08-15 16:44:55 -07001783 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001784 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001785 else
tsepezf3dc8c62016-08-10 06:29:29 -07001786 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001787
tsepez4cf55152016-11-02 14:37:54 -07001788 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001789}