blob: c0ea84c0aece0caeb73b0477709a9950e5740960 [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"
dsinclaira52ab742016-09-29 13:59:29 -070017#include "core/fxcrt/fx_ext.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"
dsinclair64376be2016-03-31 20:03:24 -070025#include "fpdfsdk/javascript/cjs_context.h"
26#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
33BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format)
35JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke)
36JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format)
37JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke)
38JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx)
39JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx)
40JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format)
41JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke)
42JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx)
43JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx)
44JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format)
45JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke)
46JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format)
47JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke)
48JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx)
49JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple)
50JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
51JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
52JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
53JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
54JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
55JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056END_JS_STATIC_GLOBAL_FUN()
57
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
tsepez745611b2016-04-12 16:46:34 -070062const FX_WCHAR* 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
tsepez745611b2016-04-12 16:46:34 -070066const FX_WCHAR* 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"};
70
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
tsepeze1e7bd02016-08-08 13:03:16 -070085void AlertIfPossible(CJS_Context* pContext, const FX_WCHAR* 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);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 const FX_WCHAR* pTrim = sTrim.c_str();
119 const FX_WCHAR* 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
171double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
172 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 Sinclair3ebd1212016-03-09 09:59:23 -0500238 FX_WCHAR 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++) {
257 FX_WCHAR 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
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 FX_WCHAR 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
376 FX_WCHAR c = format.GetAt(i);
377 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()) {
633 FX_WCHAR c = format.GetAt(i);
634 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)
tsepez4cf55152016-11-02 14:37:54 -0700753bool CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
754 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
tsepezcd5dc852016-09-08 11:23:24 -0700763 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
764 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Tom Sepez67fd5df2015-10-08 12:24:19 -0700765 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700766 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700767 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700768
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700769 CFX_WideString& Value = pEvent->Value();
770 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700772 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700773
tsepezb4694242016-08-15 16:44:55 -0700774 int iDec = params[0].ToInt(pRuntime);
775 int iSepStyle = params[1].ToInt(pRuntime);
776 int iNegStyle = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700777 // params[3] is iCurrStyle, it's not used.
tsepezb4694242016-08-15 16:44:55 -0700778 CFX_WideString wstrCurrency = params[4].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700779 bool bCurrencyPrepend = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700780
781 if (iDec < 0)
782 iDec = -iDec;
783
784 if (iSepStyle < 0 || iSepStyle > 3)
785 iSepStyle = 0;
786
787 if (iNegStyle < 0 || iNegStyle > 3)
788 iNegStyle = 0;
789
npm49c59282016-11-15 15:14:04 -0800790 // Processing decimal places
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700791 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700792 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700793 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700794 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795
npm49c59282016-11-15 15:14:04 -0800796 // Calculating number string
797 bool bNegative;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700798 int iDec2;
npm49c59282016-11-15 15:14:04 -0800799 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800 if (strValue.IsEmpty()) {
801 dValue = 0;
npm49c59282016-11-15 15:14:04 -0800802 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700803 if (strValue.IsEmpty()) {
804 strValue = "0";
805 iDec2 = 1;
806 }
807 }
808
npm49c59282016-11-15 15:14:04 -0800809 // Processing separator style
810 if (iDec2 < strValue.GetLength()) {
811 if (iSepStyle == 2 || iSepStyle == 3)
812 strValue.Replace(".", ",");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700813
814 if (iDec2 == 0)
815 strValue.Insert(iDec2, '0');
816 }
817 if (iSepStyle == 0 || iSepStyle == 2) {
npm49c59282016-11-15 15:14:04 -0800818 char cSeparator;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700819 if (iSepStyle == 0)
npm49c59282016-11-15 15:14:04 -0800820 cSeparator = ',';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821 else
npm49c59282016-11-15 15:14:04 -0800822 cSeparator = '.';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823
npm49c59282016-11-15 15:14:04 -0800824 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3)
825 strValue.Insert(iDecPositive, cSeparator);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826 }
827
npm49c59282016-11-15 15:14:04 -0800828 // Processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700829 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830
831 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700832 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700833 else
thestigcf03f8e2016-05-09 12:36:18 -0700834 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835
npm49c59282016-11-15 15:14:04 -0800836 // Processing negative style
837 if (bNegative) {
838 if (iNegStyle == 0)
thestigcf03f8e2016-05-09 12:36:18 -0700839 Value = L"-" + Value;
npm49c59282016-11-15 15:14:04 -0800840 else if (iNegStyle == 2 || iNegStyle == 3)
thestigcf03f8e2016-05-09 12:36:18 -0700841 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842 if (iNegStyle == 1 || iNegStyle == 3) {
843 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700844 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700845 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700846 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700847 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700848 vColElm = CJS_Value(pRuntime, 1);
tsepezb4694242016-08-15 16:44:55 -0700849 arColor.SetElement(pRuntime, 1, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700850 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700851 arColor.SetElement(pRuntime, 2, vColElm);
852 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700853
Tom Sepez67fd5df2015-10-08 12:24:19 -0700854 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700855 vProp.StartGetting();
856 vProp << arColor;
857 vProp.StartSetting();
858 fTarget->textColor(cc, vProp, sError); // red
859 }
860 }
861 } else {
862 if (iNegStyle == 1 || iNegStyle == 3) {
863 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700864 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700865 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700866 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700867 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700868 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700869 arColor.SetElement(pRuntime, 1, vColElm);
870 arColor.SetElement(pRuntime, 2, vColElm);
871 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872
Tom Sepez67fd5df2015-10-08 12:24:19 -0700873 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874 vProp.StartGetting();
875 fTarget->textColor(cc, vProp, sError);
876
tsepeze5aff742016-08-08 09:49:42 -0700877 CJS_Array aProp;
tsepezb4694242016-08-15 16:44:55 -0700878 vProp.GetJSValue()->ConvertToArray(pRuntime, aProp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879
880 CPWL_Color crProp;
881 CPWL_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700882 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
883 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884
885 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700886 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 vProp2.StartGetting();
888 vProp2 << arColor;
889 vProp2.StartSetting();
890 fTarget->textColor(cc, vProp2, sError);
891 }
892 }
893 }
894 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895#endif
tsepez4cf55152016-11-02 14:37:54 -0700896 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700897}
898
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
900// bCurrencyPrepend)
tsepez4cf55152016-11-02 14:37:54 -0700901bool CJS_PublicMethods::AFNumber_Keystroke(IJS_Context* cc,
902 const std::vector<CJS_Value>& params,
903 CJS_Value& vRet,
904 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700905 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700907
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700909 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700910
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700912 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700913
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914 CFX_WideString& val = pEvent->Value();
thestigcf03f8e2016-05-09 12:36:18 -0700915 CFX_WideString& wstrChange = pEvent->Change();
916 CFX_WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700917
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 if (pEvent->WillCommit()) {
ochanga0a3bc32016-05-12 15:22:48 -0700919 CFX_WideString swTemp = StrTrim(wstrValue);
920 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700921 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700922
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700923 swTemp.Replace(L",", L".");
924 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700925 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700926 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700927 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700928 }
tsepez4cf55152016-11-02 14:37:54 -0700929 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700931
thestigcf03f8e2016-05-09 12:36:18 -0700932 CFX_WideString wstrSelected;
933 if (pEvent->SelStart() != -1) {
934 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
935 pEvent->SelEnd() - pEvent->SelStart());
936 }
937
938 bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700939 if (bHasSign) {
940 // can't insert "change" in front to sign postion.
941 if (pEvent->SelStart() == 0) {
tsepez4cf55152016-11-02 14:37:54 -0700942 bool& bRc = pEvent->Rc();
943 bRc = false;
944 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700945 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700947
tsepezf3dc8c62016-08-10 06:29:29 -0700948 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -0700949 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700950 if (iSepStyle < 0 || iSepStyle > 3)
951 iSepStyle = 0;
952 const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700953
thestigcf03f8e2016-05-09 12:36:18 -0700954 bool bHasSep = wstrValue.Find(cSep) != -1;
955 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
956 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 if (bHasSep) {
tsepez4cf55152016-11-02 14:37:54 -0700958 bool& bRc = pEvent->Rc();
959 bRc = false;
960 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961 }
tsepez4cf55152016-11-02 14:37:54 -0700962 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 continue;
964 }
thestigcf03f8e2016-05-09 12:36:18 -0700965 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966 if (bHasSign) {
tsepez4cf55152016-11-02 14:37:54 -0700967 bool& bRc = pEvent->Rc();
968 bRc = false;
969 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800971 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700972 if (i != 0) {
tsepez4cf55152016-11-02 14:37:54 -0700973 bool& bRc = pEvent->Rc();
974 bRc = false;
975 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976 }
977 if (pEvent->SelStart() != 0) {
tsepez4cf55152016-11-02 14:37:54 -0700978 bool& bRc = pEvent->Rc();
979 bRc = false;
980 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981 }
tsepez4cf55152016-11-02 14:37:54 -0700982 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983 continue;
984 }
985
thestigcf03f8e2016-05-09 12:36:18 -0700986 if (!FXSYS_iswdigit(wstrChange[i])) {
tsepez4cf55152016-11-02 14:37:54 -0700987 bool& bRc = pEvent->Rc();
988 bRc = false;
989 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990 }
991 }
992
thestigcf03f8e2016-05-09 12:36:18 -0700993 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
994 CFX_WideString wpostfix;
995 if (pEvent->SelEnd() < wstrValue.GetLength())
996 wpostfix = wstrValue.Mid(pEvent->SelEnd());
997 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700998 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999}
1000
1001// function AFPercent_Format(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001002bool CJS_PublicMethods::AFPercent_Format(IJS_Context* cc,
1003 const std::vector<CJS_Value>& params,
1004 CJS_Value& vRet,
1005 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006#if _FX_OS_ != _FX_ANDROID_
tsepezcd5dc852016-09-08 11:23:24 -07001007 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001008 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010
1011 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001012 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001013 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014 }
1015 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001016 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017
1018 CFX_WideString& Value = pEvent->Value();
1019 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1020 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001021 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022
tsepezb4694242016-08-15 16:44:55 -07001023 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 if (iDec < 0)
1025 iDec = -iDec;
1026
tsepezb4694242016-08-15 16:44:55 -07001027 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028 if (iSepStyle < 0 || iSepStyle > 3)
1029 iSepStyle = 0;
1030
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001032 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033 dValue *= 100;
1034 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001035 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001036
1037 int iDec2;
1038 int iNegative = 0;
1039 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1040 if (strValue.IsEmpty()) {
1041 dValue = 0;
1042 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1043 }
1044
1045 if (iDec2 < 0) {
1046 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1047 strValue = "0" + strValue;
1048 }
1049 iDec2 = 0;
1050 }
1051 int iMax = strValue.GetLength();
1052 if (iDec2 > iMax) {
1053 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1054 strValue += "0";
1055 }
1056 iMax = iDec2 + 1;
1057 }
dsinclair64376be2016-03-31 20:03:24 -07001058
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 // for processing seperator style
1060 if (iDec2 < iMax) {
1061 if (iSepStyle == 0 || iSepStyle == 1) {
1062 strValue.Insert(iDec2, '.');
1063 iMax++;
1064 } else if (iSepStyle == 2 || iSepStyle == 3) {
1065 strValue.Insert(iDec2, ',');
1066 iMax++;
1067 }
1068
1069 if (iDec2 == 0)
1070 strValue.Insert(iDec2, '0');
1071 }
1072 if (iSepStyle == 0 || iSepStyle == 2) {
1073 char cSeperator;
1074 if (iSepStyle == 0)
1075 cSeperator = ',';
1076 else
1077 cSeperator = '.';
1078
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001079 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080 strValue.Insert(iDecPositive, cSeperator);
1081 iMax++;
1082 }
1083 }
dsinclair64376be2016-03-31 20:03:24 -07001084
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085 // negative mark
1086 if (iNegative)
1087 strValue = "-" + strValue;
1088 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001089 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001090#endif
tsepez4cf55152016-11-02 14:37:54 -07001091 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092}
1093// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001094bool CJS_PublicMethods::AFPercent_Keystroke(
Lei Zhang945fdb72015-11-11 10:18:16 -08001095 IJS_Context* cc,
1096 const std::vector<CJS_Value>& params,
1097 CJS_Value& vRet,
1098 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099 return AFNumber_Keystroke(cc, params, vRet, sError);
1100}
1101
1102// function AFDate_FormatEx(cFormat)
tsepez4cf55152016-11-02 14:37:54 -07001103bool CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
1104 const std::vector<CJS_Value>& params,
1105 CJS_Value& vRet,
1106 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -07001107 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001108 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001109 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001110
1111 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001112 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001113 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001114 }
1115 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001116 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117
1118 CFX_WideString& val = pEvent->Value();
1119 CFX_WideString strValue = val;
1120 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001121 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001122
tsepezb4694242016-08-15 16:44:55 -07001123 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124 double dDate = 0.0f;
1125
1126 if (strValue.Find(L"GMT") != -1) {
1127 // for GMT format time
1128 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1129 dDate = MakeInterDate(strValue);
1130 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001131 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001132 }
1133
1134 if (JS_PortIsNan(dDate)) {
1135 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001136 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001137 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001138 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001139 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001140 }
1141
1142 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001143 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001144}
1145
tsepez745611b2016-04-12 16:46:34 -07001146double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001147 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001149 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 FX_WCHAR c = strValue.GetAt(i);
1151 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001152 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 sTemp = L"";
1154 continue;
1155 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 sTemp += c;
1157 }
Tom Sepezab277682016-02-17 10:07:21 -08001158 wsArray.push_back(sTemp);
1159 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 return 0;
1161
Tom Sepez4246b002016-01-20 11:48:29 -08001162 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 sTemp = wsArray[1];
1164 if (sTemp.Compare(L"Jan") == 0)
1165 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001166 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001168 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001170 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001172 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001174 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001176 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001178 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001180 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001182 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001184 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001185 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001186 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001187 nMonth = 12;
1188
tsepez4c3debb2016-04-08 12:20:38 -07001189 int nDay = FX_atof(wsArray[2].AsStringC());
1190 int nHour = FX_atof(wsArray[3].AsStringC());
1191 int nMin = FX_atof(wsArray[4].AsStringC());
1192 int nSec = FX_atof(wsArray[5].AsStringC());
1193 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001194 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1195 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001196 if (JS_PortIsNan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001197 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198
1199 return dRet;
1200}
1201
1202// AFDate_KeystrokeEx(cFormat)
tsepez4cf55152016-11-02 14:37:54 -07001203bool CJS_PublicMethods::AFDate_KeystrokeEx(IJS_Context* cc,
1204 const std::vector<CJS_Value>& params,
1205 CJS_Value& vRet,
1206 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 CJS_Context* pContext = (CJS_Context*)cc;
tsepezf3dc8c62016-08-10 06:29:29 -07001208 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210
1211 if (params.size() != 1) {
1212 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001213 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214 }
1215
1216 if (pEvent->WillCommit()) {
1217 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001218 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 CFX_WideString strValue = pEvent->Value();
1220 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001221 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001222
tsepezb4694242016-08-15 16:44:55 -07001223 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001224 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001225 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226 if (bWrongFormat || JS_PortIsNan(dRet)) {
1227 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001228 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001230 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001231 pEvent->Rc() = false;
1232 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001233 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234 }
tsepez4cf55152016-11-02 14:37:54 -07001235 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001236}
1237
tsepez4cf55152016-11-02 14:37:54 -07001238bool CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
1239 const std::vector<CJS_Value>& params,
1240 CJS_Value& vRet,
1241 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001242 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001243 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001244 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001245 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001246
tsepezf3dc8c62016-08-10 06:29:29 -07001247 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001248 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001249 const FX_WCHAR* cFormats[] = {L"m/d",
1250 L"m/d/yy",
1251 L"mm/dd/yy",
1252 L"mm/yy",
1253 L"d-mmm",
1254 L"d-mmm-yy",
1255 L"dd-mmm-yy",
1256 L"yy-mm-dd",
1257 L"mmm-yy",
1258 L"mmmm-yy",
1259 L"mmm d, yyyy",
1260 L"mmmm d, yyyy",
1261 L"m/d/yy h:MM tt",
1262 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001263
Lei Zhanga0f67242015-08-17 15:39:30 -07001264 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1265 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001266
Lei Zhang945fdb72015-11-11 10:18:16 -08001267 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001268 newParams.push_back(
1269 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270 return AFDate_FormatEx(cc, newParams, vRet, sError);
1271}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001272
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001273// AFDate_KeystrokeEx(cFormat)
tsepez4cf55152016-11-02 14:37:54 -07001274bool CJS_PublicMethods::AFDate_Keystroke(IJS_Context* cc,
1275 const std::vector<CJS_Value>& params,
1276 CJS_Value& vRet,
1277 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001278 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001279 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001280 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001281 }
1282
tsepezf3dc8c62016-08-10 06:29:29 -07001283 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001284 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001285 const FX_WCHAR* cFormats[] = {L"m/d",
1286 L"m/d/yy",
1287 L"mm/dd/yy",
1288 L"mm/yy",
1289 L"d-mmm",
1290 L"d-mmm-yy",
1291 L"dd-mmm-yy",
1292 L"yy-mm-dd",
1293 L"mmm-yy",
1294 L"mmmm-yy",
1295 L"mmm d, yyyy",
1296 L"mmmm d, yyyy",
1297 L"m/d/yy h:MM tt",
1298 L"m/d/yy HH:MM"};
1299
Lei Zhanga0f67242015-08-17 15:39:30 -07001300 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1301 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302
Lei Zhang945fdb72015-11-11 10:18:16 -08001303 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001304 newParams.push_back(
1305 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001306 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1307}
1308
1309// function AFTime_Format(ptf)
tsepez4cf55152016-11-02 14:37:54 -07001310bool CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
1311 const std::vector<CJS_Value>& params,
1312 CJS_Value& vRet,
1313 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001315 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001316 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317 }
1318
tsepezf3dc8c62016-08-10 06:29:29 -07001319 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001320 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1322 L"h:MM:ss tt"};
1323
Lei Zhanga0f67242015-08-17 15:39:30 -07001324 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1325 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326
Lei Zhang945fdb72015-11-11 10:18:16 -08001327 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001328 newParams.push_back(
1329 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001330 return AFDate_FormatEx(cc, newParams, vRet, sError);
1331}
1332
tsepez4cf55152016-11-02 14:37:54 -07001333bool CJS_PublicMethods::AFTime_Keystroke(IJS_Context* cc,
1334 const std::vector<CJS_Value>& params,
1335 CJS_Value& vRet,
1336 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001337 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001338 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001339 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001340 }
1341
tsepezf3dc8c62016-08-10 06:29:29 -07001342 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001343 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001344 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1345 L"h:MM:ss tt"};
1346
Lei Zhanga0f67242015-08-17 15:39:30 -07001347 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1348 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001349
Lei Zhang945fdb72015-11-11 10:18:16 -08001350 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001351 newParams.push_back(
1352 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001353 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1354}
1355
tsepez4cf55152016-11-02 14:37:54 -07001356bool CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
1357 const std::vector<CJS_Value>& params,
1358 CJS_Value& vRet,
1359 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360 return AFDate_FormatEx(cc, params, vRet, sError);
1361}
1362
tsepez4cf55152016-11-02 14:37:54 -07001363bool CJS_PublicMethods::AFTime_KeystrokeEx(IJS_Context* cc,
1364 const std::vector<CJS_Value>& params,
1365 CJS_Value& vRet,
1366 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1368}
1369
1370// function AFSpecial_Format(psf)
tsepez4cf55152016-11-02 14:37:54 -07001371bool CJS_PublicMethods::AFSpecial_Format(IJS_Context* cc,
1372 const std::vector<CJS_Value>& params,
1373 CJS_Value& vRet,
1374 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001376 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001377 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 }
1379
tsepezcd5dc852016-09-08 11:23:24 -07001380 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001383 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001384
tsepezf3dc8c62016-08-10 06:29:29 -07001385 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez4f1f41f2016-03-28 14:13:16 -07001386 CFX_WideString wsSource = pEvent->Value();
1387 CFX_WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001388 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001389 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001390 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001391 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001392 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001393 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001395 case 2:
1396 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1397 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001398 else
tsepez4f1f41f2016-03-28 14:13:16 -07001399 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001400 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001401 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001402 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403 break;
1404 }
1405
tsepez4f1f41f2016-03-28 14:13:16 -07001406 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001407 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408}
1409
1410// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001411bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Lei Zhang945fdb72015-11-11 10:18:16 -08001412 IJS_Context* cc,
1413 const std::vector<CJS_Value>& params,
1414 CJS_Value& vRet,
1415 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001416 CJS_Context* pContext = (CJS_Context*)cc;
tsepezf3dc8c62016-08-10 06:29:29 -07001417 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001418 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1419
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001420 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001421 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001422 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 }
1424
1425 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001426 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427
tsepezcd5dc852016-09-08 11:23:24 -07001428 CFX_WideString& valEvent = pEvent->Value();
tsepezb4694242016-08-15 16:44:55 -07001429 CFX_WideString wstrMask = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001430 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001431 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001433 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001434 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001435 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001436
1437 FX_STRSIZE iIndexMask = 0;
1438 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1439 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001440 break;
1441 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001442
thestigcf03f8e2016-05-09 12:36:18 -07001443 if (iIndexMask != wstrMask.GetLength() ||
1444 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001445 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001446 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001447 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448 }
tsepez4cf55152016-11-02 14:37:54 -07001449 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001450 }
1451
1452 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001453 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001454 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001455
thestigcf03f8e2016-05-09 12:36:18 -07001456 CFX_WideString wChange = wideChange;
1457 FX_STRSIZE iIndexMask = pEvent->SelStart();
1458 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1459 pEvent->SelStart() - pEvent->SelEnd();
1460 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001461 AlertIfPossible(pContext,
1462 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001463 pEvent->Rc() = false;
1464 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001465 }
1466
thestigcf03f8e2016-05-09 12:36:18 -07001467 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001468 AlertIfPossible(pContext,
1469 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001470 pEvent->Rc() = false;
1471 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472 }
1473
thestigcf03f8e2016-05-09 12:36:18 -07001474 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1475 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001476 AlertIfPossible(pContext,
1477 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001478 pEvent->Rc() = false;
1479 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480 }
thestigcf03f8e2016-05-09 12:36:18 -07001481 FX_WCHAR wMask = wstrMask[iIndexMask];
1482 if (!isReservedMaskChar(wMask))
1483 wChange.SetAt(i, wMask);
1484
1485 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001486 pEvent->Rc() = false;
1487 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001488 }
1489 iIndexMask++;
1490 }
thestigcf03f8e2016-05-09 12:36:18 -07001491 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001492 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001493}
1494
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001496bool CJS_PublicMethods::AFSpecial_Keystroke(
Lei Zhang945fdb72015-11-11 10:18:16 -08001497 IJS_Context* cc,
1498 const std::vector<CJS_Value>& params,
1499 CJS_Value& vRet,
1500 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001502 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001503 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001505
tsepezcd5dc852016-09-08 11:23:24 -07001506 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001507 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001508 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001509 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001510
thestigcf03f8e2016-05-09 12:36:18 -07001511 const char* cFormat = "";
tsepezf3dc8c62016-08-10 06:29:29 -07001512 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001513 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001514 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515 cFormat = "99999";
1516 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001517 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518 cFormat = "999999999";
1519 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001520 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001521 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 cFormat = "9999999999";
1523 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001524 cFormat = "9999999";
1525 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001526 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 cFormat = "999999999";
1528 break;
1529 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001530
Lei Zhang945fdb72015-11-11 10:18:16 -08001531 std::vector<CJS_Value> params2;
thestigcf03f8e2016-05-09 12:36:18 -07001532 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001533 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001534}
1535
tsepez4cf55152016-11-02 14:37:54 -07001536bool CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
1537 const std::vector<CJS_Value>& params,
1538 CJS_Value& vRet,
1539 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001540 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001541 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001542 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001544
tsepezcd5dc852016-09-08 11:23:24 -07001545 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1546 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1547 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
1548
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001549 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001550 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001551 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001552
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001553 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001554 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001555 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001556 }
1557
1558 CFX_WideString prefix, postfix;
1559
1560 if (pEventHandler->SelStart() >= 0)
1561 prefix = swValue.Mid(0, pEventHandler->SelStart());
1562 else
1563 prefix = L"";
1564
1565 if (pEventHandler->SelEnd() >= 0 &&
1566 pEventHandler->SelEnd() <= swValue.GetLength())
1567 postfix = swValue.Mid(pEventHandler->SelEnd(),
1568 swValue.GetLength() - pEventHandler->SelEnd());
1569 else
1570 postfix = L"";
1571
tsepezf3dc8c62016-08-10 06:29:29 -07001572 vRet =
1573 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001574 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001575}
1576
tsepez4cf55152016-11-02 14:37:54 -07001577bool CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
1578 const std::vector<CJS_Value>& params,
1579 CJS_Value& vRet,
1580 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001581 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001582 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001583 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001584 }
1585
tsepezcd5dc852016-09-08 11:23:24 -07001586 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001587 CFX_WideString sValue = params[0].ToCFXWideString(pRuntime);
1588 CFX_WideString sFormat = params[1].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001589
Lei Zhang9559b7a2015-12-21 11:12:20 -08001590 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001591
1592 if (JS_PortIsNan(dDate)) {
1593 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001594 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001595 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001596 AlertIfPossible((CJS_Context*)cc, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001597 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001598 }
1599
tsepezf3dc8c62016-08-10 06:29:29 -07001600 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001601 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001602}
1603
tsepez4cf55152016-11-02 14:37:54 -07001604bool CJS_PublicMethods::AFSimple(IJS_Context* cc,
1605 const std::vector<CJS_Value>& params,
1606 CJS_Value& vRet,
1607 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001608 if (params.size() != 3) {
tsepezcd5dc852016-09-08 11:23:24 -07001609 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001610 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001611 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001612
tsepezcd5dc852016-09-08 11:23:24 -07001613 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001614 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
1615 params[0].ToCFXWideString(pRuntime).c_str(),
1616 params[1].ToDouble(pRuntime),
1617 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001618
tsepez4cf55152016-11-02 14:37:54 -07001619 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001620}
1621
tsepez4cf55152016-11-02 14:37:54 -07001622bool CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
1623 const std::vector<CJS_Value>& params,
1624 CJS_Value& vRet,
1625 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001626 if (params.size() != 1) {
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 }
tsepezf3dc8c62016-08-10 06:29:29 -07001630
tsepezcd5dc852016-09-08 11:23:24 -07001631 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001632 CFX_WideString ws = params[0].ToCFXWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001633 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001634 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001635 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001636 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001637 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001638 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001639}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001640
tsepez4cf55152016-11-02 14:37:54 -07001641bool CJS_PublicMethods::AFSimple_Calculate(IJS_Context* cc,
1642 const std::vector<CJS_Value>& params,
1643 CJS_Value& vRet,
1644 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001646 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001647 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001649
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001650 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001651 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
tsepezcd5dc852016-09-08 11:23:24 -07001652 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001653 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001654 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001655
tsepezcd5dc852016-09-08 11:23:24 -07001656 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1657 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
dsinclair4526faf2016-10-11 10:54:49 -07001658 CPDFSDK_InterForm* pReaderInterForm =
dsinclair7cbe68e2016-10-12 11:56:23 -07001659 pContext->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001660 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001661
tsepezb4694242016-08-15 16:44:55 -07001662 CFX_WideString sFunction = params[0].ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001663 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001664
Tom Sepez67fd5df2015-10-08 12:24:19 -07001665 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001666 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001667
tsepezb4694242016-08-15 16:44:55 -07001668 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001669 CJS_Value jsValue(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001670 FieldNameArray.GetElement(pRuntime, i, jsValue);
1671 CFX_WideString wsFieldName = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001672
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001673 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1674 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1675 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676 switch (pFormField->GetFieldType()) {
1677 case FIELDTYPE_TEXTFIELD:
1678 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001679 CFX_WideString trimmed = pFormField->GetValue();
1680 trimmed.TrimRight();
1681 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001682 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001683 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001684 case FIELDTYPE_PUSHBUTTON: {
1685 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001686 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001687 case FIELDTYPE_CHECKBOX:
1688 case FIELDTYPE_RADIOBUTTON: {
1689 dTemp = 0.0;
1690 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1691 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1692 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001693 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1694 trimmed.TrimRight();
1695 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001696 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001697 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001698 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001699 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001700 }
Tom Sepez4246b002016-01-20 11:48:29 -08001701 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001702 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001703 if (pFormField->CountSelectedItems() <= 1) {
1704 CFX_WideString trimmed = pFormField->GetValue();
1705 trimmed.TrimRight();
1706 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001707 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001708 }
1709 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001710 default:
1711 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001712 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713
1714 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1715 wcscmp(sFunction.c_str(), L"MAX") == 0))
1716 dValue = dTemp;
1717
1718 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1719
1720 nFieldsCount++;
1721 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001722 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001724
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001725 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1726 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001727
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001728 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1729 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001730 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001731 if (pContext->GetEventHandler()->m_pValue)
tsepezb4694242016-08-15 16:44:55 -07001732 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001733
tsepez4cf55152016-11-02 14:37:54 -07001734 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001735}
1736
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001737/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001738** within the specified range. */
1739
tsepez4cf55152016-11-02 14:37:54 -07001740bool CJS_PublicMethods::AFRange_Validate(IJS_Context* cc,
1741 const std::vector<CJS_Value>& params,
1742 CJS_Value& vRet,
1743 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001744 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001745 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001746 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001747 }
tsepezcd5dc852016-09-08 11:23:24 -07001748 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1749 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1750 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001751 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001752 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001753
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001754 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001755 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001756
tsepezb4c9f3f2016-04-13 15:41:21 -07001757 double dEentValue =
1758 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001759 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001760 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001761 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001762 double dLessThan = params[3].ToDouble(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001763 CFX_WideString swMsg;
1764
1765 if (bGreaterThan && bLessThan) {
1766 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001767 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001768 params[1].ToCFXWideString(pRuntime).c_str(),
1769 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001770 } else if (bGreaterThan) {
1771 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001772 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001773 params[1].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001774 } else if (bLessThan) {
1775 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001776 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001777 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001778 }
1779
1780 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001781 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001782 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001783 }
tsepez4cf55152016-11-02 14:37:54 -07001784 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001785}
1786
tsepez4cf55152016-11-02 14:37:54 -07001787bool CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
1788 const std::vector<CJS_Value>& params,
1789 CJS_Value& vRet,
1790 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001791 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001792 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001793 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001794 }
1795
tsepezcd5dc852016-09-08 11:23:24 -07001796 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001797 CFX_WideString str = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001798 CFX_WideString sPart;
tsepezcd5dc852016-09-08 11:23:24 -07001799 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001800
1801 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1802 str = L"0" + str;
1803
1804 int nIndex = 0;
1805 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1806 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001807 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001808 sPart += wc;
1809 } else {
1810 if (sPart.GetLength() > 0) {
tsepezb4694242016-08-15 16:44:55 -07001811 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812 sPart = L"";
1813 nIndex++;
1814 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001815 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001816 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001817
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001818 if (sPart.GetLength() > 0) {
tsepezb4694242016-08-15 16:44:55 -07001819 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001820 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001821
tsepezb4694242016-08-15 16:44:55 -07001822 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001823 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001824 else
tsepezf3dc8c62016-08-10 06:29:29 -07001825 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001826
tsepez4cf55152016-11-02 14:37:54 -07001827 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001828}