blob: b3495375e0e2da5850389f8b4134eb2b492a1eb1 [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>
npm49c59282016-11-15 15:14:04 -080010#include <iomanip>
dsinclair992ecf72016-12-14 05:45:57 -080011#include <limits>
npm49c59282016-11-15 15:14:04 -080012#include <sstream>
13#include <string>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050014#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080015
dsinclair1727aee2016-09-29 13:12:56 -070016#include "core/fpdfdoc/cpdf_interform.h"
Dan Sinclaircfb19442017-04-20 13:13:04 -040017#include "core/fxcrt/fx_extension.h"
dsinclair735606d2016-10-05 15:47:02 -070018#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070019#include "fpdfsdk/cpdfsdk_interform.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040020#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040021#include "fpdfsdk/javascript/JS_Define.h"
22#include "fpdfsdk/javascript/JS_EventHandler.h"
23#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040024#include "fpdfsdk/javascript/JS_Value.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080025#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070026#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040027#include "fpdfsdk/javascript/color.h"
28#include "fpdfsdk/javascript/resource.h"
29#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Tom Sepez04557b82017-02-16 09:43:10 -080033JSMethodSpec CJS_PublicMethods::GlobalFunctionSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -080034 {"AFNumber_Format", AFNumber_Format_static},
35 {"AFNumber_Keystroke", AFNumber_Keystroke_static},
36 {"AFPercent_Format", AFPercent_Format_static},
37 {"AFPercent_Keystroke", AFPercent_Keystroke_static},
38 {"AFDate_FormatEx", AFDate_FormatEx_static},
39 {"AFDate_KeystrokeEx", AFDate_KeystrokeEx_static},
40 {"AFDate_Format", AFDate_Format_static},
41 {"AFDate_Keystroke", AFDate_Keystroke_static},
42 {"AFTime_FormatEx", AFTime_FormatEx_static},
43 {"AFTime_KeystrokeEx", AFTime_KeystrokeEx_static},
44 {"AFTime_Format", AFTime_Format_static},
45 {"AFTime_Keystroke", AFTime_Keystroke_static},
46 {"AFSpecial_Format", AFSpecial_Format_static},
47 {"AFSpecial_Keystroke", AFSpecial_Keystroke_static},
48 {"AFSpecial_KeystrokeEx", AFSpecial_KeystrokeEx_static},
49 {"AFSimple", AFSimple_static},
50 {"AFMakeNumber", AFMakeNumber_static},
51 {"AFSimple_Calculate", AFSimple_Calculate_static},
52 {"AFRange_Validate", AFRange_Validate_static},
53 {"AFMergeChange", AFMergeChange_static},
54 {"AFParseDateEx", AFParseDateEx_static},
55 {"AFExtractNums", AFExtractNums_static},
Tom Sepez04557b82017-02-16 09:43:10 -080056 {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
58IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
59
tsepez745611b2016-04-12 16:46:34 -070060namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061
Dan Sinclair812e96c2017-03-13 16:43:37 -040062const wchar_t* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
63 L"May", L"Jun", L"Jul", L"Aug",
64 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
Dan Sinclair812e96c2017-03-13 16:43:37 -040066const wchar_t* const fullmonths[] = {L"January", L"February", L"March",
67 L"April", L"May", L"June",
68 L"July", L"August", L"September",
69 L"October", L"November", L"December"};
tsepez745611b2016-04-12 16:46:34 -070070
71CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
72 CFX_ByteString result(pStr);
73 result.TrimLeft(' ');
74 result.TrimRight(' ');
75 return result;
76}
77
78CFX_WideString StrTrim(const CFX_WideString& pStr) {
79 CFX_WideString result(pStr);
80 result.TrimLeft(' ');
81 result.TrimRight(' ');
82 return result;
83}
84
Dan Sinclair812e96c2017-03-13 16:43:37 -040085void AlertIfPossible(CJS_EventContext* pContext, const wchar_t* swMsg) {
dsinclair8779fa82016-10-12 12:05:44 -070086 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
87 if (pFormFillEnv)
88 pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3);
tsepeze1e7bd02016-08-08 13:03:16 -070089}
90
npm49c59282016-11-15 15:14:04 -080091#if _FX_OS_ != _FX_ANDROID_
92CFX_ByteString CalculateString(double dValue,
93 int iDec,
94 int* iDec2,
95 bool* bNegative) {
96 *bNegative = dValue < 0;
97 if (*bNegative)
98 dValue = -dValue;
dsinclair992ecf72016-12-14 05:45:57 -080099
100 // Make sure the number of precision characters will fit.
101 if (iDec > std::numeric_limits<double>::digits10)
102 iDec = std::numeric_limits<double>::digits10;
103
npm49c59282016-11-15 15:14:04 -0800104 std::stringstream ss;
105 ss << std::fixed << std::setprecision(iDec) << dValue;
106 std::string stringValue = ss.str();
107 size_t iDecimalPos = stringValue.find(".");
108 *iDec2 = iDecimalPos == std::string::npos ? stringValue.size()
109 : static_cast<int>(iDecimalPos);
110 return CFX_ByteString(stringValue.c_str());
111}
112#endif
113
tsepez745611b2016-04-12 16:46:34 -0700114} // namespace
115
116bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500117 CFX_WideString sTrim = StrTrim(str);
Dan Sinclair812e96c2017-03-13 16:43:37 -0400118 const wchar_t* pTrim = sTrim.c_str();
119 const wchar_t* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -0700120 bool bDot = false;
121 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -0700124 while ((c = *p) != L'\0') {
125 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -0700127 return false;
128 bDot = true;
129 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -0700131 return false;
132 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700134 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 p++;
137 c = *p;
Wei Li614d20a2016-03-15 13:55:12 -0700138 if (c == L'+' || c == L'-') {
139 bKXJS = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 } else {
Wei Li614d20a2016-03-15 13:55:12 -0700141 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800143 } else if (!FXSYS_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 Zhang9559b7a2015-12-21 11:12:20 -0800155 return FXSYS_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 }
tsepezb4694242016-08-15 16:44:55 -0700197 CFX_WideString wsStr = val.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 CFX_ByteString t = CFX_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) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700207 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700208 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700209 CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 }
Lei Zhang997de612015-11-04 18:17:53 -0800212
213 char* pSub = new char[pTemp - p + 1];
214 strncpy(pSub, p, pTemp - p);
215 *(pSub + (pTemp - p)) = '\0';
216
tsepezb4c9f3f2016-04-13 15:41:21 -0700217 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700218 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700219 CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800220 delete[] pSub;
221
222 nIndex++;
223 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 }
225 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226}
227
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500228int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 int nStart,
230 int& nSkip,
231 int nMaxStep) {
232 int nRet = 0;
233 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500234 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 if (i - nStart > 10)
236 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237
Dan Sinclair812e96c2017-03-13 16:43:37 -0400238 wchar_t c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800239 if (!FXSYS_iswdigit(c))
240 break;
241
Dan Sinclair1c915372016-03-03 17:12:58 -0500242 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800243 nSkip = i - nStart + 1;
244 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 break;
246 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249}
250
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500251CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
252 int nStart,
253 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 CFX_WideString swRet;
255 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500256 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Dan Sinclair812e96c2017-03-13 16:43:37 -0400257 wchar_t c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800258 if (!FXSYS_iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800260
261 swRet += c;
262 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266}
267
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800269 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 int nYear = JS_GetYearFromTime(dt);
273 int nMonth = JS_GetMonthFromTime(dt) + 1;
274 int nDay = JS_GetDayFromTime(dt);
275 int nHour = JS_GetHourFromTime(dt);
276 int nMin = JS_GetMinFromTime(dt);
277 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 int nSkip = 0;
282 int nLen = value.GetLength();
283 int nIndex = 0;
284 int i = 0;
285 while (i < nLen) {
286 if (nIndex > 2)
287 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288
Dan Sinclair812e96c2017-03-13 16:43:37 -0400289 wchar_t c = value.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800290 if (FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
292 i += nSkip;
293 } else {
294 i++;
295 }
296 }
297
298 if (nIndex == 2) {
299 // case2: month/day
300 // case3: day/month
301 if ((number[0] >= 1 && number[0] <= 12) &&
302 (number[1] >= 1 && number[1] <= 31)) {
303 nMonth = number[0];
304 nDay = number[1];
305 } else if ((number[0] >= 1 && number[0] <= 31) &&
306 (number[1] >= 1 && number[1] <= 12)) {
307 nDay = number[0];
308 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700309 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310
Lei Zhang9559b7a2015-12-21 11:12:20 -0800311 if (bWrongFormat)
312 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 } else if (nIndex == 3) {
314 // case1: year/month/day
315 // case2: month/day/year
316 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
319 (number[2] >= 1 && number[2] <= 31)) {
320 nYear = number[0];
321 nMonth = number[1];
322 nDay = number[2];
323 } else if ((number[0] >= 1 && number[0] <= 12) &&
324 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
325 nMonth = number[0];
326 nDay = number[1];
327 nYear = number[2];
328 } else if ((number[0] >= 1 && number[0] <= 31) &&
329 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
330 nDay = number[0];
331 nMonth = number[1];
332 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700333 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334
Lei Zhang9559b7a2015-12-21 11:12:20 -0800335 if (bWrongFormat)
336 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800338 if (bWrongFormat)
339 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 return dt;
341 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 CFX_WideString swTemp;
344 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700345 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346}
347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
349 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800350 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 if (format.IsEmpty() || value.IsEmpty())
354 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 int nYear = JS_GetYearFromTime(dt);
357 int nMonth = JS_GetMonthFromTime(dt) + 1;
358 int nDay = JS_GetDayFromTime(dt);
359 int nHour = JS_GetHourFromTime(dt);
360 int nMin = JS_GetMinFromTime(dt);
361 int nSec = JS_GetSecFromTime(dt);
362
363 int nYearSub = 99; // nYear - 2000;
364
tsepez4cf55152016-11-02 14:37:54 -0700365 bool bPm = false;
366 bool bExit = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800367 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368
369 int i = 0;
370 int j = 0;
371
372 while (i < format.GetLength()) {
373 if (bExit)
374 break;
375
Dan Sinclair812e96c2017-03-13 16:43:37 -0400376 wchar_t c = format.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 switch (c) {
378 case ':':
379 case '.':
380 case '-':
381 case '\\':
382 case '/':
383 i++;
384 j++;
385 break;
386
387 case 'y':
388 case 'm':
389 case 'd':
390 case 'H':
391 case 'h':
392 case 'M':
393 case 's':
394 case 't': {
395 int oldj = j;
396 int nSkip = 0;
397 int remaining = format.GetLength() - i - 1;
398
399 if (remaining == 0 || format.GetAt(i + 1) != c) {
400 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700401 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 i++;
403 j++;
404 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700405 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 nMonth = ParseStringInteger(value, j, nSkip, 2);
407 i++;
408 j += nSkip;
409 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700410 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 nDay = ParseStringInteger(value, j, nSkip, 2);
412 i++;
413 j += nSkip;
414 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700415 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 nHour = ParseStringInteger(value, j, nSkip, 2);
417 i++;
418 j += nSkip;
419 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700420 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 nHour = ParseStringInteger(value, j, nSkip, 2);
422 i++;
423 j += nSkip;
424 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700425 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 nMin = ParseStringInteger(value, j, nSkip, 2);
427 i++;
428 j += nSkip;
429 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700430 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 nSec = ParseStringInteger(value, j, nSkip, 2);
432 i++;
433 j += nSkip;
434 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700435 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
437 i++;
438 j++;
439 break;
440 }
441 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
442 switch (c) {
443 case 'y':
444 nYear = ParseStringInteger(value, j, nSkip, 4);
445 i += 2;
446 j += nSkip;
447 break;
448 case 'm':
449 nMonth = ParseStringInteger(value, j, nSkip, 2);
450 i += 2;
451 j += nSkip;
452 break;
453 case 'd':
454 nDay = ParseStringInteger(value, j, nSkip, 2);
455 i += 2;
456 j += nSkip;
457 break;
458 case 'H':
459 nHour = ParseStringInteger(value, j, nSkip, 2);
460 i += 2;
461 j += nSkip;
462 break;
463 case 'h':
464 nHour = ParseStringInteger(value, j, nSkip, 2);
465 i += 2;
466 j += nSkip;
467 break;
468 case 'M':
469 nMin = ParseStringInteger(value, j, nSkip, 2);
470 i += 2;
471 j += nSkip;
472 break;
473 case 's':
474 nSec = ParseStringInteger(value, j, nSkip, 2);
475 i += 2;
476 j += nSkip;
477 break;
478 case 't':
479 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
480 value.GetAt(j + 1) == 'm');
481 i += 2;
482 j += 2;
483 break;
484 }
485 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
486 switch (c) {
487 case 'm': {
488 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
tsepez4cf55152016-11-02 14:37:54 -0700489 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 for (int m = 0; m < 12; m++) {
491 if (sMonth.CompareNoCase(months[m]) == 0) {
492 nMonth = m + 1;
493 i += 3;
494 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700495 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700497 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 }
499
500 if (!bFind) {
501 nMonth = ParseStringInteger(value, j, nSkip, 3);
502 i += 3;
503 j += nSkip;
504 }
505 } break;
506 case 'y':
507 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700508 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 i += 3;
510 j += 3;
511 break;
512 }
513 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
514 switch (c) {
515 case 'y':
516 nYear = ParseStringInteger(value, j, nSkip, 4);
517 j += nSkip;
518 i += 4;
519 break;
520 case 'm': {
tsepez4cf55152016-11-02 14:37:54 -0700521 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522
523 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
524 sMonth.MakeLower();
525
526 for (int m = 0; m < 12; m++) {
527 CFX_WideString sFullMonths = fullmonths[m];
528 sFullMonths.MakeLower();
529
530 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
531 nMonth = m + 1;
532 i += 4;
533 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700534 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535 break;
536 }
537 }
538
539 if (!bFind) {
540 nMonth = ParseStringInteger(value, j, nSkip, 4);
541 i += 4;
542 j += nSkip;
543 }
544 } break;
545 default:
546 i += 4;
547 j += 4;
548 break;
549 }
550 } else {
551 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800552 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700553 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554 }
555 i++;
556 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700557 }
Tom Sepez85386422014-07-23 10:28:37 -0700558
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800560 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700561 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562 }
563 }
564
565 break;
566 default:
567 if (value.GetLength() <= j) {
tsepez4cf55152016-11-02 14:37:54 -0700568 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800570 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700571 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572 }
573
574 i++;
575 j++;
576 break;
577 }
578 }
579
580 if (bPm)
581 nHour += 12;
582
583 if (nYear >= 0 && nYear <= nYearSub)
584 nYear += 2000;
585
586 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800587 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588
589 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800590 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591
592 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800593 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594
595 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800596 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700597
598 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800599 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600
601 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800602 if (bBadFormat) {
603 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 } else {
605 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
606 JS_MakeTime(nHour, nMin, nSec, 0));
tsepez018935c2016-04-15 13:15:12 -0700607 if (JS_PortIsNan(dRet))
608 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 }
610
tsepez018935c2016-04-15 13:15:12 -0700611 if (JS_PortIsNan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800612 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613
Lei Zhang9559b7a2015-12-21 11:12:20 -0800614 if (bWrongFormat)
615 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700616
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 return dRet;
618}
619
620CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
621 const CFX_WideString& format) {
622 CFX_WideString sRet = L"", sPart = L"";
623
624 int nYear = JS_GetYearFromTime(dDate);
625 int nMonth = JS_GetMonthFromTime(dDate) + 1;
626 int nDay = JS_GetDayFromTime(dDate);
627 int nHour = JS_GetHourFromTime(dDate);
628 int nMin = JS_GetMinFromTime(dDate);
629 int nSec = JS_GetSecFromTime(dDate);
630
631 int i = 0;
632 while (i < format.GetLength()) {
Dan Sinclair812e96c2017-03-13 16:43:37 -0400633 wchar_t c = format.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 int remaining = format.GetLength() - i - 1;
635 sPart = L"";
636 switch (c) {
637 case 'y':
638 case 'm':
639 case 'd':
640 case 'H':
641 case 'h':
642 case 'M':
643 case 's':
644 case 't':
645 if (remaining == 0 || format.GetAt(i + 1) != c) {
646 switch (c) {
647 case 'y':
648 sPart += c;
649 break;
650 case 'm':
651 sPart.Format(L"%d", nMonth);
652 break;
653 case 'd':
654 sPart.Format(L"%d", nDay);
655 break;
656 case 'H':
657 sPart.Format(L"%d", nHour);
658 break;
659 case 'h':
660 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
661 break;
662 case 'M':
663 sPart.Format(L"%d", nMin);
664 break;
665 case 's':
666 sPart.Format(L"%d", nSec);
667 break;
668 case 't':
669 sPart += nHour > 12 ? 'p' : 'a';
670 break;
671 }
672 i++;
673 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
674 switch (c) {
675 case 'y':
676 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
677 break;
678 case 'm':
679 sPart.Format(L"%02d", nMonth);
680 break;
681 case 'd':
682 sPart.Format(L"%02d", nDay);
683 break;
684 case 'H':
685 sPart.Format(L"%02d", nHour);
686 break;
687 case 'h':
688 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
689 break;
690 case 'M':
691 sPart.Format(L"%02d", nMin);
692 break;
693 case 's':
694 sPart.Format(L"%02d", nSec);
695 break;
696 case 't':
697 sPart = nHour > 12 ? L"pm" : L"am";
698 break;
699 }
700 i += 2;
701 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
702 switch (c) {
703 case 'm':
704 i += 3;
705 if (nMonth > 0 && nMonth <= 12)
706 sPart += months[nMonth - 1];
707 break;
708 default:
709 i += 3;
710 sPart += c;
711 sPart += c;
712 sPart += c;
713 break;
714 }
715 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
716 switch (c) {
717 case 'y':
718 sPart.Format(L"%04d", nYear);
719 i += 4;
720 break;
721 case 'm':
722 i += 4;
723 if (nMonth > 0 && nMonth <= 12)
724 sPart += fullmonths[nMonth - 1];
725 break;
726 default:
727 i += 4;
728 sPart += c;
729 sPart += c;
730 sPart += c;
731 sPart += c;
732 break;
733 }
734 } else {
735 i++;
736 sPart += c;
737 }
738 break;
739 default:
740 i++;
741 sPart += c;
742 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700743 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700744
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745 sRet += sPart;
746 }
747
748 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700749}
750
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
752// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800753bool CJS_PublicMethods::AFNumber_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700754 const std::vector<CJS_Value>& params,
755 CJS_Value& vRet,
756 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700757#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758 if (params.size() != 6) {
tsepezcd5dc852016-09-08 11:23:24 -0700759 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700760 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700761 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700762
Tom Sepezb1670b52017-02-16 17:01:00 -0800763 CJS_EventHandler* pEvent =
764 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700766 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700767
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700768 CFX_WideString& Value = pEvent->Value();
769 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700771 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700772
tsepezb4694242016-08-15 16:44:55 -0700773 int iDec = params[0].ToInt(pRuntime);
774 int iSepStyle = params[1].ToInt(pRuntime);
775 int iNegStyle = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776 // params[3] is iCurrStyle, it's not used.
tsepezb4694242016-08-15 16:44:55 -0700777 CFX_WideString wstrCurrency = params[4].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700778 bool bCurrencyPrepend = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700779
780 if (iDec < 0)
781 iDec = -iDec;
782
783 if (iSepStyle < 0 || iSepStyle > 3)
784 iSepStyle = 0;
785
786 if (iNegStyle < 0 || iNegStyle > 3)
787 iNegStyle = 0;
788
npm49c59282016-11-15 15:14:04 -0800789 // Processing decimal places
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700790 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700791 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700793 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794
npm49c59282016-11-15 15:14:04 -0800795 // Calculating number string
796 bool bNegative;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797 int iDec2;
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 dValue = 0;
npm49c59282016-11-15 15:14:04 -0800801 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700802 if (strValue.IsEmpty()) {
803 strValue = "0";
804 iDec2 = 1;
805 }
806 }
807
npm49c59282016-11-15 15:14:04 -0800808 // Processing separator style
809 if (iDec2 < strValue.GetLength()) {
810 if (iSepStyle == 2 || iSepStyle == 3)
811 strValue.Replace(".", ",");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700812
813 if (iDec2 == 0)
814 strValue.Insert(iDec2, '0');
815 }
816 if (iSepStyle == 0 || iSepStyle == 2) {
npm49c59282016-11-15 15:14:04 -0800817 char cSeparator;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 if (iSepStyle == 0)
npm49c59282016-11-15 15:14:04 -0800819 cSeparator = ',';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820 else
npm49c59282016-11-15 15:14:04 -0800821 cSeparator = '.';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822
npm49c59282016-11-15 15:14:04 -0800823 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3)
824 strValue.Insert(iDecPositive, cSeparator);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825 }
826
npm49c59282016-11-15 15:14:04 -0800827 // Processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700828 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829
830 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700831 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832 else
thestigcf03f8e2016-05-09 12:36:18 -0700833 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700834
npm49c59282016-11-15 15:14:04 -0800835 // Processing negative style
836 if (bNegative) {
837 if (iNegStyle == 0)
thestigcf03f8e2016-05-09 12:36:18 -0700838 Value = L"-" + Value;
npm49c59282016-11-15 15:14:04 -0800839 else if (iNegStyle == 2 || iNegStyle == 3)
thestigcf03f8e2016-05-09 12:36:18 -0700840 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700841 if (iNegStyle == 1 || iNegStyle == 3) {
842 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700843 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700844 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700845 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700846 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700847 vColElm = CJS_Value(pRuntime, 1);
tsepezb4694242016-08-15 16:44:55 -0700848 arColor.SetElement(pRuntime, 1, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700849 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700850 arColor.SetElement(pRuntime, 2, vColElm);
851 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852
Tom Sepez67fd5df2015-10-08 12:24:19 -0700853 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 vProp.StartGetting();
855 vProp << arColor;
856 vProp.StartSetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800857 fTarget->textColor(pRuntime, vProp, sError); // red
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858 }
859 }
860 } else {
861 if (iNegStyle == 1 || iNegStyle == 3) {
862 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700863 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700864 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700865 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700866 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700867 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700868 arColor.SetElement(pRuntime, 1, vColElm);
869 arColor.SetElement(pRuntime, 2, vColElm);
870 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871
Tom Sepez67fd5df2015-10-08 12:24:19 -0700872 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873 vProp.StartGetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800874 fTarget->textColor(pRuntime, vProp, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875
tsepeze5aff742016-08-08 09:49:42 -0700876 CJS_Array aProp;
tsepezb4694242016-08-15 16:44:55 -0700877 vProp.GetJSValue()->ConvertToArray(pRuntime, aProp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878
879 CPWL_Color crProp;
880 CPWL_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700881 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
882 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883
884 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700885 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700886 vProp2.StartGetting();
887 vProp2 << arColor;
888 vProp2.StartSetting();
Tom Sepezb1670b52017-02-16 17:01:00 -0800889 fTarget->textColor(pRuntime, vProp2, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890 }
891 }
892 }
893 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894#endif
tsepez4cf55152016-11-02 14:37:54 -0700895 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700896}
897
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
899// bCurrencyPrepend)
Tom Sepezb1670b52017-02-16 17:01:00 -0800900bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700901 const std::vector<CJS_Value>& params,
902 CJS_Value& vRet,
903 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700904 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700905 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700906
Tom Sepezb1670b52017-02-16 17:01:00 -0800907 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
908 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700909 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700910 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700911
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912 CFX_WideString& val = pEvent->Value();
thestigcf03f8e2016-05-09 12:36:18 -0700913 CFX_WideString& wstrChange = pEvent->Change();
914 CFX_WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700915
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 if (pEvent->WillCommit()) {
ochanga0a3bc32016-05-12 15:22:48 -0700917 CFX_WideString swTemp = StrTrim(wstrValue);
918 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700919 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700920
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 swTemp.Replace(L",", L".");
922 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700923 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700924 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700925 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700926 }
tsepez4cf55152016-11-02 14:37:54 -0700927 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700928 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700929
thestigcf03f8e2016-05-09 12:36:18 -0700930 CFX_WideString wstrSelected;
931 if (pEvent->SelStart() != -1) {
932 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
933 pEvent->SelEnd() - pEvent->SelStart());
934 }
935
936 bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 if (bHasSign) {
938 // can't insert "change" in front to sign postion.
939 if (pEvent->SelStart() == 0) {
tsepez4cf55152016-11-02 14:37:54 -0700940 bool& bRc = pEvent->Rc();
941 bRc = false;
942 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700943 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700945
tsepezb4694242016-08-15 16:44:55 -0700946 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700947 if (iSepStyle < 0 || iSepStyle > 3)
948 iSepStyle = 0;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400949 const wchar_t cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700950
thestigcf03f8e2016-05-09 12:36:18 -0700951 bool bHasSep = wstrValue.Find(cSep) != -1;
952 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
953 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700954 if (bHasSep) {
tsepez4cf55152016-11-02 14:37:54 -0700955 bool& bRc = pEvent->Rc();
956 bRc = false;
957 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 }
tsepez4cf55152016-11-02 14:37:54 -0700959 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 continue;
961 }
thestigcf03f8e2016-05-09 12:36:18 -0700962 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 if (bHasSign) {
tsepez4cf55152016-11-02 14:37:54 -0700964 bool& bRc = pEvent->Rc();
965 bRc = false;
966 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800968 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700969 if (i != 0) {
tsepez4cf55152016-11-02 14:37:54 -0700970 bool& bRc = pEvent->Rc();
971 bRc = false;
972 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 }
974 if (pEvent->SelStart() != 0) {
tsepez4cf55152016-11-02 14:37:54 -0700975 bool& bRc = pEvent->Rc();
976 bRc = false;
977 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 }
tsepez4cf55152016-11-02 14:37:54 -0700979 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 continue;
981 }
982
thestigcf03f8e2016-05-09 12:36:18 -0700983 if (!FXSYS_iswdigit(wstrChange[i])) {
tsepez4cf55152016-11-02 14:37:54 -0700984 bool& bRc = pEvent->Rc();
985 bRc = false;
986 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700987 }
988 }
989
thestigcf03f8e2016-05-09 12:36:18 -0700990 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
991 CFX_WideString wpostfix;
992 if (pEvent->SelEnd() < wstrValue.GetLength())
993 wpostfix = wstrValue.Mid(pEvent->SelEnd());
994 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700995 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996}
997
998// function AFPercent_Format(nDec, sepStyle)
Tom Sepezb1670b52017-02-16 17:01:00 -0800999bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001000 const std::vector<CJS_Value>& params,
1001 CJS_Value& vRet,
1002 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001005 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001006 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001008
1009 CJS_EventHandler* pEvent =
1010 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001012 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013
1014 CFX_WideString& Value = pEvent->Value();
1015 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1016 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001017 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018
tsepezb4694242016-08-15 16:44:55 -07001019 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 if (iDec < 0)
1021 iDec = -iDec;
1022
tsepezb4694242016-08-15 16:44:55 -07001023 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 if (iSepStyle < 0 || iSepStyle > 3)
1025 iSepStyle = 0;
1026
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001028 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029 dValue *= 100;
1030 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001031 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032
1033 int iDec2;
1034 int iNegative = 0;
1035 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1036 if (strValue.IsEmpty()) {
1037 dValue = 0;
1038 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1039 }
1040
1041 if (iDec2 < 0) {
1042 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1043 strValue = "0" + strValue;
1044 }
1045 iDec2 = 0;
1046 }
1047 int iMax = strValue.GetLength();
1048 if (iDec2 > iMax) {
1049 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1050 strValue += "0";
1051 }
1052 iMax = iDec2 + 1;
1053 }
dsinclair64376be2016-03-31 20:03:24 -07001054
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055 // for processing seperator style
1056 if (iDec2 < iMax) {
1057 if (iSepStyle == 0 || iSepStyle == 1) {
1058 strValue.Insert(iDec2, '.');
1059 iMax++;
1060 } else if (iSepStyle == 2 || iSepStyle == 3) {
1061 strValue.Insert(iDec2, ',');
1062 iMax++;
1063 }
1064
1065 if (iDec2 == 0)
1066 strValue.Insert(iDec2, '0');
1067 }
1068 if (iSepStyle == 0 || iSepStyle == 2) {
1069 char cSeperator;
1070 if (iSepStyle == 0)
1071 cSeperator = ',';
1072 else
1073 cSeperator = '.';
1074
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001075 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076 strValue.Insert(iDecPositive, cSeperator);
1077 iMax++;
1078 }
1079 }
dsinclair64376be2016-03-31 20:03:24 -07001080
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081 // negative mark
1082 if (iNegative)
1083 strValue = "-" + strValue;
1084 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001085 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086#endif
tsepez4cf55152016-11-02 14:37:54 -07001087 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088}
1089// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001090bool CJS_PublicMethods::AFPercent_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001091 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001092 const std::vector<CJS_Value>& params,
1093 CJS_Value& vRet,
1094 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001095 return AFNumber_Keystroke(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096}
1097
1098// function AFDate_FormatEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001099bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001100 const std::vector<CJS_Value>& params,
1101 CJS_Value& vRet,
1102 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001103 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001104 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001105 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001107
1108 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1109 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001110 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001111 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112
1113 CFX_WideString& val = pEvent->Value();
1114 CFX_WideString strValue = val;
1115 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001116 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117
tsepezb4694242016-08-15 16:44:55 -07001118 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001119 double dDate = 0.0f;
1120
1121 if (strValue.Find(L"GMT") != -1) {
1122 // for GMT format time
1123 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1124 dDate = MakeInterDate(strValue);
1125 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001126 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127 }
1128
1129 if (JS_PortIsNan(dDate)) {
1130 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001131 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001132 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001133 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001134 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 }
1136
1137 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001138 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139}
1140
tsepez745611b2016-04-12 16:46:34 -07001141double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001142 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 CFX_WideString sTemp = L"";
Tom Sepez3c3e2712017-04-17 15:38:19 -07001144 for (const auto& c : strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001146 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001147 sTemp = L"";
1148 continue;
1149 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 sTemp += c;
1151 }
Tom Sepezab277682016-02-17 10:07:21 -08001152 wsArray.push_back(sTemp);
1153 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 return 0;
1155
Tom Sepez4246b002016-01-20 11:48:29 -08001156 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 sTemp = wsArray[1];
1158 if (sTemp.Compare(L"Jan") == 0)
1159 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001160 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001162 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001164 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001166 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001168 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001170 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001172 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001174 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001176 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001178 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001180 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181 nMonth = 12;
1182
tsepez4c3debb2016-04-08 12:20:38 -07001183 int nDay = FX_atof(wsArray[2].AsStringC());
1184 int nHour = FX_atof(wsArray[3].AsStringC());
1185 int nMin = FX_atof(wsArray[4].AsStringC());
1186 int nSec = FX_atof(wsArray[5].AsStringC());
1187 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001188 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1189 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001190 if (JS_PortIsNan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001191 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001192
1193 return dRet;
1194}
1195
1196// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001197bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001198 const std::vector<CJS_Value>& params,
1199 CJS_Value& vRet,
1200 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 if (params.size() != 1) {
1202 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001203 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 }
1205
Tom Sepezb1670b52017-02-16 17:01:00 -08001206 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1207 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001208 if (pEvent->WillCommit()) {
1209 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001210 return false;
Tom Sepezb1670b52017-02-16 17:01:00 -08001211
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 CFX_WideString strValue = pEvent->Value();
1213 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001214 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001215
tsepezb4694242016-08-15 16:44:55 -07001216 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001217 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001218 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 if (bWrongFormat || JS_PortIsNan(dRet)) {
1220 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001221 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001223 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001224 pEvent->Rc() = false;
1225 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001226 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227 }
tsepez4cf55152016-11-02 14:37:54 -07001228 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001229}
1230
Tom Sepezb1670b52017-02-16 17:01:00 -08001231bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001232 const std::vector<CJS_Value>& params,
1233 CJS_Value& vRet,
1234 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001236 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001237 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001238 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001239
tsepezb4694242016-08-15 16:44:55 -07001240 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001241 const wchar_t* cFormats[] = {L"m/d",
1242 L"m/d/yy",
1243 L"mm/dd/yy",
1244 L"mm/yy",
1245 L"d-mmm",
1246 L"d-mmm-yy",
1247 L"dd-mmm-yy",
1248 L"yy-mm-dd",
1249 L"mmm-yy",
1250 L"mmmm-yy",
1251 L"mmm d, yyyy",
1252 L"mmmm d, yyyy",
1253 L"m/d/yy h:MM tt",
1254 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001255
Lei Zhanga0f67242015-08-17 15:39:30 -07001256 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1257 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001258
Lei Zhang945fdb72015-11-11 10:18:16 -08001259 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001260 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1261 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001262}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001263
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264// AFDate_KeystrokeEx(cFormat)
Tom Sepezb1670b52017-02-16 17:01:00 -08001265bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001266 const std::vector<CJS_Value>& params,
1267 CJS_Value& vRet,
1268 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001269 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001270 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001271 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001272 }
1273
tsepezb4694242016-08-15 16:44:55 -07001274 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001275 const wchar_t* cFormats[] = {L"m/d",
1276 L"m/d/yy",
1277 L"mm/dd/yy",
1278 L"mm/yy",
1279 L"d-mmm",
1280 L"d-mmm-yy",
1281 L"dd-mmm-yy",
1282 L"yy-mm-dd",
1283 L"mmm-yy",
1284 L"mmmm-yy",
1285 L"mmm d, yyyy",
1286 L"mmmm d, yyyy",
1287 L"m/d/yy h:MM tt",
1288 L"m/d/yy HH:MM"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289
Lei Zhanga0f67242015-08-17 15:39:30 -07001290 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1291 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292
Lei Zhang945fdb72015-11-11 10:18:16 -08001293 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001294 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1295 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296}
1297
1298// function AFTime_Format(ptf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001299bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001300 const std::vector<CJS_Value>& params,
1301 CJS_Value& vRet,
1302 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001304 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001305 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001306 }
1307
tsepezb4694242016-08-15 16:44:55 -07001308 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001309 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1310 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311
Lei Zhanga0f67242015-08-17 15:39:30 -07001312 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1313 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314
Lei Zhang945fdb72015-11-11 10:18:16 -08001315 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001316 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1317 return AFDate_FormatEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318}
1319
Tom Sepezb1670b52017-02-16 17:01:00 -08001320bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001321 const std::vector<CJS_Value>& params,
1322 CJS_Value& vRet,
1323 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001325 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001326 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327 }
1328
tsepezb4694242016-08-15 16:44:55 -07001329 int iIndex = params[0].ToInt(pRuntime);
Dan Sinclair812e96c2017-03-13 16:43:37 -04001330 const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1331 L"h:MM:ss tt"};
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001332
Lei Zhanga0f67242015-08-17 15:39:30 -07001333 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1334 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001335
Lei Zhang945fdb72015-11-11 10:18:16 -08001336 std::vector<CJS_Value> newParams;
Tom Sepezb1670b52017-02-16 17:01:00 -08001337 newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex]));
1338 return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339}
1340
Tom Sepezb1670b52017-02-16 17:01:00 -08001341bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001342 const std::vector<CJS_Value>& params,
1343 CJS_Value& vRet,
1344 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001345 return AFDate_FormatEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001346}
1347
Tom Sepezb1670b52017-02-16 17:01:00 -08001348bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001349 const std::vector<CJS_Value>& params,
1350 CJS_Value& vRet,
1351 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001352 return AFDate_KeystrokeEx(pRuntime, params, vRet, sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001353}
1354
1355// function AFSpecial_Format(psf)
Tom Sepezb1670b52017-02-16 17:01:00 -08001356bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001357 const std::vector<CJS_Value>& params,
1358 CJS_Value& vRet,
1359 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001361 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001362 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363 }
1364
Tom Sepezb1670b52017-02-16 17:01:00 -08001365 CJS_EventHandler* pEvent =
1366 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001368 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369
tsepez4f1f41f2016-03-28 14:13:16 -07001370 CFX_WideString wsSource = pEvent->Value();
1371 CFX_WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001372 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001373 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001374 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001376 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001377 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001379 case 2:
1380 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1381 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382 else
tsepez4f1f41f2016-03-28 14:13:16 -07001383 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001384 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001385 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001386 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387 break;
1388 }
1389
tsepez4f1f41f2016-03-28 14:13:16 -07001390 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001391 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001392}
1393
1394// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001395bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Tom Sepezb1670b52017-02-16 17:01:00 -08001396 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001397 const std::vector<CJS_Value>& params,
1398 CJS_Value& vRet,
1399 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001400 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001401 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001402 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403 }
1404
Tom Sepezb1670b52017-02-16 17:01:00 -08001405 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
1406 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001408 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409
tsepezcd5dc852016-09-08 11:23:24 -07001410 CFX_WideString& valEvent = pEvent->Value();
tsepezb4694242016-08-15 16:44:55 -07001411 CFX_WideString wstrMask = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001412 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001413 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001414
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001415 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001416 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001417 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001418
1419 FX_STRSIZE iIndexMask = 0;
1420 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1421 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001422 break;
1423 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001424
thestigcf03f8e2016-05-09 12:36:18 -07001425 if (iIndexMask != wstrMask.GetLength() ||
1426 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001427 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001428 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001429 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001430 }
tsepez4cf55152016-11-02 14:37:54 -07001431 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432 }
1433
1434 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001435 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001436 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001437
thestigcf03f8e2016-05-09 12:36:18 -07001438 CFX_WideString wChange = wideChange;
1439 FX_STRSIZE iIndexMask = pEvent->SelStart();
1440 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1441 pEvent->SelStart() - pEvent->SelEnd();
1442 if (combined_len > 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 }
1448
thestigcf03f8e2016-05-09 12:36:18 -07001449 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001450 AlertIfPossible(pContext,
1451 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001452 pEvent->Rc() = false;
1453 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001454 }
1455
thestigcf03f8e2016-05-09 12:36:18 -07001456 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1457 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001458 AlertIfPossible(pContext,
1459 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001460 pEvent->Rc() = false;
1461 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001462 }
Dan Sinclair812e96c2017-03-13 16:43:37 -04001463 wchar_t wMask = wstrMask[iIndexMask];
thestigcf03f8e2016-05-09 12:36:18 -07001464 if (!isReservedMaskChar(wMask))
1465 wChange.SetAt(i, wMask);
1466
1467 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001468 pEvent->Rc() = false;
1469 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001470 }
1471 iIndexMask++;
1472 }
thestigcf03f8e2016-05-09 12:36:18 -07001473 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001474 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001475}
1476
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001478bool CJS_PublicMethods::AFSpecial_Keystroke(
Tom Sepezb1670b52017-02-16 17:01:00 -08001479 CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001480 const std::vector<CJS_Value>& params,
1481 CJS_Value& vRet,
1482 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001483 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001484 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001485 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001486 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001487
Tom Sepezb1670b52017-02-16 17:01:00 -08001488 CJS_EventHandler* pEvent =
1489 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001491 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001492
thestigcf03f8e2016-05-09 12:36:18 -07001493 const char* cFormat = "";
tsepezb4694242016-08-15 16:44:55 -07001494 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001495 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496 cFormat = "99999";
1497 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001498 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 cFormat = "999999999";
1500 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001501 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001502 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503 cFormat = "9999999999";
1504 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505 cFormat = "9999999";
1506 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001507 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001508 cFormat = "999999999";
1509 break;
1510 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001511
Lei Zhang945fdb72015-11-11 10:18:16 -08001512 std::vector<CJS_Value> params2;
Tom Sepezb1670b52017-02-16 17:01:00 -08001513 params2.push_back(CJS_Value(pRuntime, cFormat));
1514 return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001515}
1516
Tom Sepezb1670b52017-02-16 17:01:00 -08001517bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001518 const std::vector<CJS_Value>& params,
1519 CJS_Value& vRet,
1520 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001522 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001523 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001524 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001525
Tom Sepezb1670b52017-02-16 17:01:00 -08001526 CJS_EventHandler* pEventHandler =
1527 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepezcd5dc852016-09-08 11:23:24 -07001528
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001529 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001530 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001531 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001532
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001533 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001534 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001535 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001536 }
1537
1538 CFX_WideString prefix, postfix;
1539
1540 if (pEventHandler->SelStart() >= 0)
1541 prefix = swValue.Mid(0, pEventHandler->SelStart());
1542 else
1543 prefix = L"";
1544
1545 if (pEventHandler->SelEnd() >= 0 &&
1546 pEventHandler->SelEnd() <= swValue.GetLength())
1547 postfix = swValue.Mid(pEventHandler->SelEnd(),
1548 swValue.GetLength() - pEventHandler->SelEnd());
1549 else
1550 postfix = L"";
1551
tsepezf3dc8c62016-08-10 06:29:29 -07001552 vRet =
1553 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001554 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001555}
1556
Tom Sepezb1670b52017-02-16 17:01:00 -08001557bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001558 const std::vector<CJS_Value>& params,
1559 CJS_Value& vRet,
1560 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001561 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001562 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001563 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001564 }
1565
tsepezb4694242016-08-15 16:44:55 -07001566 CFX_WideString sValue = params[0].ToCFXWideString(pRuntime);
1567 CFX_WideString sFormat = params[1].ToCFXWideString(pRuntime);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001568 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001569 if (JS_PortIsNan(dDate)) {
1570 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001571 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001572 sFormat.c_str());
Tom Sepezb1670b52017-02-16 17:01:00 -08001573 AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001574 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001575 }
1576
tsepezf3dc8c62016-08-10 06:29:29 -07001577 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001578 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579}
1580
Tom Sepezb1670b52017-02-16 17:01:00 -08001581bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001582 const std::vector<CJS_Value>& params,
1583 CJS_Value& vRet,
1584 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001585 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001586 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001587 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001588 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001589
tsepezb4694242016-08-15 16:44:55 -07001590 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
1591 params[0].ToCFXWideString(pRuntime).c_str(),
1592 params[1].ToDouble(pRuntime),
1593 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001594
tsepez4cf55152016-11-02 14:37:54 -07001595 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001596}
1597
Tom Sepezb1670b52017-02-16 17:01:00 -08001598bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001599 const std::vector<CJS_Value>& params,
1600 CJS_Value& vRet,
1601 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001602 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001603 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001604 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001605 }
tsepezf3dc8c62016-08-10 06:29:29 -07001606
tsepezb4694242016-08-15 16:44:55 -07001607 CFX_WideString ws = params[0].ToCFXWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001608 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001609 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001610 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001611 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001612 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001613 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001614}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001615
Tom Sepezb1670b52017-02-16 17:01:00 -08001616bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001617 const std::vector<CJS_Value>& params,
1618 CJS_Value& vRet,
1619 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001620 if (params.size() != 2) {
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
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001625 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001626 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001627 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001628 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001629 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001630
dsinclair4526faf2016-10-11 10:54:49 -07001631 CPDFSDK_InterForm* pReaderInterForm =
Tom Sepezb1670b52017-02-16 17:01:00 -08001632 pRuntime->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001633 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001634
tsepezb4694242016-08-15 16:44:55 -07001635 CFX_WideString sFunction = params[0].ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001636 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001637
Tom Sepez67fd5df2015-10-08 12:24:19 -07001638 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001639 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001640
tsepezb4694242016-08-15 16:44:55 -07001641 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001642 CJS_Value jsValue(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001643 FieldNameArray.GetElement(pRuntime, i, jsValue);
1644 CFX_WideString wsFieldName = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001645
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1647 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1648 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001649 switch (pFormField->GetFieldType()) {
1650 case FIELDTYPE_TEXTFIELD:
1651 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001652 CFX_WideString trimmed = pFormField->GetValue();
1653 trimmed.TrimRight();
1654 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001655 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001656 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657 case FIELDTYPE_PUSHBUTTON: {
1658 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001659 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001660 case FIELDTYPE_CHECKBOX:
1661 case FIELDTYPE_RADIOBUTTON: {
1662 dTemp = 0.0;
1663 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1664 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1665 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001666 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1667 trimmed.TrimRight();
1668 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001669 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001670 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001671 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001672 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001673 }
Tom Sepez4246b002016-01-20 11:48:29 -08001674 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001675 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001676 if (pFormField->CountSelectedItems() <= 1) {
1677 CFX_WideString trimmed = pFormField->GetValue();
1678 trimmed.TrimRight();
1679 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001680 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001681 }
1682 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001683 default:
1684 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001685 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001686
1687 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1688 wcscmp(sFunction.c_str(), L"MAX") == 0))
1689 dValue = dTemp;
1690
1691 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1692
1693 nFieldsCount++;
1694 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001695 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001696 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001697
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001698 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1699 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001700
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001701 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1702 FXSYS_pow((double)10, (double)6);
Tom Sepezb1670b52017-02-16 17:01:00 -08001703
Tom Sepez67fd5df2015-10-08 12:24:19 -07001704 CJS_Value jsValue(pRuntime, dValue);
Tom Sepezb1670b52017-02-16 17:01:00 -08001705 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
foxit8b544ed2015-09-10 14:57:54 +08001706 if (pContext->GetEventHandler()->m_pValue)
tsepezb4694242016-08-15 16:44:55 -07001707 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001708
tsepez4cf55152016-11-02 14:37:54 -07001709 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001710}
1711
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001712/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001713** within the specified range. */
1714
Tom Sepezb1670b52017-02-16 17:01:00 -08001715bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001716 const std::vector<CJS_Value>& params,
1717 CJS_Value& vRet,
1718 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001719 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001720 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001721 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001722 }
Tom Sepezb1670b52017-02-16 17:01:00 -08001723 CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
tsepezcd5dc852016-09-08 11:23:24 -07001724 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001725 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001726 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001727
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001728 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001729 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001730
tsepezb4c9f3f2016-04-13 15:41:21 -07001731 double dEentValue =
1732 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001733 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001734 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001735 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001736 double dLessThan = params[3].ToDouble(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737 CFX_WideString swMsg;
1738
1739 if (bGreaterThan && bLessThan) {
1740 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001741 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001742 params[1].ToCFXWideString(pRuntime).c_str(),
1743 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001744 } else if (bGreaterThan) {
1745 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001746 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001747 params[1].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001748 } else if (bLessThan) {
1749 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001750 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001751 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001752 }
1753
1754 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001755 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001756 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001757 }
tsepez4cf55152016-11-02 14:37:54 -07001758 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001759}
1760
Tom Sepezb1670b52017-02-16 17:01:00 -08001761bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001762 const std::vector<CJS_Value>& params,
1763 CJS_Value& vRet,
1764 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001765 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001766 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001767 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001768 }
1769
tsepezb4694242016-08-15 16:44:55 -07001770 CFX_WideString str = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001771 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1772 str = L"0" + str;
1773
Tom Sepezb1670b52017-02-16 17:01:00 -08001774 CFX_WideString sPart;
1775 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001776 int nIndex = 0;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001777 for (const auto& wc : str) {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001778 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001779 sPart += wc;
Tom Sepez3c3e2712017-04-17 15:38:19 -07001780 } else if (sPart.GetLength() > 0) {
1781 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
1782 sPart = L"";
1783 nIndex++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001784 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001785 }
Tom Sepez3c3e2712017-04-17 15:38:19 -07001786 if (sPart.GetLength() > 0)
tsepezb4694242016-08-15 16:44:55 -07001787 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001788
tsepezb4694242016-08-15 16:44:55 -07001789 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001790 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001791 else
tsepezf3dc8c62016-08-10 06:29:29 -07001792 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001793
tsepez4cf55152016-11-02 14:37:54 -07001794 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001795}