blob: bfe48d5b65a9906b6b54356bdd38d79be8c32112 [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>
11#include <sstream>
12#include <string>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050013#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080014
dsinclair1727aee2016-09-29 13:12:56 -070015#include "core/fpdfdoc/cpdf_interform.h"
dsinclaira52ab742016-09-29 13:59:29 -070016#include "core/fxcrt/fx_ext.h"
dsinclair735606d2016-10-05 15:47:02 -070017#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070018#include "fpdfsdk/cpdfsdk_interform.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040019#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040020#include "fpdfsdk/javascript/JS_Define.h"
21#include "fpdfsdk/javascript/JS_EventHandler.h"
22#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040023#include "fpdfsdk/javascript/JS_Value.h"
dsinclair64376be2016-03-31 20:03:24 -070024#include "fpdfsdk/javascript/cjs_context.h"
25#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040026#include "fpdfsdk/javascript/color.h"
27#include "fpdfsdk/javascript/resource.h"
28#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
32BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format)
34JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke)
35JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format)
36JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke)
37JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx)
38JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx)
39JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format)
40JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke)
41JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx)
42JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx)
43JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format)
44JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke)
45JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format)
46JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke)
47JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx)
48JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple)
49JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
50JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
51JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
52JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
53JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
54JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055END_JS_STATIC_GLOBAL_FUN()
56
57IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
58
tsepez745611b2016-04-12 16:46:34 -070059namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060
tsepez745611b2016-04-12 16:46:34 -070061const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
62 L"May", L"Jun", L"Jul", L"Aug",
63 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064
tsepez745611b2016-04-12 16:46:34 -070065const FX_WCHAR* const fullmonths[] = {L"January", L"February", L"March",
66 L"April", L"May", L"June",
67 L"July", L"August", L"September",
68 L"October", L"November", L"December"};
69
70CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
71 CFX_ByteString result(pStr);
72 result.TrimLeft(' ');
73 result.TrimRight(' ');
74 return result;
75}
76
77CFX_WideString StrTrim(const CFX_WideString& pStr) {
78 CFX_WideString result(pStr);
79 result.TrimLeft(' ');
80 result.TrimRight(' ');
81 return result;
82}
83
tsepeze1e7bd02016-08-08 13:03:16 -070084void AlertIfPossible(CJS_Context* pContext, const FX_WCHAR* swMsg) {
dsinclair8779fa82016-10-12 12:05:44 -070085 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
86 if (pFormFillEnv)
87 pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3);
tsepeze1e7bd02016-08-08 13:03:16 -070088}
89
npm49c59282016-11-15 15:14:04 -080090#if _FX_OS_ != _FX_ANDROID_
91CFX_ByteString CalculateString(double dValue,
92 int iDec,
93 int* iDec2,
94 bool* bNegative) {
95 *bNegative = dValue < 0;
96 if (*bNegative)
97 dValue = -dValue;
98 std::stringstream ss;
99 ss << std::fixed << std::setprecision(iDec) << dValue;
100 std::string stringValue = ss.str();
101 size_t iDecimalPos = stringValue.find(".");
102 *iDec2 = iDecimalPos == std::string::npos ? stringValue.size()
103 : static_cast<int>(iDecimalPos);
104 return CFX_ByteString(stringValue.c_str());
105}
106#endif
107
tsepez745611b2016-04-12 16:46:34 -0700108} // namespace
109
110bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500111 CFX_WideString sTrim = StrTrim(str);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 const FX_WCHAR* pTrim = sTrim.c_str();
113 const FX_WCHAR* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -0700114 bool bDot = false;
115 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -0700118 while ((c = *p) != L'\0') {
119 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -0700121 return false;
122 bDot = true;
123 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -0700125 return false;
126 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700128 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 p++;
131 c = *p;
Wei Li614d20a2016-03-15 13:55:12 -0700132 if (c == L'+' || c == L'-') {
133 bKXJS = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 } else {
Wei Li614d20a2016-03-15 13:55:12 -0700135 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800137 } else if (!FXSYS_iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700138 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 }
140 p++;
141 }
142
Wei Li614d20a2016-03-15 13:55:12 -0700143 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144}
145
Wei Li614d20a2016-03-15 13:55:12 -0700146bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 switch (c_Mask) {
148 case L'9':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800149 return FXSYS_iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800151 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800153 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700155 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 default:
157 return (c_Change == c_Mask);
158 }
159}
160
Wei Li614d20a2016-03-15 13:55:12 -0700161bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
163}
164
165double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
166 double dValue1,
167 double dValue2) {
168 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
169 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
170 return dValue1 + dValue2;
171 }
172 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
173 return dValue1 * dValue2;
174 }
175 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800176 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 }
178 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800179 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 }
181 return dValue1;
182}
183
Tom Sepez67fd5df2015-10-08 12:24:19 -0700184CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 CJS_Value val) {
tsepeze5aff742016-08-08 09:49:42 -0700186 CJS_Array StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 if (val.IsArrayObject()) {
tsepezb4694242016-08-15 16:44:55 -0700188 val.ConvertToArray(pRuntime, StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700189 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 }
tsepezb4694242016-08-15 16:44:55 -0700191 CFX_WideString wsStr = val.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700193 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194
195 int ch = ',';
196 int nIndex = 0;
197
198 while (*p) {
199 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800200 if (!pTemp) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700201 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700202 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700203 CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 }
Lei Zhang997de612015-11-04 18:17:53 -0800206
207 char* pSub = new char[pTemp - p + 1];
208 strncpy(pSub, p, pTemp - p);
209 *(pSub + (pTemp - p)) = '\0';
210
tsepezb4c9f3f2016-04-13 15:41:21 -0700211 StrArray.SetElement(
tsepezb4694242016-08-15 16:44:55 -0700212 pRuntime, nIndex,
tsepeze5aff742016-08-08 09:49:42 -0700213 CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800214 delete[] pSub;
215
216 nIndex++;
217 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 }
219 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220}
221
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500222int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 int nStart,
224 int& nSkip,
225 int nMaxStep) {
226 int nRet = 0;
227 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500228 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 if (i - nStart > 10)
230 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700231
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500232 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800233 if (!FXSYS_iswdigit(c))
234 break;
235
Dan Sinclair1c915372016-03-03 17:12:58 -0500236 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800237 nSkip = i - nStart + 1;
238 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 break;
240 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243}
244
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500245CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
246 int nStart,
247 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 CFX_WideString swRet;
249 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500250 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
251 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800252 if (!FXSYS_iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800254
255 swRet += c;
256 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260}
261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800263 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 int nYear = JS_GetYearFromTime(dt);
267 int nMonth = JS_GetMonthFromTime(dt) + 1;
268 int nDay = JS_GetDayFromTime(dt);
269 int nHour = JS_GetHourFromTime(dt);
270 int nMin = JS_GetMinFromTime(dt);
271 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 int nSkip = 0;
276 int nLen = value.GetLength();
277 int nIndex = 0;
278 int i = 0;
279 while (i < nLen) {
280 if (nIndex > 2)
281 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 FX_WCHAR c = value.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800284 if (FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
286 i += nSkip;
287 } else {
288 i++;
289 }
290 }
291
292 if (nIndex == 2) {
293 // case2: month/day
294 // case3: day/month
295 if ((number[0] >= 1 && number[0] <= 12) &&
296 (number[1] >= 1 && number[1] <= 31)) {
297 nMonth = number[0];
298 nDay = number[1];
299 } else if ((number[0] >= 1 && number[0] <= 31) &&
300 (number[1] >= 1 && number[1] <= 12)) {
301 nDay = number[0];
302 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700303 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700304
Lei Zhang9559b7a2015-12-21 11:12:20 -0800305 if (bWrongFormat)
306 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 } else if (nIndex == 3) {
308 // case1: year/month/day
309 // case2: month/day/year
310 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700311
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
313 (number[2] >= 1 && number[2] <= 31)) {
314 nYear = number[0];
315 nMonth = number[1];
316 nDay = number[2];
317 } else if ((number[0] >= 1 && number[0] <= 12) &&
318 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
319 nMonth = number[0];
320 nDay = number[1];
321 nYear = number[2];
322 } else if ((number[0] >= 1 && number[0] <= 31) &&
323 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
324 nDay = number[0];
325 nMonth = number[1];
326 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700327 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700328
Lei Zhang9559b7a2015-12-21 11:12:20 -0800329 if (bWrongFormat)
330 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800332 if (bWrongFormat)
333 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 return dt;
335 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 CFX_WideString swTemp;
338 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700339 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340}
341
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
343 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800344 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 if (format.IsEmpty() || value.IsEmpty())
348 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700349
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 int nYear = JS_GetYearFromTime(dt);
351 int nMonth = JS_GetMonthFromTime(dt) + 1;
352 int nDay = JS_GetDayFromTime(dt);
353 int nHour = JS_GetHourFromTime(dt);
354 int nMin = JS_GetMinFromTime(dt);
355 int nSec = JS_GetSecFromTime(dt);
356
357 int nYearSub = 99; // nYear - 2000;
358
tsepez4cf55152016-11-02 14:37:54 -0700359 bool bPm = false;
360 bool bExit = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800361 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362
363 int i = 0;
364 int j = 0;
365
366 while (i < format.GetLength()) {
367 if (bExit)
368 break;
369
370 FX_WCHAR c = format.GetAt(i);
371 switch (c) {
372 case ':':
373 case '.':
374 case '-':
375 case '\\':
376 case '/':
377 i++;
378 j++;
379 break;
380
381 case 'y':
382 case 'm':
383 case 'd':
384 case 'H':
385 case 'h':
386 case 'M':
387 case 's':
388 case 't': {
389 int oldj = j;
390 int nSkip = 0;
391 int remaining = format.GetLength() - i - 1;
392
393 if (remaining == 0 || format.GetAt(i + 1) != c) {
394 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700395 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 i++;
397 j++;
398 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700399 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 nMonth = ParseStringInteger(value, j, nSkip, 2);
401 i++;
402 j += nSkip;
403 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700404 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 nDay = ParseStringInteger(value, j, nSkip, 2);
406 i++;
407 j += nSkip;
408 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700409 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 nHour = ParseStringInteger(value, j, nSkip, 2);
411 i++;
412 j += nSkip;
413 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700414 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 nHour = ParseStringInteger(value, j, nSkip, 2);
416 i++;
417 j += nSkip;
418 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700419 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 nMin = ParseStringInteger(value, j, nSkip, 2);
421 i++;
422 j += nSkip;
423 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700424 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 nSec = ParseStringInteger(value, j, nSkip, 2);
426 i++;
427 j += nSkip;
428 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700429 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
431 i++;
432 j++;
433 break;
434 }
435 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
436 switch (c) {
437 case 'y':
438 nYear = ParseStringInteger(value, j, nSkip, 4);
439 i += 2;
440 j += nSkip;
441 break;
442 case 'm':
443 nMonth = ParseStringInteger(value, j, nSkip, 2);
444 i += 2;
445 j += nSkip;
446 break;
447 case 'd':
448 nDay = ParseStringInteger(value, j, nSkip, 2);
449 i += 2;
450 j += nSkip;
451 break;
452 case 'H':
453 nHour = ParseStringInteger(value, j, nSkip, 2);
454 i += 2;
455 j += nSkip;
456 break;
457 case 'h':
458 nHour = ParseStringInteger(value, j, nSkip, 2);
459 i += 2;
460 j += nSkip;
461 break;
462 case 'M':
463 nMin = ParseStringInteger(value, j, nSkip, 2);
464 i += 2;
465 j += nSkip;
466 break;
467 case 's':
468 nSec = ParseStringInteger(value, j, nSkip, 2);
469 i += 2;
470 j += nSkip;
471 break;
472 case 't':
473 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
474 value.GetAt(j + 1) == 'm');
475 i += 2;
476 j += 2;
477 break;
478 }
479 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
480 switch (c) {
481 case 'm': {
482 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
tsepez4cf55152016-11-02 14:37:54 -0700483 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 for (int m = 0; m < 12; m++) {
485 if (sMonth.CompareNoCase(months[m]) == 0) {
486 nMonth = m + 1;
487 i += 3;
488 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700489 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700491 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492 }
493
494 if (!bFind) {
495 nMonth = ParseStringInteger(value, j, nSkip, 3);
496 i += 3;
497 j += nSkip;
498 }
499 } break;
500 case 'y':
501 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700502 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700503 i += 3;
504 j += 3;
505 break;
506 }
507 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
508 switch (c) {
509 case 'y':
510 nYear = ParseStringInteger(value, j, nSkip, 4);
511 j += nSkip;
512 i += 4;
513 break;
514 case 'm': {
tsepez4cf55152016-11-02 14:37:54 -0700515 bool bFind = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516
517 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
518 sMonth.MakeLower();
519
520 for (int m = 0; m < 12; m++) {
521 CFX_WideString sFullMonths = fullmonths[m];
522 sFullMonths.MakeLower();
523
524 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
525 nMonth = m + 1;
526 i += 4;
527 j += nSkip;
tsepez4cf55152016-11-02 14:37:54 -0700528 bFind = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529 break;
530 }
531 }
532
533 if (!bFind) {
534 nMonth = ParseStringInteger(value, j, nSkip, 4);
535 i += 4;
536 j += nSkip;
537 }
538 } break;
539 default:
540 i += 4;
541 j += 4;
542 break;
543 }
544 } else {
545 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800546 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700547 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700548 }
549 i++;
550 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700551 }
Tom Sepez85386422014-07-23 10:28:37 -0700552
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800554 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700555 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556 }
557 }
558
559 break;
560 default:
561 if (value.GetLength() <= j) {
tsepez4cf55152016-11-02 14:37:54 -0700562 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800564 bBadFormat = true;
tsepez4cf55152016-11-02 14:37:54 -0700565 bExit = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566 }
567
568 i++;
569 j++;
570 break;
571 }
572 }
573
574 if (bPm)
575 nHour += 12;
576
577 if (nYear >= 0 && nYear <= nYearSub)
578 nYear += 2000;
579
580 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800581 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582
583 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800584 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585
586 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800587 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588
589 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800590 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591
592 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800593 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594
595 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800596 if (bBadFormat) {
597 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598 } else {
599 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
600 JS_MakeTime(nHour, nMin, nSec, 0));
tsepez018935c2016-04-15 13:15:12 -0700601 if (JS_PortIsNan(dRet))
602 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 }
604
tsepez018935c2016-04-15 13:15:12 -0700605 if (JS_PortIsNan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800606 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700607
Lei Zhang9559b7a2015-12-21 11:12:20 -0800608 if (bWrongFormat)
609 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700610
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 return dRet;
612}
613
614CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
615 const CFX_WideString& format) {
616 CFX_WideString sRet = L"", sPart = L"";
617
618 int nYear = JS_GetYearFromTime(dDate);
619 int nMonth = JS_GetMonthFromTime(dDate) + 1;
620 int nDay = JS_GetDayFromTime(dDate);
621 int nHour = JS_GetHourFromTime(dDate);
622 int nMin = JS_GetMinFromTime(dDate);
623 int nSec = JS_GetSecFromTime(dDate);
624
625 int i = 0;
626 while (i < format.GetLength()) {
627 FX_WCHAR c = format.GetAt(i);
628 int remaining = format.GetLength() - i - 1;
629 sPart = L"";
630 switch (c) {
631 case 'y':
632 case 'm':
633 case 'd':
634 case 'H':
635 case 'h':
636 case 'M':
637 case 's':
638 case 't':
639 if (remaining == 0 || format.GetAt(i + 1) != c) {
640 switch (c) {
641 case 'y':
642 sPart += c;
643 break;
644 case 'm':
645 sPart.Format(L"%d", nMonth);
646 break;
647 case 'd':
648 sPart.Format(L"%d", nDay);
649 break;
650 case 'H':
651 sPart.Format(L"%d", nHour);
652 break;
653 case 'h':
654 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
655 break;
656 case 'M':
657 sPart.Format(L"%d", nMin);
658 break;
659 case 's':
660 sPart.Format(L"%d", nSec);
661 break;
662 case 't':
663 sPart += nHour > 12 ? 'p' : 'a';
664 break;
665 }
666 i++;
667 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
668 switch (c) {
669 case 'y':
670 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
671 break;
672 case 'm':
673 sPart.Format(L"%02d", nMonth);
674 break;
675 case 'd':
676 sPart.Format(L"%02d", nDay);
677 break;
678 case 'H':
679 sPart.Format(L"%02d", nHour);
680 break;
681 case 'h':
682 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
683 break;
684 case 'M':
685 sPart.Format(L"%02d", nMin);
686 break;
687 case 's':
688 sPart.Format(L"%02d", nSec);
689 break;
690 case 't':
691 sPart = nHour > 12 ? L"pm" : L"am";
692 break;
693 }
694 i += 2;
695 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
696 switch (c) {
697 case 'm':
698 i += 3;
699 if (nMonth > 0 && nMonth <= 12)
700 sPart += months[nMonth - 1];
701 break;
702 default:
703 i += 3;
704 sPart += c;
705 sPart += c;
706 sPart += c;
707 break;
708 }
709 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
710 switch (c) {
711 case 'y':
712 sPart.Format(L"%04d", nYear);
713 i += 4;
714 break;
715 case 'm':
716 i += 4;
717 if (nMonth > 0 && nMonth <= 12)
718 sPart += fullmonths[nMonth - 1];
719 break;
720 default:
721 i += 4;
722 sPart += c;
723 sPart += c;
724 sPart += c;
725 sPart += c;
726 break;
727 }
728 } else {
729 i++;
730 sPart += c;
731 }
732 break;
733 default:
734 i++;
735 sPart += c;
736 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700737 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700738
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739 sRet += sPart;
740 }
741
742 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700743}
744
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
746// bCurrencyPrepend)
tsepez4cf55152016-11-02 14:37:54 -0700747bool CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
748 const std::vector<CJS_Value>& params,
749 CJS_Value& vRet,
750 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700751#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700752 if (params.size() != 6) {
tsepezcd5dc852016-09-08 11:23:24 -0700753 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700754 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700755 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700756
tsepezcd5dc852016-09-08 11:23:24 -0700757 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
758 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Tom Sepez67fd5df2015-10-08 12:24:19 -0700759 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700760 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700761 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700762
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763 CFX_WideString& Value = pEvent->Value();
764 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700766 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767
tsepezb4694242016-08-15 16:44:55 -0700768 int iDec = params[0].ToInt(pRuntime);
769 int iSepStyle = params[1].ToInt(pRuntime);
770 int iNegStyle = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771 // params[3] is iCurrStyle, it's not used.
tsepezb4694242016-08-15 16:44:55 -0700772 CFX_WideString wstrCurrency = params[4].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700773 bool bCurrencyPrepend = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700774
775 if (iDec < 0)
776 iDec = -iDec;
777
778 if (iSepStyle < 0 || iSepStyle > 3)
779 iSepStyle = 0;
780
781 if (iNegStyle < 0 || iNegStyle > 3)
782 iNegStyle = 0;
783
npm49c59282016-11-15 15:14:04 -0800784 // Processing decimal places
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700785 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700786 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700787 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700788 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700789
npm49c59282016-11-15 15:14:04 -0800790 // Calculating number string
791 bool bNegative;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792 int iDec2;
npm49c59282016-11-15 15:14:04 -0800793 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794 if (strValue.IsEmpty()) {
795 dValue = 0;
npm49c59282016-11-15 15:14:04 -0800796 strValue = CalculateString(dValue, iDec, &iDec2, &bNegative);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797 if (strValue.IsEmpty()) {
798 strValue = "0";
799 iDec2 = 1;
800 }
801 }
802
npm49c59282016-11-15 15:14:04 -0800803 // Processing separator style
804 if (iDec2 < strValue.GetLength()) {
805 if (iSepStyle == 2 || iSepStyle == 3)
806 strValue.Replace(".", ",");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700807
808 if (iDec2 == 0)
809 strValue.Insert(iDec2, '0');
810 }
811 if (iSepStyle == 0 || iSepStyle == 2) {
npm49c59282016-11-15 15:14:04 -0800812 char cSeparator;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700813 if (iSepStyle == 0)
npm49c59282016-11-15 15:14:04 -0800814 cSeparator = ',';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815 else
npm49c59282016-11-15 15:14:04 -0800816 cSeparator = '.';
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817
npm49c59282016-11-15 15:14:04 -0800818 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3)
819 strValue.Insert(iDecPositive, cSeparator);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820 }
821
npm49c59282016-11-15 15:14:04 -0800822 // Processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700823 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700824
825 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700826 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827 else
thestigcf03f8e2016-05-09 12:36:18 -0700828 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829
npm49c59282016-11-15 15:14:04 -0800830 // Processing negative style
831 if (bNegative) {
832 if (iNegStyle == 0)
thestigcf03f8e2016-05-09 12:36:18 -0700833 Value = L"-" + Value;
npm49c59282016-11-15 15:14:04 -0800834 else if (iNegStyle == 2 || iNegStyle == 3)
thestigcf03f8e2016-05-09 12:36:18 -0700835 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836 if (iNegStyle == 1 || iNegStyle == 3) {
837 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700838 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700839 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700840 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700841 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700842 vColElm = CJS_Value(pRuntime, 1);
tsepezb4694242016-08-15 16:44:55 -0700843 arColor.SetElement(pRuntime, 1, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700844 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700845 arColor.SetElement(pRuntime, 2, vColElm);
846 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700847
Tom Sepez67fd5df2015-10-08 12:24:19 -0700848 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849 vProp.StartGetting();
850 vProp << arColor;
851 vProp.StartSetting();
852 fTarget->textColor(cc, vProp, sError); // red
853 }
854 }
855 } else {
856 if (iNegStyle == 1 || iNegStyle == 3) {
857 if (Field* fTarget = pEvent->Target_Field()) {
tsepeze5aff742016-08-08 09:49:42 -0700858 CJS_Array arColor;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700859 CJS_Value vColElm(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700860 vColElm = CJS_Value(pRuntime, L"RGB");
tsepezb4694242016-08-15 16:44:55 -0700861 arColor.SetElement(pRuntime, 0, vColElm);
tsepezf3dc8c62016-08-10 06:29:29 -0700862 vColElm = CJS_Value(pRuntime, 0);
tsepezb4694242016-08-15 16:44:55 -0700863 arColor.SetElement(pRuntime, 1, vColElm);
864 arColor.SetElement(pRuntime, 2, vColElm);
865 arColor.SetElement(pRuntime, 3, vColElm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866
Tom Sepez67fd5df2015-10-08 12:24:19 -0700867 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700868 vProp.StartGetting();
869 fTarget->textColor(cc, vProp, sError);
870
tsepeze5aff742016-08-08 09:49:42 -0700871 CJS_Array aProp;
tsepezb4694242016-08-15 16:44:55 -0700872 vProp.GetJSValue()->ConvertToArray(pRuntime, aProp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873
874 CPWL_Color crProp;
875 CPWL_Color crColor;
tsepeze5aff742016-08-08 09:49:42 -0700876 color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp);
877 color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878
879 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700880 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 vProp2.StartGetting();
882 vProp2 << arColor;
883 vProp2.StartSetting();
884 fTarget->textColor(cc, vProp2, sError);
885 }
886 }
887 }
888 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889#endif
tsepez4cf55152016-11-02 14:37:54 -0700890 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700891}
892
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
894// bCurrencyPrepend)
tsepez4cf55152016-11-02 14:37:54 -0700895bool CJS_PublicMethods::AFNumber_Keystroke(IJS_Context* cc,
896 const std::vector<CJS_Value>& params,
897 CJS_Value& vRet,
898 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700899 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700901
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700903 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700904
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700906 return false;
thestigcf03f8e2016-05-09 12:36:18 -0700907
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 CFX_WideString& val = pEvent->Value();
thestigcf03f8e2016-05-09 12:36:18 -0700909 CFX_WideString& wstrChange = pEvent->Change();
910 CFX_WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700911
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912 if (pEvent->WillCommit()) {
ochanga0a3bc32016-05-12 15:22:48 -0700913 CFX_WideString swTemp = StrTrim(wstrValue);
914 if (swTemp.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700915 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700916
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 swTemp.Replace(L",", L".");
918 if (!IsNumber(swTemp.c_str())) {
tsepez4cf55152016-11-02 14:37:54 -0700919 pEvent->Rc() = false;
tsepezcd5dc852016-09-08 11:23:24 -0700920 sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE);
tsepeze1e7bd02016-08-08 13:03:16 -0700921 AlertIfPossible(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700922 }
tsepez4cf55152016-11-02 14:37:54 -0700923 return true; // it happens after the last keystroke and before validating,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700925
thestigcf03f8e2016-05-09 12:36:18 -0700926 CFX_WideString wstrSelected;
927 if (pEvent->SelStart() != -1) {
928 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
929 pEvent->SelEnd() - pEvent->SelStart());
930 }
931
932 bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700933 if (bHasSign) {
934 // can't insert "change" in front to sign postion.
935 if (pEvent->SelStart() == 0) {
tsepez4cf55152016-11-02 14:37:54 -0700936 bool& bRc = pEvent->Rc();
937 bRc = false;
938 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700939 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700940 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700941
tsepezf3dc8c62016-08-10 06:29:29 -0700942 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -0700943 int iSepStyle = params[1].ToInt(pRuntime);
thestigcf03f8e2016-05-09 12:36:18 -0700944 if (iSepStyle < 0 || iSepStyle > 3)
945 iSepStyle = 0;
946 const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700947
thestigcf03f8e2016-05-09 12:36:18 -0700948 bool bHasSep = wstrValue.Find(cSep) != -1;
949 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
950 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 if (bHasSep) {
tsepez4cf55152016-11-02 14:37:54 -0700952 bool& bRc = pEvent->Rc();
953 bRc = false;
954 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955 }
tsepez4cf55152016-11-02 14:37:54 -0700956 bHasSep = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 continue;
958 }
thestigcf03f8e2016-05-09 12:36:18 -0700959 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 if (bHasSign) {
tsepez4cf55152016-11-02 14:37:54 -0700961 bool& bRc = pEvent->Rc();
962 bRc = false;
963 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800965 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700966 if (i != 0) {
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 }
971 if (pEvent->SelStart() != 0) {
tsepez4cf55152016-11-02 14:37:54 -0700972 bool& bRc = pEvent->Rc();
973 bRc = false;
974 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 }
tsepez4cf55152016-11-02 14:37:54 -0700976 bHasSign = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 continue;
978 }
979
thestigcf03f8e2016-05-09 12:36:18 -0700980 if (!FXSYS_iswdigit(wstrChange[i])) {
tsepez4cf55152016-11-02 14:37:54 -0700981 bool& bRc = pEvent->Rc();
982 bRc = false;
983 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 }
985 }
986
thestigcf03f8e2016-05-09 12:36:18 -0700987 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
988 CFX_WideString wpostfix;
989 if (pEvent->SelEnd() < wstrValue.GetLength())
990 wpostfix = wstrValue.Mid(pEvent->SelEnd());
991 val = wprefix + wstrChange + wpostfix;
tsepez4cf55152016-11-02 14:37:54 -0700992 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993}
994
995// function AFPercent_Format(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -0700996bool CJS_PublicMethods::AFPercent_Format(IJS_Context* cc,
997 const std::vector<CJS_Value>& params,
998 CJS_Value& vRet,
999 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000#if _FX_OS_ != _FX_ANDROID_
tsepezcd5dc852016-09-08 11:23:24 -07001001 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001002 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004
1005 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001006 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001007 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008 }
1009 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001010 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011
1012 CFX_WideString& Value = pEvent->Value();
1013 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1014 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001015 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016
tsepezb4694242016-08-15 16:44:55 -07001017 int iDec = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 if (iDec < 0)
1019 iDec = -iDec;
1020
tsepezb4694242016-08-15 16:44:55 -07001021 int iSepStyle = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 if (iSepStyle < 0 || iSepStyle > 3)
1023 iSepStyle = 0;
1024
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001026 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027 dValue *= 100;
1028 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001029 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030
1031 int iDec2;
1032 int iNegative = 0;
1033 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1034 if (strValue.IsEmpty()) {
1035 dValue = 0;
1036 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1037 }
1038
1039 if (iDec2 < 0) {
1040 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1041 strValue = "0" + strValue;
1042 }
1043 iDec2 = 0;
1044 }
1045 int iMax = strValue.GetLength();
1046 if (iDec2 > iMax) {
1047 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1048 strValue += "0";
1049 }
1050 iMax = iDec2 + 1;
1051 }
dsinclair64376be2016-03-31 20:03:24 -07001052
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 // for processing seperator style
1054 if (iDec2 < iMax) {
1055 if (iSepStyle == 0 || iSepStyle == 1) {
1056 strValue.Insert(iDec2, '.');
1057 iMax++;
1058 } else if (iSepStyle == 2 || iSepStyle == 3) {
1059 strValue.Insert(iDec2, ',');
1060 iMax++;
1061 }
1062
1063 if (iDec2 == 0)
1064 strValue.Insert(iDec2, '0');
1065 }
1066 if (iSepStyle == 0 || iSepStyle == 2) {
1067 char cSeperator;
1068 if (iSepStyle == 0)
1069 cSeperator = ',';
1070 else
1071 cSeperator = '.';
1072
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001073 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074 strValue.Insert(iDecPositive, cSeperator);
1075 iMax++;
1076 }
1077 }
dsinclair64376be2016-03-31 20:03:24 -07001078
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079 // negative mark
1080 if (iNegative)
1081 strValue = "-" + strValue;
1082 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001083 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084#endif
tsepez4cf55152016-11-02 14:37:54 -07001085 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086}
1087// AFPercent_Keystroke(nDec, sepStyle)
tsepez4cf55152016-11-02 14:37:54 -07001088bool CJS_PublicMethods::AFPercent_Keystroke(
Lei Zhang945fdb72015-11-11 10:18:16 -08001089 IJS_Context* cc,
1090 const std::vector<CJS_Value>& params,
1091 CJS_Value& vRet,
1092 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 return AFNumber_Keystroke(cc, params, vRet, sError);
1094}
1095
1096// function AFDate_FormatEx(cFormat)
tsepez4cf55152016-11-02 14:37:54 -07001097bool CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
1098 const std::vector<CJS_Value>& params,
1099 CJS_Value& vRet,
1100 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -07001101 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001102 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001103 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104
1105 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001106 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001107 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001108 }
1109 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001110 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111
1112 CFX_WideString& val = pEvent->Value();
1113 CFX_WideString strValue = val;
1114 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001115 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116
tsepezb4694242016-08-15 16:44:55 -07001117 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 double dDate = 0.0f;
1119
1120 if (strValue.Find(L"GMT") != -1) {
1121 // for GMT format time
1122 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1123 dDate = MakeInterDate(strValue);
1124 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001125 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 }
1127
1128 if (JS_PortIsNan(dDate)) {
1129 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001130 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001131 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001132 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001133 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134 }
1135
1136 val = MakeFormatDate(dDate, sFormat);
tsepez4cf55152016-11-02 14:37:54 -07001137 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138}
1139
tsepez745611b2016-04-12 16:46:34 -07001140double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001141 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001143 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001144 FX_WCHAR c = strValue.GetAt(i);
1145 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)
tsepez4cf55152016-11-02 14:37:54 -07001197bool CJS_PublicMethods::AFDate_KeystrokeEx(IJS_Context* cc,
1198 const std::vector<CJS_Value>& params,
1199 CJS_Value& vRet,
1200 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 CJS_Context* pContext = (CJS_Context*)cc;
tsepezf3dc8c62016-08-10 06:29:29 -07001202 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001203 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204
1205 if (params.size() != 1) {
1206 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
tsepez4cf55152016-11-02 14:37:54 -07001207 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001208 }
1209
1210 if (pEvent->WillCommit()) {
1211 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001212 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213 CFX_WideString strValue = pEvent->Value();
1214 if (strValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001215 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001216
tsepezb4694242016-08-15 16:44:55 -07001217 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001218 bool bWrongFormat = false;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001219 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 if (bWrongFormat || JS_PortIsNan(dRet)) {
1221 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001222 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001223 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001224 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001225 pEvent->Rc() = false;
1226 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001227 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001228 }
tsepez4cf55152016-11-02 14:37:54 -07001229 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001230}
1231
tsepez4cf55152016-11-02 14:37:54 -07001232bool CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
1233 const std::vector<CJS_Value>& params,
1234 CJS_Value& vRet,
1235 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001236 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001237 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001238 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001239 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001240
tsepezf3dc8c62016-08-10 06:29:29 -07001241 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001242 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001243 const FX_WCHAR* cFormats[] = {L"m/d",
1244 L"m/d/yy",
1245 L"mm/dd/yy",
1246 L"mm/yy",
1247 L"d-mmm",
1248 L"d-mmm-yy",
1249 L"dd-mmm-yy",
1250 L"yy-mm-dd",
1251 L"mmm-yy",
1252 L"mmmm-yy",
1253 L"mmm d, yyyy",
1254 L"mmmm d, yyyy",
1255 L"m/d/yy h:MM tt",
1256 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001257
Lei Zhanga0f67242015-08-17 15:39:30 -07001258 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1259 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001260
Lei Zhang945fdb72015-11-11 10:18:16 -08001261 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001262 newParams.push_back(
1263 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264 return AFDate_FormatEx(cc, newParams, vRet, sError);
1265}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001266
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267// AFDate_KeystrokeEx(cFormat)
tsepez4cf55152016-11-02 14:37:54 -07001268bool CJS_PublicMethods::AFDate_Keystroke(IJS_Context* cc,
1269 const std::vector<CJS_Value>& params,
1270 CJS_Value& vRet,
1271 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001272 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001273 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001274 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001275 }
1276
tsepezf3dc8c62016-08-10 06:29:29 -07001277 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001278 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001279 const FX_WCHAR* cFormats[] = {L"m/d",
1280 L"m/d/yy",
1281 L"mm/dd/yy",
1282 L"mm/yy",
1283 L"d-mmm",
1284 L"d-mmm-yy",
1285 L"dd-mmm-yy",
1286 L"yy-mm-dd",
1287 L"mmm-yy",
1288 L"mmmm-yy",
1289 L"mmm d, yyyy",
1290 L"mmmm d, yyyy",
1291 L"m/d/yy h:MM tt",
1292 L"m/d/yy HH:MM"};
1293
Lei Zhanga0f67242015-08-17 15:39:30 -07001294 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1295 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296
Lei Zhang945fdb72015-11-11 10:18:16 -08001297 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001298 newParams.push_back(
1299 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1301}
1302
1303// function AFTime_Format(ptf)
tsepez4cf55152016-11-02 14:37:54 -07001304bool CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
1305 const std::vector<CJS_Value>& params,
1306 CJS_Value& vRet,
1307 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001308 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001309 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001310 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311 }
1312
tsepezf3dc8c62016-08-10 06:29:29 -07001313 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001314 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001315 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1316 L"h:MM:ss tt"};
1317
Lei Zhanga0f67242015-08-17 15:39:30 -07001318 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1319 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320
Lei Zhang945fdb72015-11-11 10:18:16 -08001321 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001322 newParams.push_back(
1323 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324 return AFDate_FormatEx(cc, newParams, vRet, sError);
1325}
1326
tsepez4cf55152016-11-02 14:37:54 -07001327bool CJS_PublicMethods::AFTime_Keystroke(IJS_Context* cc,
1328 const std::vector<CJS_Value>& params,
1329 CJS_Value& vRet,
1330 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001331 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001332 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001333 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001334 }
1335
tsepezf3dc8c62016-08-10 06:29:29 -07001336 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001337 int iIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001338 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1339 L"h:MM:ss tt"};
1340
Lei Zhanga0f67242015-08-17 15:39:30 -07001341 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1342 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001343
Lei Zhang945fdb72015-11-11 10:18:16 -08001344 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001345 newParams.push_back(
1346 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001347 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1348}
1349
tsepez4cf55152016-11-02 14:37:54 -07001350bool CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
1351 const std::vector<CJS_Value>& params,
1352 CJS_Value& vRet,
1353 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354 return AFDate_FormatEx(cc, params, vRet, sError);
1355}
1356
tsepez4cf55152016-11-02 14:37:54 -07001357bool CJS_PublicMethods::AFTime_KeystrokeEx(IJS_Context* cc,
1358 const std::vector<CJS_Value>& params,
1359 CJS_Value& vRet,
1360 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1362}
1363
1364// function AFSpecial_Format(psf)
tsepez4cf55152016-11-02 14:37:54 -07001365bool CJS_PublicMethods::AFSpecial_Format(IJS_Context* cc,
1366 const std::vector<CJS_Value>& params,
1367 CJS_Value& vRet,
1368 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001370 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001371 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 }
1373
tsepezcd5dc852016-09-08 11:23:24 -07001374 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001377 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378
tsepezf3dc8c62016-08-10 06:29:29 -07001379 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez4f1f41f2016-03-28 14:13:16 -07001380 CFX_WideString wsSource = pEvent->Value();
1381 CFX_WideString wsFormat;
tsepezb4694242016-08-15 16:44:55 -07001382 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001383 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001384 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001385 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001386 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001387 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001389 case 2:
1390 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1391 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001392 else
tsepez4f1f41f2016-03-28 14:13:16 -07001393 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001395 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001396 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 break;
1398 }
1399
tsepez4f1f41f2016-03-28 14:13:16 -07001400 pEvent->Value() = util::printx(wsFormat, wsSource);
tsepez4cf55152016-11-02 14:37:54 -07001401 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001402}
1403
1404// function AFSpecial_KeystrokeEx(mask)
tsepez4cf55152016-11-02 14:37:54 -07001405bool CJS_PublicMethods::AFSpecial_KeystrokeEx(
Lei Zhang945fdb72015-11-11 10:18:16 -08001406 IJS_Context* cc,
1407 const std::vector<CJS_Value>& params,
1408 CJS_Value& vRet,
1409 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001410 CJS_Context* pContext = (CJS_Context*)cc;
tsepezf3dc8c62016-08-10 06:29:29 -07001411 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001412 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1413
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001414 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001415 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001416 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001417 }
1418
1419 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001420 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421
tsepezcd5dc852016-09-08 11:23:24 -07001422 CFX_WideString& valEvent = pEvent->Value();
tsepezb4694242016-08-15 16:44:55 -07001423 CFX_WideString wstrMask = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424 if (wstrMask.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001425 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001426
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001428 if (valEvent.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001429 return true;
thestigcf03f8e2016-05-09 12:36:18 -07001430
1431 FX_STRSIZE iIndexMask = 0;
1432 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1433 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001434 break;
1435 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001436
thestigcf03f8e2016-05-09 12:36:18 -07001437 if (iIndexMask != wstrMask.GetLength() ||
1438 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
tsepeze1e7bd02016-08-08 13:03:16 -07001439 AlertIfPossible(
tsepezcd5dc852016-09-08 11:23:24 -07001440 pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001441 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001442 }
tsepez4cf55152016-11-02 14:37:54 -07001443 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001444 }
1445
1446 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001447 if (wideChange.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001448 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001449
thestigcf03f8e2016-05-09 12:36:18 -07001450 CFX_WideString wChange = wideChange;
1451 FX_STRSIZE iIndexMask = pEvent->SelStart();
1452 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1453 pEvent->SelStart() - pEvent->SelEnd();
1454 if (combined_len > wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001455 AlertIfPossible(pContext,
1456 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001457 pEvent->Rc() = false;
1458 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001459 }
1460
thestigcf03f8e2016-05-09 12:36:18 -07001461 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
tsepezcd5dc852016-09-08 11:23:24 -07001462 AlertIfPossible(pContext,
1463 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001464 pEvent->Rc() = false;
1465 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001466 }
1467
thestigcf03f8e2016-05-09 12:36:18 -07001468 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1469 if (iIndexMask >= wstrMask.GetLength()) {
tsepezcd5dc852016-09-08 11:23:24 -07001470 AlertIfPossible(pContext,
1471 JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001472 pEvent->Rc() = false;
1473 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 }
thestigcf03f8e2016-05-09 12:36:18 -07001475 FX_WCHAR wMask = wstrMask[iIndexMask];
1476 if (!isReservedMaskChar(wMask))
1477 wChange.SetAt(i, wMask);
1478
1479 if (!maskSatisfied(wChange[i], wMask)) {
tsepez4cf55152016-11-02 14:37:54 -07001480 pEvent->Rc() = false;
1481 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001482 }
1483 iIndexMask++;
1484 }
thestigcf03f8e2016-05-09 12:36:18 -07001485 wideChange = wChange;
tsepez4cf55152016-11-02 14:37:54 -07001486 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001487}
1488
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489// function AFSpecial_Keystroke(psf)
tsepez4cf55152016-11-02 14:37:54 -07001490bool CJS_PublicMethods::AFSpecial_Keystroke(
Lei Zhang945fdb72015-11-11 10:18:16 -08001491 IJS_Context* cc,
1492 const std::vector<CJS_Value>& params,
1493 CJS_Value& vRet,
1494 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001496 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001497 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001499
tsepezcd5dc852016-09-08 11:23:24 -07001500 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001501 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001503 return false;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001504
thestigcf03f8e2016-05-09 12:36:18 -07001505 const char* cFormat = "";
tsepezf3dc8c62016-08-10 06:29:29 -07001506 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001507 switch (params[0].ToInt(pRuntime)) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001508 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509 cFormat = "99999";
1510 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001511 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512 cFormat = "999999999";
1513 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001514 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001515 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516 cFormat = "9999999999";
1517 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518 cFormat = "9999999";
1519 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001520 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 cFormat = "999999999";
1522 break;
1523 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001524
Lei Zhang945fdb72015-11-11 10:18:16 -08001525 std::vector<CJS_Value> params2;
thestigcf03f8e2016-05-09 12:36:18 -07001526 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001528}
1529
tsepez4cf55152016-11-02 14:37:54 -07001530bool CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
1531 const std::vector<CJS_Value>& params,
1532 CJS_Value& vRet,
1533 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001534 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001535 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001536 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001537 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001538
tsepezcd5dc852016-09-08 11:23:24 -07001539 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1540 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1541 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
1542
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001544 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001545 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001546
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001547 if (pEventHandler->WillCommit()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001548 vRet = CJS_Value(pRuntime, swValue.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001549 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001550 }
1551
1552 CFX_WideString prefix, postfix;
1553
1554 if (pEventHandler->SelStart() >= 0)
1555 prefix = swValue.Mid(0, pEventHandler->SelStart());
1556 else
1557 prefix = L"";
1558
1559 if (pEventHandler->SelEnd() >= 0 &&
1560 pEventHandler->SelEnd() <= swValue.GetLength())
1561 postfix = swValue.Mid(pEventHandler->SelEnd(),
1562 swValue.GetLength() - pEventHandler->SelEnd());
1563 else
1564 postfix = L"";
1565
tsepezf3dc8c62016-08-10 06:29:29 -07001566 vRet =
1567 CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001568 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001569}
1570
tsepez4cf55152016-11-02 14:37:54 -07001571bool CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
1572 const std::vector<CJS_Value>& params,
1573 CJS_Value& vRet,
1574 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001575 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001576 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001577 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001578 }
1579
tsepezcd5dc852016-09-08 11:23:24 -07001580 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001581 CFX_WideString sValue = params[0].ToCFXWideString(pRuntime);
1582 CFX_WideString sFormat = params[1].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001583
Lei Zhang9559b7a2015-12-21 11:12:20 -08001584 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001585
1586 if (JS_PortIsNan(dDate)) {
1587 CFX_WideString swMsg;
tsepezcd5dc852016-09-08 11:23:24 -07001588 swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001589 sFormat.c_str());
tsepeze1e7bd02016-08-08 13:03:16 -07001590 AlertIfPossible((CJS_Context*)cc, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001591 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001592 }
1593
tsepezf3dc8c62016-08-10 06:29:29 -07001594 vRet = CJS_Value(pRuntime, dDate);
tsepez4cf55152016-11-02 14:37:54 -07001595 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001596}
1597
tsepez4cf55152016-11-02 14:37:54 -07001598bool CJS_PublicMethods::AFSimple(IJS_Context* cc,
1599 const std::vector<CJS_Value>& params,
1600 CJS_Value& vRet,
1601 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -07001602 if (params.size() != 3) {
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 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001606
tsepezcd5dc852016-09-08 11:23:24 -07001607 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001608 vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple(
1609 params[0].ToCFXWideString(pRuntime).c_str(),
1610 params[1].ToDouble(pRuntime),
1611 params[2].ToDouble(pRuntime))));
tsepezf3dc8c62016-08-10 06:29:29 -07001612
tsepez4cf55152016-11-02 14:37:54 -07001613 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001614}
1615
tsepez4cf55152016-11-02 14:37:54 -07001616bool CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
1617 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() != 1) {
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 }
tsepezf3dc8c62016-08-10 06:29:29 -07001624
tsepezcd5dc852016-09-08 11:23:24 -07001625 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001626 CFX_WideString ws = params[0].ToCFXWideString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001627 ws.Replace(L",", L".");
tsepezf3dc8c62016-08-10 06:29:29 -07001628 vRet = CJS_Value(pRuntime, ws.c_str());
tsepezb4694242016-08-15 16:44:55 -07001629 vRet.MaybeCoerceToNumber(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -08001630 if (vRet.GetType() != CJS_Value::VT_number)
tsepezf3dc8c62016-08-10 06:29:29 -07001631 vRet = CJS_Value(pRuntime, 0);
tsepez4cf55152016-11-02 14:37:54 -07001632 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001633}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001634
tsepez4cf55152016-11-02 14:37:54 -07001635bool CJS_PublicMethods::AFSimple_Calculate(IJS_Context* cc,
1636 const std::vector<CJS_Value>& params,
1637 CJS_Value& vRet,
1638 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001639 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001640 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001641 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001642 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001643
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001644 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001645 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
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
tsepezcd5dc852016-09-08 11:23:24 -07001650 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1651 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
dsinclair4526faf2016-10-11 10:54:49 -07001652 CPDFSDK_InterForm* pReaderInterForm =
dsinclair7cbe68e2016-10-12 11:56:23 -07001653 pContext->GetFormFillEnv()->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001654 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001655
tsepezb4694242016-08-15 16:44:55 -07001656 CFX_WideString sFunction = params[0].ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001657 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001658
Tom Sepez67fd5df2015-10-08 12:24:19 -07001659 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001660 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001661
tsepezb4694242016-08-15 16:44:55 -07001662 for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001663 CJS_Value jsValue(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001664 FieldNameArray.GetElement(pRuntime, i, jsValue);
1665 CFX_WideString wsFieldName = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001666
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001667 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1668 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1669 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001670 switch (pFormField->GetFieldType()) {
1671 case FIELDTYPE_TEXTFIELD:
1672 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001673 CFX_WideString trimmed = pFormField->GetValue();
1674 trimmed.TrimRight();
1675 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001676 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001677 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001678 case FIELDTYPE_PUSHBUTTON: {
1679 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001680 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001681 case FIELDTYPE_CHECKBOX:
1682 case FIELDTYPE_RADIOBUTTON: {
1683 dTemp = 0.0;
1684 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1685 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1686 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001687 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1688 trimmed.TrimRight();
1689 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001690 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001691 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001692 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001693 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001694 }
Tom Sepez4246b002016-01-20 11:48:29 -08001695 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001696 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001697 if (pFormField->CountSelectedItems() <= 1) {
1698 CFX_WideString trimmed = pFormField->GetValue();
1699 trimmed.TrimRight();
1700 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001701 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001702 }
1703 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001704 default:
1705 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001706 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001707
1708 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1709 wcscmp(sFunction.c_str(), L"MAX") == 0))
1710 dValue = dTemp;
1711
1712 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1713
1714 nFieldsCount++;
1715 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001716 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001717 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001718
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001719 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1720 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001721
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001722 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1723 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001724 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001725 if (pContext->GetEventHandler()->m_pValue)
tsepezb4694242016-08-15 16:44:55 -07001726 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001727
tsepez4cf55152016-11-02 14:37:54 -07001728 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001729}
1730
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001731/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001732** within the specified range. */
1733
tsepez4cf55152016-11-02 14:37:54 -07001734bool CJS_PublicMethods::AFRange_Validate(IJS_Context* cc,
1735 const std::vector<CJS_Value>& params,
1736 CJS_Value& vRet,
1737 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001738 if (params.size() != 4) {
tsepezcd5dc852016-09-08 11:23:24 -07001739 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001740 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001741 }
tsepezcd5dc852016-09-08 11:23:24 -07001742 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
1743 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1744 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001745 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -07001746 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001747
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001748 if (pEvent->Value().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001749 return true;
tsepezcd5dc852016-09-08 11:23:24 -07001750
tsepezb4c9f3f2016-04-13 15:41:21 -07001751 double dEentValue =
1752 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
tsepez4cf55152016-11-02 14:37:54 -07001753 bool bGreaterThan = params[0].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001754 double dGreaterThan = params[1].ToDouble(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001755 bool bLessThan = params[2].ToBool(pRuntime);
tsepezb4694242016-08-15 16:44:55 -07001756 double dLessThan = params[3].ToDouble(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001757 CFX_WideString swMsg;
1758
1759 if (bGreaterThan && bLessThan) {
1760 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001761 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001762 params[1].ToCFXWideString(pRuntime).c_str(),
1763 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001764 } else if (bGreaterThan) {
1765 if (dEentValue < dGreaterThan)
tsepezcd5dc852016-09-08 11:23:24 -07001766 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001767 params[1].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001768 } else if (bLessThan) {
1769 if (dEentValue > dLessThan)
tsepezcd5dc852016-09-08 11:23:24 -07001770 swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(),
tsepezb4694242016-08-15 16:44:55 -07001771 params[3].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001772 }
1773
1774 if (!swMsg.IsEmpty()) {
tsepeze1e7bd02016-08-08 13:03:16 -07001775 AlertIfPossible(pContext, swMsg.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001776 pEvent->Rc() = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001777 }
tsepez4cf55152016-11-02 14:37:54 -07001778 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001779}
1780
tsepez4cf55152016-11-02 14:37:54 -07001781bool CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
1782 const std::vector<CJS_Value>& params,
1783 CJS_Value& vRet,
1784 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001785 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001786 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001787 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001788 }
1789
tsepezcd5dc852016-09-08 11:23:24 -07001790 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001791 CFX_WideString str = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001792 CFX_WideString sPart;
tsepezcd5dc852016-09-08 11:23:24 -07001793 CJS_Array nums;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001794
1795 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1796 str = L"0" + str;
1797
1798 int nIndex = 0;
1799 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1800 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001801 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001802 sPart += wc;
1803 } else {
1804 if (sPart.GetLength() > 0) {
tsepezb4694242016-08-15 16:44:55 -07001805 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001806 sPart = L"";
1807 nIndex++;
1808 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001809 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001810 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001811
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812 if (sPart.GetLength() > 0) {
tsepezb4694242016-08-15 16:44:55 -07001813 nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001814 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001815
tsepezb4694242016-08-15 16:44:55 -07001816 if (nums.GetLength(pRuntime) > 0)
tsepeze5aff742016-08-08 09:49:42 -07001817 vRet = CJS_Value(pRuntime, nums);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001818 else
tsepezf3dc8c62016-08-10 06:29:29 -07001819 vRet.SetNull(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001820
tsepez4cf55152016-11-02 14:37:54 -07001821 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001822}