blob: 2e7387e9d22b419afdec269a77258bea90e9e7f0 [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
tsepez745611b2016-04-12 16:46:34 -070060namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061
Dan Sinclair812e96c2017-03-13 16:43:37 -040062const wchar_t* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
63 L"May", L"Jun", L"Jul", L"Aug",
64 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
Dan Sinclair812e96c2017-03-13 16:43:37 -040066const wchar_t* const fullmonths[] = {L"January", L"February", L"March",
67 L"April", L"May", L"June",
68 L"July", L"August", L"September",
69 L"October", L"November", L"December"};
tsepez745611b2016-04-12 16:46:34 -070070
Ryan Harrison275e2602017-09-18 14:23:18 -040071ByteString StrTrim(const ByteString& pStr) {
72 ByteString result(pStr);
tsepez745611b2016-04-12 16:46:34 -070073 result.TrimLeft(' ');
74 result.TrimRight(' ');
75 return result;
76}
77
Ryan Harrison275e2602017-09-18 14:23:18 -040078WideString StrTrim(const WideString& pStr) {
79 WideString result(pStr);
tsepez745611b2016-04-12 16:46:34 -070080 result.TrimLeft(' ');
81 result.TrimRight(' ');
82 return result;
83}
84
Dan Sinclair812e96c2017-03-13 16:43:37 -040085void AlertIfPossible(CJS_EventContext* pContext, const wchar_t* swMsg) {
dsinclair8779fa82016-10-12 12:05:44 -070086 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
87 if (pFormFillEnv)
88 pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3);
tsepeze1e7bd02016-08-08 13:03:16 -070089}
90
Dan Sinclair698aed72017-09-26 16:24:49 -040091#if _FX_OS_ != _FX_OS_ANDROID_
Ryan Harrison275e2602017-09-18 14:23:18 -040092ByteString CalculateString(double dValue,
93 int iDec,
94 int* iDec2,
95 bool* bNegative) {
npm49c59282016-11-15 15:14:04 -080096 *bNegative = dValue < 0;
97 if (*bNegative)
98 dValue = -dValue;
dsinclair992ecf72016-12-14 05:45:57 -080099
100 // Make sure the number of precision characters will fit.
101 if (iDec > std::numeric_limits<double>::digits10)
102 iDec = std::numeric_limits<double>::digits10;
103
npm49c59282016-11-15 15:14:04 -0800104 std::stringstream ss;
105 ss << std::fixed << std::setprecision(iDec) << dValue;
106 std::string stringValue = ss.str();
107 size_t iDecimalPos = stringValue.find(".");
108 *iDec2 = iDecimalPos == std::string::npos ? stringValue.size()
109 : static_cast<int>(iDecimalPos);
Ryan Harrison275e2602017-09-18 14:23:18 -0400110 return ByteString(stringValue.c_str());
npm49c59282016-11-15 15:14:04 -0800111}
112#endif
113
Dan Sinclair4b172c42017-10-23 11:22:31 -0400114// NOLINTNEXTLINE(whitespace/parens)
115template <bool (
116 *F)(CJS_Runtime*, const std::vector<CJS_Value>&, CJS_Value&, WideString&)>
117void JSGlobalFunc(const char* func_name_string,
118 const v8::FunctionCallbackInfo<v8::Value>& info) {
119 CJS_Runtime* pRuntime =
120 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate());
121 if (!pRuntime)
122 return;
123 std::vector<CJS_Value> parameters;
124 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) {
125 parameters.push_back(CJS_Value(pRuntime, info[i]));
126 }
127 CJS_Value valueRes(pRuntime);
128 WideString sError;
129 if (!(*F)(pRuntime, parameters, valueRes, sError)) {
130 pRuntime->Error(JSFormatErrorString(func_name_string, nullptr, sError));
131 return;
132 }
133 info.GetReturnValue().Set(valueRes.ToV8Value(pRuntime));
134}
135
tsepez745611b2016-04-12 16:46:34 -0700136} // namespace
137
Dan Sinclair4b172c42017-10-23 11:22:31 -0400138// static
139void CJS_PublicMethods::DefineJSObjects(CFXJS_Engine* pEngine) {
140 for (size_t i = 0; i < FX_ArraySize(GlobalFunctionSpecs) - 1; ++i) {
141 pEngine->DefineGlobalMethod(
142 CJS_PublicMethods::GlobalFunctionSpecs[i].pName,
143 CJS_PublicMethods::GlobalFunctionSpecs[i].pMethodCall);
144 }
145}
146
147#define JS_STATIC_GLOBAL_FUN(fun_name) \
148 void CJS_PublicMethods::fun_name##_static( \
149 const v8::FunctionCallbackInfo<v8::Value>& info) { \
150 JSGlobalFunc<fun_name>(#fun_name, info); \
151 }
152
153JS_STATIC_GLOBAL_FUN(AFNumber_Format);
154JS_STATIC_GLOBAL_FUN(AFNumber_Keystroke);
155JS_STATIC_GLOBAL_FUN(AFPercent_Format);
156JS_STATIC_GLOBAL_FUN(AFPercent_Keystroke);
157JS_STATIC_GLOBAL_FUN(AFDate_FormatEx);
158JS_STATIC_GLOBAL_FUN(AFDate_KeystrokeEx);
159JS_STATIC_GLOBAL_FUN(AFDate_Format);
160JS_STATIC_GLOBAL_FUN(AFDate_Keystroke);
161JS_STATIC_GLOBAL_FUN(AFTime_FormatEx);
162JS_STATIC_GLOBAL_FUN(AFTime_KeystrokeEx);
163JS_STATIC_GLOBAL_FUN(AFTime_Format);
164JS_STATIC_GLOBAL_FUN(AFTime_Keystroke);
165JS_STATIC_GLOBAL_FUN(AFSpecial_Format);
166JS_STATIC_GLOBAL_FUN(AFSpecial_Keystroke);
167JS_STATIC_GLOBAL_FUN(AFSpecial_KeystrokeEx);
168JS_STATIC_GLOBAL_FUN(AFSimple);
169JS_STATIC_GLOBAL_FUN(AFMakeNumber);
170JS_STATIC_GLOBAL_FUN(AFSimple_Calculate);
171JS_STATIC_GLOBAL_FUN(AFRange_Validate);
172JS_STATIC_GLOBAL_FUN(AFMergeChange);
173JS_STATIC_GLOBAL_FUN(AFParseDateEx);
174JS_STATIC_GLOBAL_FUN(AFExtractNums);
175
Ryan Harrison275e2602017-09-18 14:23:18 -0400176bool CJS_PublicMethods::IsNumber(const WideString& str) {
177 WideString sTrim = StrTrim(str);
Dan Sinclair812e96c2017-03-13 16:43:37 -0400178 const wchar_t* pTrim = sTrim.c_str();
179 const wchar_t* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -0700180 bool bDot = false;
181 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -0700184 while ((c = *p) != L'\0') {
185 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -0700187 return false;
188 bDot = true;
189 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -0700191 return false;
192 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700194 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 p++;
197 c = *p;
Lei Zhange247ec42017-04-20 21:41:36 -0700198 if (c != L'+' && c != L'-')
Wei Li614d20a2016-03-15 13:55:12 -0700199 return false;
Lei Zhange247ec42017-04-20 21:41:36 -0700200 bKXJS = true;
201 } else if (!std::iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700202 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 }
204 p++;
205 }
206
Wei Li614d20a2016-03-15 13:55:12 -0700207 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208}
209
Wei Li614d20a2016-03-15 13:55:12 -0700210bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 switch (c_Mask) {
212 case L'9':
Lei Zhange247ec42017-04-20 21:41:36 -0700213 return !!std::iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800215 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800217 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700219 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 default:
221 return (c_Change == c_Mask);
222 }
223}
224
Wei Li614d20a2016-03-15 13:55:12 -0700225bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
227}
228
Dan Sinclair812e96c2017-03-13 16:43:37 -0400229double CJS_PublicMethods::AF_Simple(const wchar_t* sFuction,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 double dValue1,
231 double dValue2) {
232 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
233 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
234 return dValue1 + dValue2;
235 }
236 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
237 return dValue1 * dValue2;
238 }
239 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800240 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 }
242 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800243 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 }
245 return dValue1;
246}
247
Tom Sepez67fd5df2015-10-08 12:24:19 -0700248CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 CJS_Value val) {
Dan Sinclairc9708952017-10-23 09:40:59 -0400250 if (val.IsArrayObject())
251 return val.ToArray(pRuntime);
252
253 WideString wsStr = val.ToWideString(pRuntime);
Ryan Harrison275e2602017-09-18 14:23:18 -0400254 ByteString t = ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700255 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256
257 int ch = ',';
258 int nIndex = 0;
259
Dan Sinclairc9708952017-10-23 09:40:59 -0400260 CJS_Array StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 while (*p) {
262 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800263 if (!pTemp) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400264 StrArray.SetElement(pRuntime, nIndex,
265 CJS_Value(pRuntime, StrTrim(ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 }
Lei Zhang997de612015-11-04 18:17:53 -0800268
269 char* pSub = new char[pTemp - p + 1];
270 strncpy(pSub, p, pTemp - p);
271 *(pSub + (pTemp - p)) = '\0';
272
Ryan Harrison275e2602017-09-18 14:23:18 -0400273 StrArray.SetElement(pRuntime, nIndex,
274 CJS_Value(pRuntime, StrTrim(ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800275 delete[] pSub;
276
277 nIndex++;
278 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 }
280 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281}
282
Ryan Harrison275e2602017-09-18 14:23:18 -0400283int CJS_PublicMethods::ParseStringInteger(const WideString& str,
Ryan Harrison875e98c2017-09-27 10:53:11 -0400284 size_t nStart,
285 size_t& nSkip,
286 size_t nMaxStep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 int nRet = 0;
288 nSkip = 0;
Ryan Harrison875e98c2017-09-27 10:53:11 -0400289 for (size_t i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 if (i - nStart > 10)
291 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400293 wchar_t c = str[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700294 if (!std::iswdigit(c))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800295 break;
296
Lei Zhange8c1d412017-05-04 12:13:55 -0700297 nRet = nRet * 10 + FXSYS_DecimalCharToInt(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800298 nSkip = i - nStart + 1;
299 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 break;
301 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700302
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700304}
305
Ryan Harrison275e2602017-09-18 14:23:18 -0400306WideString CJS_PublicMethods::ParseStringString(const WideString& str,
Ryan Harrison875e98c2017-09-27 10:53:11 -0400307 size_t nStart,
308 size_t& nSkip) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400309 WideString swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 nSkip = 0;
Ryan Harrison875e98c2017-09-27 10:53:11 -0400311 for (size_t i = nStart, sz = str.GetLength(); i < sz; i++) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400312 wchar_t c = str[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700313 if (!std::iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800315
316 swRet += c;
317 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321}
322
Ryan Harrison275e2602017-09-18 14:23:18 -0400323double CJS_PublicMethods::ParseNormalDate(const WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800324 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700326
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 int nYear = JS_GetYearFromTime(dt);
328 int nMonth = JS_GetMonthFromTime(dt) + 1;
329 int nDay = JS_GetDayFromTime(dt);
330 int nHour = JS_GetHourFromTime(dt);
331 int nMin = JS_GetMinFromTime(dt);
332 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700335
Ryan Harrison875e98c2017-09-27 10:53:11 -0400336 size_t nSkip = 0;
337 size_t nLen = value.GetLength();
338 size_t nIndex = 0;
339 size_t i = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 while (i < nLen) {
341 if (nIndex > 2)
342 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400344 wchar_t c = value[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700345 if (std::iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
347 i += nSkip;
348 } else {
349 i++;
350 }
351 }
352
353 if (nIndex == 2) {
354 // case2: month/day
355 // case3: day/month
356 if ((number[0] >= 1 && number[0] <= 12) &&
357 (number[1] >= 1 && number[1] <= 31)) {
358 nMonth = number[0];
359 nDay = number[1];
360 } else if ((number[0] >= 1 && number[0] <= 31) &&
361 (number[1] >= 1 && number[1] <= 12)) {
362 nDay = number[0];
363 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700364 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700365
Lei Zhang9559b7a2015-12-21 11:12:20 -0800366 if (bWrongFormat)
367 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 } else if (nIndex == 3) {
369 // case1: year/month/day
370 // case2: month/day/year
371 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700372
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
374 (number[2] >= 1 && number[2] <= 31)) {
375 nYear = number[0];
376 nMonth = number[1];
377 nDay = number[2];
378 } else if ((number[0] >= 1 && number[0] <= 12) &&
379 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
380 nMonth = number[0];
381 nDay = number[1];
382 nYear = number[2];
383 } else if ((number[0] >= 1 && number[0] <= 31) &&
384 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
385 nDay = number[0];
386 nMonth = number[1];
387 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700388 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389
Lei Zhang9559b7a2015-12-21 11:12:20 -0800390 if (bWrongFormat)
391 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800393 if (bWrongFormat)
394 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 return dt;
396 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700397
Ryan Harrison275e2602017-09-18 14:23:18 -0400398 WideString swTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700400 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700401}
402
Ryan Harrison275e2602017-09-18 14:23:18 -0400403double CJS_PublicMethods::MakeRegularDate(const WideString& value,
404 const WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800405 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 if (format.IsEmpty() || value.IsEmpty())
409 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700410
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 int nYear = JS_GetYearFromTime(dt);
412 int nMonth = JS_GetMonthFromTime(dt) + 1;
413 int nDay = JS_GetDayFromTime(dt);
414 int nHour = JS_GetHourFromTime(dt);
415 int nMin = JS_GetMinFromTime(dt);
416 int nSec = JS_GetSecFromTime(dt);
417
418 int nYearSub = 99; // nYear - 2000;
419
tsepez4cf55152016-11-02 14:37:54 -0700420 bool bPm = false;
421 bool bExit = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800422 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423
Ryan Harrison875e98c2017-09-27 10:53:11 -0400424 size_t i = 0;
425 size_t j = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426
427 while (i < format.GetLength()) {
428 if (bExit)
429 break;
430
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400431 wchar_t c = format[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432 switch (c) {
433 case ':':
434 case '.':
435 case '-':
436 case '\\':
437 case '/':
438 i++;
439 j++;
440 break;
441
442 case 'y':
443 case 'm':
444 case 'd':
445 case 'H':
446 case 'h':
447 case 'M':
448 case 's':
449 case 't': {
Ryan Harrison875e98c2017-09-27 10:53:11 -0400450 size_t oldj = j;
451 size_t nSkip = 0;
452 size_t remaining = format.GetLength() - i - 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400454 if (remaining == 0 || format[i + 1] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700456 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 i++;
458 j++;
459 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700460 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 nMonth = ParseStringInteger(value, j, nSkip, 2);
462 i++;
463 j += nSkip;
464 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700465 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 nDay = ParseStringInteger(value, j, nSkip, 2);
467 i++;
468 j += nSkip;
469 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700470 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 nHour = ParseStringInteger(value, j, nSkip, 2);
472 i++;
473 j += nSkip;
474 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700475 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 nHour = ParseStringInteger(value, j, nSkip, 2);
477 i++;
478 j += nSkip;
479 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700480 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 nMin = ParseStringInteger(value, j, nSkip, 2);
482 i++;
483 j += nSkip;
484 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700485 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 nSec = ParseStringInteger(value, j, nSkip, 2);
487 i++;
488 j += nSkip;
489 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700490 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400491 bPm = (j < value.GetLength() && value[j] == 'p');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492 i++;
493 j++;
494 break;
495 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400496 } else if (remaining == 1 || format[i + 2] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 switch (c) {
498 case 'y':
499 nYear = ParseStringInteger(value, j, nSkip, 4);
500 i += 2;
501 j += nSkip;
502 break;
503 case 'm':
504 nMonth = ParseStringInteger(value, j, nSkip, 2);
505 i += 2;
506 j += nSkip;
507 break;
508 case 'd':
509 nDay = ParseStringInteger(value, j, nSkip, 2);
510 i += 2;
511 j += nSkip;
512 break;
513 case 'H':
514 nHour = ParseStringInteger(value, j, nSkip, 2);
515 i += 2;
516 j += nSkip;
517 break;
518 case 'h':
519 nHour = ParseStringInteger(value, j, nSkip, 2);
520 i += 2;
521 j += nSkip;
522 break;
523 case 'M':
524 nMin = ParseStringInteger(value, j, nSkip, 2);
525 i += 2;
526 j += nSkip;
527 break;
528 case 's':
529 nSec = ParseStringInteger(value, j, nSkip, 2);
530 i += 2;
531 j += nSkip;
532 break;
533 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400534 bPm = (j + 1 < value.GetLength() && value[j] == 'p' &&
535 value[j + 1] == 'm');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536 i += 2;
537 j += 2;
538 break;
539 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400540 } else if (remaining == 2 || format[i + 3] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 switch (c) {
542 case 'm': {
Ryan Harrison275e2602017-09-18 14:23:18 -0400543 WideString sMonth = ParseStringString(value, j, nSkip);
tsepez4cf55152016-11-02 14:37:54 -0700544 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700545 for (int m = 0; m < 12; m++) {
546 if (sMonth.CompareNoCase(months[m]) == 0) {
547 nMonth = m + 1;
548 i += 3;
549 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700550 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700551 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700552 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 }
554
555 if (!bFind) {
556 nMonth = ParseStringInteger(value, j, nSkip, 3);
557 i += 3;
558 j += nSkip;
559 }
560 } break;
561 case 'y':
562 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700563 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564 i += 3;
565 j += 3;
566 break;
567 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400568 } else if (remaining == 3 || format[i + 4] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 switch (c) {
570 case 'y':
571 nYear = ParseStringInteger(value, j, nSkip, 4);
572 j += nSkip;
573 i += 4;
574 break;
575 case 'm': {
tsepez4cf55152016-11-02 14:37:54 -0700576 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577
Ryan Harrison275e2602017-09-18 14:23:18 -0400578 WideString sMonth = ParseStringString(value, j, nSkip);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579 sMonth.MakeLower();
580
581 for (int m = 0; m < 12; m++) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400582 WideString sFullMonths = fullmonths[m];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 sFullMonths.MakeLower();
584
Ryan Harrison12db7512017-08-23 10:39:35 -0400585 if (sFullMonths.Contains(sMonth.c_str())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 nMonth = m + 1;
587 i += 4;
588 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700589 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590 break;
591 }
592 }
593
594 if (!bFind) {
595 nMonth = ParseStringInteger(value, j, nSkip, 4);
596 i += 4;
597 j += nSkip;
598 }
599 } break;
600 default:
601 i += 4;
602 j += 4;
603 break;
604 }
605 } else {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400606 if (j >= value.GetLength() || format[i] != value[j]) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800607 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700608 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 }
610 i++;
611 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700612 }
Tom Sepez85386422014-07-23 10:28:37 -0700613
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800615 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700616 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 }
618 }
619
620 break;
621 default:
622 if (value.GetLength() <= j) {
tsepez4cf55152016-11-02 14:37:54 -0700623 bExit = true;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400624 } else if (format[i] != value[j]) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800625 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700626 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700627 }
628
629 i++;
630 j++;
631 break;
632 }
633 }
634
635 if (bPm)
636 nHour += 12;
637
638 if (nYear >= 0 && nYear <= nYearSub)
639 nYear += 2000;
640
641 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800642 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643
644 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800645 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646
647 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800648 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649
650 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800651 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652
653 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800654 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700655
656 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800657 if (bBadFormat) {
658 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700659 } else {
660 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
661 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -0700662 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -0700663 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 }
665
Tom Sepezdf950b82017-08-04 11:33:49 -0700666 if (std::isnan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800667 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668
Lei Zhang9559b7a2015-12-21 11:12:20 -0800669 if (bWrongFormat)
670 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700671
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 return dRet;
673}
674
Ryan Harrison275e2602017-09-18 14:23:18 -0400675WideString CJS_PublicMethods::MakeFormatDate(double dDate,
676 const WideString& format) {
677 WideString sRet = L"", sPart = L"";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700678
679 int nYear = JS_GetYearFromTime(dDate);
680 int nMonth = JS_GetMonthFromTime(dDate) + 1;
681 int nDay = JS_GetDayFromTime(dDate);
682 int nHour = JS_GetHourFromTime(dDate);
683 int nMin = JS_GetMinFromTime(dDate);
684 int nSec = JS_GetSecFromTime(dDate);
685
Ryan Harrison875e98c2017-09-27 10:53:11 -0400686 size_t i = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687 while (i < format.GetLength()) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400688 wchar_t c = format[i];
Ryan Harrison875e98c2017-09-27 10:53:11 -0400689 size_t remaining = format.GetLength() - i - 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 sPart = L"";
691 switch (c) {
692 case 'y':
693 case 'm':
694 case 'd':
695 case 'H':
696 case 'h':
697 case 'M':
698 case 's':
699 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400700 if (remaining == 0 || format[i + 1] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700701 switch (c) {
702 case 'y':
703 sPart += c;
704 break;
705 case 'm':
706 sPart.Format(L"%d", nMonth);
707 break;
708 case 'd':
709 sPart.Format(L"%d", nDay);
710 break;
711 case 'H':
712 sPart.Format(L"%d", nHour);
713 break;
714 case 'h':
715 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
716 break;
717 case 'M':
718 sPart.Format(L"%d", nMin);
719 break;
720 case 's':
721 sPart.Format(L"%d", nSec);
722 break;
723 case 't':
724 sPart += nHour > 12 ? 'p' : 'a';
725 break;
726 }
727 i++;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400728 } else if (remaining == 1 || format[i + 2] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729 switch (c) {
730 case 'y':
731 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
732 break;
733 case 'm':
734 sPart.Format(L"%02d", nMonth);
735 break;
736 case 'd':
737 sPart.Format(L"%02d", nDay);
738 break;
739 case 'H':
740 sPart.Format(L"%02d", nHour);
741 break;
742 case 'h':
743 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
744 break;
745 case 'M':
746 sPart.Format(L"%02d", nMin);
747 break;
748 case 's':
749 sPart.Format(L"%02d", nSec);
750 break;
751 case 't':
752 sPart = nHour > 12 ? L"pm" : L"am";
753 break;
754 }
755 i += 2;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400756 } else if (remaining == 2 || format[i + 3] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 switch (c) {
758 case 'm':
759 i += 3;
760 if (nMonth > 0 && nMonth <= 12)
761 sPart += months[nMonth - 1];
762 break;
763 default:
764 i += 3;
765 sPart += c;
766 sPart += c;
767 sPart += c;
768 break;
769 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400770 } else if (remaining == 3 || format[i + 4] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771 switch (c) {
772 case 'y':
773 sPart.Format(L"%04d", nYear);
774 i += 4;
775 break;
776 case 'm':
777 i += 4;
778 if (nMonth > 0 && nMonth <= 12)
779 sPart += fullmonths[nMonth - 1];
780 break;
781 default:
782 i += 4;
783 sPart += c;
784 sPart += c;
785 sPart += c;
786 sPart += c;
787 break;
788 }
789 } else {
790 i++;
791 sPart += c;
792 }
793 break;
794 default:
795 i++;
796 sPart += c;
797 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700798 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700799
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800 sRet += sPart;
801 }
802
803 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700804}
805
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700806// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
807// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800808bool CJS_PublicMethods::AFNumber_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700809 const std::vector<CJS_Value>& params,
810 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400811 WideString& sError) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400812#if _FX_OS_ != _FX_OS_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700813 if (params.size() != 6) {
tsepezcd5dc852016-09-08 11:23:24 -0700814 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700815 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700817
Tom Sepezb1670b52017-02-16 17:01:00 -0800818 CJS_EventHandler* pEvent =
819 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700821 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700822
Ryan Harrison275e2602017-09-18 14:23:18 -0400823 WideString& Value = pEvent->Value();
824 ByteString strValue = StrTrim(ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700826 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827
tsepezb4694242016-08-15 16:44:55 -0700828 int iDec = params[0].ToInt(pRuntime);
829 int iSepStyle = params[1].ToInt(pRuntime);
830 int iNegStyle = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831 // params[3] is iCurrStyle, it's not used.
Dan Sinclairc9708952017-10-23 09:40:59 -0400832 WideString wstrCurrency = params[4].ToWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700833 bool bCurrencyPrepend = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700834
835 if (iDec < 0)
836 iDec = -iDec;
837
838 if (iSepStyle < 0 || iSepStyle > 3)
839 iSepStyle = 0;
840
841 if (iNegStyle < 0 || iNegStyle > 3)
842 iNegStyle = 0;
843
npm49c59282016-11-15 15:14:04 -0800844 // Processing decimal places
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700846 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700847 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700848 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849
npm49c59282016-11-15 15:14:04 -0800850 // Calculating number string
851 bool bNegative;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852 int iDec2;
npm49c59282016-11-15 15:14:04 -0800853 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 if (strValue.IsEmpty()) {
855 dValue = 0;
npm49c59282016-11-15 15:14:04 -0800856 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857 if (strValue.IsEmpty()) {
858 strValue = "0";
859 iDec2 = 1;
860 }
861 }
862
npm49c59282016-11-15 15:14:04 -0800863 // Processing separator style
Ryan Harrison875e98c2017-09-27 10:53:11 -0400864 if (static_cast<size_t>(iDec2) < strValue.GetLength()) {
npm49c59282016-11-15 15:14:04 -0800865 if (iSepStyle == 2 || iSepStyle == 3)
866 strValue.Replace(".", ",");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867
868 if (iDec2 == 0)
869 strValue.Insert(iDec2, '0');
870 }
871 if (iSepStyle == 0 || iSepStyle == 2) {
npm49c59282016-11-15 15:14:04 -0800872 char cSeparator;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873 if (iSepStyle == 0)
npm49c59282016-11-15 15:14:04 -0800874 cSeparator = ',';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875 else
npm49c59282016-11-15 15:14:04 -0800876 cSeparator = '.';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877
npm49c59282016-11-15 15:14:04 -0800878 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3)
879 strValue.Insert(iDecPositive, cSeparator);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700880 }
881
npm49c59282016-11-15 15:14:04 -0800882 // Processing currency string
Ryan Harrison275e2602017-09-18 14:23:18 -0400883 Value = WideString::FromLocal(strValue.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884
885 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700886 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 else
thestigcf03f8e2016-05-09 12:36:18 -0700888 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889
npm49c59282016-11-15 15:14:04 -0800890 // Processing negative style
891 if (bNegative) {
892 if (iNegStyle == 0)
thestigcf03f8e2016-05-09 12:36:18 -0700893 Value = L"-" + Value;
npm49c59282016-11-15 15:14:04 -0800894 else if (iNegStyle == 2 || iNegStyle == 3)
thestigcf03f8e2016-05-09 12:36:18 -0700895 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896 if (iNegStyle == 1 || iNegStyle == 3) {
897 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700898 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700899 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700900 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700901 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700902 vColElm = CJS_Value(pRuntime, 1);
tsepezb4694242016-08-15 16:44:55 -0700903 arColor.SetElement(pRuntime, 1, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700904 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700905 arColor.SetElement(pRuntime, 2, vColElm);
906 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907
Dan Sinclair33d13f22017-10-23 09:44:30 -0400908 CJS_Value vProp(pRuntime, arColor);
dan sinclaircbe23db2017-10-19 14:29:33 -0400909 fTarget->set_text_color(pRuntime, vProp, &sError); // red
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910 }
911 }
912 } else {
913 if (iNegStyle == 1 || iNegStyle == 3) {
914 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700915 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700916 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700917 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700918 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700919 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700920 arColor.SetElement(pRuntime, 1, vColElm);
921 arColor.SetElement(pRuntime, 2, vColElm);
922 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700923
Dan Sinclair33d13f22017-10-23 09:44:30 -0400924 CJS_Value vProp(pRuntime);
dan sinclaircbe23db2017-10-19 14:29:33 -0400925 fTarget->get_text_color(pRuntime, &vProp, &sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926
Dan Sinclair33d13f22017-10-23 09:44:30 -0400927 CJS_Array aProp = vProp.ToArray(pRuntime);
Dan Sinclair7f55a542017-07-13 14:17:10 -0400928 CFX_Color crProp;
929 CFX_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700930 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
931 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700932
933 if (crColor != crProp) {
Dan Sinclair33d13f22017-10-23 09:44:30 -0400934 CJS_Value vProp2(pRuntime, arColor);
935
dan sinclaircbe23db2017-10-19 14:29:33 -0400936 fTarget->set_text_color(pRuntime, vProp2, &sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 }
938 }
939 }
940 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941#endif
tsepez4cf55152016-11-02 14:37:54 -0700942 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700943}
944
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
946// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800947bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700948 const std::vector<CJS_Value>& params,
949 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400950 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700952 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700953
Tom Sepezb1670b52017-02-16 17:01:00 -0800954 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
955 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700956 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700957 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700958
Ryan Harrison275e2602017-09-18 14:23:18 -0400959 WideString& val = pEvent->Value();
960 WideString& wstrChange = pEvent->Change();
961 WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700962
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 if (pEvent->WillCommit()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400964 WideString swTemp = StrTrim(wstrValue);
ochanga0a3bc32016-05-12 15:22:48 -0700965 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700966 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700967
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 swTemp.Replace(L",", L".");
969 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700970 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700971 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700972 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700973 }
tsepez4cf55152016-11-02 14:37:54 -0700974 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700976
Ryan Harrison275e2602017-09-18 14:23:18 -0400977 WideString wstrSelected;
thestigcf03f8e2016-05-09 12:36:18 -0700978 if (pEvent->SelStart() != -1) {
979 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
980 pEvent->SelEnd() - pEvent->SelStart());
981 }
982
Ryan Harrison12db7512017-08-23 10:39:35 -0400983 bool bHasSign = wstrValue.Contains(L'-') && !wstrSelected.Contains(L'-');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 if (bHasSign) {
985 // can't insert "change" in front to sign postion.
986 if (pEvent->SelStart() == 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700987 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700988 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700989 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700991
tsepezb4694242016-08-15 16:44:55 -0700992 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700993 if (iSepStyle < 0 || iSepStyle > 3)
994 iSepStyle = 0;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400995 const wchar_t cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700996
Ryan Harrison12db7512017-08-23 10:39:35 -0400997 bool bHasSep = wstrValue.Contains(cSep);
Ryan Harrison875e98c2017-09-27 10:53:11 -0400998 for (size_t i = 0; i < wstrChange.GetLength(); ++i) {
thestigcf03f8e2016-05-09 12:36:18 -0700999 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 if (bHasSep) {
Lei Zhange247ec42017-04-20 21:41:36 -07001001 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -07001002 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003 }
tsepez4cf55152016-11-02 14:37:54 -07001004 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 continue;
1006 }
thestigcf03f8e2016-05-09 12:36:18 -07001007 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008 if (bHasSign) {
Lei Zhange247ec42017-04-20 21:41:36 -07001009 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -07001010 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011 }
Lei Zhang9559b7a2015-12-21 11:12:20 -08001012 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -07001013 if (i != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -07001014 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -07001015 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016 }
1017 if (pEvent->SelStart() != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -07001018 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -07001019 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 }
tsepez4cf55152016-11-02 14:37:54 -07001021 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 continue;
1023 }
1024
Lei Zhange247ec42017-04-20 21:41:36 -07001025 if (!std::iswdigit(wstrChange[i])) {
1026 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -07001027 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028 }
1029 }
1030
Ryan Harrison275e2602017-09-18 14:23:18 -04001031 WideString wprefix = wstrValue.Left(pEvent->SelStart());
1032 WideString wpostfix;
Ryan Harrison75584142017-09-01 13:30:19 -04001033 if (pEvent->SelEnd() >= 0 &&
Ryan Harrison875e98c2017-09-27 10:53:11 -04001034 static_cast<size_t>(pEvent->SelEnd()) < wstrValue.GetLength())
Ryan Harrison75584142017-09-01 13:30:19 -04001035 wpostfix = wstrValue.Right(wstrValue.GetLength() -
Ryan Harrison875e98c2017-09-27 10:53:11 -04001036 static_cast<size_t>(pEvent->SelEnd()));
thestigcf03f8e2016-05-09 12:36:18 -07001037 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -07001038 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039}
1040
1041// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -08001042bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001043 const std::vector<CJS_Value>& params,
1044 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001045 WideString& sError) {
Dan Sinclair698aed72017-09-26 16:24:49 -04001046#if _FX_OS_ != _FX_OS_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001048 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001049 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001050 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001051
1052 CJS_EventHandler* pEvent =
1053 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001055 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056
Ryan Harrison275e2602017-09-18 14:23:18 -04001057 WideString& Value = pEvent->Value();
1058 ByteString strValue = StrTrim(ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001060 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061
tsepezb4694242016-08-15 16:44:55 -07001062 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063 if (iDec < 0)
1064 iDec = -iDec;
1065
tsepezb4694242016-08-15 16:44:55 -07001066 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 if (iSepStyle < 0 || iSepStyle > 3)
1068 iSepStyle = 0;
1069
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001071 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072 dValue *= 100;
1073 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001074 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075
1076 int iDec2;
1077 int iNegative = 0;
1078 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1079 if (strValue.IsEmpty()) {
1080 dValue = 0;
1081 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1082 }
1083
1084 if (iDec2 < 0) {
1085 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1086 strValue = "0" + strValue;
1087 }
1088 iDec2 = 0;
1089 }
1090 int iMax = strValue.GetLength();
1091 if (iDec2 > iMax) {
1092 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1093 strValue += "0";
1094 }
1095 iMax = iDec2 + 1;
1096 }
dsinclair64376be2016-03-31 20:03:24 -07001097
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001098 // for processing seperator style
1099 if (iDec2 < iMax) {
1100 if (iSepStyle == 0 || iSepStyle == 1) {
1101 strValue.Insert(iDec2, '.');
1102 iMax++;
1103 } else if (iSepStyle == 2 || iSepStyle == 3) {
1104 strValue.Insert(iDec2, ',');
1105 iMax++;
1106 }
1107
1108 if (iDec2 == 0)
1109 strValue.Insert(iDec2, '0');
1110 }
1111 if (iSepStyle == 0 || iSepStyle == 2) {
1112 char cSeperator;
1113 if (iSepStyle == 0)
1114 cSeperator = ',';
1115 else
1116 cSeperator = '.';
1117
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001118 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001119 strValue.Insert(iDecPositive, cSeperator);
1120 iMax++;
1121 }
1122 }
dsinclair64376be2016-03-31 20:03:24 -07001123
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124 // negative mark
1125 if (iNegative)
1126 strValue = "-" + strValue;
1127 strValue += "%";
Ryan Harrison275e2602017-09-18 14:23:18 -04001128 Value = WideString::FromLocal(strValue.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001129#endif
tsepez4cf55152016-11-02 14:37:54 -07001130 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001131}
1132// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001133bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001134 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001135 const std::vector<CJS_Value>& params,
1136 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001137 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001138 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139}
1140
1141// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001142bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001143 const std::vector<CJS_Value>& params,
1144 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001145 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001147 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001148 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001150
1151 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1152 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001154 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155
Ryan Harrison275e2602017-09-18 14:23:18 -04001156 WideString& val = pEvent->Value();
1157 WideString strValue = val;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001159 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160
Dan Sinclairc9708952017-10-23 09:40:59 -04001161 WideString sFormat = params[0].ToWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 double dDate = 0.0f;
1163
Ryan Harrison12db7512017-08-23 10:39:35 -04001164 if (strValue.Contains(L"GMT")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 // for GMT format time
1166 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1167 dDate = MakeInterDate(strValue);
1168 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001169 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 }
1171
Tom Sepezdf950b82017-08-04 11:33:49 -07001172 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001173 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001174 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001176 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001177 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 }
1179
1180 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001181 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182}
1183
Ryan Harrison275e2602017-09-18 14:23:18 -04001184double CJS_PublicMethods::MakeInterDate(const WideString& strValue) {
1185 std::vector<WideString> wsArray;
1186 WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001187 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001188 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001189 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190 sTemp = L"";
1191 continue;
1192 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193 sTemp += c;
1194 }
Tom Sepezab277682016-02-17 10:07:21 -08001195 wsArray.push_back(sTemp);
1196 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197 return 0;
1198
Tom Sepez4246b002016-01-20 11:48:29 -08001199 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200 sTemp = wsArray[1];
1201 if (sTemp.Compare(L"Jan") == 0)
1202 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001203 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001205 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001207 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001208 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001209 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001211 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001213 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001215 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001217 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001219 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001221 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001223 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001224 nMonth = 12;
1225
Ryan Harrison275e2602017-09-18 14:23:18 -04001226 int nDay = FX_atof(wsArray[2].AsStringView());
1227 int nHour = FX_atof(wsArray[3].AsStringView());
1228 int nMin = FX_atof(wsArray[4].AsStringView());
1229 int nSec = FX_atof(wsArray[5].AsStringView());
1230 int nYear = FX_atof(wsArray[7].AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001231 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1232 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -07001233 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001234 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235
1236 return dRet;
1237}
1238
1239// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001240bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001241 const std::vector<CJS_Value>& params,
1242 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001243 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001244 if (params.size() != 1) {
1245 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001246 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001247 }
1248
Tom Sepezb1670b52017-02-16 17:01:00 -08001249 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1250 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251 if (pEvent->WillCommit()) {
1252 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001253 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001254
Ryan Harrison275e2602017-09-18 14:23:18 -04001255 WideString strValue = pEvent->Value();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001257 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001258
Dan Sinclairc9708952017-10-23 09:40:59 -04001259 WideString sFormat = params[0].ToWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001260 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001261 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Tom Sepezdf950b82017-08-04 11:33:49 -07001262 if (bWrongFormat || std::isnan(dRet)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001263 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001264 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001266 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001267 pEvent->Rc() = false;
1268 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001269 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270 }
tsepez4cf55152016-11-02 14:37:54 -07001271 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001272}
1273
Tom Sepezb1670b52017-02-16 17:01:00 -08001274bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001275 const std::vector<CJS_Value>& params,
1276 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001277 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001278 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001279 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001280 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001281 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001282
tsepezb4694242016-08-15 16:44:55 -07001283 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001284 const wchar_t* cFormats[] = {L"m/d",
1285 L"m/d/yy",
1286 L"mm/dd/yy",
1287 L"mm/yy",
1288 L"d-mmm",
1289 L"d-mmm-yy",
1290 L"dd-mmm-yy",
1291 L"yy-mm-dd",
1292 L"mmm-yy",
1293 L"mmmm-yy",
1294 L"mmm d, yyyy",
1295 L"mmmm d, yyyy",
1296 L"m/d/yy h:MM tt",
1297 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001298
Lei Zhanga0f67242015-08-17 15:39:30 -07001299 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1300 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001301
Lei Zhang945fdb72015-11-11 10:18:16 -08001302 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001303 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1304 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001306
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001307// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001308bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001309 const std::vector<CJS_Value>& params,
1310 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001311 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001313 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001314 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001315 }
1316
tsepezb4694242016-08-15 16:44:55 -07001317 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001318 const wchar_t* cFormats[] = {L"m/d",
1319 L"m/d/yy",
1320 L"mm/dd/yy",
1321 L"mm/yy",
1322 L"d-mmm",
1323 L"d-mmm-yy",
1324 L"dd-mmm-yy",
1325 L"yy-mm-dd",
1326 L"mmm-yy",
1327 L"mmmm-yy",
1328 L"mmm d, yyyy",
1329 L"mmmm d, yyyy",
1330 L"m/d/yy h:MM tt",
1331 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001332
Lei Zhanga0f67242015-08-17 15:39:30 -07001333 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1334 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001335
Lei Zhang945fdb72015-11-11 10:18:16 -08001336 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001337 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1338 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339}
1340
1341// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001342bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001343 const std::vector<CJS_Value>& params,
1344 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001345 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001346 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001347 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001348 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001349 }
1350
tsepezb4694242016-08-15 16:44:55 -07001351 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001352 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1353 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354
Lei Zhanga0f67242015-08-17 15:39:30 -07001355 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1356 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357
Lei Zhang945fdb72015-11-11 10:18:16 -08001358 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001359 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1360 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361}
1362
Tom Sepezb1670b52017-02-16 17:01:00 -08001363bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001364 const std::vector<CJS_Value>& params,
1365 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001366 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001368 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001369 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001370 }
1371
tsepezb4694242016-08-15 16:44:55 -07001372 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001373 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1374 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375
Lei Zhanga0f67242015-08-17 15:39:30 -07001376 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1377 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378
Lei Zhang945fdb72015-11-11 10:18:16 -08001379 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001380 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1381 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382}
1383
Tom Sepezb1670b52017-02-16 17:01:00 -08001384bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001385 const std::vector<CJS_Value>& params,
1386 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001387 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001388 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001389}
1390
Tom Sepezb1670b52017-02-16 17:01:00 -08001391bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001392 const std::vector<CJS_Value>& params,
1393 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001394 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001395 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396}
1397
1398// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001399bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001400 const std::vector<CJS_Value>& params,
1401 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001402 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001404 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001405 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 }
1407
Tom Sepezb1670b52017-02-16 17:01:00 -08001408 CJS_EventHandler* pEvent =
1409 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001410 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001411 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001412
Ryan Harrison275e2602017-09-18 14:23:18 -04001413 WideString wsSource = pEvent->Value();
1414 WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001415 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001416 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001417 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001418 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001419 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001420 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001422 case 2:
1423 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1424 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 else
tsepez4f1f41f2016-03-28 14:13:16 -07001426 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001428 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001429 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001430 break;
1431 }
1432
tsepez4f1f41f2016-03-28 14:13:16 -07001433 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001434 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001435}
1436
1437// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001438bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001439 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001440 const std::vector<CJS_Value>& params,
1441 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001442 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001444 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001445 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446 }
1447
Tom Sepezb1670b52017-02-16 17:01:00 -08001448 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1449 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001450 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001451 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001452
Ryan Harrison275e2602017-09-18 14:23:18 -04001453 WideString& valEvent = pEvent->Value();
Dan Sinclairc9708952017-10-23 09:40:59 -04001454 WideString wstrMask = params[0].ToWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001455 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001456 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001458 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001459 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001460 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001461
Ryan Harrison875e98c2017-09-27 10:53:11 -04001462 size_t iIndexMask = 0;
thestigcf03f8e2016-05-09 12:36:18 -07001463 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1464 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001465 break;
1466 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001467
thestigcf03f8e2016-05-09 12:36:18 -07001468 if (iIndexMask != wstrMask.GetLength() ||
1469 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001470 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001471 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001472 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001473 }
tsepez4cf55152016-11-02 14:37:54 -07001474 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475 }
1476
Ryan Harrison275e2602017-09-18 14:23:18 -04001477 WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001478 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001479 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480
Ryan Harrison275e2602017-09-18 14:23:18 -04001481 WideString wChange = wideChange;
Ryan Harrison875e98c2017-09-27 10:53:11 -04001482 size_t iIndexMask = pEvent->SelStart();
1483 size_t combined_len = valEvent.GetLength() + wChange.GetLength() +
1484 pEvent->SelStart() - pEvent->SelEnd();
thestigcf03f8e2016-05-09 12:36:18 -07001485 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001486 AlertIfPossible(pContext,
1487 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001488 pEvent->Rc() = false;
1489 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 }
1491
thestigcf03f8e2016-05-09 12:36:18 -07001492 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001493 AlertIfPossible(pContext,
1494 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001495 pEvent->Rc() = false;
1496 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001497 }
1498
Ryan Harrison875e98c2017-09-27 10:53:11 -04001499 for (size_t i = 0; i < wChange.GetLength(); ++i) {
thestigcf03f8e2016-05-09 12:36:18 -07001500 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001501 AlertIfPossible(pContext,
1502 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001503 pEvent->Rc() = false;
1504 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001506 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001507 if (!isReservedMaskChar(wMask))
1508 wChange.SetAt(i, wMask);
1509
1510 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001511 pEvent->Rc() = false;
1512 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001513 }
1514 iIndexMask++;
1515 }
thestigcf03f8e2016-05-09 12:36:18 -07001516 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001517 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001518}
1519
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001520// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001521bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001522 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001523 const std::vector<CJS_Value>& params,
1524 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001525 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001526 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001527 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001528 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001529 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001530
Tom Sepezb1670b52017-02-16 17:01:00 -08001531 CJS_EventHandler* pEvent =
1532 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001533 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001534 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001535
thestigcf03f8e2016-05-09 12:36:18 -07001536 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001537 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001538 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001539 cFormat = "99999";
1540 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001541 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542 cFormat = "999999999";
1543 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001544 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001545 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001546 cFormat = "9999999999";
1547 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001548 cFormat = "9999999";
1549 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001550 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001551 cFormat = "999999999";
1552 break;
1553 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001554
Lei Zhang945fdb72015-11-11 10:18:16 -08001555 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001556 params2.push_back(CJS_Value(pRuntime, cFormat));
1557 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001558}
1559
Tom Sepezb1670b52017-02-16 17:01:00 -08001560bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001561 const std::vector<CJS_Value>& params,
1562 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001563 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001564 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001565 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001566 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001567 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001568
Tom Sepezb1670b52017-02-16 17:01:00 -08001569 CJS_EventHandler* pEventHandler =
1570 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001571
Ryan Harrison275e2602017-09-18 14:23:18 -04001572 WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001573 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001574 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001575
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001576 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001577 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001578 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579 }
1580
Ryan Harrison275e2602017-09-18 14:23:18 -04001581 WideString prefix, postfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001582
1583 if (pEventHandler->SelStart() >= 0)
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001584 prefix = swValue.Left(pEventHandler->SelStart());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001585 else
1586 prefix = L"";
1587
1588 if (pEventHandler->SelEnd() >= 0 &&
Ryan Harrison875e98c2017-09-27 10:53:11 -04001589 static_cast<size_t>(pEventHandler->SelEnd()) <= swValue.GetLength())
Ryan Harrison75584142017-09-01 13:30:19 -04001590 postfix = swValue.Right(swValue.GetLength() -
Ryan Harrison875e98c2017-09-27 10:53:11 -04001591 static_cast<size_t>(pEventHandler->SelEnd()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001592 else
1593 postfix = L"";
1594
tsepezf3dc8c62016-08-10 06:29:29 -07001595 vRet =
1596 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001597 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001598}
1599
Tom Sepezb1670b52017-02-16 17:01:00 -08001600bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001601 const std::vector<CJS_Value>& params,
1602 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001603 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001604 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001605 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001606 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001607 }
1608
Dan Sinclairc9708952017-10-23 09:40:59 -04001609 WideString sValue = params[0].ToWideString(pRuntime);
1610 WideString sFormat = params[1].ToWideString(pRuntime);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001611 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Tom Sepezdf950b82017-08-04 11:33:49 -07001612 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001613 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001614 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001615 sFormat.c_str());
Tom Sepezb1670b52017-02-16 17:01:00 -08001616 AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001617 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001618 }
1619
tsepezf3dc8c62016-08-10 06:29:29 -07001620 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001621 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001622}
1623
Tom Sepezb1670b52017-02-16 17:01:00 -08001624bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001625 const std::vector<CJS_Value>& params,
1626 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001627 WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001628 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001629 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001630 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001631 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001632
tsepezb4694242016-08-15 16:44:55 -07001633 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
Dan Sinclairc9708952017-10-23 09:40:59 -04001634 params[0].ToWideString(pRuntime).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001635 params[1].ToDouble(pRuntime),
1636 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001637
tsepez4cf55152016-11-02 14:37:54 -07001638 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001639}
1640
Tom Sepezb1670b52017-02-16 17:01:00 -08001641bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001642 const std::vector<CJS_Value>& params,
1643 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001644 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001646 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001647 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648 }
tsepezf3dc8c62016-08-10 06:29:29 -07001649
Dan Sinclairc9708952017-10-23 09:40:59 -04001650 WideString ws = params[0].ToWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001651 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001652 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001653 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001654 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001655 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001656 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001658
Tom Sepezb1670b52017-02-16 17:01:00 -08001659bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001660 const std::vector<CJS_Value>& params,
1661 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001662 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001663 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001664 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001665 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001666 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001667
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001669 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001670 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001671 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001672 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001673
dsinclair4526faf2016-10-11 10:54:49 -07001674 CPDFSDK_InterForm* pReaderInterForm =
Tom Sepezb1670b52017-02-16 17:01:00 -08001675 pRuntime->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001677
Dan Sinclairc9708952017-10-23 09:40:59 -04001678 WideString sFunction = params[0].ToWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001679 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001680
Tom Sepez67fd5df2015-10-08 12:24:19 -07001681 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001682 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001683
tsepezb4694242016-08-15 16:44:55 -07001684 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Dan Sinclairc9708952017-10-23 09:40:59 -04001685 CJS_Value jsValue(FieldNameArray.GetElement(pRuntime, i));
1686 WideString wsFieldName = jsValue.ToWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001687
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001688 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1689 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1690 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001691 switch (pFormField->GetFieldType()) {
1692 case FIELDTYPE_TEXTFIELD:
1693 case FIELDTYPE_COMBOBOX: {
Ryan Harrison275e2602017-09-18 14:23:18 -04001694 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001695 trimmed.TrimRight();
1696 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001697 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001698 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001699 case FIELDTYPE_PUSHBUTTON: {
1700 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001701 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001702 case FIELDTYPE_CHECKBOX:
1703 case FIELDTYPE_RADIOBUTTON: {
1704 dTemp = 0.0;
1705 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1706 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1707 if (pFormCtrl->IsChecked()) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001708 WideString trimmed = pFormCtrl->GetExportValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001709 trimmed.TrimRight();
1710 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001711 dTemp = FX_atof(trimmed.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001712 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001713 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001714 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001715 }
Tom Sepez4246b002016-01-20 11:48:29 -08001716 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001717 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001718 if (pFormField->CountSelectedItems() <= 1) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001719 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001720 trimmed.TrimRight();
1721 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001722 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001723 }
1724 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001725 default:
1726 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001727 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001728
1729 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1730 wcscmp(sFunction.c_str(), L"MAX") == 0))
1731 dValue = dTemp;
1732
1733 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1734
1735 nFieldsCount++;
1736 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001737 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001738 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001739
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001740 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1741 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001742
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001743 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1744 FXSYS_pow((double)10, (double)6);
Tom Sepezb1670b52017-02-16 17:01:00 -08001745
Tom Sepez67fd5df2015-10-08 12:24:19 -07001746 CJS_Value jsValue(pRuntime, dValue);
Tom Sepezb1670b52017-02-16 17:01:00 -08001747 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
foxit8b544ed2015-09-10 14:57:54 +08001748 if (pContext->GetEventHandler()->m_pValue)
Dan Sinclairc9708952017-10-23 09:40:59 -04001749 pContext->GetEventHandler()->Value() = jsValue.ToWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001750
tsepez4cf55152016-11-02 14:37:54 -07001751 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001752}
1753
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001754/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001755** within the specified range. */
1756
Tom Sepezb1670b52017-02-16 17:01:00 -08001757bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001758 const std::vector<CJS_Value>& params,
1759 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001760 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001761 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001762 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001763 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001764 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001765 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
tsepezcd5dc852016-09-08 11:23:24 -07001766 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001767 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001768 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001769
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001770 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001771 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001772
Ryan Harrison275e2602017-09-18 14:23:18 -04001773 double dEentValue = atof(ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001774 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001775 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001776 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001777 double dLessThan = params[3].ToDouble(pRuntime);
Ryan Harrison275e2602017-09-18 14:23:18 -04001778 WideString swMsg;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001779
1780 if (bGreaterThan && bLessThan) {
1781 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001782 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001783 params[1].ToWideString(pRuntime).c_str(),
1784 params[3].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001785 } else if (bGreaterThan) {
1786 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001787 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001788 params[1].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001789 } else if (bLessThan) {
1790 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001791 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001792 params[3].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001793 }
1794
1795 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001796 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001797 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001798 }
tsepez4cf55152016-11-02 14:37:54 -07001799 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001800}
1801
Tom Sepezb1670b52017-02-16 17:01:00 -08001802bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001803 const std::vector<CJS_Value>& params,
1804 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001805 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001806 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001807 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001808 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001809 }
1810
Dan Sinclairc9708952017-10-23 09:40:59 -04001811 WideString str = params[0].ToWideString(pRuntime);
Ryan Harrison75c65212017-08-16 13:52:15 -04001812 if (str.GetLength() > 0 && (str[0] == L'.' || str[0] == L','))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001813 str = L"0" + str;
1814
Ryan Harrison275e2602017-09-18 14:23:18 -04001815 WideString sPart;
Tom Sepezb1670b52017-02-16 17:01:00 -08001816 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001817 int nIndex = 0;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001818 for (const auto& wc : str) {
Lei Zhange247ec42017-04-20 21:41:36 -07001819 if (std::iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001820 sPart += wc;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001821 } else if (sPart.GetLength() > 0) {
1822 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
1823 sPart = L"";
1824 nIndex++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001825 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001826 }
Tom Sepez3c3e2712017-04-17 15:38:19 -07001827 if (sPart.GetLength() > 0)
tsepezb4694242016-08-15 16:44:55 -07001828 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001829
tsepezb4694242016-08-15 16:44:55 -07001830 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001831 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001832 else
tsepezf3dc8c62016-08-10 06:29:29 -07001833 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001834
tsepez4cf55152016-11-02 14:37:54 -07001835 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001836}