blob: a48f647b775b48cf10fd228d3750eea255cb2d46 [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
Dan Sinclair33d13f22017-10-23 09:44:30 -0400850 CJS_Value vProp(pRuntime, arColor);
dan sinclaircbe23db2017-10-19 14:29:33 -0400851 fTarget->set_text_color(pRuntime, vProp, &sError); // red
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852 }
853 }
854 } else {
855 if (iNegStyle == 1 || iNegStyle == 3) {
856 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700857 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700858 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700859 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700860 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700861 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700862 arColor.SetElement(pRuntime, 1, vColElm);
863 arColor.SetElement(pRuntime, 2, vColElm);
864 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700865
Dan Sinclair33d13f22017-10-23 09:44:30 -0400866 CJS_Value vProp(pRuntime);
dan sinclaircbe23db2017-10-19 14:29:33 -0400867 fTarget->get_text_color(pRuntime, &vProp, &sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700868
Dan Sinclair33d13f22017-10-23 09:44:30 -0400869 CJS_Array aProp = vProp.ToArray(pRuntime);
Dan Sinclair7f55a542017-07-13 14:17:10 -0400870 CFX_Color crProp;
871 CFX_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700872 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
873 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874
875 if (crColor != crProp) {
Dan Sinclair33d13f22017-10-23 09:44:30 -0400876 CJS_Value vProp2(pRuntime, arColor);
877
dan sinclaircbe23db2017-10-19 14:29:33 -0400878 fTarget->set_text_color(pRuntime, vProp2, &sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879 }
880 }
881 }
882 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883#endif
tsepez4cf55152016-11-02 14:37:54 -0700884 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700885}
886
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
888// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800889bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700890 const std::vector<CJS_Value>& params,
891 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400892 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700894 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700895
Tom Sepezb1670b52017-02-16 17:01:00 -0800896 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
897 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700899 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700900
Ryan Harrison275e2602017-09-18 14:23:18 -0400901 WideString& val = pEvent->Value();
902 WideString& wstrChange = pEvent->Change();
903 WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700904
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 if (pEvent->WillCommit()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400906 WideString swTemp = StrTrim(wstrValue);
ochanga0a3bc32016-05-12 15:22:48 -0700907 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700908 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700909
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910 swTemp.Replace(L",", L".");
911 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700912 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700913 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700914 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700915 }
tsepez4cf55152016-11-02 14:37:54 -0700916 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700918
Ryan Harrison275e2602017-09-18 14:23:18 -0400919 WideString wstrSelected;
thestigcf03f8e2016-05-09 12:36:18 -0700920 if (pEvent->SelStart() != -1) {
921 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
922 pEvent->SelEnd() - pEvent->SelStart());
923 }
924
Ryan Harrison12db7512017-08-23 10:39:35 -0400925 bool bHasSign = wstrValue.Contains(L'-') && !wstrSelected.Contains(L'-');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926 if (bHasSign) {
927 // can't insert "change" in front to sign postion.
928 if (pEvent->SelStart() == 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700929 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700930 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700931 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700932 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700933
tsepezb4694242016-08-15 16:44:55 -0700934 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700935 if (iSepStyle < 0 || iSepStyle > 3)
936 iSepStyle = 0;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400937 const wchar_t cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700938
Ryan Harrison12db7512017-08-23 10:39:35 -0400939 bool bHasSep = wstrValue.Contains(cSep);
Ryan Harrison875e98c2017-09-27 10:53:11 -0400940 for (size_t i = 0; i < wstrChange.GetLength(); ++i) {
thestigcf03f8e2016-05-09 12:36:18 -0700941 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 if (bHasSep) {
Lei Zhange247ec42017-04-20 21:41:36 -0700943 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700944 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 }
tsepez4cf55152016-11-02 14:37:54 -0700946 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 continue;
948 }
thestigcf03f8e2016-05-09 12:36:18 -0700949 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950 if (bHasSign) {
Lei Zhange247ec42017-04-20 21:41:36 -0700951 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700952 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800954 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700955 if (i != 0) {
Lei Zhange247ec42017-04-20 21:41:36 -0700956 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700957 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 }
959 if (pEvent->SelStart() != 0) {
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 }
tsepez4cf55152016-11-02 14:37:54 -0700963 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964 continue;
965 }
966
Lei Zhange247ec42017-04-20 21:41:36 -0700967 if (!std::iswdigit(wstrChange[i])) {
968 pEvent->Rc() = false;
tsepez4cf55152016-11-02 14:37:54 -0700969 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 }
971 }
972
Ryan Harrison275e2602017-09-18 14:23:18 -0400973 WideString wprefix = wstrValue.Left(pEvent->SelStart());
974 WideString wpostfix;
Ryan Harrison75584142017-09-01 13:30:19 -0400975 if (pEvent->SelEnd() >= 0 &&
Ryan Harrison875e98c2017-09-27 10:53:11 -0400976 static_cast<size_t>(pEvent->SelEnd()) < wstrValue.GetLength())
Ryan Harrison75584142017-09-01 13:30:19 -0400977 wpostfix = wstrValue.Right(wstrValue.GetLength() -
Ryan Harrison875e98c2017-09-27 10:53:11 -0400978 static_cast<size_t>(pEvent->SelEnd()));
thestigcf03f8e2016-05-09 12:36:18 -0700979 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700980 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981}
982
983// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -0800984bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700985 const std::vector<CJS_Value>& params,
986 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400987 WideString& sError) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400988#if _FX_OS_ != _FX_OS_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700989 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700990 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700991 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800993
994 CJS_EventHandler* pEvent =
995 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700997 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998
Ryan Harrison275e2602017-09-18 14:23:18 -0400999 WideString& Value = pEvent->Value();
1000 ByteString strValue = StrTrim(ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001002 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003
tsepezb4694242016-08-15 16:44:55 -07001004 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 if (iDec < 0)
1006 iDec = -iDec;
1007
tsepezb4694242016-08-15 16:44:55 -07001008 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 if (iSepStyle < 0 || iSepStyle > 3)
1010 iSepStyle = 0;
1011
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001013 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014 dValue *= 100;
1015 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001016 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017
1018 int iDec2;
1019 int iNegative = 0;
1020 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1021 if (strValue.IsEmpty()) {
1022 dValue = 0;
1023 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1024 }
1025
1026 if (iDec2 < 0) {
1027 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1028 strValue = "0" + strValue;
1029 }
1030 iDec2 = 0;
1031 }
1032 int iMax = strValue.GetLength();
1033 if (iDec2 > iMax) {
1034 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1035 strValue += "0";
1036 }
1037 iMax = iDec2 + 1;
1038 }
dsinclair64376be2016-03-31 20:03:24 -07001039
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 // for processing seperator style
1041 if (iDec2 < iMax) {
1042 if (iSepStyle == 0 || iSepStyle == 1) {
1043 strValue.Insert(iDec2, '.');
1044 iMax++;
1045 } else if (iSepStyle == 2 || iSepStyle == 3) {
1046 strValue.Insert(iDec2, ',');
1047 iMax++;
1048 }
1049
1050 if (iDec2 == 0)
1051 strValue.Insert(iDec2, '0');
1052 }
1053 if (iSepStyle == 0 || iSepStyle == 2) {
1054 char cSeperator;
1055 if (iSepStyle == 0)
1056 cSeperator = ',';
1057 else
1058 cSeperator = '.';
1059
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001060 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061 strValue.Insert(iDecPositive, cSeperator);
1062 iMax++;
1063 }
1064 }
dsinclair64376be2016-03-31 20:03:24 -07001065
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 // negative mark
1067 if (iNegative)
1068 strValue = "-" + strValue;
1069 strValue += "%";
Ryan Harrison275e2602017-09-18 14:23:18 -04001070 Value = WideString::FromLocal(strValue.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071#endif
tsepez4cf55152016-11-02 14:37:54 -07001072 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001073}
1074// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001075bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001076 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001077 const std::vector<CJS_Value>& params,
1078 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001079 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001080 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081}
1082
1083// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001084bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001085 const std::vector<CJS_Value>& params,
1086 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001087 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001089 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001090 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001091 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001092
1093 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1094 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001096 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001097
Ryan Harrison275e2602017-09-18 14:23:18 -04001098 WideString& val = pEvent->Value();
1099 WideString strValue = val;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001101 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102
Dan Sinclairc9708952017-10-23 09:40:59 -04001103 WideString sFormat = params[0].ToWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104 double dDate = 0.0f;
1105
Ryan Harrison12db7512017-08-23 10:39:35 -04001106 if (strValue.Contains(L"GMT")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107 // for GMT format time
1108 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1109 dDate = MakeInterDate(strValue);
1110 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001111 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112 }
1113
Tom Sepezdf950b82017-08-04 11:33:49 -07001114 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001115 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001116 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001118 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001119 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120 }
1121
1122 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001123 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124}
1125
Ryan Harrison275e2602017-09-18 14:23:18 -04001126double CJS_PublicMethods::MakeInterDate(const WideString& strValue) {
1127 std::vector<WideString> wsArray;
1128 WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001129 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001130 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001131 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001132 sTemp = L"";
1133 continue;
1134 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 sTemp += c;
1136 }
Tom Sepezab277682016-02-17 10:07:21 -08001137 wsArray.push_back(sTemp);
1138 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 return 0;
1140
Tom Sepez4246b002016-01-20 11:48:29 -08001141 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 sTemp = wsArray[1];
1143 if (sTemp.Compare(L"Jan") == 0)
1144 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001145 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001147 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001149 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001151 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001152 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001153 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001155 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001157 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001159 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001161 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001163 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001165 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 nMonth = 12;
1167
Ryan Harrison275e2602017-09-18 14:23:18 -04001168 int nDay = FX_atof(wsArray[2].AsStringView());
1169 int nHour = FX_atof(wsArray[3].AsStringView());
1170 int nMin = FX_atof(wsArray[4].AsStringView());
1171 int nSec = FX_atof(wsArray[5].AsStringView());
1172 int nYear = FX_atof(wsArray[7].AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1174 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepezdf950b82017-08-04 11:33:49 -07001175 if (std::isnan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001176 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177
1178 return dRet;
1179}
1180
1181// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001182bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001183 const std::vector<CJS_Value>& params,
1184 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001185 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186 if (params.size() != 1) {
1187 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001188 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189 }
1190
Tom Sepezb1670b52017-02-16 17:01:00 -08001191 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1192 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193 if (pEvent->WillCommit()) {
1194 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001195 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001196
Ryan Harrison275e2602017-09-18 14:23:18 -04001197 WideString strValue = pEvent->Value();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001199 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001200
Dan Sinclairc9708952017-10-23 09:40:59 -04001201 WideString sFormat = params[0].ToWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001202 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001203 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Tom Sepezdf950b82017-08-04 11:33:49 -07001204 if (bWrongFormat || std::isnan(dRet)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001205 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001206 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001208 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001209 pEvent->Rc() = false;
1210 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001211 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 }
tsepez4cf55152016-11-02 14:37:54 -07001213 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001214}
1215
Tom Sepezb1670b52017-02-16 17:01:00 -08001216bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001217 const std::vector<CJS_Value>& params,
1218 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001219 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001221 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001222 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001223 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001224
tsepezb4694242016-08-15 16:44:55 -07001225 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001226 const wchar_t* cFormats[] = {L"m/d",
1227 L"m/d/yy",
1228 L"mm/dd/yy",
1229 L"mm/yy",
1230 L"d-mmm",
1231 L"d-mmm-yy",
1232 L"dd-mmm-yy",
1233 L"yy-mm-dd",
1234 L"mmm-yy",
1235 L"mmmm-yy",
1236 L"mmm d, yyyy",
1237 L"mmmm d, yyyy",
1238 L"m/d/yy h:MM tt",
1239 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001240
Lei Zhanga0f67242015-08-17 15:39:30 -07001241 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1242 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001243
Lei Zhang945fdb72015-11-11 10:18:16 -08001244 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001245 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1246 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001247}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001248
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001249// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001250bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001251 const std::vector<CJS_Value>& params,
1252 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001253 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001255 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001256 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001257 }
1258
tsepezb4694242016-08-15 16:44:55 -07001259 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001260 const wchar_t* cFormats[] = {L"m/d",
1261 L"m/d/yy",
1262 L"mm/dd/yy",
1263 L"mm/yy",
1264 L"d-mmm",
1265 L"d-mmm-yy",
1266 L"dd-mmm-yy",
1267 L"yy-mm-dd",
1268 L"mmm-yy",
1269 L"mmmm-yy",
1270 L"mmm d, yyyy",
1271 L"mmmm d, yyyy",
1272 L"m/d/yy h:MM tt",
1273 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001274
Lei Zhanga0f67242015-08-17 15:39:30 -07001275 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1276 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277
Lei Zhang945fdb72015-11-11 10:18:16 -08001278 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001279 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1280 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001281}
1282
1283// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001284bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001285 const std::vector<CJS_Value>& params,
1286 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001287 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001289 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001290 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291 }
1292
tsepezb4694242016-08-15 16:44:55 -07001293 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001294 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1295 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296
Lei Zhanga0f67242015-08-17 15:39:30 -07001297 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1298 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299
Lei Zhang945fdb72015-11-11 10:18:16 -08001300 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001301 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1302 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303}
1304
Tom Sepezb1670b52017-02-16 17:01:00 -08001305bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001306 const std::vector<CJS_Value>& params,
1307 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001308 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001310 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001311 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312 }
1313
tsepezb4694242016-08-15 16:44:55 -07001314 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001315 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1316 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317
Lei Zhanga0f67242015-08-17 15:39:30 -07001318 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1319 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320
Lei Zhang945fdb72015-11-11 10:18:16 -08001321 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001322 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1323 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324}
1325
Tom Sepezb1670b52017-02-16 17:01:00 -08001326bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001327 const std::vector<CJS_Value>& params,
1328 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001329 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001330 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001331}
1332
Tom Sepezb1670b52017-02-16 17:01:00 -08001333bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001334 const std::vector<CJS_Value>& params,
1335 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001336 WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001337 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001338}
1339
1340// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001341bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001342 const std::vector<CJS_Value>& params,
1343 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001344 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001345 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001346 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001347 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348 }
1349
Tom Sepezb1670b52017-02-16 17:01:00 -08001350 CJS_EventHandler* pEvent =
1351 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001352 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001353 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354
Ryan Harrison275e2602017-09-18 14:23:18 -04001355 WideString wsSource = pEvent->Value();
1356 WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001357 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001358 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001359 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001361 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001362 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001364 case 2:
1365 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1366 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 else
tsepez4f1f41f2016-03-28 14:13:16 -07001368 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001370 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001371 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 break;
1373 }
1374
tsepez4f1f41f2016-03-28 14:13:16 -07001375 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001376 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377}
1378
1379// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001380bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001381 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001382 const std::vector<CJS_Value>& params,
1383 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001384 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001385 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001386 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001387 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388 }
1389
Tom Sepezb1670b52017-02-16 17:01:00 -08001390 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1391 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001392 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001393 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394
Ryan Harrison275e2602017-09-18 14:23:18 -04001395 WideString& valEvent = pEvent->Value();
Dan Sinclairc9708952017-10-23 09:40:59 -04001396 WideString wstrMask = params[0].ToWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001398 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001399
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001400 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001401 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001402 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001403
Ryan Harrison875e98c2017-09-27 10:53:11 -04001404 size_t iIndexMask = 0;
thestigcf03f8e2016-05-09 12:36:18 -07001405 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1406 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001407 break;
1408 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001409
thestigcf03f8e2016-05-09 12:36:18 -07001410 if (iIndexMask != wstrMask.GetLength() ||
1411 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001412 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001413 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001414 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001415 }
tsepez4cf55152016-11-02 14:37:54 -07001416 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001417 }
1418
Ryan Harrison275e2602017-09-18 14:23:18 -04001419 WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001420 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001421 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422
Ryan Harrison275e2602017-09-18 14:23:18 -04001423 WideString wChange = wideChange;
Ryan Harrison875e98c2017-09-27 10:53:11 -04001424 size_t iIndexMask = pEvent->SelStart();
1425 size_t combined_len = valEvent.GetLength() + wChange.GetLength() +
1426 pEvent->SelStart() - pEvent->SelEnd();
thestigcf03f8e2016-05-09 12:36:18 -07001427 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001428 AlertIfPossible(pContext,
1429 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001430 pEvent->Rc() = false;
1431 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432 }
1433
thestigcf03f8e2016-05-09 12:36:18 -07001434 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001435 AlertIfPossible(pContext,
1436 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001437 pEvent->Rc() = false;
1438 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001439 }
1440
Ryan Harrison875e98c2017-09-27 10:53:11 -04001441 for (size_t i = 0; i < wChange.GetLength(); ++i) {
thestigcf03f8e2016-05-09 12:36:18 -07001442 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001443 AlertIfPossible(pContext,
1444 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001445 pEvent->Rc() = false;
1446 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001448 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001449 if (!isReservedMaskChar(wMask))
1450 wChange.SetAt(i, wMask);
1451
1452 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001453 pEvent->Rc() = false;
1454 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001455 }
1456 iIndexMask++;
1457 }
thestigcf03f8e2016-05-09 12:36:18 -07001458 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001459 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001460}
1461
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001462// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001463bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001464 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001465 const std::vector<CJS_Value>& params,
1466 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001467 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001468 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001469 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001470 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001471 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001472
Tom Sepezb1670b52017-02-16 17:01:00 -08001473 CJS_EventHandler* pEvent =
1474 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001476 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001477
thestigcf03f8e2016-05-09 12:36:18 -07001478 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001479 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001480 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001481 cFormat = "99999";
1482 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001483 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001484 cFormat = "999999999";
1485 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001486 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001487 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001488 cFormat = "9999999999";
1489 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 cFormat = "9999999";
1491 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001492 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001493 cFormat = "999999999";
1494 break;
1495 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001496
Lei Zhang945fdb72015-11-11 10:18:16 -08001497 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001498 params2.push_back(CJS_Value(pRuntime, cFormat));
1499 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001500}
1501
Tom Sepezb1670b52017-02-16 17:01:00 -08001502bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001503 const std::vector<CJS_Value>& params,
1504 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001505 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001506 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001507 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001508 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001510
Tom Sepezb1670b52017-02-16 17:01:00 -08001511 CJS_EventHandler* pEventHandler =
1512 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001513
Ryan Harrison275e2602017-09-18 14:23:18 -04001514 WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001515 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001517
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001519 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001520 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 }
1522
Ryan Harrison275e2602017-09-18 14:23:18 -04001523 WideString prefix, postfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001524
1525 if (pEventHandler->SelStart() >= 0)
Ryan Harrisone7a99de2017-07-28 14:07:04 -04001526 prefix = swValue.Left(pEventHandler->SelStart());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 else
1528 prefix = L"";
1529
1530 if (pEventHandler->SelEnd() >= 0 &&
Ryan Harrison875e98c2017-09-27 10:53:11 -04001531 static_cast<size_t>(pEventHandler->SelEnd()) <= swValue.GetLength())
Ryan Harrison75584142017-09-01 13:30:19 -04001532 postfix = swValue.Right(swValue.GetLength() -
Ryan Harrison875e98c2017-09-27 10:53:11 -04001533 static_cast<size_t>(pEventHandler->SelEnd()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001534 else
1535 postfix = L"";
1536
tsepezf3dc8c62016-08-10 06:29:29 -07001537 vRet =
1538 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001539 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001540}
1541
Tom Sepezb1670b52017-02-16 17:01:00 -08001542bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001543 const std::vector<CJS_Value>& params,
1544 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001545 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001546 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001547 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001548 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001549 }
1550
Dan Sinclairc9708952017-10-23 09:40:59 -04001551 WideString sValue = params[0].ToWideString(pRuntime);
1552 WideString sFormat = params[1].ToWideString(pRuntime);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001553 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Tom Sepezdf950b82017-08-04 11:33:49 -07001554 if (std::isnan(dDate)) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001555 WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001556 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001557 sFormat.c_str());
Tom Sepezb1670b52017-02-16 17:01:00 -08001558 AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001559 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001560 }
1561
tsepezf3dc8c62016-08-10 06:29:29 -07001562 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001563 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001564}
1565
Tom Sepezb1670b52017-02-16 17:01:00 -08001566bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001567 const std::vector<CJS_Value>& params,
1568 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001569 WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001570 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001571 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001572 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001573 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001574
tsepezb4694242016-08-15 16:44:55 -07001575 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
Dan Sinclairc9708952017-10-23 09:40:59 -04001576 params[0].ToWideString(pRuntime).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001577 params[1].ToDouble(pRuntime),
1578 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001579
tsepez4cf55152016-11-02 14:37:54 -07001580 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001581}
1582
Tom Sepezb1670b52017-02-16 17:01:00 -08001583bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001584 const std::vector<CJS_Value>& params,
1585 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001586 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001587 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001588 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001589 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001590 }
tsepezf3dc8c62016-08-10 06:29:29 -07001591
Dan Sinclairc9708952017-10-23 09:40:59 -04001592 WideString ws = params[0].ToWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001593 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001594 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001595 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001596 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001597 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001598 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001599}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001600
Tom Sepezb1670b52017-02-16 17:01:00 -08001601bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001602 const std::vector<CJS_Value>& params,
1603 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001604 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001605 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001606 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001607 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001608 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001609
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001610 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001611 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
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
dsinclair4526faf2016-10-11 10:54:49 -07001616 CPDFSDK_InterForm* pReaderInterForm =
Tom Sepezb1670b52017-02-16 17:01:00 -08001617 pRuntime->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001618 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001619
Dan Sinclairc9708952017-10-23 09:40:59 -04001620 WideString sFunction = params[0].ToWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001621 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001622
Tom Sepez67fd5df2015-10-08 12:24:19 -07001623 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001624 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001625
tsepezb4694242016-08-15 16:44:55 -07001626 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Dan Sinclairc9708952017-10-23 09:40:59 -04001627 CJS_Value jsValue(FieldNameArray.GetElement(pRuntime, i));
1628 WideString wsFieldName = jsValue.ToWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001629
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001630 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1631 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1632 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001633 switch (pFormField->GetFieldType()) {
1634 case FIELDTYPE_TEXTFIELD:
1635 case FIELDTYPE_COMBOBOX: {
Ryan Harrison275e2602017-09-18 14:23:18 -04001636 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001637 trimmed.TrimRight();
1638 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001639 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001640 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001641 case FIELDTYPE_PUSHBUTTON: {
1642 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001643 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001644 case FIELDTYPE_CHECKBOX:
1645 case FIELDTYPE_RADIOBUTTON: {
1646 dTemp = 0.0;
1647 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1648 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1649 if (pFormCtrl->IsChecked()) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001650 WideString trimmed = pFormCtrl->GetExportValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001651 trimmed.TrimRight();
1652 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001653 dTemp = FX_atof(trimmed.AsStringView());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001654 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001655 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001656 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001657 }
Tom Sepez4246b002016-01-20 11:48:29 -08001658 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001659 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001660 if (pFormField->CountSelectedItems() <= 1) {
Ryan Harrison275e2602017-09-18 14:23:18 -04001661 WideString trimmed = pFormField->GetValue();
Tom Sepez4246b002016-01-20 11:48:29 -08001662 trimmed.TrimRight();
1663 trimmed.TrimLeft();
Ryan Harrison275e2602017-09-18 14:23:18 -04001664 dTemp = FX_atof(trimmed.AsStringView());
Tom Sepez4246b002016-01-20 11:48:29 -08001665 }
1666 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001667 default:
1668 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001669 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001670
1671 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1672 wcscmp(sFunction.c_str(), L"MAX") == 0))
1673 dValue = dTemp;
1674
1675 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1676
1677 nFieldsCount++;
1678 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001679 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001680 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001681
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001682 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1683 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001684
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001685 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1686 FXSYS_pow((double)10, (double)6);
Tom Sepezb1670b52017-02-16 17:01:00 -08001687
Tom Sepez67fd5df2015-10-08 12:24:19 -07001688 CJS_Value jsValue(pRuntime, dValue);
Tom Sepezb1670b52017-02-16 17:01:00 -08001689 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
foxit8b544ed2015-09-10 14:57:54 +08001690 if (pContext->GetEventHandler()->m_pValue)
Dan Sinclairc9708952017-10-23 09:40:59 -04001691 pContext->GetEventHandler()->Value() = jsValue.ToWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001692
tsepez4cf55152016-11-02 14:37:54 -07001693 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001694}
1695
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001696/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001697** within the specified range. */
1698
Tom Sepezb1670b52017-02-16 17:01:00 -08001699bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001700 const std::vector<CJS_Value>& params,
1701 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001702 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001704 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001705 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001706 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001707 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
tsepezcd5dc852016-09-08 11:23:24 -07001708 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001709 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001710 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001711
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001712 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001713 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001714
Ryan Harrison275e2602017-09-18 14:23:18 -04001715 double dEentValue = atof(ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001716 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001717 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001718 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001719 double dLessThan = params[3].ToDouble(pRuntime);
Ryan Harrison275e2602017-09-18 14:23:18 -04001720 WideString swMsg;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001721
1722 if (bGreaterThan && bLessThan) {
1723 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001724 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001725 params[1].ToWideString(pRuntime).c_str(),
1726 params[3].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001727 } else if (bGreaterThan) {
1728 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001729 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001730 params[1].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731 } else if (bLessThan) {
1732 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001733 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
Dan Sinclairc9708952017-10-23 09:40:59 -04001734 params[3].ToWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001735 }
1736
1737 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001738 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001739 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001740 }
tsepez4cf55152016-11-02 14:37:54 -07001741 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001742}
1743
Tom Sepezb1670b52017-02-16 17:01:00 -08001744bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001745 const std::vector<CJS_Value>& params,
1746 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -04001747 WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001748 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001749 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001750 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001751 }
1752
Dan Sinclairc9708952017-10-23 09:40:59 -04001753 WideString str = params[0].ToWideString(pRuntime);
Ryan Harrison75c65212017-08-16 13:52:15 -04001754 if (str.GetLength() > 0 && (str[0] == L'.' || str[0] == L','))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001755 str = L"0" + str;
1756
Ryan Harrison275e2602017-09-18 14:23:18 -04001757 WideString sPart;
Tom Sepezb1670b52017-02-16 17:01:00 -08001758 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001759 int nIndex = 0;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001760 for (const auto& wc : str) {
Lei Zhange247ec42017-04-20 21:41:36 -07001761 if (std::iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001762 sPart += wc;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001763 } else if (sPart.GetLength() > 0) {
1764 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
1765 sPart = L"";
1766 nIndex++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001767 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001768 }
Tom Sepez3c3e2712017-04-17 15:38:19 -07001769 if (sPart.GetLength() > 0)
tsepezb4694242016-08-15 16:44:55 -07001770 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001771
tsepezb4694242016-08-15 16:44:55 -07001772 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001773 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001774 else
tsepezf3dc8c62016-08-10 06:29:29 -07001775 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001776
tsepez4cf55152016-11-02 14:37:54 -07001777 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001778}