blob: c065bd0946bf568b7aa8345d25a77cd7b57b3143 [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) {
tsepeze5aff742016-08-08 09:49:42 -0700192 CJS_Array StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 if (val.IsArrayObject()) {
tsepezb4694242016-08-15 16:44:55 -0700194 val.ConvertToArray(pRuntime, StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700195 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 }
Ryan Harrison275e2602017-09-18 14:23:18 -0400197 WideString wsStr = val.ToCFXWideString(pRuntime);
198 ByteString t = ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700199 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200
201 int ch = ',';
202 int nIndex = 0;
203
204 while (*p) {
205 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800206 if (!pTemp) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400207 StrArray.SetElement(pRuntime, nIndex,
208 CJS_Value(pRuntime, StrTrim(ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 }
Lei Zhang997de612015-11-04 18:17:53 -0800211
212 char* pSub = new char[pTemp - p + 1];
213 strncpy(pSub, p, pTemp - p);
214 *(pSub + (pTemp - p)) = '\0';
215
Ryan Harrison275e2602017-09-18 14:23:18 -0400216 StrArray.SetElement(pRuntime, nIndex,
217 CJS_Value(pRuntime, StrTrim(ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800218 delete[] pSub;
219
220 nIndex++;
221 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 }
223 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224}
225
Ryan Harrison275e2602017-09-18 14:23:18 -0400226int CJS_PublicMethods::ParseStringInteger(const WideString& str,
Ryan Harrison75584142017-09-01 13:30:19 -0400227 FX_STRSIZE nStart,
228 FX_STRSIZE& nSkip,
229 FX_STRSIZE nMaxStep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 int nRet = 0;
231 nSkip = 0;
Ryan Harrison75584142017-09-01 13:30:19 -0400232 for (FX_STRSIZE i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 if (i - nStart > 10)
234 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400236 wchar_t c = str[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700237 if (!std::iswdigit(c))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800238 break;
239
Lei Zhange8c1d412017-05-04 12:13:55 -0700240 nRet = nRet * 10 + FXSYS_DecimalCharToInt(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800241 nSkip = i - nStart + 1;
242 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 break;
244 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247}
248
Ryan Harrison275e2602017-09-18 14:23:18 -0400249WideString CJS_PublicMethods::ParseStringString(const WideString& str,
250 FX_STRSIZE nStart,
251 FX_STRSIZE& nSkip) {
252 WideString swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 nSkip = 0;
Ryan Harrison75584142017-09-01 13:30:19 -0400254 for (FX_STRSIZE i = nStart, sz = str.GetLength(); i < sz; i++) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400255 wchar_t c = str[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700256 if (!std::iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800258
259 swRet += c;
260 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265
Ryan Harrison275e2602017-09-18 14:23:18 -0400266double CJS_PublicMethods::ParseNormalDate(const WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800267 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 int nYear = JS_GetYearFromTime(dt);
271 int nMonth = JS_GetMonthFromTime(dt) + 1;
272 int nDay = JS_GetDayFromTime(dt);
273 int nHour = JS_GetHourFromTime(dt);
274 int nMin = JS_GetMinFromTime(dt);
275 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278
Ryan Harrison75584142017-09-01 13:30:19 -0400279 FX_STRSIZE nSkip = 0;
280 FX_STRSIZE nLen = value.GetLength();
281 FX_STRSIZE nIndex = 0;
282 FX_STRSIZE i = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 while (i < nLen) {
284 if (nIndex > 2)
285 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400287 wchar_t c = value[i];
Lei Zhange247ec42017-04-20 21:41:36 -0700288 if (std::iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
290 i += nSkip;
291 } else {
292 i++;
293 }
294 }
295
296 if (nIndex == 2) {
297 // case2: month/day
298 // case3: day/month
299 if ((number[0] >= 1 && number[0] <= 12) &&
300 (number[1] >= 1 && number[1] <= 31)) {
301 nMonth = number[0];
302 nDay = number[1];
303 } else if ((number[0] >= 1 && number[0] <= 31) &&
304 (number[1] >= 1 && number[1] <= 12)) {
305 nDay = number[0];
306 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700307 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308
Lei Zhang9559b7a2015-12-21 11:12:20 -0800309 if (bWrongFormat)
310 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 } else if (nIndex == 3) {
312 // case1: year/month/day
313 // case2: month/day/year
314 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700315
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
317 (number[2] >= 1 && number[2] <= 31)) {
318 nYear = number[0];
319 nMonth = number[1];
320 nDay = number[2];
321 } else if ((number[0] >= 1 && number[0] <= 12) &&
322 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
323 nMonth = number[0];
324 nDay = number[1];
325 nYear = number[2];
326 } else if ((number[0] >= 1 && number[0] <= 31) &&
327 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
328 nDay = number[0];
329 nMonth = number[1];
330 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700331 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700332
Lei Zhang9559b7a2015-12-21 11:12:20 -0800333 if (bWrongFormat)
334 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800336 if (bWrongFormat)
337 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 return dt;
339 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340
Ryan Harrison275e2602017-09-18 14:23:18 -0400341 WideString swTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700343 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700344}
345
Ryan Harrison275e2602017-09-18 14:23:18 -0400346double CJS_PublicMethods::MakeRegularDate(const WideString& value,
347 const WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800348 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700350
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 if (format.IsEmpty() || value.IsEmpty())
352 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700353
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 int nYear = JS_GetYearFromTime(dt);
355 int nMonth = JS_GetMonthFromTime(dt) + 1;
356 int nDay = JS_GetDayFromTime(dt);
357 int nHour = JS_GetHourFromTime(dt);
358 int nMin = JS_GetMinFromTime(dt);
359 int nSec = JS_GetSecFromTime(dt);
360
361 int nYearSub = 99; // nYear - 2000;
362
tsepez4cf55152016-11-02 14:37:54 -0700363 bool bPm = false;
364 bool bExit = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800365 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366
Ryan Harrisonaa3a9cd2017-08-29 16:39:44 -0400367 FX_STRSIZE i = 0;
Ryan Harrison75584142017-09-01 13:30:19 -0400368 FX_STRSIZE j = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369
370 while (i < format.GetLength()) {
371 if (bExit)
372 break;
373
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400374 wchar_t c = format[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 switch (c) {
376 case ':':
377 case '.':
378 case '-':
379 case '\\':
380 case '/':
381 i++;
382 j++;
383 break;
384
385 case 'y':
386 case 'm':
387 case 'd':
388 case 'H':
389 case 'h':
390 case 'M':
391 case 's':
392 case 't': {
Ryan Harrison75584142017-09-01 13:30:19 -0400393 FX_STRSIZE oldj = j;
394 FX_STRSIZE nSkip = 0;
Ryan Harrisonaa3a9cd2017-08-29 16:39:44 -0400395 FX_STRSIZE remaining = format.GetLength() - i - 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400397 if (remaining == 0 || format[i + 1] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700399 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 i++;
401 j++;
402 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700403 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 nMonth = ParseStringInteger(value, j, nSkip, 2);
405 i++;
406 j += nSkip;
407 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700408 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 nDay = ParseStringInteger(value, j, nSkip, 2);
410 i++;
411 j += nSkip;
412 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700413 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 nHour = ParseStringInteger(value, j, nSkip, 2);
415 i++;
416 j += nSkip;
417 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700418 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 nHour = ParseStringInteger(value, j, nSkip, 2);
420 i++;
421 j += nSkip;
422 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700423 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 nMin = ParseStringInteger(value, j, nSkip, 2);
425 i++;
426 j += nSkip;
427 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700428 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429 nSec = ParseStringInteger(value, j, nSkip, 2);
430 i++;
431 j += nSkip;
432 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700433 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400434 bPm = (j < value.GetLength() && value[j] == 'p');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 i++;
436 j++;
437 break;
438 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400439 } else if (remaining == 1 || format[i + 2] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 switch (c) {
441 case 'y':
442 nYear = ParseStringInteger(value, j, nSkip, 4);
443 i += 2;
444 j += nSkip;
445 break;
446 case 'm':
447 nMonth = ParseStringInteger(value, j, nSkip, 2);
448 i += 2;
449 j += nSkip;
450 break;
451 case 'd':
452 nDay = ParseStringInteger(value, j, nSkip, 2);
453 i += 2;
454 j += nSkip;
455 break;
456 case 'H':
457 nHour = ParseStringInteger(value, j, nSkip, 2);
458 i += 2;
459 j += nSkip;
460 break;
461 case 'h':
462 nHour = ParseStringInteger(value, j, nSkip, 2);
463 i += 2;
464 j += nSkip;
465 break;
466 case 'M':
467 nMin = ParseStringInteger(value, j, nSkip, 2);
468 i += 2;
469 j += nSkip;
470 break;
471 case 's':
472 nSec = ParseStringInteger(value, j, nSkip, 2);
473 i += 2;
474 j += nSkip;
475 break;
476 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400477 bPm = (j + 1 < value.GetLength() && value[j] == 'p' &&
478 value[j + 1] == 'm');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 i += 2;
480 j += 2;
481 break;
482 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400483 } else if (remaining == 2 || format[i + 3] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 switch (c) {
485 case 'm': {
Ryan Harrison275e2602017-09-18 14:23:18 -0400486 WideString sMonth = ParseStringString(value, j, nSkip);
tsepez4cf55152016-11-02 14:37:54 -0700487 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 for (int m = 0; m < 12; m++) {
489 if (sMonth.CompareNoCase(months[m]) == 0) {
490 nMonth = m + 1;
491 i += 3;
492 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700493 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700495 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 }
497
498 if (!bFind) {
499 nMonth = ParseStringInteger(value, j, nSkip, 3);
500 i += 3;
501 j += nSkip;
502 }
503 } break;
504 case 'y':
505 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700506 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507 i += 3;
508 j += 3;
509 break;
510 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400511 } else if (remaining == 3 || format[i + 4] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512 switch (c) {
513 case 'y':
514 nYear = ParseStringInteger(value, j, nSkip, 4);
515 j += nSkip;
516 i += 4;
517 break;
518 case 'm': {
tsepez4cf55152016-11-02 14:37:54 -0700519 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520
Ryan Harrison275e2602017-09-18 14:23:18 -0400521 WideString sMonth = ParseStringString(value, j, nSkip);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522 sMonth.MakeLower();
523
524 for (int m = 0; m < 12; m++) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400525 WideString sFullMonths = fullmonths[m];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700526 sFullMonths.MakeLower();
527
Ryan Harrison12db7512017-08-23 10:39:35 -0400528 if (sFullMonths.Contains(sMonth.c_str())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529 nMonth = m + 1;
530 i += 4;
531 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700532 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533 break;
534 }
535 }
536
537 if (!bFind) {
538 nMonth = ParseStringInteger(value, j, nSkip, 4);
539 i += 4;
540 j += nSkip;
541 }
542 } break;
543 default:
544 i += 4;
545 j += 4;
546 break;
547 }
548 } else {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400549 if (j >= value.GetLength() || format[i] != value[j]) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800550 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700551 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552 }
553 i++;
554 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700555 }
Tom Sepez85386422014-07-23 10:28:37 -0700556
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800558 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700559 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560 }
561 }
562
563 break;
564 default:
565 if (value.GetLength() <= j) {
tsepez4cf55152016-11-02 14:37:54 -0700566 bExit = true;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400567 } else if (format[i] != value[j]) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800568 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700569 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570 }
571
572 i++;
573 j++;
574 break;
575 }
576 }
577
578 if (bPm)
579 nHour += 12;
580
581 if (nYear >= 0 && nYear <= nYearSub)
582 nYear += 2000;
583
584 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800585 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586
587 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800588 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700589
590 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800591 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592
593 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800594 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700595
596 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800597 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598
599 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800600 if (bBadFormat) {
601 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602 } else {
603 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
604 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -0700605 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -0700606 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700607 }
608
Tom Sepezdf950b82017-08-04 11:33:49 -0700609 if (std::isnan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800610 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611
Lei Zhang9559b7a2015-12-21 11:12:20 -0800612 if (bWrongFormat)
613 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700614
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 return dRet;
616}
617
Ryan Harrison275e2602017-09-18 14:23:18 -0400618WideString CJS_PublicMethods::MakeFormatDate(double dDate,
619 const WideString& format) {
620 WideString sRet = L"", sPart = L"";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621
622 int nYear = JS_GetYearFromTime(dDate);
623 int nMonth = JS_GetMonthFromTime(dDate) + 1;
624 int nDay = JS_GetDayFromTime(dDate);
625 int nHour = JS_GetHourFromTime(dDate);
626 int nMin = JS_GetMinFromTime(dDate);
627 int nSec = JS_GetSecFromTime(dDate);
628
Ryan Harrisonaa3a9cd2017-08-29 16:39:44 -0400629 FX_STRSIZE i = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700630 while (i < format.GetLength()) {
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400631 wchar_t c = format[i];
Ryan Harrisonaa3a9cd2017-08-29 16:39:44 -0400632 FX_STRSIZE remaining = format.GetLength() - i - 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700633 sPart = L"";
634 switch (c) {
635 case 'y':
636 case 'm':
637 case 'd':
638 case 'H':
639 case 'h':
640 case 'M':
641 case 's':
642 case 't':
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400643 if (remaining == 0 || format[i + 1] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644 switch (c) {
645 case 'y':
646 sPart += c;
647 break;
648 case 'm':
649 sPart.Format(L"%d", nMonth);
650 break;
651 case 'd':
652 sPart.Format(L"%d", nDay);
653 break;
654 case 'H':
655 sPart.Format(L"%d", nHour);
656 break;
657 case 'h':
658 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
659 break;
660 case 'M':
661 sPart.Format(L"%d", nMin);
662 break;
663 case 's':
664 sPart.Format(L"%d", nSec);
665 break;
666 case 't':
667 sPart += nHour > 12 ? 'p' : 'a';
668 break;
669 }
670 i++;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400671 } else if (remaining == 1 || format[i + 2] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 switch (c) {
673 case 'y':
674 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
675 break;
676 case 'm':
677 sPart.Format(L"%02d", nMonth);
678 break;
679 case 'd':
680 sPart.Format(L"%02d", nDay);
681 break;
682 case 'H':
683 sPart.Format(L"%02d", nHour);
684 break;
685 case 'h':
686 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
687 break;
688 case 'M':
689 sPart.Format(L"%02d", nMin);
690 break;
691 case 's':
692 sPart.Format(L"%02d", nSec);
693 break;
694 case 't':
695 sPart = nHour > 12 ? L"pm" : L"am";
696 break;
697 }
698 i += 2;
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400699 } else if (remaining == 2 || format[i + 3] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700 switch (c) {
701 case 'm':
702 i += 3;
703 if (nMonth > 0 && nMonth <= 12)
704 sPart += months[nMonth - 1];
705 break;
706 default:
707 i += 3;
708 sPart += c;
709 sPart += c;
710 sPart += c;
711 break;
712 }
Ryan Harrison8a1758b2017-08-15 10:37:59 -0400713 } else if (remaining == 3 || format[i + 4] != c) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714 switch (c) {
715 case 'y':
716 sPart.Format(L"%04d", nYear);
717 i += 4;
718 break;
719 case 'm':
720 i += 4;
721 if (nMonth > 0 && nMonth <= 12)
722 sPart += fullmonths[nMonth - 1];
723 break;
724 default:
725 i += 4;
726 sPart += c;
727 sPart += c;
728 sPart += c;
729 sPart += c;
730 break;
731 }
732 } else {
733 i++;
734 sPart += c;
735 }
736 break;
737 default:
738 i++;
739 sPart += c;
740 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700741 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700742
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743 sRet += sPart;
744 }
745
746 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700747}
748
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700749// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
750// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800751bool CJS_PublicMethods::AFNumber_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700752 const std::vector<CJS_Value>& params,
753 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400754 WideString& sError) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400755#if _FX_OS_ != _FX_OS_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756 if (params.size() != 6) {
tsepezcd5dc852016-09-08 11:23:24 -0700757 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700758 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700760
Tom Sepezb1670b52017-02-16 17:01:00 -0800761 CJS_EventHandler* pEvent =
762 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700764 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700765
Ryan Harrison275e2602017-09-18 14:23:18 -0400766 WideString& Value = pEvent->Value();
767 ByteString strValue = StrTrim(ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700768 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700769 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770
tsepezb4694242016-08-15 16:44:55 -0700771 int iDec = params[0].ToInt(pRuntime);
772 int iSepStyle = params[1].ToInt(pRuntime);
773 int iNegStyle = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700774 // params[3] is iCurrStyle, it's not used.
Ryan Harrison275e2602017-09-18 14:23:18 -0400775 WideString wstrCurrency = params[4].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700776 bool bCurrencyPrepend = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700777
778 if (iDec < 0)
779 iDec = -iDec;
780
781 if (iSepStyle < 0 || iSepStyle > 3)
782 iSepStyle = 0;
783
784 if (iNegStyle < 0 || iNegStyle > 3)
785 iNegStyle = 0;
786
npm49c59282016-11-15 15:14:04 -0800787 // Processing decimal places
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700788 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700789 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700790 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700791 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792
npm49c59282016-11-15 15:14:04 -0800793 // Calculating number string
794 bool bNegative;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795 int iDec2;
npm49c59282016-11-15 15:14:04 -0800796 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797 if (strValue.IsEmpty()) {
798 dValue = 0;
npm49c59282016-11-15 15:14:04 -0800799 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800 if (strValue.IsEmpty()) {
801 strValue = "0";
802 iDec2 = 1;
803 }
804 }
805
npm49c59282016-11-15 15:14:04 -0800806 // Processing separator style
Ryan Harrison75584142017-09-01 13:30:19 -0400807 if (static_cast<FX_STRSIZE>(iDec2) < strValue.GetLength()) {
npm49c59282016-11-15 15:14:04 -0800808 if (iSepStyle == 2 || iSepStyle == 3)
809 strValue.Replace(".", ",");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700810
811 if (iDec2 == 0)
812 strValue.Insert(iDec2, '0');
813 }
814 if (iSepStyle == 0 || iSepStyle == 2) {
npm49c59282016-11-15 15:14:04 -0800815 char cSeparator;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 if (iSepStyle == 0)
npm49c59282016-11-15 15:14:04 -0800817 cSeparator = ',';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 else
npm49c59282016-11-15 15:14:04 -0800819 cSeparator = '.';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820
npm49c59282016-11-15 15:14:04 -0800821 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3)
822 strValue.Insert(iDecPositive, cSeparator);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823 }
824
npm49c59282016-11-15 15:14:04 -0800825 // Processing currency string
Ryan Harrison275e2602017-09-18 14:23:18 -0400826 Value = WideString::FromLocal(strValue.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827
828 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700829 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830 else
thestigcf03f8e2016-05-09 12:36:18 -0700831 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832
npm49c59282016-11-15 15:14:04 -0800833 // Processing negative style
834 if (bNegative) {
835 if (iNegStyle == 0)
thestigcf03f8e2016-05-09 12:36:18 -0700836 Value = L"-" + Value;
npm49c59282016-11-15 15:14:04 -0800837 else if (iNegStyle == 2 || iNegStyle == 3)
thestigcf03f8e2016-05-09 12:36:18 -0700838 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700839 if (iNegStyle == 1 || iNegStyle == 3) {
840 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700841 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700842 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700843 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700844 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700845 vColElm = CJS_Value(pRuntime, 1);
tsepezb4694242016-08-15 16:44:55 -0700846 arColor.SetElement(pRuntime, 1, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700847 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700848 arColor.SetElement(pRuntime, 2, vColElm);
849 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850
Tom Sepez67fd5df2015-10-08 12:24:19 -0700851 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852 vProp.StartGetting();
853 vProp << arColor;
854 vProp.StartSetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800855 fTarget->textColor(pRuntime, vProp, sError); // red
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856 }
857 }
858 } else {
859 if (iNegStyle == 1 || iNegStyle == 3) {
860 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700861 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700862 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700863 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700864 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700865 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700866 arColor.SetElement(pRuntime, 1, vColElm);
867 arColor.SetElement(pRuntime, 2, vColElm);
868 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869
Tom Sepez67fd5df2015-10-08 12:24:19 -0700870 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871 vProp.StartGetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800872 fTarget->textColor(pRuntime, vProp, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873
tsepeze5aff742016-08-08 09:49:42 -0700874 CJS_Array aProp;
tsepezb4694242016-08-15 16:44:55 -0700875 vProp.GetJSValue()->ConvertToArray(pRuntime, aProp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700876
Dan Sinclair7f55a542017-07-13 14:17:10 -0400877 CFX_Color crProp;
878 CFX_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700879 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
880 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881
882 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700883 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 vProp2.StartGetting();
885 vProp2 << arColor;
886 vProp2.StartSetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800887 fTarget->textColor(pRuntime, vProp2, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700888 }
889 }
890 }
891 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700892#endif
tsepez4cf55152016-11-02 14:37:54 -0700893 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700894}
895
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
897// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800898bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700899 const std::vector<CJS_Value>& params,
900 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400901 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700903 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700904
Tom Sepezb1670b52017-02-16 17:01:00 -0800905 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
906 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700908 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700909
Ryan Harrison275e2602017-09-18 14:23:18 -0400910 WideString& val = pEvent->Value();
911 WideString& wstrChange = pEvent->Change();
912 WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700913
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914 if (pEvent->WillCommit()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400915 WideString swTemp = StrTrim(wstrValue);
ochanga0a3bc32016-05-12 15:22:48 -0700916 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700917 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700918
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700919 swTemp.Replace(L",", L".");
920 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700921 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700922 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700923 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700924 }
tsepez4cf55152016-11-02 14:37:54 -0700925 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700927
Ryan Harrison275e2602017-09-18 14:23:18 -0400928 WideString wstrSelected;
thestigcf03f8e2016-05-09 12:36:18 -0700929 if (pEvent->SelStart() != -1) {
930 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
931 pEvent->SelEnd() - pEvent->SelStart());
932 }
933
Ryan Harrison12db7512017-08-23 10:39:35 -0400934 bool bHasSign = wstrValue.Contains(L'-') && !wstrSelected.Contains(L'-');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935 if (bHasSign) {
936 // can't insert "change" in front to sign postion.
937 if (pEvent->SelStart() == 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700938 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700939 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700940 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700942
tsepezb4694242016-08-15 16:44:55 -0700943 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700944 if (iSepStyle < 0 || iSepStyle > 3)
945 iSepStyle = 0;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400946 const wchar_t cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700947
Ryan Harrison12db7512017-08-23 10:39:35 -0400948 bool bHasSep = wstrValue.Contains(cSep);
thestigcf03f8e2016-05-09 12:36:18 -0700949 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
950 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 if (bHasSep) {
Lei Zhange247ec42017-04-20 21:41:36 -0700952 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700953 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700954 }
tsepez4cf55152016-11-02 14:37:54 -0700955 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700956 continue;
957 }
thestigcf03f8e2016-05-09 12:36:18 -0700958 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 if (bHasSign) {
Lei Zhange247ec42017-04-20 21:41:36 -0700960 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700961 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700962 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800963 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700964 if (i != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700965 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700966 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967 }
968 if (pEvent->SelStart() != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700969 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700970 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971 }
tsepez4cf55152016-11-02 14:37:54 -0700972 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 continue;
974 }
975
Lei Zhange247ec42017-04-20 21:41:36 -0700976 if (!std::iswdigit(wstrChange[i])) {
977 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700978 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979 }
980 }
981
Ryan Harrison275e2602017-09-18 14:23:18 -0400982 WideString wprefix = wstrValue.Left(pEvent->SelStart());
983 WideString wpostfix;
Ryan Harrison75584142017-09-01 13:30:19 -0400984 if (pEvent->SelEnd() >= 0 &&
985 static_cast<FX_STRSIZE>(pEvent->SelEnd()) < wstrValue.GetLength())
986 wpostfix = wstrValue.Right(wstrValue.GetLength() -
987 static_cast<FX_STRSIZE>(pEvent->SelEnd()));
thestigcf03f8e2016-05-09 12:36:18 -0700988 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700989 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990}
991
992// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -0800993bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700994 const std::vector<CJS_Value>& params,
995 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400996 WideString& sError) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400997#if _FX_OS_ != _FX_OS_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700999 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001000 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001002
1003 CJS_EventHandler* pEvent =
1004 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001006 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007
Ryan Harrison275e2602017-09-18 14:23:18 -04001008 WideString& Value = pEvent->Value();
1009 ByteString strValue = StrTrim(ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001011 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012
tsepezb4694242016-08-15 16:44:55 -07001013 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014 if (iDec < 0)
1015 iDec = -iDec;
1016
tsepezb4694242016-08-15 16:44:55 -07001017 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 if (iSepStyle < 0 || iSepStyle > 3)
1019 iSepStyle = 0;
1020
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001021 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001022 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023 dValue *= 100;
1024 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001025 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026
1027 int iDec2;
1028 int iNegative = 0;
1029 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1030 if (strValue.IsEmpty()) {
1031 dValue = 0;
1032 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1033 }
1034
1035 if (iDec2 < 0) {
1036 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1037 strValue = "0" + strValue;
1038 }
1039 iDec2 = 0;
1040 }
1041 int iMax = strValue.GetLength();
1042 if (iDec2 > iMax) {
1043 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1044 strValue += "0";
1045 }
1046 iMax = iDec2 + 1;
1047 }
dsinclair64376be2016-03-31 20:03:24 -07001048
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 // for processing seperator style
1050 if (iDec2 < iMax) {
1051 if (iSepStyle == 0 || iSepStyle == 1) {
1052 strValue.Insert(iDec2, '.');
1053 iMax++;
1054 } else if (iSepStyle == 2 || iSepStyle == 3) {
1055 strValue.Insert(iDec2, ',');
1056 iMax++;
1057 }
1058
1059 if (iDec2 == 0)
1060 strValue.Insert(iDec2, '0');
1061 }
1062 if (iSepStyle == 0 || iSepStyle == 2) {
1063 char cSeperator;
1064 if (iSepStyle == 0)
1065 cSeperator = ',';
1066 else
1067 cSeperator = '.';
1068
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001069 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070 strValue.Insert(iDecPositive, cSeperator);
1071 iMax++;
1072 }
1073 }
dsinclair64376be2016-03-31 20:03:24 -07001074
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 // negative mark
1076 if (iNegative)
1077 strValue = "-" + strValue;
1078 strValue += "%";
Ryan Harrison275e2602017-09-18 14:23:18 -04001079 Value = WideString::FromLocal(strValue.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080#endif
tsepez4cf55152016-11-02 14:37:54 -07001081 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082}
1083// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001084bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001085 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001086 const std::vector<CJS_Value>& params,
1087 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001088 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001089 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001090}
1091
1092// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001093bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001094 const std::vector<CJS_Value>& params,
1095 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001096 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001097 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001098 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001099 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001101
1102 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1103 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001105 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106
Ryan Harrison275e2602017-09-18 14:23:18 -04001107 WideString& val = pEvent->Value();
1108 WideString strValue = val;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001109 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001110 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111
Ryan Harrison275e2602017-09-18 14:23:18 -04001112 WideString sFormat = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113 double dDate = 0.0f;
1114
Ryan Harrison12db7512017-08-23 10:39:35 -04001115 if (strValue.Contains(L"GMT")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116 // for GMT format time
1117 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1118 dDate = MakeInterDate(strValue);
1119 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001120 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001121 }
1122
Tom Sepezdf950b82017-08-04 11:33:49 -07001123 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001124 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001125 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001127 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001128 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001129 }
1130
1131 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001132 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133}
1134
Ryan Harrison275e2602017-09-18 14:23:18 -04001135double CJS_PublicMethods::MakeInterDate(const WideString& strValue) {
1136 std::vector<WideString> wsArray;
1137 WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001138 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001140 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 sTemp = L"";
1142 continue;
1143 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001144 sTemp += c;
1145 }
Tom Sepezab277682016-02-17 10:07:21 -08001146 wsArray.push_back(sTemp);
1147 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 return 0;
1149
Tom Sepez4246b002016-01-20 11:48:29 -08001150 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 sTemp = wsArray[1];
1152 if (sTemp.Compare(L"Jan") == 0)
1153 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001154 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001156 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001158 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001159 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001160 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001162 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001164 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001166 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001168 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001170 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001172 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001174 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 nMonth = 12;
1176
Ryan Harrison275e2602017-09-18 14:23:18 -04001177 int nDay = FX_atof(wsArray[2].AsStringView());
1178 int nHour = FX_atof(wsArray[3].AsStringView());
1179 int nMin = FX_atof(wsArray[4].AsStringView());
1180 int nSec = FX_atof(wsArray[5].AsStringView());
1181 int nYear = FX_atof(wsArray[7].AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1183 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -07001184 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001185 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186
1187 return dRet;
1188}
1189
1190// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001191bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001192 const std::vector<CJS_Value>& params,
1193 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001194 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195 if (params.size() != 1) {
1196 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001197 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198 }
1199
Tom Sepezb1670b52017-02-16 17:01:00 -08001200 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1201 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 if (pEvent->WillCommit()) {
1203 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001204 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001205
Ryan Harrison275e2602017-09-18 14:23:18 -04001206 WideString strValue = pEvent->Value();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001208 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001209
Ryan Harrison275e2602017-09-18 14:23:18 -04001210 WideString sFormat = params[0].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001211 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001212 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Tom Sepezdf950b82017-08-04 11:33:49 -07001213 if (bWrongFormat || std::isnan(dRet)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001214 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001215 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001217 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001218 pEvent->Rc() = false;
1219 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001220 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001221 }
tsepez4cf55152016-11-02 14:37:54 -07001222 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001223}
1224
Tom Sepezb1670b52017-02-16 17:01:00 -08001225bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001226 const std::vector<CJS_Value>& params,
1227 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001228 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001230 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001231 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001233
tsepezb4694242016-08-15 16:44:55 -07001234 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001235 const wchar_t* cFormats[] = {L"m/d",
1236 L"m/d/yy",
1237 L"mm/dd/yy",
1238 L"mm/yy",
1239 L"d-mmm",
1240 L"d-mmm-yy",
1241 L"dd-mmm-yy",
1242 L"yy-mm-dd",
1243 L"mmm-yy",
1244 L"mmmm-yy",
1245 L"mmm d, yyyy",
1246 L"mmmm d, yyyy",
1247 L"m/d/yy h:MM tt",
1248 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001249
Lei Zhanga0f67242015-08-17 15:39:30 -07001250 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1251 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001252
Lei Zhang945fdb72015-11-11 10:18:16 -08001253 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001254 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1255 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001257
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001259bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001260 const std::vector<CJS_Value>& params,
1261 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001262 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001264 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001265 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 }
1267
tsepezb4694242016-08-15 16:44:55 -07001268 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001269 const wchar_t* cFormats[] = {L"m/d",
1270 L"m/d/yy",
1271 L"mm/dd/yy",
1272 L"mm/yy",
1273 L"d-mmm",
1274 L"d-mmm-yy",
1275 L"dd-mmm-yy",
1276 L"yy-mm-dd",
1277 L"mmm-yy",
1278 L"mmmm-yy",
1279 L"mmm d, yyyy",
1280 L"mmmm d, yyyy",
1281 L"m/d/yy h:MM tt",
1282 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283
Lei Zhanga0f67242015-08-17 15:39:30 -07001284 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1285 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001286
Lei Zhang945fdb72015-11-11 10:18:16 -08001287 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001288 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1289 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001290}
1291
1292// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001293bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001294 const std::vector<CJS_Value>& params,
1295 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001296 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001298 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001299 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 }
1301
tsepezb4694242016-08-15 16:44:55 -07001302 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001303 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1304 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305
Lei Zhanga0f67242015-08-17 15:39:30 -07001306 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1307 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001308
Lei Zhang945fdb72015-11-11 10:18:16 -08001309 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001310 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1311 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312}
1313
Tom Sepezb1670b52017-02-16 17:01:00 -08001314bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001315 const std::vector<CJS_Value>& params,
1316 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001317 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001319 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001320 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321 }
1322
tsepezb4694242016-08-15 16:44:55 -07001323 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001324 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1325 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326
Lei Zhanga0f67242015-08-17 15:39:30 -07001327 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1328 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001329
Lei Zhang945fdb72015-11-11 10:18:16 -08001330 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001331 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1332 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001333}
1334
Tom Sepezb1670b52017-02-16 17:01:00 -08001335bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001336 const std::vector<CJS_Value>& params,
1337 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001338 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001339 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001340}
1341
Tom Sepezb1670b52017-02-16 17:01:00 -08001342bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001343 const std::vector<CJS_Value>& params,
1344 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001345 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001346 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001347}
1348
1349// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001350bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001351 const std::vector<CJS_Value>& params,
1352 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001353 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001355 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001356 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357 }
1358
Tom Sepezb1670b52017-02-16 17:01:00 -08001359 CJS_EventHandler* pEvent =
1360 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001362 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363
Ryan Harrison275e2602017-09-18 14:23:18 -04001364 WideString wsSource = pEvent->Value();
1365 WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001366 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001367 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001368 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001370 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001371 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001373 case 2:
1374 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1375 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376 else
tsepez4f1f41f2016-03-28 14:13:16 -07001377 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001379 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001380 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 break;
1382 }
1383
tsepez4f1f41f2016-03-28 14:13:16 -07001384 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001385 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001386}
1387
1388// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001389bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001390 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001391 const std::vector<CJS_Value>& params,
1392 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001393 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001395 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001396 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 }
1398
Tom Sepezb1670b52017-02-16 17:01:00 -08001399 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1400 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001401 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001402 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403
Ryan Harrison275e2602017-09-18 14:23:18 -04001404 WideString& valEvent = pEvent->Value();
1405 WideString wstrMask = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001407 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001410 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001411 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001412
1413 FX_STRSIZE iIndexMask = 0;
1414 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1415 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001416 break;
1417 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001418
thestigcf03f8e2016-05-09 12:36:18 -07001419 if (iIndexMask != wstrMask.GetLength() ||
1420 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001421 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001422 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001423 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424 }
tsepez4cf55152016-11-02 14:37:54 -07001425 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001426 }
1427
Ryan Harrison275e2602017-09-18 14:23:18 -04001428 WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001429 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001430 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431
Ryan Harrison275e2602017-09-18 14:23:18 -04001432 WideString wChange = wideChange;
thestigcf03f8e2016-05-09 12:36:18 -07001433 FX_STRSIZE iIndexMask = pEvent->SelStart();
1434 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1435 pEvent->SelStart() - pEvent->SelEnd();
1436 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001437 AlertIfPossible(pContext,
1438 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001439 pEvent->Rc() = false;
1440 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441 }
1442
thestigcf03f8e2016-05-09 12:36:18 -07001443 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001444 AlertIfPossible(pContext,
1445 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001446 pEvent->Rc() = false;
1447 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448 }
1449
thestigcf03f8e2016-05-09 12:36:18 -07001450 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1451 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001452 AlertIfPossible(pContext,
1453 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001454 pEvent->Rc() = false;
1455 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001456 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001457 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001458 if (!isReservedMaskChar(wMask))
1459 wChange.SetAt(i, wMask);
1460
1461 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001462 pEvent->Rc() = false;
1463 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001464 }
1465 iIndexMask++;
1466 }
thestigcf03f8e2016-05-09 12:36:18 -07001467 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001468 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001469}
1470
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001471// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001472bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001473 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001474 const std::vector<CJS_Value>& params,
1475 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001476 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001478 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001479 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001481
Tom Sepezb1670b52017-02-16 17:01:00 -08001482 CJS_EventHandler* pEvent =
1483 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001484 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001485 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001486
thestigcf03f8e2016-05-09 12:36:18 -07001487 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001488 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001489 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 cFormat = "99999";
1491 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001492 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001493 cFormat = "999999999";
1494 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001495 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001496 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001497 cFormat = "9999999999";
1498 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 cFormat = "9999999";
1500 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001501 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502 cFormat = "999999999";
1503 break;
1504 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001505
Lei Zhang945fdb72015-11-11 10:18:16 -08001506 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001507 params2.push_back(CJS_Value(pRuntime, cFormat));
1508 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001509}
1510
Tom Sepezb1670b52017-02-16 17:01:00 -08001511bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001512 const std::vector<CJS_Value>& params,
1513 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001514 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001516 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001517 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001519
Tom Sepezb1670b52017-02-16 17:01:00 -08001520 CJS_EventHandler* pEventHandler =
1521 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001522
Ryan Harrison275e2602017-09-18 14:23:18 -04001523 WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001524 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001525 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001526
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001528 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001529 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001530 }
1531
Ryan Harrison275e2602017-09-18 14:23:18 -04001532 WideString prefix, postfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001533
1534 if (pEventHandler->SelStart() >= 0)
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001535 prefix = swValue.Left(pEventHandler->SelStart());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001536 else
1537 prefix = L"";
1538
1539 if (pEventHandler->SelEnd() >= 0 &&
Ryan Harrison75584142017-09-01 13:30:19 -04001540 static_cast<FX_STRSIZE>(pEventHandler->SelEnd()) <= swValue.GetLength())
1541 postfix = swValue.Right(swValue.GetLength() -
1542 static_cast<FX_STRSIZE>(pEventHandler->SelEnd()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 else
1544 postfix = L"";
1545
tsepezf3dc8c62016-08-10 06:29:29 -07001546 vRet =
1547 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001548 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001549}
1550
Tom Sepezb1670b52017-02-16 17:01:00 -08001551bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001552 const std::vector<CJS_Value>& params,
1553 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001554 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001555 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001556 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001557 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001558 }
1559
Ryan Harrison275e2602017-09-18 14:23:18 -04001560 WideString sValue = params[0].ToCFXWideString(pRuntime);
1561 WideString sFormat = params[1].ToCFXWideString(pRuntime);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001562 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Tom Sepezdf950b82017-08-04 11:33:49 -07001563 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001564 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001565 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566 sFormat.c_str());
Tom Sepezb1670b52017-02-16 17:01:00 -08001567 AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001568 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001569 }
1570
tsepezf3dc8c62016-08-10 06:29:29 -07001571 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001572 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001573}
1574
Tom Sepezb1670b52017-02-16 17:01:00 -08001575bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001576 const std::vector<CJS_Value>& params,
1577 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001578 WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001579 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001580 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001581 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001582 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001583
tsepezb4694242016-08-15 16:44:55 -07001584 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
1585 params[0].ToCFXWideString(pRuntime).c_str(),
1586 params[1].ToDouble(pRuntime),
1587 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001588
tsepez4cf55152016-11-02 14:37:54 -07001589 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001590}
1591
Tom Sepezb1670b52017-02-16 17:01:00 -08001592bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001593 const std::vector<CJS_Value>& params,
1594 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001595 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001596 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001597 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001598 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001599 }
tsepezf3dc8c62016-08-10 06:29:29 -07001600
Ryan Harrison275e2602017-09-18 14:23:18 -04001601 WideString ws = params[0].ToCFXWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001602 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001603 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001604 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001605 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001606 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001607 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001608}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001609
Tom Sepezb1670b52017-02-16 17:01:00 -08001610bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001611 const std::vector<CJS_Value>& params,
1612 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001613 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001614 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001615 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001616 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001617 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001618
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001619 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001620 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001621 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001622 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001623 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001624
dsinclair4526faf2016-10-11 10:54:49 -07001625 CPDFSDK_InterForm* pReaderInterForm =
Tom Sepezb1670b52017-02-16 17:01:00 -08001626 pRuntime->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001628
Ryan Harrison275e2602017-09-18 14:23:18 -04001629 WideString sFunction = params[0].ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001630 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001631
Tom Sepez67fd5df2015-10-08 12:24:19 -07001632 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001633 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001634
tsepezb4694242016-08-15 16:44:55 -07001635 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001636 CJS_Value jsValue(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001637 FieldNameArray.GetElement(pRuntime, i, jsValue);
Ryan Harrison275e2602017-09-18 14:23:18 -04001638 WideString wsFieldName = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001639
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001640 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1641 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1642 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001643 switch (pFormField->GetFieldType()) {
1644 case FIELDTYPE_TEXTFIELD:
1645 case FIELDTYPE_COMBOBOX: {
Ryan Harrison275e2602017-09-18 14:23:18 -04001646 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001647 trimmed.TrimRight();
1648 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001649 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001650 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001651 case FIELDTYPE_PUSHBUTTON: {
1652 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001653 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001654 case FIELDTYPE_CHECKBOX:
1655 case FIELDTYPE_RADIOBUTTON: {
1656 dTemp = 0.0;
1657 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1658 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1659 if (pFormCtrl->IsChecked()) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001660 WideString trimmed = pFormCtrl->GetExportValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001661 trimmed.TrimRight();
1662 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001663 dTemp = FX_atof(trimmed.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001665 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001666 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001667 }
Tom Sepez4246b002016-01-20 11:48:29 -08001668 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001669 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001670 if (pFormField->CountSelectedItems() <= 1) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001671 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001672 trimmed.TrimRight();
1673 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001674 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001675 }
1676 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001677 default:
1678 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001679 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001680
1681 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1682 wcscmp(sFunction.c_str(), L"MAX") == 0))
1683 dValue = dTemp;
1684
1685 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1686
1687 nFieldsCount++;
1688 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001689 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001690 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001691
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001692 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1693 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001694
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001695 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1696 FXSYS_pow((double)10, (double)6);
Tom Sepezb1670b52017-02-16 17:01:00 -08001697
Tom Sepez67fd5df2015-10-08 12:24:19 -07001698 CJS_Value jsValue(pRuntime, dValue);
Tom Sepezb1670b52017-02-16 17:01:00 -08001699 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
foxit8b544ed2015-09-10 14:57:54 +08001700 if (pContext->GetEventHandler()->m_pValue)
tsepezb4694242016-08-15 16:44:55 -07001701 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001702
tsepez4cf55152016-11-02 14:37:54 -07001703 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001704}
1705
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001706/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001707** within the specified range. */
1708
Tom Sepezb1670b52017-02-16 17:01:00 -08001709bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001710 const std::vector<CJS_Value>& params,
1711 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001712 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001714 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001715 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001716 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001717 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
tsepezcd5dc852016-09-08 11:23:24 -07001718 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001719 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001720 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001721
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001722 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001723 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001724
Ryan Harrison275e2602017-09-18 14:23:18 -04001725 double dEentValue = atof(ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001726 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001727 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001728 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001729 double dLessThan = params[3].ToDouble(pRuntime);
Ryan Harrison275e2602017-09-18 14:23:18 -04001730 WideString swMsg;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731
1732 if (bGreaterThan && bLessThan) {
1733 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001734 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001735 params[1].ToCFXWideString(pRuntime).c_str(),
1736 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737 } else if (bGreaterThan) {
1738 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001739 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001740 params[1].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001741 } else if (bLessThan) {
1742 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001743 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001744 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001745 }
1746
1747 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001748 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001749 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001750 }
tsepez4cf55152016-11-02 14:37:54 -07001751 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001752}
1753
Tom Sepezb1670b52017-02-16 17:01:00 -08001754bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001755 const std::vector<CJS_Value>& params,
1756 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001757 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001759 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001760 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001761 }
1762
Ryan Harrison275e2602017-09-18 14:23:18 -04001763 WideString str = params[0].ToCFXWideString(pRuntime);
Ryan Harrison75c65212017-08-16 13:52:15 -04001764 if (str.GetLength() > 0 && (str[0] == L'.' || str[0] == L','))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001765 str = L"0" + str;
1766
Ryan Harrison275e2602017-09-18 14:23:18 -04001767 WideString sPart;
Tom Sepezb1670b52017-02-16 17:01:00 -08001768 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001769 int nIndex = 0;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001770 for (const auto& wc : str) {
Lei Zhange247ec42017-04-20 21:41:36 -07001771 if (std::iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001772 sPart += wc;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001773 } else if (sPart.GetLength() > 0) {
1774 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
1775 sPart = L"";
1776 nIndex++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001777 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001778 }
Tom Sepez3c3e2712017-04-17 15:38:19 -07001779 if (sPart.GetLength() > 0)
tsepezb4694242016-08-15 16:44:55 -07001780 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001781
tsepezb4694242016-08-15 16:44:55 -07001782 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001783 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001784 else
tsepezf3dc8c62016-08-10 06:29:29 -07001785 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001786
tsepez4cf55152016-11-02 14:37:54 -07001787 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001788}