blob: 5a694c742a0d2e12cf34ff83c5625cb5e18fa86a [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>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <string>
11#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080012
Dan Sinclaira8a28e02016-03-23 15:41:39 -040013#include "core/fxcrt/include/fx_ext.h"
dsinclair64376be2016-03-31 20:03:24 -070014#include "fpdfsdk/include/fsdk_mgr.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040015#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040016#include "fpdfsdk/javascript/JS_Define.h"
17#include "fpdfsdk/javascript/JS_EventHandler.h"
18#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040019#include "fpdfsdk/javascript/JS_Value.h"
dsinclair64376be2016-03-31 20:03:24 -070020#include "fpdfsdk/javascript/cjs_context.h"
21#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040022#include "fpdfsdk/javascript/color.h"
23#include "fpdfsdk/javascript/resource.h"
24#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
28BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format)
30JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke)
31JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format)
32JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke)
33JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx)
34JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx)
35JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format)
36JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke)
37JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx)
38JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx)
39JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format)
40JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke)
41JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format)
42JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke)
43JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx)
44JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple)
45JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
46JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
47JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
48JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
49JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
50JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051END_JS_STATIC_GLOBAL_FUN()
52
53IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
54
tsepez745611b2016-04-12 16:46:34 -070055namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
tsepez745611b2016-04-12 16:46:34 -070057const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
58 L"May", L"Jun", L"Jul", L"Aug",
59 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060
tsepez745611b2016-04-12 16:46:34 -070061const FX_WCHAR* const fullmonths[] = {L"January", L"February", L"March",
62 L"April", L"May", L"June",
63 L"July", L"August", L"September",
64 L"October", L"November", L"December"};
65
66CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
67 CFX_ByteString result(pStr);
68 result.TrimLeft(' ');
69 result.TrimRight(' ');
70 return result;
71}
72
73CFX_WideString StrTrim(const CFX_WideString& pStr) {
74 CFX_WideString result(pStr);
75 result.TrimLeft(' ');
76 result.TrimRight(' ');
77 return result;
78}
79
80} // namespace
81
82bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -050083 CFX_WideString sTrim = StrTrim(str);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 const FX_WCHAR* pTrim = sTrim.c_str();
85 const FX_WCHAR* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -070086 bool bDot = false;
87 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -070090 while ((c = *p) != L'\0') {
91 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -070093 return false;
94 bDot = true;
95 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -070097 return false;
98 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -0700100 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 p++;
103 c = *p;
Wei Li614d20a2016-03-15 13:55:12 -0700104 if (c == L'+' || c == L'-') {
105 bKXJS = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 } else {
Wei Li614d20a2016-03-15 13:55:12 -0700107 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800109 } else if (!FXSYS_iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700110 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 }
112 p++;
113 }
114
Wei Li614d20a2016-03-15 13:55:12 -0700115 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116}
117
Wei Li614d20a2016-03-15 13:55:12 -0700118bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 switch (c_Mask) {
120 case L'9':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800121 return FXSYS_iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800123 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800125 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700127 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 default:
129 return (c_Change == c_Mask);
130 }
131}
132
Wei Li614d20a2016-03-15 13:55:12 -0700133bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
135}
136
137double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
138 double dValue1,
139 double dValue2) {
140 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
141 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
142 return dValue1 + dValue2;
143 }
144 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
145 return dValue1 * dValue2;
146 }
147 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800148 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 }
150 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800151 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 }
153 return dValue1;
154}
155
Tom Sepez67fd5df2015-10-08 12:24:19 -0700156CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 CJS_Value val) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700158 CJS_Array StrArray(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 if (val.IsArrayObject()) {
160 val.ConvertToArray(StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700161 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 }
163 CFX_WideString wsStr = val.ToCFXWideString();
164 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
165 const char* p = (const char*)t;
166
167 int ch = ',';
168 int nIndex = 0;
169
170 while (*p) {
171 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800172 if (!pTemp) {
tsepez745611b2016-04-12 16:46:34 -0700173 StrArray.SetElement(nIndex,
174 CJS_Value(pRuntime, StrTrim(CFX_ByteString(p))));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 }
Lei Zhang997de612015-11-04 18:17:53 -0800177
178 char* pSub = new char[pTemp - p + 1];
179 strncpy(pSub, p, pTemp - p);
180 *(pSub + (pTemp - p)) = '\0';
181
tsepez745611b2016-04-12 16:46:34 -0700182 StrArray.SetElement(nIndex,
183 CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub))));
Lei Zhang997de612015-11-04 18:17:53 -0800184 delete[] pSub;
185
186 nIndex++;
187 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 }
189 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190}
191
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500192int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 int nStart,
194 int& nSkip,
195 int nMaxStep) {
196 int nRet = 0;
197 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500198 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 if (i - nStart > 10)
200 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500202 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800203 if (!FXSYS_iswdigit(c))
204 break;
205
Dan Sinclair1c915372016-03-03 17:12:58 -0500206 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800207 nSkip = i - nStart + 1;
208 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 break;
210 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213}
214
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500215CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
216 int nStart,
217 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 CFX_WideString swRet;
219 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500220 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
221 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800222 if (!FXSYS_iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800224
225 swRet += c;
226 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230}
231
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800233 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 int nYear = JS_GetYearFromTime(dt);
237 int nMonth = JS_GetMonthFromTime(dt) + 1;
238 int nDay = JS_GetDayFromTime(dt);
239 int nHour = JS_GetHourFromTime(dt);
240 int nMin = JS_GetMinFromTime(dt);
241 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 int nSkip = 0;
246 int nLen = value.GetLength();
247 int nIndex = 0;
248 int i = 0;
249 while (i < nLen) {
250 if (nIndex > 2)
251 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 FX_WCHAR c = value.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800254 if (FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
256 i += nSkip;
257 } else {
258 i++;
259 }
260 }
261
262 if (nIndex == 2) {
263 // case2: month/day
264 // case3: day/month
265 if ((number[0] >= 1 && number[0] <= 12) &&
266 (number[1] >= 1 && number[1] <= 31)) {
267 nMonth = number[0];
268 nDay = number[1];
269 } else if ((number[0] >= 1 && number[0] <= 31) &&
270 (number[1] >= 1 && number[1] <= 12)) {
271 nDay = number[0];
272 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700273 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274
Lei Zhang9559b7a2015-12-21 11:12:20 -0800275 if (bWrongFormat)
276 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 } else if (nIndex == 3) {
278 // case1: year/month/day
279 // case2: month/day/year
280 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700281
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
283 (number[2] >= 1 && number[2] <= 31)) {
284 nYear = number[0];
285 nMonth = number[1];
286 nDay = number[2];
287 } else if ((number[0] >= 1 && number[0] <= 12) &&
288 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
289 nMonth = number[0];
290 nDay = number[1];
291 nYear = number[2];
292 } else if ((number[0] >= 1 && number[0] <= 31) &&
293 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
294 nDay = number[0];
295 nMonth = number[1];
296 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700297 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298
Lei Zhang9559b7a2015-12-21 11:12:20 -0800299 if (bWrongFormat)
300 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800302 if (bWrongFormat)
303 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 return dt;
305 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 CFX_WideString swTemp;
308 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
309 return JS_DateParse(swTemp.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310}
311
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
313 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800314 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700316
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 if (format.IsEmpty() || value.IsEmpty())
318 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 int nYear = JS_GetYearFromTime(dt);
321 int nMonth = JS_GetMonthFromTime(dt) + 1;
322 int nDay = JS_GetDayFromTime(dt);
323 int nHour = JS_GetHourFromTime(dt);
324 int nMin = JS_GetMinFromTime(dt);
325 int nSec = JS_GetSecFromTime(dt);
326
327 int nYearSub = 99; // nYear - 2000;
328
329 FX_BOOL bPm = FALSE;
330 FX_BOOL bExit = FALSE;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800331 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332
333 int i = 0;
334 int j = 0;
335
336 while (i < format.GetLength()) {
337 if (bExit)
338 break;
339
340 FX_WCHAR c = format.GetAt(i);
341 switch (c) {
342 case ':':
343 case '.':
344 case '-':
345 case '\\':
346 case '/':
347 i++;
348 j++;
349 break;
350
351 case 'y':
352 case 'm':
353 case 'd':
354 case 'H':
355 case 'h':
356 case 'M':
357 case 's':
358 case 't': {
359 int oldj = j;
360 int nSkip = 0;
361 int remaining = format.GetLength() - i - 1;
362
363 if (remaining == 0 || format.GetAt(i + 1) != c) {
364 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700365 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 i++;
367 j++;
368 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700369 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 nMonth = ParseStringInteger(value, j, nSkip, 2);
371 i++;
372 j += nSkip;
373 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700374 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 nDay = ParseStringInteger(value, j, nSkip, 2);
376 i++;
377 j += nSkip;
378 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700379 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 nHour = ParseStringInteger(value, j, nSkip, 2);
381 i++;
382 j += nSkip;
383 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700384 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 nHour = ParseStringInteger(value, j, nSkip, 2);
386 i++;
387 j += nSkip;
388 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700389 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 nMin = ParseStringInteger(value, j, nSkip, 2);
391 i++;
392 j += nSkip;
393 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700394 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 nSec = ParseStringInteger(value, j, nSkip, 2);
396 i++;
397 j += nSkip;
398 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700399 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
401 i++;
402 j++;
403 break;
404 }
405 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
406 switch (c) {
407 case 'y':
408 nYear = ParseStringInteger(value, j, nSkip, 4);
409 i += 2;
410 j += nSkip;
411 break;
412 case 'm':
413 nMonth = ParseStringInteger(value, j, nSkip, 2);
414 i += 2;
415 j += nSkip;
416 break;
417 case 'd':
418 nDay = ParseStringInteger(value, j, nSkip, 2);
419 i += 2;
420 j += nSkip;
421 break;
422 case 'H':
423 nHour = ParseStringInteger(value, j, nSkip, 2);
424 i += 2;
425 j += nSkip;
426 break;
427 case 'h':
428 nHour = ParseStringInteger(value, j, nSkip, 2);
429 i += 2;
430 j += nSkip;
431 break;
432 case 'M':
433 nMin = ParseStringInteger(value, j, nSkip, 2);
434 i += 2;
435 j += nSkip;
436 break;
437 case 's':
438 nSec = ParseStringInteger(value, j, nSkip, 2);
439 i += 2;
440 j += nSkip;
441 break;
442 case 't':
443 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
444 value.GetAt(j + 1) == 'm');
445 i += 2;
446 j += 2;
447 break;
448 }
449 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
450 switch (c) {
451 case 'm': {
452 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
453 FX_BOOL bFind = FALSE;
454 for (int m = 0; m < 12; m++) {
455 if (sMonth.CompareNoCase(months[m]) == 0) {
456 nMonth = m + 1;
457 i += 3;
458 j += nSkip;
459 bFind = TRUE;
460 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700461 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 }
463
464 if (!bFind) {
465 nMonth = ParseStringInteger(value, j, nSkip, 3);
466 i += 3;
467 j += nSkip;
468 }
469 } break;
470 case 'y':
471 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700472 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 i += 3;
474 j += 3;
475 break;
476 }
477 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
478 switch (c) {
479 case 'y':
480 nYear = ParseStringInteger(value, j, nSkip, 4);
481 j += nSkip;
482 i += 4;
483 break;
484 case 'm': {
485 FX_BOOL bFind = FALSE;
486
487 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
488 sMonth.MakeLower();
489
490 for (int m = 0; m < 12; m++) {
491 CFX_WideString sFullMonths = fullmonths[m];
492 sFullMonths.MakeLower();
493
494 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
495 nMonth = m + 1;
496 i += 4;
497 j += nSkip;
498 bFind = TRUE;
499 break;
500 }
501 }
502
503 if (!bFind) {
504 nMonth = ParseStringInteger(value, j, nSkip, 4);
505 i += 4;
506 j += nSkip;
507 }
508 } break;
509 default:
510 i += 4;
511 j += 4;
512 break;
513 }
514 } else {
515 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800516 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517 bExit = TRUE;
518 }
519 i++;
520 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700521 }
Tom Sepez85386422014-07-23 10:28:37 -0700522
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800524 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525 bExit = TRUE;
526 }
527 }
528
529 break;
530 default:
531 if (value.GetLength() <= j) {
532 bExit = TRUE;
533 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800534 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535 bExit = TRUE;
536 }
537
538 i++;
539 j++;
540 break;
541 }
542 }
543
544 if (bPm)
545 nHour += 12;
546
547 if (nYear >= 0 && nYear <= nYearSub)
548 nYear += 2000;
549
550 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800551 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552
553 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800554 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555
556 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800557 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558
559 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800560 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561
562 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800563 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564
565 double dRet = 0;
566
Lei Zhang9559b7a2015-12-21 11:12:20 -0800567 if (bBadFormat) {
568 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 } else {
570 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
571 JS_MakeTime(nHour, nMin, nSec, 0));
572
573 if (JS_PortIsNan(dRet)) {
574 dRet = JS_DateParse(value.c_str());
575 }
576 }
577
578 if (JS_PortIsNan(dRet)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800579 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 }
581
Lei Zhang9559b7a2015-12-21 11:12:20 -0800582 if (bWrongFormat)
583 *bWrongFormat = bBadFormat;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 return dRet;
585}
586
587CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
588 const CFX_WideString& format) {
589 CFX_WideString sRet = L"", sPart = L"";
590
591 int nYear = JS_GetYearFromTime(dDate);
592 int nMonth = JS_GetMonthFromTime(dDate) + 1;
593 int nDay = JS_GetDayFromTime(dDate);
594 int nHour = JS_GetHourFromTime(dDate);
595 int nMin = JS_GetMinFromTime(dDate);
596 int nSec = JS_GetSecFromTime(dDate);
597
598 int i = 0;
599 while (i < format.GetLength()) {
600 FX_WCHAR c = format.GetAt(i);
601 int remaining = format.GetLength() - i - 1;
602 sPart = L"";
603 switch (c) {
604 case 'y':
605 case 'm':
606 case 'd':
607 case 'H':
608 case 'h':
609 case 'M':
610 case 's':
611 case 't':
612 if (remaining == 0 || format.GetAt(i + 1) != c) {
613 switch (c) {
614 case 'y':
615 sPart += c;
616 break;
617 case 'm':
618 sPart.Format(L"%d", nMonth);
619 break;
620 case 'd':
621 sPart.Format(L"%d", nDay);
622 break;
623 case 'H':
624 sPart.Format(L"%d", nHour);
625 break;
626 case 'h':
627 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
628 break;
629 case 'M':
630 sPart.Format(L"%d", nMin);
631 break;
632 case 's':
633 sPart.Format(L"%d", nSec);
634 break;
635 case 't':
636 sPart += nHour > 12 ? 'p' : 'a';
637 break;
638 }
639 i++;
640 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
641 switch (c) {
642 case 'y':
643 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
644 break;
645 case 'm':
646 sPart.Format(L"%02d", nMonth);
647 break;
648 case 'd':
649 sPart.Format(L"%02d", nDay);
650 break;
651 case 'H':
652 sPart.Format(L"%02d", nHour);
653 break;
654 case 'h':
655 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
656 break;
657 case 'M':
658 sPart.Format(L"%02d", nMin);
659 break;
660 case 's':
661 sPart.Format(L"%02d", nSec);
662 break;
663 case 't':
664 sPart = nHour > 12 ? L"pm" : L"am";
665 break;
666 }
667 i += 2;
668 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
669 switch (c) {
670 case 'm':
671 i += 3;
672 if (nMonth > 0 && nMonth <= 12)
673 sPart += months[nMonth - 1];
674 break;
675 default:
676 i += 3;
677 sPart += c;
678 sPart += c;
679 sPart += c;
680 break;
681 }
682 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
683 switch (c) {
684 case 'y':
685 sPart.Format(L"%04d", nYear);
686 i += 4;
687 break;
688 case 'm':
689 i += 4;
690 if (nMonth > 0 && nMonth <= 12)
691 sPart += fullmonths[nMonth - 1];
692 break;
693 default:
694 i += 4;
695 sPart += c;
696 sPart += c;
697 sPart += c;
698 sPart += c;
699 break;
700 }
701 } else {
702 i++;
703 sPart += c;
704 }
705 break;
706 default:
707 i++;
708 sPart += c;
709 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700710 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700711
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700712 sRet += sPart;
713 }
714
715 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700716}
717
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
719// bCurrencyPrepend)
Tom Sepezba038bc2015-10-08 12:03:00 -0700720FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800721 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700722 CJS_Value& vRet,
723 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700724#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726 if (params.size() != 6) {
727 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
728 return FALSE;
729 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700730
731 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
732 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700733 if (!pEvent->m_pValue)
734 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700735
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736 CFX_WideString& Value = pEvent->Value();
737 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700738 if (strValue.IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700739 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700740
741 int iDec = params[0].ToInt();
742 int iSepStyle = params[1].ToInt();
743 int iNegStyle = params[2].ToInt();
744 // params[3] is iCurrStyle, it's not used.
745 std::wstring wstrCurrency(params[4].ToCFXWideString().c_str());
746 FX_BOOL bCurrencyPrepend = params[5].ToBool();
747
748 if (iDec < 0)
749 iDec = -iDec;
750
751 if (iSepStyle < 0 || iSepStyle > 3)
752 iSepStyle = 0;
753
754 if (iNegStyle < 0 || iNegStyle > 3)
755 iNegStyle = 0;
756
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 // for processing decimal places
758 strValue.Replace(",", ".");
759 double dValue = atof(strValue);
760 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700761 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762
763 int iDec2;
764 int iNegative = 0;
765
766 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
767 if (strValue.IsEmpty()) {
768 dValue = 0;
769 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
770 if (strValue.IsEmpty()) {
771 strValue = "0";
772 iDec2 = 1;
773 }
774 }
775
776 if (iDec2 < 0) {
777 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
778 strValue = "0" + strValue;
779 }
780 iDec2 = 0;
781 }
782 int iMax = strValue.GetLength();
783 if (iDec2 > iMax) {
784 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
785 strValue += "0";
786 }
787 iMax = iDec2 + 1;
788 }
dsinclair64376be2016-03-31 20:03:24 -0700789
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700790 // for processing seperator style
791 if (iDec2 < iMax) {
792 if (iSepStyle == 0 || iSepStyle == 1) {
793 strValue.Insert(iDec2, '.');
794 iMax++;
795 } else if (iSepStyle == 2 || iSepStyle == 3) {
796 strValue.Insert(iDec2, ',');
797 iMax++;
798 }
799
800 if (iDec2 == 0)
801 strValue.Insert(iDec2, '0');
802 }
803 if (iSepStyle == 0 || iSepStyle == 2) {
804 char cSeperator;
805 if (iSepStyle == 0)
806 cSeperator = ',';
807 else
808 cSeperator = '.';
809
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700810 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700811 strValue.Insert(iDecPositive, cSeperator);
812 iMax++;
813 }
814 }
815
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 // for processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700817 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 std::wstring strValue2 = Value.c_str();
819
820 if (bCurrencyPrepend)
821 strValue2 = wstrCurrency + strValue2;
822 else
823 strValue2 = strValue2 + wstrCurrency;
824
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825 // for processing negative style
826 if (iNegative) {
827 if (iNegStyle == 0) {
828 strValue2.insert(0, L"-");
829 }
830 if (iNegStyle == 2 || iNegStyle == 3) {
831 strValue2.insert(0, L"(");
832 strValue2.insert(strValue2.length(), L")");
833 }
834 if (iNegStyle == 1 || iNegStyle == 3) {
835 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700836 CJS_Array arColor(pRuntime);
837 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700838 vColElm = L"RGB";
839 arColor.SetElement(0, vColElm);
840 vColElm = 1;
841 arColor.SetElement(1, vColElm);
842 vColElm = 0;
843 arColor.SetElement(2, vColElm);
844
845 arColor.SetElement(3, vColElm);
846
Tom Sepez67fd5df2015-10-08 12:24:19 -0700847 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700848 vProp.StartGetting();
849 vProp << arColor;
850 vProp.StartSetting();
851 fTarget->textColor(cc, vProp, sError); // red
852 }
853 }
854 } else {
855 if (iNegStyle == 1 || iNegStyle == 3) {
856 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700857 CJS_Array arColor(pRuntime);
858 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859 vColElm = L"RGB";
860 arColor.SetElement(0, vColElm);
861 vColElm = 0;
862 arColor.SetElement(1, vColElm);
863 arColor.SetElement(2, vColElm);
864 arColor.SetElement(3, vColElm);
865
Tom Sepez67fd5df2015-10-08 12:24:19 -0700866 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867 vProp.StartGetting();
868 fTarget->textColor(cc, vProp, sError);
869
Tom Sepez67fd5df2015-10-08 12:24:19 -0700870 CJS_Array aProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871 vProp.ConvertToArray(aProp);
872
873 CPWL_Color crProp;
874 CPWL_Color crColor;
875 color::ConvertArrayToPWLColor(aProp, crProp);
876 color::ConvertArrayToPWLColor(arColor, crColor);
877
878 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700879 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700880 vProp2.StartGetting();
881 vProp2 << arColor;
882 vProp2.StartSetting();
883 fTarget->textColor(cc, vProp2, sError);
884 }
885 }
886 }
887 }
888 Value = strValue2.c_str();
889#endif
890 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)
Lei Zhang945fdb72015-11-11 10:18:16 -0800895FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(
896 IJS_Context* cc,
897 const std::vector<CJS_Value>& params,
898 CJS_Value& vRet,
899 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700901 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700902
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 if (params.size() < 2)
904 return FALSE;
905 int iSepStyle = params[1].ToInt();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700906
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 if (iSepStyle < 0 || iSepStyle > 3)
908 iSepStyle = 0;
909 if (!pEvent->m_pValue)
910 return FALSE;
911 CFX_WideString& val = pEvent->Value();
912 CFX_WideString& w_strChange = pEvent->Change();
913 CFX_WideString w_strValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700914
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 if (pEvent->WillCommit()) {
916 CFX_WideString wstrChange = w_strChange;
tsepez745611b2016-04-12 16:46:34 -0700917 CFX_WideString wstrValue = StrTrim(w_strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 if (wstrValue.IsEmpty())
919 return TRUE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700920
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 CFX_WideString swTemp = wstrValue;
922 swTemp.Replace(L",", L".");
923 if (!IsNumber(swTemp.c_str())) {
924 pEvent->Rc() = FALSE;
925 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
926 Alert(pContext, sError.c_str());
927 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700928 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700929 return TRUE; // it happens after the last keystroke and before validating,
930 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700931
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700932 std::wstring w_strValue2 = w_strValue.c_str();
933 std::wstring w_strChange2 = w_strChange.c_str();
934 std::wstring w_strSelected;
935 if (-1 != pEvent->SelStart())
936 w_strSelected = w_strValue2.substr(pEvent->SelStart(),
937 (pEvent->SelEnd() - pEvent->SelStart()));
Lei Zhangb9c31972015-08-11 14:09:35 -0700938 bool bHasSign = (w_strValue2.find('-') != std::wstring::npos) &&
939 (w_strSelected.find('-') == std::wstring::npos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700940 if (bHasSign) {
941 // can't insert "change" in front to sign postion.
942 if (pEvent->SelStart() == 0) {
943 FX_BOOL& bRc = pEvent->Rc();
944 bRc = FALSE;
945 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700946 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700948
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949 char cSep = L'.';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700950
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 switch (iSepStyle) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700952 case 0:
953 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700954 cSep = L'.';
955 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700956 case 2:
957 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 cSep = L',';
959 break;
960 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700961
Lei Zhangb9c31972015-08-11 14:09:35 -0700962 bool bHasSep = (w_strValue2.find(cSep) != std::wstring::npos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 for (std::wstring::iterator it = w_strChange2.begin();
964 it != w_strChange2.end(); it++) {
965 if (*it == cSep) {
966 if (bHasSep) {
967 FX_BOOL& bRc = pEvent->Rc();
968 bRc = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700969 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 }
971 bHasSep = TRUE;
972 continue;
973 }
974 if (*it == L'-') {
975 if (bHasSign) {
976 FX_BOOL& bRc = pEvent->Rc();
977 bRc = FALSE;
978 return TRUE;
979 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800980 // sign's position is not correct
981 if (it != w_strChange2.begin()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700982 FX_BOOL& bRc = pEvent->Rc();
983 bRc = FALSE;
984 return TRUE;
985 }
986 if (pEvent->SelStart() != 0) {
987 FX_BOOL& bRc = pEvent->Rc();
988 bRc = FALSE;
989 return TRUE;
990 }
991 bHasSign = TRUE;
992 continue;
993 }
994
Lei Zhang9559b7a2015-12-21 11:12:20 -0800995 if (!FXSYS_iswdigit(*it)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 FX_BOOL& bRc = pEvent->Rc();
997 bRc = FALSE;
998 return TRUE;
999 }
1000 }
1001
1002 std::wstring w_prefix = w_strValue2.substr(0, pEvent->SelStart());
1003 std::wstring w_postfix;
1004 if (pEvent->SelEnd() < (int)w_strValue2.length())
1005 w_postfix = w_strValue2.substr(pEvent->SelEnd());
1006 w_strValue2 = w_prefix + w_strChange2 + w_postfix;
1007 w_strValue = w_strValue2.c_str();
1008 val = w_strValue;
1009 return TRUE;
1010}
1011
1012// function AFPercent_Format(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001013FX_BOOL CJS_PublicMethods::AFPercent_Format(
1014 IJS_Context* cc,
1015 const std::vector<CJS_Value>& params,
1016 CJS_Value& vRet,
1017 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018#if _FX_OS_ != _FX_ANDROID_
1019 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001021
1022 if (params.size() != 2) {
1023 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1024 return FALSE;
1025 }
1026 if (!pEvent->m_pValue)
1027 return FALSE;
1028
1029 CFX_WideString& Value = pEvent->Value();
1030 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1031 if (strValue.IsEmpty())
1032 return TRUE;
1033
1034 int iDec = params[0].ToInt();
1035 if (iDec < 0)
1036 iDec = -iDec;
1037
1038 int iSepStyle = params[1].ToInt();
1039 if (iSepStyle < 0 || iSepStyle > 3)
1040 iSepStyle = 0;
1041
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 // for processing decimal places
1043 double dValue = atof(strValue);
1044 dValue *= 100;
1045 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001046 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047
1048 int iDec2;
1049 int iNegative = 0;
1050 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1051 if (strValue.IsEmpty()) {
1052 dValue = 0;
1053 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1054 }
1055
1056 if (iDec2 < 0) {
1057 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1058 strValue = "0" + strValue;
1059 }
1060 iDec2 = 0;
1061 }
1062 int iMax = strValue.GetLength();
1063 if (iDec2 > iMax) {
1064 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1065 strValue += "0";
1066 }
1067 iMax = iDec2 + 1;
1068 }
dsinclair64376be2016-03-31 20:03:24 -07001069
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070 // for processing seperator style
1071 if (iDec2 < iMax) {
1072 if (iSepStyle == 0 || iSepStyle == 1) {
1073 strValue.Insert(iDec2, '.');
1074 iMax++;
1075 } else if (iSepStyle == 2 || iSepStyle == 3) {
1076 strValue.Insert(iDec2, ',');
1077 iMax++;
1078 }
1079
1080 if (iDec2 == 0)
1081 strValue.Insert(iDec2, '0');
1082 }
1083 if (iSepStyle == 0 || iSepStyle == 2) {
1084 char cSeperator;
1085 if (iSepStyle == 0)
1086 cSeperator = ',';
1087 else
1088 cSeperator = '.';
1089
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001090 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001091 strValue.Insert(iDecPositive, cSeperator);
1092 iMax++;
1093 }
1094 }
dsinclair64376be2016-03-31 20:03:24 -07001095
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096 // negative mark
1097 if (iNegative)
1098 strValue = "-" + strValue;
1099 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001100 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101#endif
1102 return TRUE;
1103}
1104// AFPercent_Keystroke(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001105FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(
1106 IJS_Context* cc,
1107 const std::vector<CJS_Value>& params,
1108 CJS_Value& vRet,
1109 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001110 return AFNumber_Keystroke(cc, params, vRet, sError);
1111}
1112
1113// function AFDate_FormatEx(cFormat)
Tom Sepezba038bc2015-10-08 12:03:00 -07001114FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001115 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116 CJS_Value& vRet,
1117 CFX_WideString& sError) {
1118 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001119 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120
1121 if (params.size() != 1) {
1122 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1123 return FALSE;
1124 }
1125 if (!pEvent->m_pValue)
1126 return FALSE;
1127
1128 CFX_WideString& val = pEvent->Value();
1129 CFX_WideString strValue = val;
1130 if (strValue.IsEmpty())
1131 return TRUE;
1132
1133 CFX_WideString sFormat = params[0].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134 double dDate = 0.0f;
1135
1136 if (strValue.Find(L"GMT") != -1) {
1137 // for GMT format time
1138 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1139 dDate = MakeInterDate(strValue);
1140 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001141 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 }
1143
1144 if (JS_PortIsNan(dDate)) {
1145 CFX_WideString swMsg;
1146 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1147 sFormat.c_str());
1148 Alert(pContext, swMsg.c_str());
1149 return FALSE;
1150 }
1151
1152 val = MakeFormatDate(dDate, sFormat);
1153 return TRUE;
1154}
1155
tsepez745611b2016-04-12 16:46:34 -07001156double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001157 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001159 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 FX_WCHAR c = strValue.GetAt(i);
1161 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001162 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 sTemp = L"";
1164 continue;
1165 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 sTemp += c;
1167 }
Tom Sepezab277682016-02-17 10:07:21 -08001168 wsArray.push_back(sTemp);
1169 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 return 0;
1171
Tom Sepez4246b002016-01-20 11:48:29 -08001172 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 sTemp = wsArray[1];
1174 if (sTemp.Compare(L"Jan") == 0)
1175 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001176 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001178 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001180 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001182 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001184 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001185 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001186 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001187 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001188 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001190 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001191 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001192 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001194 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001196 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197 nMonth = 12;
1198
tsepez4c3debb2016-04-08 12:20:38 -07001199 int nDay = FX_atof(wsArray[2].AsStringC());
1200 int nHour = FX_atof(wsArray[3].AsStringC());
1201 int nMin = FX_atof(wsArray[4].AsStringC());
1202 int nSec = FX_atof(wsArray[5].AsStringC());
1203 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1205 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001206 if (JS_PortIsNan(dRet))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 dRet = JS_DateParse(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001208
1209 return dRet;
1210}
1211
1212// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001213FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(
1214 IJS_Context* cc,
1215 const std::vector<CJS_Value>& params,
1216 CJS_Value& vRet,
1217 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220
1221 if (params.size() != 1) {
1222 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1223 return FALSE;
1224 }
1225
1226 if (pEvent->WillCommit()) {
1227 if (!pEvent->m_pValue)
1228 return FALSE;
1229 CFX_WideString strValue = pEvent->Value();
1230 if (strValue.IsEmpty())
1231 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001232
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001233 CFX_WideString sFormat = params[0].ToCFXWideString();
Lei Zhang9559b7a2015-12-21 11:12:20 -08001234 bool bWrongFormat = FALSE;
1235 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001236 if (bWrongFormat || JS_PortIsNan(dRet)) {
1237 CFX_WideString swMsg;
1238 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1239 sFormat.c_str());
1240 Alert(pContext, swMsg.c_str());
1241 pEvent->Rc() = FALSE;
1242 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001243 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001244 }
1245 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001246}
1247
Tom Sepezba038bc2015-10-08 12:03:00 -07001248FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001249 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001250 CJS_Value& vRet,
1251 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001252 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1255 return FALSE;
1256 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001257
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 int iIndex = params[0].ToInt();
1259 const FX_WCHAR* cFormats[] = {L"m/d",
1260 L"m/d/yy",
1261 L"mm/dd/yy",
1262 L"mm/yy",
1263 L"d-mmm",
1264 L"d-mmm-yy",
1265 L"dd-mmm-yy",
1266 L"yy-mm-dd",
1267 L"mmm-yy",
1268 L"mmmm-yy",
1269 L"mmm d, yyyy",
1270 L"mmmm d, yyyy",
1271 L"m/d/yy h:MM tt",
1272 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001273
Lei Zhanga0f67242015-08-17 15:39:30 -07001274 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1275 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001276
Lei Zhang945fdb72015-11-11 10:18:16 -08001277 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001278 newParams.push_back(
1279 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280 return AFDate_FormatEx(cc, newParams, vRet, sError);
1281}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001282
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001284FX_BOOL CJS_PublicMethods::AFDate_Keystroke(
1285 IJS_Context* cc,
1286 const std::vector<CJS_Value>& params,
1287 CJS_Value& vRet,
1288 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001289 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001290 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1292 return FALSE;
1293 }
1294
1295 int iIndex = params[0].ToInt();
1296 const FX_WCHAR* cFormats[] = {L"m/d",
1297 L"m/d/yy",
1298 L"mm/dd/yy",
1299 L"mm/yy",
1300 L"d-mmm",
1301 L"d-mmm-yy",
1302 L"dd-mmm-yy",
1303 L"yy-mm-dd",
1304 L"mmm-yy",
1305 L"mmmm-yy",
1306 L"mmm d, yyyy",
1307 L"mmmm d, yyyy",
1308 L"m/d/yy h:MM tt",
1309 L"m/d/yy HH:MM"};
1310
Lei Zhanga0f67242015-08-17 15:39:30 -07001311 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1312 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313
Lei Zhang945fdb72015-11-11 10:18:16 -08001314 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001315 newParams.push_back(
1316 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1318}
1319
1320// function AFTime_Format(ptf)
Tom Sepezba038bc2015-10-08 12:03:00 -07001321FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001322 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001323 CJS_Value& vRet,
1324 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001325 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1328 return FALSE;
1329 }
1330
1331 int iIndex = params[0].ToInt();
1332 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1333 L"h:MM:ss tt"};
1334
Lei Zhanga0f67242015-08-17 15:39:30 -07001335 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1336 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001337
Lei Zhang945fdb72015-11-11 10:18:16 -08001338 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001339 newParams.push_back(
1340 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001341 return AFDate_FormatEx(cc, newParams, vRet, sError);
1342}
1343
Lei Zhang945fdb72015-11-11 10:18:16 -08001344FX_BOOL CJS_PublicMethods::AFTime_Keystroke(
1345 IJS_Context* cc,
1346 const std::vector<CJS_Value>& params,
1347 CJS_Value& vRet,
1348 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001349 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001350 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001351 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1352 return FALSE;
1353 }
1354
1355 int iIndex = params[0].ToInt();
1356 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1357 L"h:MM:ss tt"};
1358
Lei Zhanga0f67242015-08-17 15:39:30 -07001359 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1360 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361
Lei Zhang945fdb72015-11-11 10:18:16 -08001362 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001363 newParams.push_back(
1364 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001365 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1366}
1367
Tom Sepezba038bc2015-10-08 12:03:00 -07001368FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001369 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001370 CJS_Value& vRet,
1371 CFX_WideString& sError) {
1372 return AFDate_FormatEx(cc, params, vRet, sError);
1373}
1374
Lei Zhang945fdb72015-11-11 10:18:16 -08001375FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(
1376 IJS_Context* cc,
1377 const std::vector<CJS_Value>& params,
1378 CJS_Value& vRet,
1379 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1381}
1382
1383// function AFSpecial_Format(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001384FX_BOOL CJS_PublicMethods::AFSpecial_Format(
1385 IJS_Context* cc,
1386 const std::vector<CJS_Value>& params,
1387 CJS_Value& vRet,
1388 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001389 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001390
1391 if (params.size() != 1) {
1392 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1393 return FALSE;
1394 }
1395
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 if (!pEvent->m_pValue)
1398 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001399
tsepez4f1f41f2016-03-28 14:13:16 -07001400 CFX_WideString wsSource = pEvent->Value();
1401 CFX_WideString wsFormat;
1402 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001403 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001404 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001406 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001407 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001409 case 2:
1410 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1411 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001412 else
tsepez4f1f41f2016-03-28 14:13:16 -07001413 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001414 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001415 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001416 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001417 break;
1418 }
1419
tsepez4f1f41f2016-03-28 14:13:16 -07001420 pEvent->Value() = util::printx(wsFormat, wsSource);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421 return TRUE;
1422}
1423
1424// function AFSpecial_KeystrokeEx(mask)
Lei Zhang945fdb72015-11-11 10:18:16 -08001425FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(
1426 IJS_Context* cc,
1427 const std::vector<CJS_Value>& params,
1428 CJS_Value& vRet,
1429 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001430 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1432
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001433 if (params.size() < 1) {
1434 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1435 return FALSE;
1436 }
1437
1438 if (!pEvent->m_pValue)
1439 return FALSE;
1440 CFX_WideString& valEvent = pEvent->Value();
1441
1442 CFX_WideString wstrMask = params[0].ToCFXWideString();
1443 if (wstrMask.IsEmpty())
1444 return TRUE;
1445
Lei Zhanga0f67242015-08-17 15:39:30 -07001446 const size_t wstrMaskLen = wstrMask.GetLength();
1447 const std::wstring wstrValue = valEvent.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448
1449 if (pEvent->WillCommit()) {
1450 if (wstrValue.empty())
1451 return TRUE;
Lei Zhanga0f67242015-08-17 15:39:30 -07001452 size_t iIndexMask = 0;
1453 for (const auto& w_Value : wstrValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001454 if (!maskSatisfied(w_Value, wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001455 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001456 iIndexMask++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001457 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001458
Lei Zhanga0f67242015-08-17 15:39:30 -07001459 if (iIndexMask != wstrMaskLen ||
1460 (iIndexMask != wstrValue.size() && wstrMaskLen != 0)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001461 Alert(
1462 pContext,
1463 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1464 pEvent->Rc() = FALSE;
1465 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001466 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001467 }
1468
1469 CFX_WideString& wideChange = pEvent->Change();
1470 std::wstring wChange = wideChange.c_str();
1471 if (wChange.empty())
1472 return TRUE;
1473
Wei Li05d53f02016-03-29 16:42:53 -07001474 size_t iIndexMask = pEvent->SelStart();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475
Lei Zhanga0f67242015-08-17 15:39:30 -07001476 size_t combined_len = wstrValue.length() + wChange.length() -
1477 (pEvent->SelEnd() - pEvent->SelStart());
1478 if (combined_len > wstrMaskLen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479 Alert(pContext,
1480 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1481 pEvent->Rc() = FALSE;
1482 return TRUE;
1483 }
1484
Lei Zhanga0f67242015-08-17 15:39:30 -07001485 if (iIndexMask >= wstrMaskLen && (!wChange.empty())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001486 Alert(pContext,
1487 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1488 pEvent->Rc() = FALSE;
1489 return TRUE;
1490 }
1491
1492 for (std::wstring::iterator it = wChange.begin(); it != wChange.end(); it++) {
Lei Zhanga0f67242015-08-17 15:39:30 -07001493 if (iIndexMask >= wstrMaskLen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001494 Alert(pContext,
1495 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1496 pEvent->Rc() = FALSE;
1497 return TRUE;
1498 }
1499 wchar_t w_Mask = wstrMask[iIndexMask];
1500 if (!isReservedMaskChar(w_Mask)) {
1501 *it = w_Mask;
1502 }
1503 wchar_t w_Change = *it;
1504 if (!maskSatisfied(w_Change, w_Mask)) {
1505 pEvent->Rc() = FALSE;
1506 return TRUE;
1507 }
1508 iIndexMask++;
1509 }
1510
1511 wideChange = wChange.c_str();
1512 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001513}
1514
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515// function AFSpecial_Keystroke(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001516FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(
1517 IJS_Context* cc,
1518 const std::vector<CJS_Value>& params,
1519 CJS_Value& vRet,
1520 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 if (params.size() != 1) {
1523 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1524 return FALSE;
1525 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001526
Tom Sepez67fd5df2015-10-08 12:24:19 -07001527 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001528 if (!pEvent->m_pValue)
1529 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001530
1531 std::string cFormat;
1532 int iIndex = params[0].ToInt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001533 CFX_WideString& val = pEvent->Value();
1534 std::string strSrc = CFX_ByteString::FromUnicode(val).c_str();
1535 std::wstring wstrChange = pEvent->Change().c_str();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001536
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001537 switch (iIndex) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001538 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001539 cFormat = "99999";
1540 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001541 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542 cFormat = "999999999";
1543 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001544 case 2:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001545 if (strSrc.length() + wstrChange.length() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001546 cFormat = "9999999999";
1547 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001548 cFormat = "9999999";
1549 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001550 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001551 cFormat = "999999999";
1552 break;
1553 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001554
Lei Zhang945fdb72015-11-11 10:18:16 -08001555 std::vector<CJS_Value> params2;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001556 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001557 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001558}
1559
Tom Sepezba038bc2015-10-08 12:03:00 -07001560FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001561 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001562 CJS_Value& vRet,
1563 CFX_WideString& sError) {
1564 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001565 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001566
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001567 if (params.size() != 1) {
1568 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1569 return FALSE;
1570 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001571
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001572 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001573 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001574 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001575
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001576 if (pEventHandler->WillCommit()) {
1577 vRet = swValue.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001578 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579 }
1580
1581 CFX_WideString prefix, postfix;
1582
1583 if (pEventHandler->SelStart() >= 0)
1584 prefix = swValue.Mid(0, pEventHandler->SelStart());
1585 else
1586 prefix = L"";
1587
1588 if (pEventHandler->SelEnd() >= 0 &&
1589 pEventHandler->SelEnd() <= swValue.GetLength())
1590 postfix = swValue.Mid(pEventHandler->SelEnd(),
1591 swValue.GetLength() - pEventHandler->SelEnd());
1592 else
1593 postfix = L"";
1594
1595 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1596
1597 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001598}
1599
Tom Sepezba038bc2015-10-08 12:03:00 -07001600FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001601 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001602 CJS_Value& vRet,
1603 CFX_WideString& sError) {
1604 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001605 ASSERT(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001606
1607 if (params.size() != 2) {
1608 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1609 return FALSE;
1610 }
1611
1612 CFX_WideString sValue = params[0].ToCFXWideString();
1613 CFX_WideString sFormat = params[1].ToCFXWideString();
1614
Lei Zhang9559b7a2015-12-21 11:12:20 -08001615 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001616
1617 if (JS_PortIsNan(dDate)) {
1618 CFX_WideString swMsg;
1619 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1620 sFormat.c_str());
1621 Alert((CJS_Context*)cc, swMsg.c_str());
1622 return FALSE;
1623 }
1624
1625 vRet = dDate;
1626 return TRUE;
1627}
1628
Tom Sepezba038bc2015-10-08 12:03:00 -07001629FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001630 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001631 CJS_Value& vRet,
1632 CFX_WideString& sError) {
1633 if (params.size() != 3) {
1634 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001635 ASSERT(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001636
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001637 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1638 return FALSE;
1639 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001640
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001641 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1642 params[1].ToDouble(), params[2].ToDouble());
1643 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001644}
1645
Tom Sepezba038bc2015-10-08 12:03:00 -07001646FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001647 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648 CJS_Value& vRet,
1649 CFX_WideString& sError) {
Tom Sepez4246b002016-01-20 11:48:29 -08001650 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001651 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001652 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1653 return FALSE;
1654 }
Tom Sepez4246b002016-01-20 11:48:29 -08001655 CFX_WideString ws = params[0].ToCFXWideString();
1656 ws.Replace(L",", L".");
1657 vRet = ws;
1658 vRet.MaybeCoerceToNumber();
1659 if (vRet.GetType() != CJS_Value::VT_number)
1660 vRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001661 return TRUE;
1662}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001663
Lei Zhang945fdb72015-11-11 10:18:16 -08001664FX_BOOL CJS_PublicMethods::AFSimple_Calculate(
1665 IJS_Context* cc,
1666 const std::vector<CJS_Value>& params,
1667 CJS_Value& vRet,
1668 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001669 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001670 if (params.size() != 2) {
1671 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1672 return FALSE;
1673 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001674
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001675 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001676 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001677 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1678 return FALSE;
1679 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001680
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001681 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001682 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001683 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001684
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001685 CFX_WideString sFunction = params[0].ToCFXWideString();
Tom Sepez67fd5df2015-10-08 12:24:19 -07001686 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001687
Tom Sepez67fd5df2015-10-08 12:24:19 -07001688 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1689 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001690 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001691
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001692 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001693 CJS_Value jsValue(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001694 FieldNameArray.GetElement(i, jsValue);
1695 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001696
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001697 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1698 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1699 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001700 switch (pFormField->GetFieldType()) {
1701 case FIELDTYPE_TEXTFIELD:
1702 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001703 CFX_WideString trimmed = pFormField->GetValue();
1704 trimmed.TrimRight();
1705 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001706 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001707 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001708 case FIELDTYPE_PUSHBUTTON: {
1709 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001710 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001711 case FIELDTYPE_CHECKBOX:
1712 case FIELDTYPE_RADIOBUTTON: {
1713 dTemp = 0.0;
1714 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1715 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1716 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001717 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1718 trimmed.TrimRight();
1719 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001720 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001721 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001722 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001724 }
Tom Sepez4246b002016-01-20 11:48:29 -08001725 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001726 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001727 if (pFormField->CountSelectedItems() <= 1) {
1728 CFX_WideString trimmed = pFormField->GetValue();
1729 trimmed.TrimRight();
1730 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001731 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001732 }
1733 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001734 default:
1735 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001736 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737
1738 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1739 wcscmp(sFunction.c_str(), L"MAX") == 0))
1740 dValue = dTemp;
1741
1742 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1743
1744 nFieldsCount++;
1745 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001746 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001747 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001748
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001749 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1750 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001751
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001752 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1753 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001754 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001755 if (pContext->GetEventHandler()->m_pValue)
1756 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001757
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001759}
1760
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001761/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001762** within the specified range. */
1763
Lei Zhang945fdb72015-11-11 10:18:16 -08001764FX_BOOL CJS_PublicMethods::AFRange_Validate(
1765 IJS_Context* cc,
1766 const std::vector<CJS_Value>& params,
1767 CJS_Value& vRet,
1768 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001769 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001770 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001771
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001772 if (params.size() != 4) {
1773 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1774 return FALSE;
1775 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001776
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001777 if (!pEvent->m_pValue)
1778 return FALSE;
1779 if (pEvent->Value().IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001780 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 double dEentValue = atof(CFX_ByteString::FromUnicode(pEvent->Value()));
1782 FX_BOOL bGreaterThan = params[0].ToBool();
1783 double dGreaterThan = params[1].ToDouble();
1784 FX_BOOL bLessThan = params[2].ToBool();
1785 double dLessThan = params[3].ToDouble();
1786 CFX_WideString swMsg;
1787
1788 if (bGreaterThan && bLessThan) {
1789 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
1790 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
1791 params[1].ToCFXWideString().c_str(),
1792 params[3].ToCFXWideString().c_str());
1793 } else if (bGreaterThan) {
1794 if (dEentValue < dGreaterThan)
1795 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
1796 params[1].ToCFXWideString().c_str());
1797 } else if (bLessThan) {
1798 if (dEentValue > dLessThan)
1799 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
1800 params[3].ToCFXWideString().c_str());
1801 }
1802
1803 if (!swMsg.IsEmpty()) {
1804 Alert(pContext, swMsg.c_str());
1805 pEvent->Rc() = FALSE;
1806 }
1807 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001808}
1809
Tom Sepezba038bc2015-10-08 12:03:00 -07001810FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001811 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812 CJS_Value& vRet,
1813 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001814 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001815 if (params.size() != 1) {
1816 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1817 return FALSE;
1818 }
1819
Tom Sepez67fd5df2015-10-08 12:24:19 -07001820 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1821 CJS_Array nums(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001822
1823 CFX_WideString str = params[0].ToCFXWideString();
1824 CFX_WideString sPart;
1825
1826 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1827 str = L"0" + str;
1828
1829 int nIndex = 0;
1830 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1831 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001832 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001833 sPart += wc;
1834 } else {
1835 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001836 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001837 sPart = L"";
1838 nIndex++;
1839 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001840 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001841 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001842
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001843 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001844 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001845 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001846
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001847 if (nums.GetLength() > 0)
1848 vRet = nums;
1849 else
1850 vRet.SetNull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001851
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001852 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001853}