blob: 6e0ebef0e850528c4cac603db49c59799b334bc1 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/PublicMethods.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
Tom Sepezdf950b82017-08-04 11:33:49 -070010#include <cmath>
Lei Zhange247ec42017-04-20 21:41:36 -070011#include <cwctype>
npm49c59282016-11-15 15:14:04 -080012#include <iomanip>
dsinclair992ecf72016-12-14 05:45:57 -080013#include <limits>
npm49c59282016-11-15 15:14:04 -080014#include <sstream>
15#include <string>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050016#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080017
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpdf_interform.h"
Dan Sinclaircfb19442017-04-20 13:13:04 -040019#include "core/fxcrt/fx_extension.h"
dsinclair735606d2016-10-05 15:47:02 -070020#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070021#include "fpdfsdk/cpdfsdk_interform.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040022#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040023#include "fpdfsdk/javascript/JS_Define.h"
24#include "fpdfsdk/javascript/JS_EventHandler.h"
25#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040026#include "fpdfsdk/javascript/JS_Value.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080027#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070028#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040029#include "fpdfsdk/javascript/color.h"
30#include "fpdfsdk/javascript/resource.h"
31#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034
Tom Sepez04557b82017-02-16 09:43:10 -080035JSMethodSpec CJS_PublicMethods::GlobalFunctionSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -080036 {"AFNumber_Format", AFNumber_Format_static},
37 {"AFNumber_Keystroke", AFNumber_Keystroke_static},
38 {"AFPercent_Format", AFPercent_Format_static},
39 {"AFPercent_Keystroke", AFPercent_Keystroke_static},
40 {"AFDate_FormatEx", AFDate_FormatEx_static},
41 {"AFDate_KeystrokeEx", AFDate_KeystrokeEx_static},
42 {"AFDate_Format", AFDate_Format_static},
43 {"AFDate_Keystroke", AFDate_Keystroke_static},
44 {"AFTime_FormatEx", AFTime_FormatEx_static},
45 {"AFTime_KeystrokeEx", AFTime_KeystrokeEx_static},
46 {"AFTime_Format", AFTime_Format_static},
47 {"AFTime_Keystroke", AFTime_Keystroke_static},
48 {"AFSpecial_Format", AFSpecial_Format_static},
49 {"AFSpecial_Keystroke", AFSpecial_Keystroke_static},
50 {"AFSpecial_KeystrokeEx", AFSpecial_KeystrokeEx_static},
51 {"AFSimple", AFSimple_static},
52 {"AFMakeNumber", AFMakeNumber_static},
53 {"AFSimple_Calculate", AFSimple_Calculate_static},
54 {"AFRange_Validate", AFRange_Validate_static},
55 {"AFMergeChange", AFMergeChange_static},
56 {"AFParseDateEx", AFParseDateEx_static},
57 {"AFExtractNums", AFExtractNums_static},
Tom Sepez04557b82017-02-16 09:43:10 -080058 {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059
60IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
61
tsepez745611b2016-04-12 16:46:34 -070062namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063
Dan Sinclair812e96c2017-03-13 16:43:37 -040064const wchar_t* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
65 L"May", L"Jun", L"Jul", L"Aug",
66 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Dan Sinclair812e96c2017-03-13 16:43:37 -040068const wchar_t* const fullmonths[] = {L"January", L"February", L"March",
69 L"April", L"May", L"June",
70 L"July", L"August", L"September",
71 L"October", L"November", L"December"};
tsepez745611b2016-04-12 16:46:34 -070072
Ryan Harrison275e2602017-09-18 14:23:18 -040073ByteString StrTrim(const ByteString& pStr) {
74 ByteString result(pStr);
tsepez745611b2016-04-12 16:46:34 -070075 result.TrimLeft(' ');
76 result.TrimRight(' ');
77 return result;
78}
79
Ryan Harrison275e2602017-09-18 14:23:18 -040080WideString StrTrim(const WideString& pStr) {
81 WideString result(pStr);
tsepez745611b2016-04-12 16:46:34 -070082 result.TrimLeft(' ');
83 result.TrimRight(' ');
84 return result;
85}
86
Dan Sinclair812e96c2017-03-13 16:43:37 -040087void AlertIfPossible(CJS_EventContext* pContext, const wchar_t* swMsg) {
dsinclair8779fa82016-10-12 12:05:44 -070088 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
89 if (pFormFillEnv)
90 pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3);
tsepeze1e7bd02016-08-08 13:03:16 -070091}
92
Dan Sinclair698aed72017-09-26 16:24:49 -040093#if _FX_OS_ != _FX_OS_ANDROID_
Ryan Harrison275e2602017-09-18 14:23:18 -040094ByteString CalculateString(double dValue,
95 int iDec,
96 int* iDec2,
97 bool* bNegative) {
npm49c59282016-11-15 15:14:04 -080098 *bNegative = dValue < 0;
99 if (*bNegative)
100 dValue = -dValue;
dsinclair992ecf72016-12-14 05:45:57 -0800101
102 // Make sure the number of precision characters will fit.
103 if (iDec > std::numeric_limits<double>::digits10)
104 iDec = std::numeric_limits<double>::digits10;
105
npm49c59282016-11-15 15:14:04 -0800106 std::stringstream ss;
107 ss << std::fixed << std::setprecision(iDec) << dValue;
108 std::string stringValue = ss.str();
109 size_t iDecimalPos = stringValue.find(".");
110 *iDec2 = iDecimalPos == std::string::npos ? stringValue.size()
111 : static_cast<int>(iDecimalPos);
Ryan Harrison275e2602017-09-18 14:23:18 -0400112 return ByteString(stringValue.c_str());
npm49c59282016-11-15 15:14:04 -0800113}
114#endif
115
tsepez745611b2016-04-12 16:46:34 -0700116} // namespace
117
Ryan Harrison275e2602017-09-18 14:23:18 -0400118bool CJS_PublicMethods::IsNumber(const WideString& str) {
119 WideString sTrim = StrTrim(str);
Dan Sinclair812e96c2017-03-13 16:43:37 -0400120 const wchar_t* pTrim = sTrim.c_str();
121 const wchar_t* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -0700122 bool bDot = false;
123 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -0700126 while ((c = *p) != L'\0') {
127 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -0700129 return false;
130 bDot = true;
131 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -0700133 return false;
134 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700136 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 p++;
139 c = *p;
Lei Zhange247ec42017-04-20 21:41:36 -0700140 if (c != L'+' && c != L'-')
Wei Li614d20a2016-03-15 13:55:12 -0700141 return false;
Lei Zhange247ec42017-04-20 21:41:36 -0700142 bKXJS = true;
143 } else if (!std::iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700144 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 }
146 p++;
147 }
148
Wei Li614d20a2016-03-15 13:55:12 -0700149 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150}
151
Wei Li614d20a2016-03-15 13:55:12 -0700152bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 switch (c_Mask) {
154 case L'9':
Lei Zhange247ec42017-04-20 21:41:36 -0700155 return !!std::iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800157 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800159 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700161 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 default:
163 return (c_Change == c_Mask);
164 }
165}
166
Wei Li614d20a2016-03-15 13:55:12 -0700167bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
169}
170
Dan Sinclair812e96c2017-03-13 16:43:37 -0400171double CJS_PublicMethods::AF_Simple(const wchar_t* sFuction,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 double dValue1,
173 double dValue2) {
174 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
175 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
176 return dValue1 + dValue2;
177 }
178 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
179 return dValue1 * dValue2;
180 }
181 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800182 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 }
184 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800185 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 }
187 return dValue1;
188}
189
Tom Sepez67fd5df2015-10-08 12:24:19 -0700190CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 CJS_Value val) {
Dan Sinclairc9708952017-10-23 09:40:59 -0400192 if (val.IsArrayObject())
193 return val.ToArray(pRuntime);
194
195 WideString wsStr = val.ToWideString(pRuntime);
Ryan Harrison275e2602017-09-18 14:23:18 -0400196 ByteString t = ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700197 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198
199 int ch = ',';
200 int nIndex = 0;
201
Dan Sinclairc9708952017-10-23 09:40:59 -0400202 CJS_Array StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 while (*p) {
204 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800205 if (!pTemp) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400206 StrArray.SetElement(pRuntime, nIndex,
207 CJS_Value(pRuntime, StrTrim(ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 }
Lei Zhang997de612015-11-04 18:17:53 -0800210
211 char* pSub = new char[pTemp - p + 1];
212 strncpy(pSub, p, pTemp - p);
213 *(pSub + (pTemp - p)) = '\0';
214
Ryan Harrison275e2602017-09-18 14:23:18 -0400215 StrArray.SetElement(pRuntime, nIndex,
216 CJS_Value(pRuntime, StrTrim(ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800217 delete[] pSub;
218
219 nIndex++;
220 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 }
222 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Ryan Harrison275e2602017-09-18 14:23:18 -0400225int CJS_PublicMethods::ParseStringInteger(const WideString& str,
Ryan Harrison875e98c2017-09-27 10:53:11 -0400226 size_t nStart,
227 size_t& nSkip,
228 size_t nMaxStep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 int nRet = 0;
230 nSkip = 0;
Ryan Harrison875e98c2017-09-27 10:53:11 -0400231 for (size_t i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 if (i - nStart > 10)
233 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400235 wchar_t c = str[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700236 if (!std::iswdigit(c))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800237 break;
238
Lei Zhange8c1d412017-05-04 12:13:55 -0700239 nRet = nRet * 10 + FXSYS_DecimalCharToInt(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800240 nSkip = i - nStart + 1;
241 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 break;
243 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246}
247
Ryan Harrison275e2602017-09-18 14:23:18 -0400248WideString CJS_PublicMethods::ParseStringString(const WideString& str,
Ryan Harrison875e98c2017-09-27 10:53:11 -0400249 size_t nStart,
250 size_t& nSkip) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400251 WideString swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 nSkip = 0;
Ryan Harrison875e98c2017-09-27 10:53:11 -0400253 for (size_t i = nStart, sz = str.GetLength(); i < sz; i++) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400254 wchar_t c = str[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700255 if (!std::iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800257
258 swRet += c;
259 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263}
264
Ryan Harrison275e2602017-09-18 14:23:18 -0400265double CJS_PublicMethods::ParseNormalDate(const WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800266 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 int nYear = JS_GetYearFromTime(dt);
270 int nMonth = JS_GetMonthFromTime(dt) + 1;
271 int nDay = JS_GetDayFromTime(dt);
272 int nHour = JS_GetHourFromTime(dt);
273 int nMin = JS_GetMinFromTime(dt);
274 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277
Ryan Harrison875e98c2017-09-27 10:53:11 -0400278 size_t nSkip = 0;
279 size_t nLen = value.GetLength();
280 size_t nIndex = 0;
281 size_t i = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 while (i < nLen) {
283 if (nIndex > 2)
284 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400286 wchar_t c = value[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700287 if (std::iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
289 i += nSkip;
290 } else {
291 i++;
292 }
293 }
294
295 if (nIndex == 2) {
296 // case2: month/day
297 // case3: day/month
298 if ((number[0] >= 1 && number[0] <= 12) &&
299 (number[1] >= 1 && number[1] <= 31)) {
300 nMonth = number[0];
301 nDay = number[1];
302 } else if ((number[0] >= 1 && number[0] <= 31) &&
303 (number[1] >= 1 && number[1] <= 12)) {
304 nDay = number[0];
305 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700306 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307
Lei Zhang9559b7a2015-12-21 11:12:20 -0800308 if (bWrongFormat)
309 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 } else if (nIndex == 3) {
311 // case1: year/month/day
312 // case2: month/day/year
313 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700314
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
316 (number[2] >= 1 && number[2] <= 31)) {
317 nYear = number[0];
318 nMonth = number[1];
319 nDay = number[2];
320 } else if ((number[0] >= 1 && number[0] <= 12) &&
321 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
322 nMonth = number[0];
323 nDay = number[1];
324 nYear = number[2];
325 } else if ((number[0] >= 1 && number[0] <= 31) &&
326 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
327 nDay = number[0];
328 nMonth = number[1];
329 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700330 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700331
Lei Zhang9559b7a2015-12-21 11:12:20 -0800332 if (bWrongFormat)
333 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800335 if (bWrongFormat)
336 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 return dt;
338 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700339
Ryan Harrison275e2602017-09-18 14:23:18 -0400340 WideString swTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700342 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343}
344
Ryan Harrison275e2602017-09-18 14:23:18 -0400345double CJS_PublicMethods::MakeRegularDate(const WideString& value,
346 const WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800347 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700349
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 if (format.IsEmpty() || value.IsEmpty())
351 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 int nYear = JS_GetYearFromTime(dt);
354 int nMonth = JS_GetMonthFromTime(dt) + 1;
355 int nDay = JS_GetDayFromTime(dt);
356 int nHour = JS_GetHourFromTime(dt);
357 int nMin = JS_GetMinFromTime(dt);
358 int nSec = JS_GetSecFromTime(dt);
359
360 int nYearSub = 99; // nYear - 2000;
361
tsepez4cf55152016-11-02 14:37:54 -0700362 bool bPm = false;
363 bool bExit = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800364 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365
Ryan Harrison875e98c2017-09-27 10:53:11 -0400366 size_t i = 0;
367 size_t j = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368
369 while (i < format.GetLength()) {
370 if (bExit)
371 break;
372
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400373 wchar_t c = format[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 switch (c) {
375 case ':':
376 case '.':
377 case '-':
378 case '\\':
379 case '/':
380 i++;
381 j++;
382 break;
383
384 case 'y':
385 case 'm':
386 case 'd':
387 case 'H':
388 case 'h':
389 case 'M':
390 case 's':
391 case 't': {
Ryan Harrison875e98c2017-09-27 10:53:11 -0400392 size_t oldj = j;
393 size_t nSkip = 0;
394 size_t remaining = format.GetLength() - i - 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400396 if (remaining == 0 || format[i + 1] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700398 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 i++;
400 j++;
401 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700402 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 nMonth = ParseStringInteger(value, j, nSkip, 2);
404 i++;
405 j += nSkip;
406 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700407 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 nDay = ParseStringInteger(value, j, nSkip, 2);
409 i++;
410 j += nSkip;
411 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700412 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413 nHour = ParseStringInteger(value, j, nSkip, 2);
414 i++;
415 j += nSkip;
416 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700417 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 nHour = ParseStringInteger(value, j, nSkip, 2);
419 i++;
420 j += nSkip;
421 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700422 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 nMin = ParseStringInteger(value, j, nSkip, 2);
424 i++;
425 j += nSkip;
426 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700427 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 nSec = ParseStringInteger(value, j, nSkip, 2);
429 i++;
430 j += nSkip;
431 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700432 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400433 bPm = (j < value.GetLength() && value[j] == 'p');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 i++;
435 j++;
436 break;
437 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400438 } else if (remaining == 1 || format[i + 2] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 switch (c) {
440 case 'y':
441 nYear = ParseStringInteger(value, j, nSkip, 4);
442 i += 2;
443 j += nSkip;
444 break;
445 case 'm':
446 nMonth = ParseStringInteger(value, j, nSkip, 2);
447 i += 2;
448 j += nSkip;
449 break;
450 case 'd':
451 nDay = ParseStringInteger(value, j, nSkip, 2);
452 i += 2;
453 j += nSkip;
454 break;
455 case 'H':
456 nHour = ParseStringInteger(value, j, nSkip, 2);
457 i += 2;
458 j += nSkip;
459 break;
460 case 'h':
461 nHour = ParseStringInteger(value, j, nSkip, 2);
462 i += 2;
463 j += nSkip;
464 break;
465 case 'M':
466 nMin = ParseStringInteger(value, j, nSkip, 2);
467 i += 2;
468 j += nSkip;
469 break;
470 case 's':
471 nSec = ParseStringInteger(value, j, nSkip, 2);
472 i += 2;
473 j += nSkip;
474 break;
475 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400476 bPm = (j + 1 < value.GetLength() && value[j] == 'p' &&
477 value[j + 1] == 'm');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 i += 2;
479 j += 2;
480 break;
481 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400482 } else if (remaining == 2 || format[i + 3] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483 switch (c) {
484 case 'm': {
Ryan Harrison275e2602017-09-18 14:23:18 -0400485 WideString sMonth = ParseStringString(value, j, nSkip);
tsepez4cf55152016-11-02 14:37:54 -0700486 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487 for (int m = 0; m < 12; m++) {
488 if (sMonth.CompareNoCase(months[m]) == 0) {
489 nMonth = m + 1;
490 i += 3;
491 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700492 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700493 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700494 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 }
496
497 if (!bFind) {
498 nMonth = ParseStringInteger(value, j, nSkip, 3);
499 i += 3;
500 j += nSkip;
501 }
502 } break;
503 case 'y':
504 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700505 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506 i += 3;
507 j += 3;
508 break;
509 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400510 } else if (remaining == 3 || format[i + 4] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511 switch (c) {
512 case 'y':
513 nYear = ParseStringInteger(value, j, nSkip, 4);
514 j += nSkip;
515 i += 4;
516 break;
517 case 'm': {
tsepez4cf55152016-11-02 14:37:54 -0700518 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519
Ryan Harrison275e2602017-09-18 14:23:18 -0400520 WideString sMonth = ParseStringString(value, j, nSkip);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521 sMonth.MakeLower();
522
523 for (int m = 0; m < 12; m++) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400524 WideString sFullMonths = fullmonths[m];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525 sFullMonths.MakeLower();
526
Ryan Harrison12db7512017-08-23 10:39:35 -0400527 if (sFullMonths.Contains(sMonth.c_str())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 nMonth = m + 1;
529 i += 4;
530 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700531 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532 break;
533 }
534 }
535
536 if (!bFind) {
537 nMonth = ParseStringInteger(value, j, nSkip, 4);
538 i += 4;
539 j += nSkip;
540 }
541 } break;
542 default:
543 i += 4;
544 j += 4;
545 break;
546 }
547 } else {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400548 if (j >= value.GetLength() || format[i] != value[j]) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800549 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700550 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700551 }
552 i++;
553 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700554 }
Tom Sepez85386422014-07-23 10:28:37 -0700555
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800557 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700558 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 }
560 }
561
562 break;
563 default:
564 if (value.GetLength() <= j) {
tsepez4cf55152016-11-02 14:37:54 -0700565 bExit = true;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400566 } else if (format[i] != value[j]) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800567 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700568 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 }
570
571 i++;
572 j++;
573 break;
574 }
575 }
576
577 if (bPm)
578 nHour += 12;
579
580 if (nYear >= 0 && nYear <= nYearSub)
581 nYear += 2000;
582
583 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800584 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585
586 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800587 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588
589 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800590 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591
592 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800593 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594
595 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800596 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700597
598 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800599 if (bBadFormat) {
600 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 } else {
602 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
603 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -0700604 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -0700605 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 }
607
Tom Sepezdf950b82017-08-04 11:33:49 -0700608 if (std::isnan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800609 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700610
Lei Zhang9559b7a2015-12-21 11:12:20 -0800611 if (bWrongFormat)
612 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700613
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 return dRet;
615}
616
Ryan Harrison275e2602017-09-18 14:23:18 -0400617WideString CJS_PublicMethods::MakeFormatDate(double dDate,
618 const WideString& format) {
619 WideString sRet = L"", sPart = L"";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700620
621 int nYear = JS_GetYearFromTime(dDate);
622 int nMonth = JS_GetMonthFromTime(dDate) + 1;
623 int nDay = JS_GetDayFromTime(dDate);
624 int nHour = JS_GetHourFromTime(dDate);
625 int nMin = JS_GetMinFromTime(dDate);
626 int nSec = JS_GetSecFromTime(dDate);
627
Ryan Harrison875e98c2017-09-27 10:53:11 -0400628 size_t i = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629 while (i < format.GetLength()) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400630 wchar_t c = format[i];
Ryan Harrison875e98c2017-09-27 10:53:11 -0400631 size_t remaining = format.GetLength() - i - 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632 sPart = L"";
633 switch (c) {
634 case 'y':
635 case 'm':
636 case 'd':
637 case 'H':
638 case 'h':
639 case 'M':
640 case 's':
641 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400642 if (remaining == 0 || format[i + 1] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 switch (c) {
644 case 'y':
645 sPart += c;
646 break;
647 case 'm':
648 sPart.Format(L"%d", nMonth);
649 break;
650 case 'd':
651 sPart.Format(L"%d", nDay);
652 break;
653 case 'H':
654 sPart.Format(L"%d", nHour);
655 break;
656 case 'h':
657 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
658 break;
659 case 'M':
660 sPart.Format(L"%d", nMin);
661 break;
662 case 's':
663 sPart.Format(L"%d", nSec);
664 break;
665 case 't':
666 sPart += nHour > 12 ? 'p' : 'a';
667 break;
668 }
669 i++;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400670 } else if (remaining == 1 || format[i + 2] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700671 switch (c) {
672 case 'y':
673 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
674 break;
675 case 'm':
676 sPart.Format(L"%02d", nMonth);
677 break;
678 case 'd':
679 sPart.Format(L"%02d", nDay);
680 break;
681 case 'H':
682 sPart.Format(L"%02d", nHour);
683 break;
684 case 'h':
685 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
686 break;
687 case 'M':
688 sPart.Format(L"%02d", nMin);
689 break;
690 case 's':
691 sPart.Format(L"%02d", nSec);
692 break;
693 case 't':
694 sPart = nHour > 12 ? L"pm" : L"am";
695 break;
696 }
697 i += 2;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400698 } else if (remaining == 2 || format[i + 3] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700699 switch (c) {
700 case 'm':
701 i += 3;
702 if (nMonth > 0 && nMonth <= 12)
703 sPart += months[nMonth - 1];
704 break;
705 default:
706 i += 3;
707 sPart += c;
708 sPart += c;
709 sPart += c;
710 break;
711 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400712 } else if (remaining == 3 || format[i + 4] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713 switch (c) {
714 case 'y':
715 sPart.Format(L"%04d", nYear);
716 i += 4;
717 break;
718 case 'm':
719 i += 4;
720 if (nMonth > 0 && nMonth <= 12)
721 sPart += fullmonths[nMonth - 1];
722 break;
723 default:
724 i += 4;
725 sPart += c;
726 sPart += c;
727 sPart += c;
728 sPart += c;
729 break;
730 }
731 } else {
732 i++;
733 sPart += c;
734 }
735 break;
736 default:
737 i++;
738 sPart += c;
739 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700740 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700741
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742 sRet += sPart;
743 }
744
745 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700746}
747
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700748// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
749// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800750bool CJS_PublicMethods::AFNumber_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700751 const std::vector<CJS_Value>& params,
752 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400753 WideString& sError) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400754#if _FX_OS_ != _FX_OS_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700755 if (params.size() != 6) {
tsepezcd5dc852016-09-08 11:23:24 -0700756 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700757 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700759
Tom Sepezb1670b52017-02-16 17:01:00 -0800760 CJS_EventHandler* pEvent =
761 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700763 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700764
Ryan Harrison275e2602017-09-18 14:23:18 -0400765 WideString& Value = pEvent->Value();
766 ByteString strValue = StrTrim(ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700768 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700769
tsepezb4694242016-08-15 16:44:55 -0700770 int iDec = params[0].ToInt(pRuntime);
771 int iSepStyle = params[1].ToInt(pRuntime);
772 int iNegStyle = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700773 // params[3] is iCurrStyle, it's not used.
Dan Sinclairc9708952017-10-23 09:40:59 -0400774 WideString wstrCurrency = params[4].ToWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700775 bool bCurrencyPrepend = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776
777 if (iDec < 0)
778 iDec = -iDec;
779
780 if (iSepStyle < 0 || iSepStyle > 3)
781 iSepStyle = 0;
782
783 if (iNegStyle < 0 || iNegStyle > 3)
784 iNegStyle = 0;
785
npm49c59282016-11-15 15:14:04 -0800786 // Processing decimal places
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700787 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700788 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700789 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700790 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700791
npm49c59282016-11-15 15:14:04 -0800792 // Calculating number string
793 bool bNegative;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794 int iDec2;
npm49c59282016-11-15 15:14:04 -0800795 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796 if (strValue.IsEmpty()) {
797 dValue = 0;
npm49c59282016-11-15 15:14:04 -0800798 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700799 if (strValue.IsEmpty()) {
800 strValue = "0";
801 iDec2 = 1;
802 }
803 }
804
npm49c59282016-11-15 15:14:04 -0800805 // Processing separator style
Ryan Harrison875e98c2017-09-27 10:53:11 -0400806 if (static_cast<size_t>(iDec2) < strValue.GetLength()) {
npm49c59282016-11-15 15:14:04 -0800807 if (iSepStyle == 2 || iSepStyle == 3)
808 strValue.Replace(".", ",");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700809
810 if (iDec2 == 0)
811 strValue.Insert(iDec2, '0');
812 }
813 if (iSepStyle == 0 || iSepStyle == 2) {
npm49c59282016-11-15 15:14:04 -0800814 char cSeparator;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815 if (iSepStyle == 0)
npm49c59282016-11-15 15:14:04 -0800816 cSeparator = ',';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817 else
npm49c59282016-11-15 15:14:04 -0800818 cSeparator = '.';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700819
npm49c59282016-11-15 15:14:04 -0800820 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3)
821 strValue.Insert(iDecPositive, cSeparator);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822 }
823
npm49c59282016-11-15 15:14:04 -0800824 // Processing currency string
Ryan Harrison275e2602017-09-18 14:23:18 -0400825 Value = WideString::FromLocal(strValue.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826
827 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700828 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829 else
thestigcf03f8e2016-05-09 12:36:18 -0700830 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831
npm49c59282016-11-15 15:14:04 -0800832 // Processing negative style
833 if (bNegative) {
834 if (iNegStyle == 0)
thestigcf03f8e2016-05-09 12:36:18 -0700835 Value = L"-" + Value;
npm49c59282016-11-15 15:14:04 -0800836 else if (iNegStyle == 2 || iNegStyle == 3)
thestigcf03f8e2016-05-09 12:36:18 -0700837 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700838 if (iNegStyle == 1 || iNegStyle == 3) {
839 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700840 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700841 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700842 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700843 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700844 vColElm = CJS_Value(pRuntime, 1);
tsepezb4694242016-08-15 16:44:55 -0700845 arColor.SetElement(pRuntime, 1, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700846 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700847 arColor.SetElement(pRuntime, 2, vColElm);
848 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849
Tom Sepez67fd5df2015-10-08 12:24:19 -0700850 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851 vProp.StartGetting();
dan sinclaircbe23db2017-10-19 14:29:33 -0400852 vProp.Set(arColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700853 vProp.StartSetting();
dan sinclaircbe23db2017-10-19 14:29:33 -0400854 fTarget->set_text_color(pRuntime, vProp, &sError); // red
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700855 }
856 }
857 } else {
858 if (iNegStyle == 1 || iNegStyle == 3) {
859 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700860 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700861 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700862 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700863 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700864 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700865 arColor.SetElement(pRuntime, 1, vColElm);
866 arColor.SetElement(pRuntime, 2, vColElm);
867 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700868
Tom Sepez67fd5df2015-10-08 12:24:19 -0700869 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700870 vProp.StartGetting();
dan sinclaircbe23db2017-10-19 14:29:33 -0400871 fTarget->get_text_color(pRuntime, &vProp, &sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872
Dan Sinclairc9708952017-10-23 09:40:59 -0400873 CJS_Array aProp = vProp.GetJSValue()->ToArray(pRuntime);
Dan Sinclair7f55a542017-07-13 14:17:10 -0400874 CFX_Color crProp;
875 CFX_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700876 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
877 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878
879 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700880 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 vProp2.StartGetting();
dan sinclaircbe23db2017-10-19 14:29:33 -0400882 vProp2.Set(arColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883 vProp2.StartSetting();
dan sinclaircbe23db2017-10-19 14:29:33 -0400884 fTarget->set_text_color(pRuntime, vProp2, &sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 }
886 }
887 }
888 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889#endif
tsepez4cf55152016-11-02 14:37:54 -0700890 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700891}
892
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
894// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800895bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700896 const std::vector<CJS_Value>& params,
897 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400898 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700900 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700901
Tom Sepezb1670b52017-02-16 17:01:00 -0800902 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
903 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700904 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700905 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700906
Ryan Harrison275e2602017-09-18 14:23:18 -0400907 WideString& val = pEvent->Value();
908 WideString& wstrChange = pEvent->Change();
909 WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700910
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911 if (pEvent->WillCommit()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400912 WideString swTemp = StrTrim(wstrValue);
ochanga0a3bc32016-05-12 15:22:48 -0700913 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700914 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700915
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 swTemp.Replace(L",", L".");
917 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700918 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700919 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700920 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700921 }
tsepez4cf55152016-11-02 14:37:54 -0700922 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700923 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700924
Ryan Harrison275e2602017-09-18 14:23:18 -0400925 WideString wstrSelected;
thestigcf03f8e2016-05-09 12:36:18 -0700926 if (pEvent->SelStart() != -1) {
927 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
928 pEvent->SelEnd() - pEvent->SelStart());
929 }
930
Ryan Harrison12db7512017-08-23 10:39:35 -0400931 bool bHasSign = wstrValue.Contains(L'-') && !wstrSelected.Contains(L'-');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700932 if (bHasSign) {
933 // can't insert "change" in front to sign postion.
934 if (pEvent->SelStart() == 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700935 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700936 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700937 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700939
tsepezb4694242016-08-15 16:44:55 -0700940 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700941 if (iSepStyle < 0 || iSepStyle > 3)
942 iSepStyle = 0;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400943 const wchar_t cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700944
Ryan Harrison12db7512017-08-23 10:39:35 -0400945 bool bHasSep = wstrValue.Contains(cSep);
Ryan Harrison875e98c2017-09-27 10:53:11 -0400946 for (size_t i = 0; i < wstrChange.GetLength(); ++i) {
thestigcf03f8e2016-05-09 12:36:18 -0700947 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948 if (bHasSep) {
Lei Zhange247ec42017-04-20 21:41:36 -0700949 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700950 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 }
tsepez4cf55152016-11-02 14:37:54 -0700952 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 continue;
954 }
thestigcf03f8e2016-05-09 12:36:18 -0700955 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700956 if (bHasSign) {
Lei Zhange247ec42017-04-20 21:41:36 -0700957 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700958 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800960 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700961 if (i != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700962 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700963 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964 }
965 if (pEvent->SelStart() != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700966 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700967 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 }
tsepez4cf55152016-11-02 14:37:54 -0700969 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 continue;
971 }
972
Lei Zhange247ec42017-04-20 21:41:36 -0700973 if (!std::iswdigit(wstrChange[i])) {
974 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700975 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976 }
977 }
978
Ryan Harrison275e2602017-09-18 14:23:18 -0400979 WideString wprefix = wstrValue.Left(pEvent->SelStart());
980 WideString wpostfix;
Ryan Harrison75584142017-09-01 13:30:19 -0400981 if (pEvent->SelEnd() >= 0 &&
Ryan Harrison875e98c2017-09-27 10:53:11 -0400982 static_cast<size_t>(pEvent->SelEnd()) < wstrValue.GetLength())
Ryan Harrison75584142017-09-01 13:30:19 -0400983 wpostfix = wstrValue.Right(wstrValue.GetLength() -
Ryan Harrison875e98c2017-09-27 10:53:11 -0400984 static_cast<size_t>(pEvent->SelEnd()));
thestigcf03f8e2016-05-09 12:36:18 -0700985 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700986 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700987}
988
989// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -0800990bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700991 const std::vector<CJS_Value>& params,
992 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400993 WideString& sError) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400994#if _FX_OS_ != _FX_OS_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700996 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700997 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800999
1000 CJS_EventHandler* pEvent =
1001 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001002 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001003 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004
Ryan Harrison275e2602017-09-18 14:23:18 -04001005 WideString& Value = pEvent->Value();
1006 ByteString strValue = StrTrim(ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001008 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009
tsepezb4694242016-08-15 16:44:55 -07001010 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011 if (iDec < 0)
1012 iDec = -iDec;
1013
tsepezb4694242016-08-15 16:44:55 -07001014 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015 if (iSepStyle < 0 || iSepStyle > 3)
1016 iSepStyle = 0;
1017
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001019 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 dValue *= 100;
1021 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001022 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023
1024 int iDec2;
1025 int iNegative = 0;
1026 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1027 if (strValue.IsEmpty()) {
1028 dValue = 0;
1029 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1030 }
1031
1032 if (iDec2 < 0) {
1033 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1034 strValue = "0" + strValue;
1035 }
1036 iDec2 = 0;
1037 }
1038 int iMax = strValue.GetLength();
1039 if (iDec2 > iMax) {
1040 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1041 strValue += "0";
1042 }
1043 iMax = iDec2 + 1;
1044 }
dsinclair64376be2016-03-31 20:03:24 -07001045
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 // for processing seperator style
1047 if (iDec2 < iMax) {
1048 if (iSepStyle == 0 || iSepStyle == 1) {
1049 strValue.Insert(iDec2, '.');
1050 iMax++;
1051 } else if (iSepStyle == 2 || iSepStyle == 3) {
1052 strValue.Insert(iDec2, ',');
1053 iMax++;
1054 }
1055
1056 if (iDec2 == 0)
1057 strValue.Insert(iDec2, '0');
1058 }
1059 if (iSepStyle == 0 || iSepStyle == 2) {
1060 char cSeperator;
1061 if (iSepStyle == 0)
1062 cSeperator = ',';
1063 else
1064 cSeperator = '.';
1065
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001066 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 strValue.Insert(iDecPositive, cSeperator);
1068 iMax++;
1069 }
1070 }
dsinclair64376be2016-03-31 20:03:24 -07001071
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072 // negative mark
1073 if (iNegative)
1074 strValue = "-" + strValue;
1075 strValue += "%";
Ryan Harrison275e2602017-09-18 14:23:18 -04001076 Value = WideString::FromLocal(strValue.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077#endif
tsepez4cf55152016-11-02 14:37:54 -07001078 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079}
1080// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001081bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001082 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001083 const std::vector<CJS_Value>& params,
1084 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001085 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001086 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087}
1088
1089// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001090bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001091 const std::vector<CJS_Value>& params,
1092 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001093 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001095 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001096 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001097 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001098
1099 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1100 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001102 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001103
Ryan Harrison275e2602017-09-18 14:23:18 -04001104 WideString& val = pEvent->Value();
1105 WideString strValue = val;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001107 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001108
Dan Sinclairc9708952017-10-23 09:40:59 -04001109 WideString sFormat = params[0].ToWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001110 double dDate = 0.0f;
1111
Ryan Harrison12db7512017-08-23 10:39:35 -04001112 if (strValue.Contains(L"GMT")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113 // for GMT format time
1114 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1115 dDate = MakeInterDate(strValue);
1116 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001117 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 }
1119
Tom Sepezdf950b82017-08-04 11:33:49 -07001120 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001121 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001122 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001123 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001124 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001125 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 }
1127
1128 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001129 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001130}
1131
Ryan Harrison275e2602017-09-18 14:23:18 -04001132double CJS_PublicMethods::MakeInterDate(const WideString& strValue) {
1133 std::vector<WideString> wsArray;
1134 WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001135 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001136 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001137 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138 sTemp = L"";
1139 continue;
1140 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 sTemp += c;
1142 }
Tom Sepezab277682016-02-17 10:07:21 -08001143 wsArray.push_back(sTemp);
1144 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145 return 0;
1146
Tom Sepez4246b002016-01-20 11:48:29 -08001147 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 sTemp = wsArray[1];
1149 if (sTemp.Compare(L"Jan") == 0)
1150 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001151 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001152 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001153 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001155 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001157 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001159 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001161 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001163 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001165 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001167 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001169 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001171 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 nMonth = 12;
1173
Ryan Harrison275e2602017-09-18 14:23:18 -04001174 int nDay = FX_atof(wsArray[2].AsStringView());
1175 int nHour = FX_atof(wsArray[3].AsStringView());
1176 int nMin = FX_atof(wsArray[4].AsStringView());
1177 int nSec = FX_atof(wsArray[5].AsStringView());
1178 int nYear = FX_atof(wsArray[7].AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1180 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -07001181 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001182 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183
1184 return dRet;
1185}
1186
1187// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001188bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001189 const std::vector<CJS_Value>& params,
1190 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001191 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001192 if (params.size() != 1) {
1193 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001194 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195 }
1196
Tom Sepezb1670b52017-02-16 17:01:00 -08001197 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1198 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199 if (pEvent->WillCommit()) {
1200 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001201 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001202
Ryan Harrison275e2602017-09-18 14:23:18 -04001203 WideString strValue = pEvent->Value();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001205 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001206
Dan Sinclairc9708952017-10-23 09:40:59 -04001207 WideString sFormat = params[0].ToWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001208 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001209 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Tom Sepezdf950b82017-08-04 11:33:49 -07001210 if (bWrongFormat || std::isnan(dRet)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001211 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001212 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001214 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001215 pEvent->Rc() = false;
1216 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001217 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218 }
tsepez4cf55152016-11-02 14:37:54 -07001219 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001220}
1221
Tom Sepezb1670b52017-02-16 17:01:00 -08001222bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001223 const std::vector<CJS_Value>& params,
1224 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001225 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001227 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001228 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001230
tsepezb4694242016-08-15 16:44:55 -07001231 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001232 const wchar_t* cFormats[] = {L"m/d",
1233 L"m/d/yy",
1234 L"mm/dd/yy",
1235 L"mm/yy",
1236 L"d-mmm",
1237 L"d-mmm-yy",
1238 L"dd-mmm-yy",
1239 L"yy-mm-dd",
1240 L"mmm-yy",
1241 L"mmmm-yy",
1242 L"mmm d, yyyy",
1243 L"mmmm d, yyyy",
1244 L"m/d/yy h:MM tt",
1245 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001246
Lei Zhanga0f67242015-08-17 15:39:30 -07001247 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1248 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001249
Lei Zhang945fdb72015-11-11 10:18:16 -08001250 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001251 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1252 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001254
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001255// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001256bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001257 const std::vector<CJS_Value>& params,
1258 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001259 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001261 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001262 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263 }
1264
tsepezb4694242016-08-15 16:44:55 -07001265 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001266 const wchar_t* cFormats[] = {L"m/d",
1267 L"m/d/yy",
1268 L"mm/dd/yy",
1269 L"mm/yy",
1270 L"d-mmm",
1271 L"d-mmm-yy",
1272 L"dd-mmm-yy",
1273 L"yy-mm-dd",
1274 L"mmm-yy",
1275 L"mmmm-yy",
1276 L"mmm d, yyyy",
1277 L"mmmm d, yyyy",
1278 L"m/d/yy h:MM tt",
1279 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280
Lei Zhanga0f67242015-08-17 15:39:30 -07001281 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1282 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283
Lei Zhang945fdb72015-11-11 10:18:16 -08001284 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001285 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1286 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287}
1288
1289// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001290bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001291 const std::vector<CJS_Value>& params,
1292 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001293 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001294 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001295 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001296 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297 }
1298
tsepezb4694242016-08-15 16:44:55 -07001299 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001300 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1301 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302
Lei Zhanga0f67242015-08-17 15:39:30 -07001303 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1304 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305
Lei Zhang945fdb72015-11-11 10:18:16 -08001306 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001307 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1308 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309}
1310
Tom Sepezb1670b52017-02-16 17:01:00 -08001311bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001312 const std::vector<CJS_Value>& params,
1313 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001314 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001315 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001316 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001317 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318 }
1319
tsepezb4694242016-08-15 16:44:55 -07001320 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001321 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1322 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001323
Lei Zhanga0f67242015-08-17 15:39:30 -07001324 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1325 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326
Lei Zhang945fdb72015-11-11 10:18:16 -08001327 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001328 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1329 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001330}
1331
Tom Sepezb1670b52017-02-16 17:01:00 -08001332bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001333 const std::vector<CJS_Value>& params,
1334 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001335 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001336 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001337}
1338
Tom Sepezb1670b52017-02-16 17:01:00 -08001339bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001340 const std::vector<CJS_Value>& params,
1341 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001342 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001343 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001344}
1345
1346// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001347bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001348 const std::vector<CJS_Value>& params,
1349 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001350 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001351 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001352 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001353 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354 }
1355
Tom Sepezb1670b52017-02-16 17:01:00 -08001356 CJS_EventHandler* pEvent =
1357 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001359 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360
Ryan Harrison275e2602017-09-18 14:23:18 -04001361 WideString wsSource = pEvent->Value();
1362 WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001363 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001364 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001365 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001366 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001367 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001368 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001370 case 2:
1371 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1372 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 else
tsepez4f1f41f2016-03-28 14:13:16 -07001374 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001376 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001377 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 break;
1379 }
1380
tsepez4f1f41f2016-03-28 14:13:16 -07001381 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001382 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383}
1384
1385// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001386bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001387 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001388 const std::vector<CJS_Value>& params,
1389 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001390 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001391 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001392 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001393 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 }
1395
Tom Sepezb1670b52017-02-16 17:01:00 -08001396 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1397 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001398 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001399 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001400
Ryan Harrison275e2602017-09-18 14:23:18 -04001401 WideString& valEvent = pEvent->Value();
Dan Sinclairc9708952017-10-23 09:40:59 -04001402 WideString wstrMask = params[0].ToWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001404 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001407 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001408 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001409
Ryan Harrison875e98c2017-09-27 10:53:11 -04001410 size_t iIndexMask = 0;
thestigcf03f8e2016-05-09 12:36:18 -07001411 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1412 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001413 break;
1414 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001415
thestigcf03f8e2016-05-09 12:36:18 -07001416 if (iIndexMask != wstrMask.GetLength() ||
1417 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001418 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001419 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001420 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421 }
tsepez4cf55152016-11-02 14:37:54 -07001422 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 }
1424
Ryan Harrison275e2602017-09-18 14:23:18 -04001425 WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001426 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001427 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001428
Ryan Harrison275e2602017-09-18 14:23:18 -04001429 WideString wChange = wideChange;
Ryan Harrison875e98c2017-09-27 10:53:11 -04001430 size_t iIndexMask = pEvent->SelStart();
1431 size_t combined_len = valEvent.GetLength() + wChange.GetLength() +
1432 pEvent->SelStart() - pEvent->SelEnd();
thestigcf03f8e2016-05-09 12:36:18 -07001433 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001434 AlertIfPossible(pContext,
1435 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001436 pEvent->Rc() = false;
1437 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001438 }
1439
thestigcf03f8e2016-05-09 12:36:18 -07001440 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001441 AlertIfPossible(pContext,
1442 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001443 pEvent->Rc() = false;
1444 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001445 }
1446
Ryan Harrison875e98c2017-09-27 10:53:11 -04001447 for (size_t i = 0; i < wChange.GetLength(); ++i) {
thestigcf03f8e2016-05-09 12:36:18 -07001448 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001449 AlertIfPossible(pContext,
1450 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001451 pEvent->Rc() = false;
1452 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001453 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001454 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001455 if (!isReservedMaskChar(wMask))
1456 wChange.SetAt(i, wMask);
1457
1458 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001459 pEvent->Rc() = false;
1460 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001461 }
1462 iIndexMask++;
1463 }
thestigcf03f8e2016-05-09 12:36:18 -07001464 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001465 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001466}
1467
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001468// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001469bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001470 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001471 const std::vector<CJS_Value>& params,
1472 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001473 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001475 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001476 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001478
Tom Sepezb1670b52017-02-16 17:01:00 -08001479 CJS_EventHandler* pEvent =
1480 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001481 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001482 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001483
thestigcf03f8e2016-05-09 12:36:18 -07001484 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001485 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001486 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001487 cFormat = "99999";
1488 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001489 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 cFormat = "999999999";
1491 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001492 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001493 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001494 cFormat = "9999999999";
1495 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496 cFormat = "9999999";
1497 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001498 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 cFormat = "999999999";
1500 break;
1501 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001502
Lei Zhang945fdb72015-11-11 10:18:16 -08001503 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001504 params2.push_back(CJS_Value(pRuntime, cFormat));
1505 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001506}
1507
Tom Sepezb1670b52017-02-16 17:01:00 -08001508bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001509 const std::vector<CJS_Value>& params,
1510 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001511 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001513 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001514 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001516
Tom Sepezb1670b52017-02-16 17:01:00 -08001517 CJS_EventHandler* pEventHandler =
1518 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001519
Ryan Harrison275e2602017-09-18 14:23:18 -04001520 WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001521 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001523
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001524 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001525 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001526 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 }
1528
Ryan Harrison275e2602017-09-18 14:23:18 -04001529 WideString prefix, postfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001530
1531 if (pEventHandler->SelStart() >= 0)
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001532 prefix = swValue.Left(pEventHandler->SelStart());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001533 else
1534 prefix = L"";
1535
1536 if (pEventHandler->SelEnd() >= 0 &&
Ryan Harrison875e98c2017-09-27 10:53:11 -04001537 static_cast<size_t>(pEventHandler->SelEnd()) <= swValue.GetLength())
Ryan Harrison75584142017-09-01 13:30:19 -04001538 postfix = swValue.Right(swValue.GetLength() -
Ryan Harrison875e98c2017-09-27 10:53:11 -04001539 static_cast<size_t>(pEventHandler->SelEnd()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001540 else
1541 postfix = L"";
1542
tsepezf3dc8c62016-08-10 06:29:29 -07001543 vRet =
1544 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001545 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001546}
1547
Tom Sepezb1670b52017-02-16 17:01:00 -08001548bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001549 const std::vector<CJS_Value>& params,
1550 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001551 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001552 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001553 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001554 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001555 }
1556
Dan Sinclairc9708952017-10-23 09:40:59 -04001557 WideString sValue = params[0].ToWideString(pRuntime);
1558 WideString sFormat = params[1].ToWideString(pRuntime);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001559 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Tom Sepezdf950b82017-08-04 11:33:49 -07001560 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001561 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001562 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001563 sFormat.c_str());
Tom Sepezb1670b52017-02-16 17:01:00 -08001564 AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001565 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566 }
1567
tsepezf3dc8c62016-08-10 06:29:29 -07001568 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001569 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570}
1571
Tom Sepezb1670b52017-02-16 17:01:00 -08001572bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001573 const std::vector<CJS_Value>& params,
1574 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001575 WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001576 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001577 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001578 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001580
tsepezb4694242016-08-15 16:44:55 -07001581 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
Dan Sinclairc9708952017-10-23 09:40:59 -04001582 params[0].ToWideString(pRuntime).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001583 params[1].ToDouble(pRuntime),
1584 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001585
tsepez4cf55152016-11-02 14:37:54 -07001586 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001587}
1588
Tom Sepezb1670b52017-02-16 17:01:00 -08001589bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001590 const std::vector<CJS_Value>& params,
1591 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001592 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001593 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001594 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001595 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001596 }
tsepezf3dc8c62016-08-10 06:29:29 -07001597
Dan Sinclairc9708952017-10-23 09:40:59 -04001598 WideString ws = params[0].ToWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001599 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001600 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001601 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001602 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001603 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001604 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001605}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001606
Tom Sepezb1670b52017-02-16 17:01:00 -08001607bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001608 const std::vector<CJS_Value>& params,
1609 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001610 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001611 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001612 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001613 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001614 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001615
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001616 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001617 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001618 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001619 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001620 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001621
dsinclair4526faf2016-10-11 10:54:49 -07001622 CPDFSDK_InterForm* pReaderInterForm =
Tom Sepezb1670b52017-02-16 17:01:00 -08001623 pRuntime->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001624 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001625
Dan Sinclairc9708952017-10-23 09:40:59 -04001626 WideString sFunction = params[0].ToWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001627 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001628
Tom Sepez67fd5df2015-10-08 12:24:19 -07001629 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001630 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001631
tsepezb4694242016-08-15 16:44:55 -07001632 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Dan Sinclairc9708952017-10-23 09:40:59 -04001633 CJS_Value jsValue(FieldNameArray.GetElement(pRuntime, i));
1634 WideString wsFieldName = jsValue.ToWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001635
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1637 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1638 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001639 switch (pFormField->GetFieldType()) {
1640 case FIELDTYPE_TEXTFIELD:
1641 case FIELDTYPE_COMBOBOX: {
Ryan Harrison275e2602017-09-18 14:23:18 -04001642 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001643 trimmed.TrimRight();
1644 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001645 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001646 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001647 case FIELDTYPE_PUSHBUTTON: {
1648 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001649 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001650 case FIELDTYPE_CHECKBOX:
1651 case FIELDTYPE_RADIOBUTTON: {
1652 dTemp = 0.0;
1653 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1654 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1655 if (pFormCtrl->IsChecked()) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001656 WideString trimmed = pFormCtrl->GetExportValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001657 trimmed.TrimRight();
1658 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001659 dTemp = FX_atof(trimmed.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001660 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001661 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001662 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001663 }
Tom Sepez4246b002016-01-20 11:48:29 -08001664 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001665 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001666 if (pFormField->CountSelectedItems() <= 1) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001667 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001668 trimmed.TrimRight();
1669 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001670 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001671 }
1672 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001673 default:
1674 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001675 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676
1677 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1678 wcscmp(sFunction.c_str(), L"MAX") == 0))
1679 dValue = dTemp;
1680
1681 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1682
1683 nFieldsCount++;
1684 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001685 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001686 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001687
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001688 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1689 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001690
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001691 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1692 FXSYS_pow((double)10, (double)6);
Tom Sepezb1670b52017-02-16 17:01:00 -08001693
Tom Sepez67fd5df2015-10-08 12:24:19 -07001694 CJS_Value jsValue(pRuntime, dValue);
Tom Sepezb1670b52017-02-16 17:01:00 -08001695 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
foxit8b544ed2015-09-10 14:57:54 +08001696 if (pContext->GetEventHandler()->m_pValue)
Dan Sinclairc9708952017-10-23 09:40:59 -04001697 pContext->GetEventHandler()->Value() = jsValue.ToWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001698
tsepez4cf55152016-11-02 14:37:54 -07001699 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001700}
1701
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001702/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001703** within the specified range. */
1704
Tom Sepezb1670b52017-02-16 17:01:00 -08001705bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001706 const std::vector<CJS_Value>& params,
1707 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001708 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001709 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001710 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001711 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001712 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001713 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
tsepezcd5dc852016-09-08 11:23:24 -07001714 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001715 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001716 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001717
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001718 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001719 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001720
Ryan Harrison275e2602017-09-18 14:23:18 -04001721 double dEentValue = atof(ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001722 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001723 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001724 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001725 double dLessThan = params[3].ToDouble(pRuntime);
Ryan Harrison275e2602017-09-18 14:23:18 -04001726 WideString swMsg;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001727
1728 if (bGreaterThan && bLessThan) {
1729 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001730 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001731 params[1].ToWideString(pRuntime).c_str(),
1732 params[3].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001733 } else if (bGreaterThan) {
1734 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001735 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001736 params[1].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737 } else if (bLessThan) {
1738 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001739 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001740 params[3].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001741 }
1742
1743 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001744 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001745 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001746 }
tsepez4cf55152016-11-02 14:37:54 -07001747 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001748}
1749
Tom Sepezb1670b52017-02-16 17:01:00 -08001750bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001751 const std::vector<CJS_Value>& params,
1752 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001753 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001754 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001755 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001756 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001757 }
1758
Dan Sinclairc9708952017-10-23 09:40:59 -04001759 WideString str = params[0].ToWideString(pRuntime);
Ryan Harrison75c65212017-08-16 13:52:15 -04001760 if (str.GetLength() > 0 && (str[0] == L'.' || str[0] == L','))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001761 str = L"0" + str;
1762
Ryan Harrison275e2602017-09-18 14:23:18 -04001763 WideString sPart;
Tom Sepezb1670b52017-02-16 17:01:00 -08001764 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001765 int nIndex = 0;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001766 for (const auto& wc : str) {
Lei Zhange247ec42017-04-20 21:41:36 -07001767 if (std::iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001768 sPart += wc;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001769 } else if (sPart.GetLength() > 0) {
1770 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
1771 sPart = L"";
1772 nIndex++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001773 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001774 }
Tom Sepez3c3e2712017-04-17 15:38:19 -07001775 if (sPart.GetLength() > 0)
tsepezb4694242016-08-15 16:44:55 -07001776 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001777
tsepezb4694242016-08-15 16:44:55 -07001778 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001779 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001780 else
tsepezf3dc8c62016-08-10 06:29:29 -07001781 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001782
tsepez4cf55152016-11-02 14:37:54 -07001783 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001784}