blob: 5e428007ad59c4965f64caa7557c453d7c0b4482 [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 Sinclair0e187d82017-10-23 12:08:34 -0400927 CFX_Color crProp =
928 color::ConvertArrayToPWLColor(pRuntime, vProp.ToArray(pRuntime));
929 CFX_Color crColor = color::ConvertArrayToPWLColor(pRuntime, arColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930 if (crColor != crProp) {
Dan Sinclair0e187d82017-10-23 12:08:34 -0400931 fTarget->set_text_color(pRuntime, CJS_Value(pRuntime, arColor),
932 &sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700933 }
934 }
935 }
936 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937#endif
tsepez4cf55152016-11-02 14:37:54 -0700938 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700939}
940
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
942// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800943bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700944 const std::vector<CJS_Value>& params,
945 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400946 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700948 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700949
Tom Sepezb1670b52017-02-16 17:01:00 -0800950 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
951 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700953 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700954
Ryan Harrison275e2602017-09-18 14:23:18 -0400955 WideString& val = pEvent->Value();
956 WideString& wstrChange = pEvent->Change();
957 WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700958
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 if (pEvent->WillCommit()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400960 WideString swTemp = StrTrim(wstrValue);
ochanga0a3bc32016-05-12 15:22:48 -0700961 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700962 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700963
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964 swTemp.Replace(L",", L".");
965 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700966 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700967 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700968 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700969 }
tsepez4cf55152016-11-02 14:37:54 -0700970 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700972
Ryan Harrison275e2602017-09-18 14:23:18 -0400973 WideString wstrSelected;
thestigcf03f8e2016-05-09 12:36:18 -0700974 if (pEvent->SelStart() != -1) {
975 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
976 pEvent->SelEnd() - pEvent->SelStart());
977 }
978
Ryan Harrison12db7512017-08-23 10:39:35 -0400979 bool bHasSign = wstrValue.Contains(L'-') && !wstrSelected.Contains(L'-');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 if (bHasSign) {
981 // can't insert "change" in front to sign postion.
982 if (pEvent->SelStart() == 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700983 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700984 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700985 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700987
tsepezb4694242016-08-15 16:44:55 -0700988 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700989 if (iSepStyle < 0 || iSepStyle > 3)
990 iSepStyle = 0;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400991 const wchar_t cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700992
Ryan Harrison12db7512017-08-23 10:39:35 -0400993 bool bHasSep = wstrValue.Contains(cSep);
Ryan Harrison875e98c2017-09-27 10:53:11 -0400994 for (size_t i = 0; i < wstrChange.GetLength(); ++i) {
thestigcf03f8e2016-05-09 12:36:18 -0700995 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 if (bHasSep) {
Lei Zhange247ec42017-04-20 21:41:36 -0700997 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700998 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999 }
tsepez4cf55152016-11-02 14:37:54 -07001000 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001 continue;
1002 }
thestigcf03f8e2016-05-09 12:36:18 -07001003 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 if (bHasSign) {
Lei Zhange247ec42017-04-20 21:41:36 -07001005 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -07001006 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007 }
Lei Zhang9559b7a2015-12-21 11:12:20 -08001008 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -07001009 if (i != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -07001010 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -07001011 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012 }
1013 if (pEvent->SelStart() != 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 }
tsepez4cf55152016-11-02 14:37:54 -07001017 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 continue;
1019 }
1020
Lei Zhange247ec42017-04-20 21:41:36 -07001021 if (!std::iswdigit(wstrChange[i])) {
1022 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -07001023 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 }
1025 }
1026
Ryan Harrison275e2602017-09-18 14:23:18 -04001027 WideString wprefix = wstrValue.Left(pEvent->SelStart());
1028 WideString wpostfix;
Ryan Harrison75584142017-09-01 13:30:19 -04001029 if (pEvent->SelEnd() >= 0 &&
Ryan Harrison875e98c2017-09-27 10:53:11 -04001030 static_cast<size_t>(pEvent->SelEnd()) < wstrValue.GetLength())
Ryan Harrison75584142017-09-01 13:30:19 -04001031 wpostfix = wstrValue.Right(wstrValue.GetLength() -
Ryan Harrison875e98c2017-09-27 10:53:11 -04001032 static_cast<size_t>(pEvent->SelEnd()));
thestigcf03f8e2016-05-09 12:36:18 -07001033 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -07001034 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035}
1036
1037// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -08001038bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001039 const std::vector<CJS_Value>& params,
1040 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001041 WideString& sError) {
Dan Sinclair698aed72017-09-26 16:24:49 -04001042#if _FX_OS_ != _FX_OS_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001044 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001045 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001047
1048 CJS_EventHandler* pEvent =
1049 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001050 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001051 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001052
Ryan Harrison275e2602017-09-18 14:23:18 -04001053 WideString& Value = pEvent->Value();
1054 ByteString strValue = StrTrim(ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001056 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057
tsepezb4694242016-08-15 16:44:55 -07001058 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 if (iDec < 0)
1060 iDec = -iDec;
1061
tsepezb4694242016-08-15 16:44:55 -07001062 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063 if (iSepStyle < 0 || iSepStyle > 3)
1064 iSepStyle = 0;
1065
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001067 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068 dValue *= 100;
1069 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001070 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071
1072 int iDec2;
1073 int iNegative = 0;
1074 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1075 if (strValue.IsEmpty()) {
1076 dValue = 0;
1077 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1078 }
1079
1080 if (iDec2 < 0) {
1081 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1082 strValue = "0" + strValue;
1083 }
1084 iDec2 = 0;
1085 }
1086 int iMax = strValue.GetLength();
1087 if (iDec2 > iMax) {
1088 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1089 strValue += "0";
1090 }
1091 iMax = iDec2 + 1;
1092 }
dsinclair64376be2016-03-31 20:03:24 -07001093
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 // for processing seperator style
1095 if (iDec2 < iMax) {
1096 if (iSepStyle == 0 || iSepStyle == 1) {
1097 strValue.Insert(iDec2, '.');
1098 iMax++;
1099 } else if (iSepStyle == 2 || iSepStyle == 3) {
1100 strValue.Insert(iDec2, ',');
1101 iMax++;
1102 }
1103
1104 if (iDec2 == 0)
1105 strValue.Insert(iDec2, '0');
1106 }
1107 if (iSepStyle == 0 || iSepStyle == 2) {
1108 char cSeperator;
1109 if (iSepStyle == 0)
1110 cSeperator = ',';
1111 else
1112 cSeperator = '.';
1113
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001114 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001115 strValue.Insert(iDecPositive, cSeperator);
1116 iMax++;
1117 }
1118 }
dsinclair64376be2016-03-31 20:03:24 -07001119
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120 // negative mark
1121 if (iNegative)
1122 strValue = "-" + strValue;
1123 strValue += "%";
Ryan Harrison275e2602017-09-18 14:23:18 -04001124 Value = WideString::FromLocal(strValue.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001125#endif
tsepez4cf55152016-11-02 14:37:54 -07001126 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127}
1128// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001129bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001130 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001131 const std::vector<CJS_Value>& params,
1132 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001133 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001134 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135}
1136
1137// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001138bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001139 const std::vector<CJS_Value>& params,
1140 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001141 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001143 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001144 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001146
1147 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1148 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001150 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151
Ryan Harrison275e2602017-09-18 14:23:18 -04001152 WideString& val = pEvent->Value();
1153 WideString strValue = val;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001155 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156
Dan Sinclairc9708952017-10-23 09:40:59 -04001157 WideString sFormat = params[0].ToWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 double dDate = 0.0f;
1159
Ryan Harrison12db7512017-08-23 10:39:35 -04001160 if (strValue.Contains(L"GMT")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 // for GMT format time
1162 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1163 dDate = MakeInterDate(strValue);
1164 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001165 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 }
1167
Tom Sepezdf950b82017-08-04 11:33:49 -07001168 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001169 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001170 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001172 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001173 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174 }
1175
1176 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001177 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178}
1179
Ryan Harrison275e2602017-09-18 14:23:18 -04001180double CJS_PublicMethods::MakeInterDate(const WideString& strValue) {
1181 std::vector<WideString> wsArray;
1182 WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001183 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001185 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186 sTemp = L"";
1187 continue;
1188 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189 sTemp += c;
1190 }
Tom Sepezab277682016-02-17 10:07:21 -08001191 wsArray.push_back(sTemp);
1192 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193 return 0;
1194
Tom Sepez4246b002016-01-20 11:48:29 -08001195 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196 sTemp = wsArray[1];
1197 if (sTemp.Compare(L"Jan") == 0)
1198 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001199 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001201 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001203 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001205 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001207 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001208 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001209 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001211 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001213 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001215 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001217 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001219 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 nMonth = 12;
1221
Ryan Harrison275e2602017-09-18 14:23:18 -04001222 int nDay = FX_atof(wsArray[2].AsStringView());
1223 int nHour = FX_atof(wsArray[3].AsStringView());
1224 int nMin = FX_atof(wsArray[4].AsStringView());
1225 int nSec = FX_atof(wsArray[5].AsStringView());
1226 int nYear = FX_atof(wsArray[7].AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1228 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -07001229 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001230 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001231
1232 return dRet;
1233}
1234
1235// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001236bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001237 const std::vector<CJS_Value>& params,
1238 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001239 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001240 if (params.size() != 1) {
1241 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001242 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001243 }
1244
Tom Sepezb1670b52017-02-16 17:01:00 -08001245 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1246 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001247 if (pEvent->WillCommit()) {
1248 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001249 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001250
Ryan Harrison275e2602017-09-18 14:23:18 -04001251 WideString strValue = pEvent->Value();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001252 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001253 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001254
Dan Sinclairc9708952017-10-23 09:40:59 -04001255 WideString sFormat = params[0].ToWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001256 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001257 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Tom Sepezdf950b82017-08-04 11:33:49 -07001258 if (bWrongFormat || std::isnan(dRet)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001259 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001260 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001261 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001262 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001263 pEvent->Rc() = false;
1264 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001265 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 }
tsepez4cf55152016-11-02 14:37:54 -07001267 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001268}
1269
Tom Sepezb1670b52017-02-16 17:01:00 -08001270bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001271 const std::vector<CJS_Value>& params,
1272 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001273 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001274 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001275 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001276 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001278
tsepezb4694242016-08-15 16:44:55 -07001279 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001280 const wchar_t* cFormats[] = {L"m/d",
1281 L"m/d/yy",
1282 L"mm/dd/yy",
1283 L"mm/yy",
1284 L"d-mmm",
1285 L"d-mmm-yy",
1286 L"dd-mmm-yy",
1287 L"yy-mm-dd",
1288 L"mmm-yy",
1289 L"mmmm-yy",
1290 L"mmm d, yyyy",
1291 L"mmmm d, yyyy",
1292 L"m/d/yy h:MM tt",
1293 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001294
Lei Zhanga0f67242015-08-17 15:39:30 -07001295 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1296 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001297
Lei Zhang945fdb72015-11-11 10:18:16 -08001298 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001299 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1300 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001302
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001304bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001305 const std::vector<CJS_Value>& params,
1306 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001307 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001308 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001309 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001310 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311 }
1312
tsepezb4694242016-08-15 16:44:55 -07001313 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001314 const wchar_t* cFormats[] = {L"m/d",
1315 L"m/d/yy",
1316 L"mm/dd/yy",
1317 L"mm/yy",
1318 L"d-mmm",
1319 L"d-mmm-yy",
1320 L"dd-mmm-yy",
1321 L"yy-mm-dd",
1322 L"mmm-yy",
1323 L"mmmm-yy",
1324 L"mmm d, yyyy",
1325 L"mmmm d, yyyy",
1326 L"m/d/yy h:MM tt",
1327 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001328
Lei Zhanga0f67242015-08-17 15:39:30 -07001329 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1330 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001331
Lei Zhang945fdb72015-11-11 10:18:16 -08001332 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001333 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1334 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001335}
1336
1337// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001338bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001339 const std::vector<CJS_Value>& params,
1340 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001341 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001342 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001343 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001344 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001345 }
1346
tsepezb4694242016-08-15 16:44:55 -07001347 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001348 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1349 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001350
Lei Zhanga0f67242015-08-17 15:39:30 -07001351 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1352 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001353
Lei Zhang945fdb72015-11-11 10:18:16 -08001354 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001355 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1356 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357}
1358
Tom Sepezb1670b52017-02-16 17:01:00 -08001359bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001360 const std::vector<CJS_Value>& params,
1361 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001362 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001364 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001365 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001366 }
1367
tsepezb4694242016-08-15 16:44:55 -07001368 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001369 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1370 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001371
Lei Zhanga0f67242015-08-17 15:39:30 -07001372 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1373 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374
Lei Zhang945fdb72015-11-11 10:18:16 -08001375 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001376 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1377 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378}
1379
Tom Sepezb1670b52017-02-16 17:01:00 -08001380bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001381 const std::vector<CJS_Value>& params,
1382 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001383 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001384 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001385}
1386
Tom Sepezb1670b52017-02-16 17:01:00 -08001387bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001388 const std::vector<CJS_Value>& params,
1389 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001390 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001391 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001392}
1393
1394// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001395bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001396 const std::vector<CJS_Value>& params,
1397 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001398 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001399 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001400 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001401 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001402 }
1403
Tom Sepezb1670b52017-02-16 17:01:00 -08001404 CJS_EventHandler* pEvent =
1405 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001407 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408
Ryan Harrison275e2602017-09-18 14:23:18 -04001409 WideString wsSource = pEvent->Value();
1410 WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001411 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001412 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001413 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001414 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001415 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001416 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001417 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001418 case 2:
1419 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1420 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421 else
tsepez4f1f41f2016-03-28 14:13:16 -07001422 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001424 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001425 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001426 break;
1427 }
1428
tsepez4f1f41f2016-03-28 14:13:16 -07001429 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001430 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431}
1432
1433// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001434bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001435 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001436 const std::vector<CJS_Value>& params,
1437 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001438 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001439 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001440 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001441 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001442 }
1443
Tom Sepezb1670b52017-02-16 17:01:00 -08001444 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1445 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001447 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448
Ryan Harrison275e2602017-09-18 14:23:18 -04001449 WideString& valEvent = pEvent->Value();
Dan Sinclairc9708952017-10-23 09:40:59 -04001450 WideString wstrMask = params[0].ToWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001452 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001453
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001454 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001455 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001456 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001457
Ryan Harrison875e98c2017-09-27 10:53:11 -04001458 size_t iIndexMask = 0;
thestigcf03f8e2016-05-09 12:36:18 -07001459 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1460 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001461 break;
1462 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001463
thestigcf03f8e2016-05-09 12:36:18 -07001464 if (iIndexMask != wstrMask.GetLength() ||
1465 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001466 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001467 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001468 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469 }
tsepez4cf55152016-11-02 14:37:54 -07001470 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001471 }
1472
Ryan Harrison275e2602017-09-18 14:23:18 -04001473 WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001474 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001475 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001476
Ryan Harrison275e2602017-09-18 14:23:18 -04001477 WideString wChange = wideChange;
Ryan Harrison875e98c2017-09-27 10:53:11 -04001478 size_t iIndexMask = pEvent->SelStart();
1479 size_t combined_len = valEvent.GetLength() + wChange.GetLength() +
1480 pEvent->SelStart() - pEvent->SelEnd();
thestigcf03f8e2016-05-09 12:36:18 -07001481 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001482 AlertIfPossible(pContext,
1483 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001484 pEvent->Rc() = false;
1485 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001486 }
1487
thestigcf03f8e2016-05-09 12:36:18 -07001488 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001489 AlertIfPossible(pContext,
1490 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001491 pEvent->Rc() = false;
1492 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001493 }
1494
Ryan Harrison875e98c2017-09-27 10:53:11 -04001495 for (size_t i = 0; i < wChange.GetLength(); ++i) {
thestigcf03f8e2016-05-09 12:36:18 -07001496 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001497 AlertIfPossible(pContext,
1498 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001499 pEvent->Rc() = false;
1500 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001502 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001503 if (!isReservedMaskChar(wMask))
1504 wChange.SetAt(i, wMask);
1505
1506 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001507 pEvent->Rc() = false;
1508 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509 }
1510 iIndexMask++;
1511 }
thestigcf03f8e2016-05-09 12:36:18 -07001512 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001513 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001514}
1515
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001517bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001518 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001519 const std::vector<CJS_Value>& params,
1520 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001521 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001523 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001524 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001525 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001526
Tom Sepezb1670b52017-02-16 17:01:00 -08001527 CJS_EventHandler* pEvent =
1528 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001529 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001530 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001531
thestigcf03f8e2016-05-09 12:36:18 -07001532 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001533 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001534 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001535 cFormat = "99999";
1536 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001537 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 cFormat = "999999999";
1539 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001540 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001541 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542 cFormat = "9999999999";
1543 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544 cFormat = "9999999";
1545 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001546 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001547 cFormat = "999999999";
1548 break;
1549 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001550
Lei Zhang945fdb72015-11-11 10:18:16 -08001551 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001552 params2.push_back(CJS_Value(pRuntime, cFormat));
1553 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001554}
1555
Tom Sepezb1670b52017-02-16 17:01:00 -08001556bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001557 const std::vector<CJS_Value>& params,
1558 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001559 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001560 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001561 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001562 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001563 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001564
Tom Sepezb1670b52017-02-16 17:01:00 -08001565 CJS_EventHandler* pEventHandler =
1566 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001567
Ryan Harrison275e2602017-09-18 14:23:18 -04001568 WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001569 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001571
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001572 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001573 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001574 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001575 }
1576
Ryan Harrison275e2602017-09-18 14:23:18 -04001577 WideString prefix, postfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001578
1579 if (pEventHandler->SelStart() >= 0)
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001580 prefix = swValue.Left(pEventHandler->SelStart());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001581 else
1582 prefix = L"";
1583
1584 if (pEventHandler->SelEnd() >= 0 &&
Ryan Harrison875e98c2017-09-27 10:53:11 -04001585 static_cast<size_t>(pEventHandler->SelEnd()) <= swValue.GetLength())
Ryan Harrison75584142017-09-01 13:30:19 -04001586 postfix = swValue.Right(swValue.GetLength() -
Ryan Harrison875e98c2017-09-27 10:53:11 -04001587 static_cast<size_t>(pEventHandler->SelEnd()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001588 else
1589 postfix = L"";
1590
tsepezf3dc8c62016-08-10 06:29:29 -07001591 vRet =
1592 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001593 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001594}
1595
Tom Sepezb1670b52017-02-16 17:01:00 -08001596bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001597 const std::vector<CJS_Value>& params,
1598 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001599 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001600 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001601 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001602 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001603 }
1604
Dan Sinclairc9708952017-10-23 09:40:59 -04001605 WideString sValue = params[0].ToWideString(pRuntime);
1606 WideString sFormat = params[1].ToWideString(pRuntime);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001607 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Tom Sepezdf950b82017-08-04 11:33:49 -07001608 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001609 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001610 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001611 sFormat.c_str());
Tom Sepezb1670b52017-02-16 17:01:00 -08001612 AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001613 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001614 }
1615
tsepezf3dc8c62016-08-10 06:29:29 -07001616 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001617 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001618}
1619
Tom Sepezb1670b52017-02-16 17:01:00 -08001620bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001621 const std::vector<CJS_Value>& params,
1622 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001623 WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001624 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001625 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001626 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001628
tsepezb4694242016-08-15 16:44:55 -07001629 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
Dan Sinclairc9708952017-10-23 09:40:59 -04001630 params[0].ToWideString(pRuntime).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001631 params[1].ToDouble(pRuntime),
1632 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001633
tsepez4cf55152016-11-02 14:37:54 -07001634 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001635}
1636
Tom Sepezb1670b52017-02-16 17:01:00 -08001637bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001638 const std::vector<CJS_Value>& params,
1639 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001640 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001641 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001642 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001643 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001644 }
tsepezf3dc8c62016-08-10 06:29:29 -07001645
Dan Sinclairc9708952017-10-23 09:40:59 -04001646 WideString ws = params[0].ToWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001647 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001648 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001649 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001650 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001651 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001652 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001653}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001654
Tom Sepezb1670b52017-02-16 17:01:00 -08001655bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001656 const std::vector<CJS_Value>& params,
1657 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001658 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001659 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001660 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001661 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001662 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001663
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001665 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001666 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001667 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001669
dsinclair4526faf2016-10-11 10:54:49 -07001670 CPDFSDK_InterForm* pReaderInterForm =
Tom Sepezb1670b52017-02-16 17:01:00 -08001671 pRuntime->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001672 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001673
Dan Sinclairc9708952017-10-23 09:40:59 -04001674 WideString sFunction = params[0].ToWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001675 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001676
Tom Sepez67fd5df2015-10-08 12:24:19 -07001677 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001678 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001679
tsepezb4694242016-08-15 16:44:55 -07001680 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Dan Sinclairc9708952017-10-23 09:40:59 -04001681 CJS_Value jsValue(FieldNameArray.GetElement(pRuntime, i));
1682 WideString wsFieldName = jsValue.ToWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001683
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001684 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1685 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1686 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001687 switch (pFormField->GetFieldType()) {
1688 case FIELDTYPE_TEXTFIELD:
1689 case FIELDTYPE_COMBOBOX: {
Ryan Harrison275e2602017-09-18 14:23:18 -04001690 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001691 trimmed.TrimRight();
1692 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001693 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001694 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001695 case FIELDTYPE_PUSHBUTTON: {
1696 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001697 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001698 case FIELDTYPE_CHECKBOX:
1699 case FIELDTYPE_RADIOBUTTON: {
1700 dTemp = 0.0;
1701 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1702 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1703 if (pFormCtrl->IsChecked()) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001704 WideString trimmed = pFormCtrl->GetExportValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001705 trimmed.TrimRight();
1706 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001707 dTemp = FX_atof(trimmed.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001708 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001709 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001710 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001711 }
Tom Sepez4246b002016-01-20 11:48:29 -08001712 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001714 if (pFormField->CountSelectedItems() <= 1) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001715 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001716 trimmed.TrimRight();
1717 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001718 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001719 }
1720 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001721 default:
1722 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001723 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001724
1725 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1726 wcscmp(sFunction.c_str(), L"MAX") == 0))
1727 dValue = dTemp;
1728
1729 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1730
1731 nFieldsCount++;
1732 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001733 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001734 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001735
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001736 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1737 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001738
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001739 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1740 FXSYS_pow((double)10, (double)6);
Tom Sepezb1670b52017-02-16 17:01:00 -08001741
Tom Sepez67fd5df2015-10-08 12:24:19 -07001742 CJS_Value jsValue(pRuntime, dValue);
Tom Sepezb1670b52017-02-16 17:01:00 -08001743 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
foxit8b544ed2015-09-10 14:57:54 +08001744 if (pContext->GetEventHandler()->m_pValue)
Dan Sinclairc9708952017-10-23 09:40:59 -04001745 pContext->GetEventHandler()->Value() = jsValue.ToWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001746
tsepez4cf55152016-11-02 14:37:54 -07001747 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001748}
1749
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001750/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001751** within the specified range. */
1752
Tom Sepezb1670b52017-02-16 17:01:00 -08001753bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001754 const std::vector<CJS_Value>& params,
1755 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001756 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001757 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001758 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001759 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001760 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001761 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
tsepezcd5dc852016-09-08 11:23:24 -07001762 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001763 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001764 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001765
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001766 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001767 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001768
Ryan Harrison275e2602017-09-18 14:23:18 -04001769 double dEentValue = atof(ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001770 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001771 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001772 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001773 double dLessThan = params[3].ToDouble(pRuntime);
Ryan Harrison275e2602017-09-18 14:23:18 -04001774 WideString swMsg;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001775
1776 if (bGreaterThan && bLessThan) {
1777 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001778 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001779 params[1].ToWideString(pRuntime).c_str(),
1780 params[3].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 } else if (bGreaterThan) {
1782 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001783 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001784 params[1].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001785 } else if (bLessThan) {
1786 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001787 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001788 params[3].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001789 }
1790
1791 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001792 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001793 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001794 }
tsepez4cf55152016-11-02 14:37:54 -07001795 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001796}
1797
Tom Sepezb1670b52017-02-16 17:01:00 -08001798bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001799 const std::vector<CJS_Value>& params,
1800 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001801 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001802 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001803 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001804 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001805 }
1806
Dan Sinclairc9708952017-10-23 09:40:59 -04001807 WideString str = params[0].ToWideString(pRuntime);
Ryan Harrison75c65212017-08-16 13:52:15 -04001808 if (str.GetLength() > 0 && (str[0] == L'.' || str[0] == L','))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001809 str = L"0" + str;
1810
Ryan Harrison275e2602017-09-18 14:23:18 -04001811 WideString sPart;
Tom Sepezb1670b52017-02-16 17:01:00 -08001812 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001813 int nIndex = 0;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001814 for (const auto& wc : str) {
Lei Zhange247ec42017-04-20 21:41:36 -07001815 if (std::iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001816 sPart += wc;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001817 } else if (sPart.GetLength() > 0) {
1818 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
1819 sPart = L"";
1820 nIndex++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001821 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001822 }
Tom Sepez3c3e2712017-04-17 15:38:19 -07001823 if (sPart.GetLength() > 0)
tsepezb4694242016-08-15 16:44:55 -07001824 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001825
tsepezb4694242016-08-15 16:44:55 -07001826 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001827 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001828 else
tsepezf3dc8c62016-08-10 06:29:29 -07001829 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001830
tsepez4cf55152016-11-02 14:37:54 -07001831 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001832}