blob: 2b14b772aad8ff36875902fc644e1fb61ddd8ab9 [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 <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080011
Dan Sinclaira8a28e02016-03-23 15:41:39 -040012#include "core/fxcrt/include/fx_ext.h"
dsinclair64376be2016-03-31 20:03:24 -070013#include "fpdfsdk/include/fsdk_mgr.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040014#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040015#include "fpdfsdk/javascript/JS_Define.h"
16#include "fpdfsdk/javascript/JS_EventHandler.h"
17#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040018#include "fpdfsdk/javascript/JS_Value.h"
dsinclair64376be2016-03-31 20:03:24 -070019#include "fpdfsdk/javascript/cjs_context.h"
20#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040021#include "fpdfsdk/javascript/color.h"
22#include "fpdfsdk/javascript/resource.h"
23#include "fpdfsdk/javascript/util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025#define DOUBLE_CORRECT 0.000000000000001
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
27BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format)
29JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke)
30JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format)
31JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke)
32JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx)
33JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx)
34JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format)
35JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke)
36JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx)
37JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx)
38JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format)
39JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke)
40JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format)
41JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke)
42JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx)
43JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple)
44JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
45JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
46JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
47JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
48JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
49JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050END_JS_STATIC_GLOBAL_FUN()
51
52IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
53
tsepez745611b2016-04-12 16:46:34 -070054namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
tsepez745611b2016-04-12 16:46:34 -070056const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
57 L"May", L"Jun", L"Jul", L"Aug",
58 L"Sep", L"Oct", L"Nov", L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059
tsepez745611b2016-04-12 16:46:34 -070060const FX_WCHAR* const fullmonths[] = {L"January", L"February", L"March",
61 L"April", L"May", L"June",
62 L"July", L"August", L"September",
63 L"October", L"November", L"December"};
64
65CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
66 CFX_ByteString result(pStr);
67 result.TrimLeft(' ');
68 result.TrimRight(' ');
69 return result;
70}
71
72CFX_WideString StrTrim(const CFX_WideString& pStr) {
73 CFX_WideString result(pStr);
74 result.TrimLeft(' ');
75 result.TrimRight(' ');
76 return result;
77}
78
79} // namespace
80
81bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
Dan Sinclair3ebd1212016-03-09 09:59:23 -050082 CFX_WideString sTrim = StrTrim(str);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 const FX_WCHAR* pTrim = sTrim.c_str();
84 const FX_WCHAR* p = pTrim;
Wei Li614d20a2016-03-15 13:55:12 -070085 bool bDot = false;
86 bool bKXJS = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 wchar_t c;
Wei Li614d20a2016-03-15 13:55:12 -070089 while ((c = *p) != L'\0') {
90 if (c == L'.' || c == L',') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 if (bDot)
Wei Li614d20a2016-03-15 13:55:12 -070092 return false;
93 bDot = true;
94 } else if (c == L'-' || c == L'+') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 if (p != pTrim)
Wei Li614d20a2016-03-15 13:55:12 -070096 return false;
97 } else if (c == L'e' || c == L'E') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 if (bKXJS)
Wei Li614d20a2016-03-15 13:55:12 -070099 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 p++;
102 c = *p;
Wei Li614d20a2016-03-15 13:55:12 -0700103 if (c == L'+' || c == L'-') {
104 bKXJS = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 } else {
Wei Li614d20a2016-03-15 13:55:12 -0700106 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800108 } else if (!FXSYS_iswdigit(c)) {
Wei Li614d20a2016-03-15 13:55:12 -0700109 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 }
111 p++;
112 }
113
Wei Li614d20a2016-03-15 13:55:12 -0700114 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115}
116
Wei Li614d20a2016-03-15 13:55:12 -0700117bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 switch (c_Mask) {
119 case L'9':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800120 return FXSYS_iswdigit(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 case L'A':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800122 return FXSYS_iswalpha(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 case L'O':
Lei Zhang9559b7a2015-12-21 11:12:20 -0800124 return FXSYS_iswalnum(c_Change);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 case L'X':
Wei Li614d20a2016-03-15 13:55:12 -0700126 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 default:
128 return (c_Change == c_Mask);
129 }
130}
131
Wei Li614d20a2016-03-15 13:55:12 -0700132bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
134}
135
136double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
137 double dValue1,
138 double dValue2) {
139 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
140 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
141 return dValue1 + dValue2;
142 }
143 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
144 return dValue1 * dValue2;
145 }
146 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800147 return std::min(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 }
149 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
Lei Zhang375a8642016-01-11 11:59:17 -0800150 return std::max(dValue1, dValue2);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 }
152 return dValue1;
153}
154
Tom Sepez67fd5df2015-10-08 12:24:19 -0700155CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 CJS_Value val) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700157 CJS_Array StrArray(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 if (val.IsArrayObject()) {
159 val.ConvertToArray(StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700160 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 }
162 CFX_WideString wsStr = val.ToCFXWideString();
163 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
tsepezb4c9f3f2016-04-13 15:41:21 -0700164 const char* p = t.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165
166 int ch = ',';
167 int nIndex = 0;
168
169 while (*p) {
170 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800171 if (!pTemp) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700172 StrArray.SetElement(
173 nIndex, CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 }
Lei Zhang997de612015-11-04 18:17:53 -0800176
177 char* pSub = new char[pTemp - p + 1];
178 strncpy(pSub, p, pTemp - p);
179 *(pSub + (pTemp - p)) = '\0';
180
tsepezb4c9f3f2016-04-13 15:41:21 -0700181 StrArray.SetElement(
182 nIndex, CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str()));
Lei Zhang997de612015-11-04 18:17:53 -0800183 delete[] pSub;
184
185 nIndex++;
186 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 }
188 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
190
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500191int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 int nStart,
193 int& nSkip,
194 int nMaxStep) {
195 int nRet = 0;
196 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500197 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 if (i - nStart > 10)
199 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500201 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800202 if (!FXSYS_iswdigit(c))
203 break;
204
Dan Sinclair1c915372016-03-03 17:12:58 -0500205 nRet = nRet * 10 + FXSYS_toDecimalDigit(c);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800206 nSkip = i - nStart + 1;
207 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 break;
209 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212}
213
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500214CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
215 int nStart,
216 int& nSkip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 CFX_WideString swRet;
218 nSkip = 0;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500219 for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
220 FX_WCHAR c = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800221 if (!FXSYS_iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800223
224 swRet += c;
225 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229}
230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800232 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 int nYear = JS_GetYearFromTime(dt);
236 int nMonth = JS_GetMonthFromTime(dt) + 1;
237 int nDay = JS_GetDayFromTime(dt);
238 int nHour = JS_GetHourFromTime(dt);
239 int nMin = JS_GetMinFromTime(dt);
240 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 int nSkip = 0;
245 int nLen = value.GetLength();
246 int nIndex = 0;
247 int i = 0;
248 while (i < nLen) {
249 if (nIndex > 2)
250 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 FX_WCHAR c = value.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800253 if (FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
255 i += nSkip;
256 } else {
257 i++;
258 }
259 }
260
261 if (nIndex == 2) {
262 // case2: month/day
263 // case3: day/month
264 if ((number[0] >= 1 && number[0] <= 12) &&
265 (number[1] >= 1 && number[1] <= 31)) {
266 nMonth = number[0];
267 nDay = number[1];
268 } else if ((number[0] >= 1 && number[0] <= 31) &&
269 (number[1] >= 1 && number[1] <= 12)) {
270 nDay = number[0];
271 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700272 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273
Lei Zhang9559b7a2015-12-21 11:12:20 -0800274 if (bWrongFormat)
275 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 } else if (nIndex == 3) {
277 // case1: year/month/day
278 // case2: month/day/year
279 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
282 (number[2] >= 1 && number[2] <= 31)) {
283 nYear = number[0];
284 nMonth = number[1];
285 nDay = number[2];
286 } else if ((number[0] >= 1 && number[0] <= 12) &&
287 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
288 nMonth = number[0];
289 nDay = number[1];
290 nYear = number[2];
291 } else if ((number[0] >= 1 && number[0] <= 31) &&
292 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
293 nDay = number[0];
294 nMonth = number[1];
295 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700296 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700297
Lei Zhang9559b7a2015-12-21 11:12:20 -0800298 if (bWrongFormat)
299 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800301 if (bWrongFormat)
302 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 return dt;
304 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306 CFX_WideString swTemp;
307 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
tsepez018935c2016-04-15 13:15:12 -0700308 return JS_DateParse(swTemp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309}
310
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
312 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800313 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 if (format.IsEmpty() || value.IsEmpty())
317 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 int nYear = JS_GetYearFromTime(dt);
320 int nMonth = JS_GetMonthFromTime(dt) + 1;
321 int nDay = JS_GetDayFromTime(dt);
322 int nHour = JS_GetHourFromTime(dt);
323 int nMin = JS_GetMinFromTime(dt);
324 int nSec = JS_GetSecFromTime(dt);
325
326 int nYearSub = 99; // nYear - 2000;
327
328 FX_BOOL bPm = FALSE;
329 FX_BOOL bExit = FALSE;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800330 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331
332 int i = 0;
333 int j = 0;
334
335 while (i < format.GetLength()) {
336 if (bExit)
337 break;
338
339 FX_WCHAR c = format.GetAt(i);
340 switch (c) {
341 case ':':
342 case '.':
343 case '-':
344 case '\\':
345 case '/':
346 i++;
347 j++;
348 break;
349
350 case 'y':
351 case 'm':
352 case 'd':
353 case 'H':
354 case 'h':
355 case 'M':
356 case 's':
357 case 't': {
358 int oldj = j;
359 int nSkip = 0;
360 int remaining = format.GetLength() - i - 1;
361
362 if (remaining == 0 || format.GetAt(i + 1) != c) {
363 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700364 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 i++;
366 j++;
367 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700368 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 nMonth = ParseStringInteger(value, j, nSkip, 2);
370 i++;
371 j += nSkip;
372 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700373 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 nDay = ParseStringInteger(value, j, nSkip, 2);
375 i++;
376 j += nSkip;
377 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700378 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 nHour = ParseStringInteger(value, j, nSkip, 2);
380 i++;
381 j += nSkip;
382 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700383 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 nHour = ParseStringInteger(value, j, nSkip, 2);
385 i++;
386 j += nSkip;
387 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700388 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 nMin = ParseStringInteger(value, j, nSkip, 2);
390 i++;
391 j += nSkip;
392 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700393 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 nSec = ParseStringInteger(value, j, nSkip, 2);
395 i++;
396 j += nSkip;
397 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700398 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
400 i++;
401 j++;
402 break;
403 }
404 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
405 switch (c) {
406 case 'y':
407 nYear = ParseStringInteger(value, j, nSkip, 4);
408 i += 2;
409 j += nSkip;
410 break;
411 case 'm':
412 nMonth = ParseStringInteger(value, j, nSkip, 2);
413 i += 2;
414 j += nSkip;
415 break;
416 case 'd':
417 nDay = ParseStringInteger(value, j, nSkip, 2);
418 i += 2;
419 j += nSkip;
420 break;
421 case 'H':
422 nHour = ParseStringInteger(value, j, nSkip, 2);
423 i += 2;
424 j += nSkip;
425 break;
426 case 'h':
427 nHour = ParseStringInteger(value, j, nSkip, 2);
428 i += 2;
429 j += nSkip;
430 break;
431 case 'M':
432 nMin = ParseStringInteger(value, j, nSkip, 2);
433 i += 2;
434 j += nSkip;
435 break;
436 case 's':
437 nSec = ParseStringInteger(value, j, nSkip, 2);
438 i += 2;
439 j += nSkip;
440 break;
441 case 't':
442 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
443 value.GetAt(j + 1) == 'm');
444 i += 2;
445 j += 2;
446 break;
447 }
448 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
449 switch (c) {
450 case 'm': {
451 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
452 FX_BOOL bFind = FALSE;
453 for (int m = 0; m < 12; m++) {
454 if (sMonth.CompareNoCase(months[m]) == 0) {
455 nMonth = m + 1;
456 i += 3;
457 j += nSkip;
458 bFind = TRUE;
459 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700460 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 }
462
463 if (!bFind) {
464 nMonth = ParseStringInteger(value, j, nSkip, 3);
465 i += 3;
466 j += nSkip;
467 }
468 } break;
469 case 'y':
470 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700471 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700472 i += 3;
473 j += 3;
474 break;
475 }
476 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
477 switch (c) {
478 case 'y':
479 nYear = ParseStringInteger(value, j, nSkip, 4);
480 j += nSkip;
481 i += 4;
482 break;
483 case 'm': {
484 FX_BOOL bFind = FALSE;
485
486 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
487 sMonth.MakeLower();
488
489 for (int m = 0; m < 12; m++) {
490 CFX_WideString sFullMonths = fullmonths[m];
491 sFullMonths.MakeLower();
492
493 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
494 nMonth = m + 1;
495 i += 4;
496 j += nSkip;
497 bFind = TRUE;
498 break;
499 }
500 }
501
502 if (!bFind) {
503 nMonth = ParseStringInteger(value, j, nSkip, 4);
504 i += 4;
505 j += nSkip;
506 }
507 } break;
508 default:
509 i += 4;
510 j += 4;
511 break;
512 }
513 } else {
514 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800515 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516 bExit = TRUE;
517 }
518 i++;
519 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700520 }
Tom Sepez85386422014-07-23 10:28:37 -0700521
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800523 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700524 bExit = TRUE;
525 }
526 }
527
528 break;
529 default:
530 if (value.GetLength() <= j) {
531 bExit = TRUE;
532 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800533 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534 bExit = TRUE;
535 }
536
537 i++;
538 j++;
539 break;
540 }
541 }
542
543 if (bPm)
544 nHour += 12;
545
546 if (nYear >= 0 && nYear <= nYearSub)
547 nYear += 2000;
548
549 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800550 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700551
552 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800553 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554
555 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800556 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557
558 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800559 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560
561 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800562 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563
564 double dRet = 0;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800565 if (bBadFormat) {
566 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700567 } else {
568 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
569 JS_MakeTime(nHour, nMin, nSec, 0));
tsepez018935c2016-04-15 13:15:12 -0700570 if (JS_PortIsNan(dRet))
571 dRet = JS_DateParse(value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572 }
573
tsepez018935c2016-04-15 13:15:12 -0700574 if (JS_PortIsNan(dRet))
Lei Zhang9559b7a2015-12-21 11:12:20 -0800575 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576
Lei Zhang9559b7a2015-12-21 11:12:20 -0800577 if (bWrongFormat)
578 *bWrongFormat = bBadFormat;
tsepez018935c2016-04-15 13:15:12 -0700579
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 return dRet;
581}
582
583CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
584 const CFX_WideString& format) {
585 CFX_WideString sRet = L"", sPart = L"";
586
587 int nYear = JS_GetYearFromTime(dDate);
588 int nMonth = JS_GetMonthFromTime(dDate) + 1;
589 int nDay = JS_GetDayFromTime(dDate);
590 int nHour = JS_GetHourFromTime(dDate);
591 int nMin = JS_GetMinFromTime(dDate);
592 int nSec = JS_GetSecFromTime(dDate);
593
594 int i = 0;
595 while (i < format.GetLength()) {
596 FX_WCHAR c = format.GetAt(i);
597 int remaining = format.GetLength() - i - 1;
598 sPart = L"";
599 switch (c) {
600 case 'y':
601 case 'm':
602 case 'd':
603 case 'H':
604 case 'h':
605 case 'M':
606 case 's':
607 case 't':
608 if (remaining == 0 || format.GetAt(i + 1) != c) {
609 switch (c) {
610 case 'y':
611 sPart += c;
612 break;
613 case 'm':
614 sPart.Format(L"%d", nMonth);
615 break;
616 case 'd':
617 sPart.Format(L"%d", nDay);
618 break;
619 case 'H':
620 sPart.Format(L"%d", nHour);
621 break;
622 case 'h':
623 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
624 break;
625 case 'M':
626 sPart.Format(L"%d", nMin);
627 break;
628 case 's':
629 sPart.Format(L"%d", nSec);
630 break;
631 case 't':
632 sPart += nHour > 12 ? 'p' : 'a';
633 break;
634 }
635 i++;
636 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
637 switch (c) {
638 case 'y':
639 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
640 break;
641 case 'm':
642 sPart.Format(L"%02d", nMonth);
643 break;
644 case 'd':
645 sPart.Format(L"%02d", nDay);
646 break;
647 case 'H':
648 sPart.Format(L"%02d", nHour);
649 break;
650 case 'h':
651 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
652 break;
653 case 'M':
654 sPart.Format(L"%02d", nMin);
655 break;
656 case 's':
657 sPart.Format(L"%02d", nSec);
658 break;
659 case 't':
660 sPart = nHour > 12 ? L"pm" : L"am";
661 break;
662 }
663 i += 2;
664 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
665 switch (c) {
666 case 'm':
667 i += 3;
668 if (nMonth > 0 && nMonth <= 12)
669 sPart += months[nMonth - 1];
670 break;
671 default:
672 i += 3;
673 sPart += c;
674 sPart += c;
675 sPart += c;
676 break;
677 }
678 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
679 switch (c) {
680 case 'y':
681 sPart.Format(L"%04d", nYear);
682 i += 4;
683 break;
684 case 'm':
685 i += 4;
686 if (nMonth > 0 && nMonth <= 12)
687 sPart += fullmonths[nMonth - 1];
688 break;
689 default:
690 i += 4;
691 sPart += c;
692 sPart += c;
693 sPart += c;
694 sPart += c;
695 break;
696 }
697 } else {
698 i++;
699 sPart += c;
700 }
701 break;
702 default:
703 i++;
704 sPart += c;
705 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700706 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700707
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708 sRet += sPart;
709 }
710
711 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700712}
713
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
715// bCurrencyPrepend)
Tom Sepezba038bc2015-10-08 12:03:00 -0700716FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800717 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718 CJS_Value& vRet,
719 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700720#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700722 if (params.size() != 6) {
723 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
724 return FALSE;
725 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700726
727 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
728 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729 if (!pEvent->m_pValue)
730 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700731
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732 CFX_WideString& Value = pEvent->Value();
733 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734 if (strValue.IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700735 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736
737 int iDec = params[0].ToInt();
738 int iSepStyle = params[1].ToInt();
739 int iNegStyle = params[2].ToInt();
740 // params[3] is iCurrStyle, it's not used.
thestigcf03f8e2016-05-09 12:36:18 -0700741 CFX_WideString wstrCurrency = params[4].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742 FX_BOOL bCurrencyPrepend = params[5].ToBool();
743
744 if (iDec < 0)
745 iDec = -iDec;
746
747 if (iSepStyle < 0 || iSepStyle > 3)
748 iSepStyle = 0;
749
750 if (iNegStyle < 0 || iNegStyle > 3)
751 iNegStyle = 0;
752
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700753 // for processing decimal places
754 strValue.Replace(",", ".");
tsepezb4c9f3f2016-04-13 15:41:21 -0700755 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700757 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758
759 int iDec2;
760 int iNegative = 0;
761
762 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
763 if (strValue.IsEmpty()) {
764 dValue = 0;
765 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
766 if (strValue.IsEmpty()) {
767 strValue = "0";
768 iDec2 = 1;
769 }
770 }
771
772 if (iDec2 < 0) {
773 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
774 strValue = "0" + strValue;
775 }
776 iDec2 = 0;
777 }
778 int iMax = strValue.GetLength();
779 if (iDec2 > iMax) {
780 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
781 strValue += "0";
782 }
783 iMax = iDec2 + 1;
784 }
dsinclair64376be2016-03-31 20:03:24 -0700785
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700786 // for processing seperator style
787 if (iDec2 < iMax) {
788 if (iSepStyle == 0 || iSepStyle == 1) {
789 strValue.Insert(iDec2, '.');
790 iMax++;
791 } else if (iSepStyle == 2 || iSepStyle == 3) {
792 strValue.Insert(iDec2, ',');
793 iMax++;
794 }
795
796 if (iDec2 == 0)
797 strValue.Insert(iDec2, '0');
798 }
799 if (iSepStyle == 0 || iSepStyle == 2) {
800 char cSeperator;
801 if (iSepStyle == 0)
802 cSeperator = ',';
803 else
804 cSeperator = '.';
805
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700806 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700807 strValue.Insert(iDecPositive, cSeperator);
808 iMax++;
809 }
810 }
811
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700812 // for processing currency string
tsepez4c3debb2016-04-08 12:20:38 -0700813 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700814
815 if (bCurrencyPrepend)
thestigcf03f8e2016-05-09 12:36:18 -0700816 Value = wstrCurrency + Value;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817 else
thestigcf03f8e2016-05-09 12:36:18 -0700818 Value = Value + wstrCurrency;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700819
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820 // for processing negative style
821 if (iNegative) {
822 if (iNegStyle == 0) {
thestigcf03f8e2016-05-09 12:36:18 -0700823 Value = L"-" + Value;
824 } else if (iNegStyle == 2 || iNegStyle == 3) {
825 Value = L"(" + Value + L")";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826 }
827 if (iNegStyle == 1 || iNegStyle == 3) {
828 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700829 CJS_Array arColor(pRuntime);
830 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831 vColElm = L"RGB";
832 arColor.SetElement(0, vColElm);
833 vColElm = 1;
834 arColor.SetElement(1, vColElm);
835 vColElm = 0;
836 arColor.SetElement(2, vColElm);
837
838 arColor.SetElement(3, vColElm);
839
Tom Sepez67fd5df2015-10-08 12:24:19 -0700840 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700841 vProp.StartGetting();
842 vProp << arColor;
843 vProp.StartSetting();
844 fTarget->textColor(cc, vProp, sError); // red
845 }
846 }
847 } else {
848 if (iNegStyle == 1 || iNegStyle == 3) {
849 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700850 CJS_Array arColor(pRuntime);
851 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852 vColElm = L"RGB";
853 arColor.SetElement(0, vColElm);
854 vColElm = 0;
855 arColor.SetElement(1, vColElm);
856 arColor.SetElement(2, vColElm);
857 arColor.SetElement(3, vColElm);
858
Tom Sepez67fd5df2015-10-08 12:24:19 -0700859 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860 vProp.StartGetting();
861 fTarget->textColor(cc, vProp, sError);
862
Tom Sepez67fd5df2015-10-08 12:24:19 -0700863 CJS_Array aProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700864 vProp.ConvertToArray(aProp);
865
866 CPWL_Color crProp;
867 CPWL_Color crColor;
868 color::ConvertArrayToPWLColor(aProp, crProp);
869 color::ConvertArrayToPWLColor(arColor, crColor);
870
871 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700872 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873 vProp2.StartGetting();
874 vProp2 << arColor;
875 vProp2.StartSetting();
876 fTarget->textColor(cc, vProp2, sError);
877 }
878 }
879 }
880 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881#endif
882 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700883}
884
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
886// bCurrencyPrepend)
Lei Zhang945fdb72015-11-11 10:18:16 -0800887FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(
888 IJS_Context* cc,
889 const std::vector<CJS_Value>& params,
890 CJS_Value& vRet,
891 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700892 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700894
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895 if (params.size() < 2)
896 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700897
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898 if (!pEvent->m_pValue)
899 return FALSE;
thestigcf03f8e2016-05-09 12:36:18 -0700900
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700901 CFX_WideString& val = pEvent->Value();
thestigcf03f8e2016-05-09 12:36:18 -0700902 CFX_WideString& wstrChange = pEvent->Change();
903 CFX_WideString wstrValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700904
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -0700906 CFX_WideString wstrValue = StrTrim(wstrValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 if (wstrValue.IsEmpty())
908 return TRUE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700909
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910 CFX_WideString swTemp = wstrValue;
911 swTemp.Replace(L",", L".");
912 if (!IsNumber(swTemp.c_str())) {
913 pEvent->Rc() = FALSE;
914 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
915 Alert(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700916 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 return TRUE; // it happens after the last keystroke and before validating,
918 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700919
thestigcf03f8e2016-05-09 12:36:18 -0700920 CFX_WideString wstrSelected;
921 if (pEvent->SelStart() != -1) {
922 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
923 pEvent->SelEnd() - pEvent->SelStart());
924 }
925
926 bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927 if (bHasSign) {
928 // can't insert "change" in front to sign postion.
929 if (pEvent->SelStart() == 0) {
930 FX_BOOL& bRc = pEvent->Rc();
931 bRc = FALSE;
932 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700933 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700935
thestigcf03f8e2016-05-09 12:36:18 -0700936 int iSepStyle = params[1].ToInt();
937 if (iSepStyle < 0 || iSepStyle > 3)
938 iSepStyle = 0;
939 const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700940
thestigcf03f8e2016-05-09 12:36:18 -0700941 bool bHasSep = wstrValue.Find(cSep) != -1;
942 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
943 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 if (bHasSep) {
945 FX_BOOL& bRc = pEvent->Rc();
946 bRc = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700947 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948 }
949 bHasSep = TRUE;
950 continue;
951 }
thestigcf03f8e2016-05-09 12:36:18 -0700952 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 if (bHasSign) {
954 FX_BOOL& bRc = pEvent->Rc();
955 bRc = FALSE;
956 return TRUE;
957 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800958 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700959 if (i != 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 FX_BOOL& bRc = pEvent->Rc();
961 bRc = FALSE;
962 return TRUE;
963 }
964 if (pEvent->SelStart() != 0) {
965 FX_BOOL& bRc = pEvent->Rc();
966 bRc = FALSE;
967 return TRUE;
968 }
969 bHasSign = TRUE;
970 continue;
971 }
972
thestigcf03f8e2016-05-09 12:36:18 -0700973 if (!FXSYS_iswdigit(wstrChange[i])) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974 FX_BOOL& bRc = pEvent->Rc();
975 bRc = FALSE;
976 return TRUE;
977 }
978 }
979
thestigcf03f8e2016-05-09 12:36:18 -0700980 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
981 CFX_WideString wpostfix;
982 if (pEvent->SelEnd() < wstrValue.GetLength())
983 wpostfix = wstrValue.Mid(pEvent->SelEnd());
984 val = wprefix + wstrChange + wpostfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985 return TRUE;
986}
987
988// function AFPercent_Format(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -0800989FX_BOOL CJS_PublicMethods::AFPercent_Format(
990 IJS_Context* cc,
991 const std::vector<CJS_Value>& params,
992 CJS_Value& vRet,
993 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700994#if _FX_OS_ != _FX_ANDROID_
995 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997
998 if (params.size() != 2) {
999 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1000 return FALSE;
1001 }
1002 if (!pEvent->m_pValue)
1003 return FALSE;
1004
1005 CFX_WideString& Value = pEvent->Value();
1006 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1007 if (strValue.IsEmpty())
1008 return TRUE;
1009
1010 int iDec = params[0].ToInt();
1011 if (iDec < 0)
1012 iDec = -iDec;
1013
1014 int iSepStyle = params[1].ToInt();
1015 if (iSepStyle < 0 || iSepStyle > 3)
1016 iSepStyle = 0;
1017
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001019 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 dValue *= 100;
1021 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001022 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023
1024 int iDec2;
1025 int iNegative = 0;
1026 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1027 if (strValue.IsEmpty()) {
1028 dValue = 0;
1029 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1030 }
1031
1032 if (iDec2 < 0) {
1033 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1034 strValue = "0" + strValue;
1035 }
1036 iDec2 = 0;
1037 }
1038 int iMax = strValue.GetLength();
1039 if (iDec2 > iMax) {
1040 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1041 strValue += "0";
1042 }
1043 iMax = iDec2 + 1;
1044 }
dsinclair64376be2016-03-31 20:03:24 -07001045
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 // for processing seperator style
1047 if (iDec2 < iMax) {
1048 if (iSepStyle == 0 || iSepStyle == 1) {
1049 strValue.Insert(iDec2, '.');
1050 iMax++;
1051 } else if (iSepStyle == 2 || iSepStyle == 3) {
1052 strValue.Insert(iDec2, ',');
1053 iMax++;
1054 }
1055
1056 if (iDec2 == 0)
1057 strValue.Insert(iDec2, '0');
1058 }
1059 if (iSepStyle == 0 || iSepStyle == 2) {
1060 char cSeperator;
1061 if (iSepStyle == 0)
1062 cSeperator = ',';
1063 else
1064 cSeperator = '.';
1065
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001066 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 strValue.Insert(iDecPositive, cSeperator);
1068 iMax++;
1069 }
1070 }
dsinclair64376be2016-03-31 20:03:24 -07001071
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072 // negative mark
1073 if (iNegative)
1074 strValue = "-" + strValue;
1075 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001076 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077#endif
1078 return TRUE;
1079}
1080// AFPercent_Keystroke(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001081FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(
1082 IJS_Context* cc,
1083 const std::vector<CJS_Value>& params,
1084 CJS_Value& vRet,
1085 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086 return AFNumber_Keystroke(cc, params, vRet, sError);
1087}
1088
1089// function AFDate_FormatEx(cFormat)
Tom Sepezba038bc2015-10-08 12:03:00 -07001090FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001091 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092 CJS_Value& vRet,
1093 CFX_WideString& sError) {
1094 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096
1097 if (params.size() != 1) {
1098 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1099 return FALSE;
1100 }
1101 if (!pEvent->m_pValue)
1102 return FALSE;
1103
1104 CFX_WideString& val = pEvent->Value();
1105 CFX_WideString strValue = val;
1106 if (strValue.IsEmpty())
1107 return TRUE;
1108
1109 CFX_WideString sFormat = params[0].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001110 double dDate = 0.0f;
1111
1112 if (strValue.Find(L"GMT") != -1) {
1113 // for GMT format time
1114 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1115 dDate = MakeInterDate(strValue);
1116 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001117 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 }
1119
1120 if (JS_PortIsNan(dDate)) {
1121 CFX_WideString swMsg;
1122 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1123 sFormat.c_str());
1124 Alert(pContext, swMsg.c_str());
1125 return FALSE;
1126 }
1127
1128 val = MakeFormatDate(dDate, sFormat);
1129 return TRUE;
1130}
1131
tsepez745611b2016-04-12 16:46:34 -07001132double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001133 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001135 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001136 FX_WCHAR c = strValue.GetAt(i);
1137 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001138 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 sTemp = L"";
1140 continue;
1141 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 sTemp += c;
1143 }
Tom Sepezab277682016-02-17 10:07:21 -08001144 wsArray.push_back(sTemp);
1145 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146 return 0;
1147
Tom Sepez4246b002016-01-20 11:48:29 -08001148 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 sTemp = wsArray[1];
1150 if (sTemp.Compare(L"Jan") == 0)
1151 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001152 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001154 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001156 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001158 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001159 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001160 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001162 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001164 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001166 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001168 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001170 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001172 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 nMonth = 12;
1174
tsepez4c3debb2016-04-08 12:20:38 -07001175 int nDay = FX_atof(wsArray[2].AsStringC());
1176 int nHour = FX_atof(wsArray[3].AsStringC());
1177 int nMin = FX_atof(wsArray[4].AsStringC());
1178 int nSec = FX_atof(wsArray[5].AsStringC());
1179 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1181 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001182 if (JS_PortIsNan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001183 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184
1185 return dRet;
1186}
1187
1188// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001189FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(
1190 IJS_Context* cc,
1191 const std::vector<CJS_Value>& params,
1192 CJS_Value& vRet,
1193 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001194 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196
1197 if (params.size() != 1) {
1198 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1199 return FALSE;
1200 }
1201
1202 if (pEvent->WillCommit()) {
1203 if (!pEvent->m_pValue)
1204 return FALSE;
1205 CFX_WideString strValue = pEvent->Value();
1206 if (strValue.IsEmpty())
1207 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001208
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001209 CFX_WideString sFormat = params[0].ToCFXWideString();
Lei Zhang9559b7a2015-12-21 11:12:20 -08001210 bool bWrongFormat = FALSE;
1211 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 if (bWrongFormat || JS_PortIsNan(dRet)) {
1213 CFX_WideString swMsg;
1214 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1215 sFormat.c_str());
1216 Alert(pContext, swMsg.c_str());
1217 pEvent->Rc() = FALSE;
1218 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001219 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 }
1221 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001222}
1223
Tom Sepezba038bc2015-10-08 12:03:00 -07001224FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001225 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226 CJS_Value& vRet,
1227 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001228 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001230 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1231 return FALSE;
1232 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001233
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234 int iIndex = params[0].ToInt();
1235 const FX_WCHAR* cFormats[] = {L"m/d",
1236 L"m/d/yy",
1237 L"mm/dd/yy",
1238 L"mm/yy",
1239 L"d-mmm",
1240 L"d-mmm-yy",
1241 L"dd-mmm-yy",
1242 L"yy-mm-dd",
1243 L"mmm-yy",
1244 L"mmmm-yy",
1245 L"mmm d, yyyy",
1246 L"mmmm d, yyyy",
1247 L"m/d/yy h:MM tt",
1248 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001249
Lei Zhanga0f67242015-08-17 15:39:30 -07001250 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1251 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001252
Lei Zhang945fdb72015-11-11 10:18:16 -08001253 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001254 newParams.push_back(
1255 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 return AFDate_FormatEx(cc, newParams, vRet, sError);
1257}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001258
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001259// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001260FX_BOOL CJS_PublicMethods::AFDate_Keystroke(
1261 IJS_Context* cc,
1262 const std::vector<CJS_Value>& params,
1263 CJS_Value& vRet,
1264 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001265 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1268 return FALSE;
1269 }
1270
1271 int iIndex = params[0].ToInt();
1272 const FX_WCHAR* cFormats[] = {L"m/d",
1273 L"m/d/yy",
1274 L"mm/dd/yy",
1275 L"mm/yy",
1276 L"d-mmm",
1277 L"d-mmm-yy",
1278 L"dd-mmm-yy",
1279 L"yy-mm-dd",
1280 L"mmm-yy",
1281 L"mmmm-yy",
1282 L"mmm d, yyyy",
1283 L"mmmm d, yyyy",
1284 L"m/d/yy h:MM tt",
1285 L"m/d/yy HH:MM"};
1286
Lei Zhanga0f67242015-08-17 15:39:30 -07001287 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1288 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289
Lei Zhang945fdb72015-11-11 10:18:16 -08001290 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001291 newParams.push_back(
1292 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001293 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1294}
1295
1296// function AFTime_Format(ptf)
Tom Sepezba038bc2015-10-08 12:03:00 -07001297FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001298 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299 CJS_Value& vRet,
1300 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001301 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1304 return FALSE;
1305 }
1306
1307 int iIndex = params[0].ToInt();
1308 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1309 L"h:MM:ss tt"};
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_FormatEx(cc, newParams, vRet, sError);
1318}
1319
Lei Zhang945fdb72015-11-11 10:18:16 -08001320FX_BOOL CJS_PublicMethods::AFTime_Keystroke(
1321 IJS_Context* cc,
1322 const std::vector<CJS_Value>& params,
1323 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_KeystrokeEx(cc, newParams, vRet, sError);
1342}
1343
Tom Sepezba038bc2015-10-08 12:03:00 -07001344FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001345 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001346 CJS_Value& vRet,
1347 CFX_WideString& sError) {
1348 return AFDate_FormatEx(cc, params, vRet, sError);
1349}
1350
Lei Zhang945fdb72015-11-11 10:18:16 -08001351FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(
1352 IJS_Context* cc,
1353 const std::vector<CJS_Value>& params,
1354 CJS_Value& vRet,
1355 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001356 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1357}
1358
1359// function AFSpecial_Format(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001360FX_BOOL CJS_PublicMethods::AFSpecial_Format(
1361 IJS_Context* cc,
1362 const std::vector<CJS_Value>& params,
1363 CJS_Value& vRet,
1364 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001365 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001366
1367 if (params.size() != 1) {
1368 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1369 return FALSE;
1370 }
1371
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 if (!pEvent->m_pValue)
1374 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375
tsepez4f1f41f2016-03-28 14:13:16 -07001376 CFX_WideString wsSource = pEvent->Value();
1377 CFX_WideString wsFormat;
1378 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001379 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001380 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001382 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001383 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001384 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001385 case 2:
1386 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1387 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388 else
tsepez4f1f41f2016-03-28 14:13:16 -07001389 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001390 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001391 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001392 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001393 break;
1394 }
1395
tsepez4f1f41f2016-03-28 14:13:16 -07001396 pEvent->Value() = util::printx(wsFormat, wsSource);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 return TRUE;
1398}
1399
1400// function AFSpecial_KeystrokeEx(mask)
Lei Zhang945fdb72015-11-11 10:18:16 -08001401FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(
1402 IJS_Context* cc,
1403 const std::vector<CJS_Value>& params,
1404 CJS_Value& vRet,
1405 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1408
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409 if (params.size() < 1) {
1410 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1411 return FALSE;
1412 }
1413
1414 if (!pEvent->m_pValue)
1415 return FALSE;
1416 CFX_WideString& valEvent = pEvent->Value();
1417
1418 CFX_WideString wstrMask = params[0].ToCFXWideString();
1419 if (wstrMask.IsEmpty())
1420 return TRUE;
1421
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001423 if (valEvent.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424 return TRUE;
thestigcf03f8e2016-05-09 12:36:18 -07001425
1426 FX_STRSIZE iIndexMask = 0;
1427 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1428 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001429 break;
1430 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001431
thestigcf03f8e2016-05-09 12:36:18 -07001432 if (iIndexMask != wstrMask.GetLength() ||
1433 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001434 Alert(
1435 pContext,
1436 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1437 pEvent->Rc() = FALSE;
1438 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001439 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001440 }
1441
1442 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001443 if (wideChange.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001444 return TRUE;
1445
thestigcf03f8e2016-05-09 12:36:18 -07001446 CFX_WideString wChange = wideChange;
1447 FX_STRSIZE iIndexMask = pEvent->SelStart();
1448 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1449 pEvent->SelStart() - pEvent->SelEnd();
1450 if (combined_len > wstrMask.GetLength()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451 Alert(pContext,
1452 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1453 pEvent->Rc() = FALSE;
1454 return TRUE;
1455 }
1456
thestigcf03f8e2016-05-09 12:36:18 -07001457 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001458 Alert(pContext,
1459 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1460 pEvent->Rc() = FALSE;
1461 return TRUE;
1462 }
1463
thestigcf03f8e2016-05-09 12:36:18 -07001464 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1465 if (iIndexMask >= wstrMask.GetLength()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001466 Alert(pContext,
1467 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1468 pEvent->Rc() = FALSE;
1469 return TRUE;
1470 }
thestigcf03f8e2016-05-09 12:36:18 -07001471 FX_WCHAR wMask = wstrMask[iIndexMask];
1472 if (!isReservedMaskChar(wMask))
1473 wChange.SetAt(i, wMask);
1474
1475 if (!maskSatisfied(wChange[i], wMask)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001476 pEvent->Rc() = FALSE;
1477 return TRUE;
1478 }
1479 iIndexMask++;
1480 }
thestigcf03f8e2016-05-09 12:36:18 -07001481 wideChange = wChange;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001482 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001483}
1484
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485// function AFSpecial_Keystroke(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001486FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(
1487 IJS_Context* cc,
1488 const std::vector<CJS_Value>& params,
1489 CJS_Value& vRet,
1490 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492 if (params.size() != 1) {
1493 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1494 return FALSE;
1495 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001496
Tom Sepez67fd5df2015-10-08 12:24:19 -07001497 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498 if (!pEvent->m_pValue)
1499 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001500
thestigcf03f8e2016-05-09 12:36:18 -07001501 const char* cFormat = "";
1502 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001503 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504 cFormat = "99999";
1505 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001506 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001507 cFormat = "999999999";
1508 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001509 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001510 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511 cFormat = "9999999999";
1512 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001513 cFormat = "9999999";
1514 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001515 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516 cFormat = "999999999";
1517 break;
1518 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001519
Lei Zhang945fdb72015-11-11 10:18:16 -08001520 std::vector<CJS_Value> params2;
thestigcf03f8e2016-05-09 12:36:18 -07001521 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001523}
1524
Tom Sepezba038bc2015-10-08 12:03:00 -07001525FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001526 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 CJS_Value& vRet,
1528 CFX_WideString& sError) {
1529 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001530 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001531
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001532 if (params.size() != 1) {
1533 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1534 return FALSE;
1535 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001536
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001537 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001538 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001539 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001540
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001541 if (pEventHandler->WillCommit()) {
1542 vRet = swValue.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001543 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544 }
1545
1546 CFX_WideString prefix, postfix;
1547
1548 if (pEventHandler->SelStart() >= 0)
1549 prefix = swValue.Mid(0, pEventHandler->SelStart());
1550 else
1551 prefix = L"";
1552
1553 if (pEventHandler->SelEnd() >= 0 &&
1554 pEventHandler->SelEnd() <= swValue.GetLength())
1555 postfix = swValue.Mid(pEventHandler->SelEnd(),
1556 swValue.GetLength() - pEventHandler->SelEnd());
1557 else
1558 postfix = L"";
1559
1560 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1561
1562 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001563}
1564
Tom Sepezba038bc2015-10-08 12:03:00 -07001565FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001566 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001567 CJS_Value& vRet,
1568 CFX_WideString& sError) {
1569 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001570 ASSERT(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001571
1572 if (params.size() != 2) {
1573 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1574 return FALSE;
1575 }
1576
1577 CFX_WideString sValue = params[0].ToCFXWideString();
1578 CFX_WideString sFormat = params[1].ToCFXWideString();
1579
Lei Zhang9559b7a2015-12-21 11:12:20 -08001580 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001581
1582 if (JS_PortIsNan(dDate)) {
1583 CFX_WideString swMsg;
1584 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1585 sFormat.c_str());
1586 Alert((CJS_Context*)cc, swMsg.c_str());
1587 return FALSE;
1588 }
1589
1590 vRet = dDate;
1591 return TRUE;
1592}
1593
Tom Sepezba038bc2015-10-08 12:03:00 -07001594FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001595 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001596 CJS_Value& vRet,
1597 CFX_WideString& sError) {
1598 if (params.size() != 3) {
1599 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001600 ASSERT(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001601
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001602 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1603 return FALSE;
1604 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001605
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001606 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1607 params[1].ToDouble(), params[2].ToDouble());
1608 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001609}
1610
Tom Sepezba038bc2015-10-08 12:03:00 -07001611FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001612 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001613 CJS_Value& vRet,
1614 CFX_WideString& sError) {
Tom Sepez4246b002016-01-20 11:48:29 -08001615 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001616 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001617 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1618 return FALSE;
1619 }
Tom Sepez4246b002016-01-20 11:48:29 -08001620 CFX_WideString ws = params[0].ToCFXWideString();
1621 ws.Replace(L",", L".");
tsepezbd9748d2016-04-13 21:40:19 -07001622 vRet = ws.c_str();
Tom Sepez4246b002016-01-20 11:48:29 -08001623 vRet.MaybeCoerceToNumber();
1624 if (vRet.GetType() != CJS_Value::VT_number)
1625 vRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001626 return TRUE;
1627}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001628
Lei Zhang945fdb72015-11-11 10:18:16 -08001629FX_BOOL CJS_PublicMethods::AFSimple_Calculate(
1630 IJS_Context* cc,
1631 const std::vector<CJS_Value>& params,
1632 CJS_Value& vRet,
1633 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001634 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001635 if (params.size() != 2) {
1636 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1637 return FALSE;
1638 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001639
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001640 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001641 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001642 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1643 return FALSE;
1644 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001645
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001647 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001649
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001650 CFX_WideString sFunction = params[0].ToCFXWideString();
Tom Sepez67fd5df2015-10-08 12:24:19 -07001651 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001652
Tom Sepez67fd5df2015-10-08 12:24:19 -07001653 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1654 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001655 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001656
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001658 CJS_Value jsValue(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001659 FieldNameArray.GetElement(i, jsValue);
1660 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001661
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001662 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1663 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1664 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001665 switch (pFormField->GetFieldType()) {
1666 case FIELDTYPE_TEXTFIELD:
1667 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001668 CFX_WideString trimmed = pFormField->GetValue();
1669 trimmed.TrimRight();
1670 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001671 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001672 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001673 case FIELDTYPE_PUSHBUTTON: {
1674 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001675 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676 case FIELDTYPE_CHECKBOX:
1677 case FIELDTYPE_RADIOBUTTON: {
1678 dTemp = 0.0;
1679 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1680 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1681 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001682 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1683 trimmed.TrimRight();
1684 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001685 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001686 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001687 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001688 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001689 }
Tom Sepez4246b002016-01-20 11:48:29 -08001690 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001691 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001692 if (pFormField->CountSelectedItems() <= 1) {
1693 CFX_WideString trimmed = pFormField->GetValue();
1694 trimmed.TrimRight();
1695 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001696 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001697 }
1698 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001699 default:
1700 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001701 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001702
1703 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1704 wcscmp(sFunction.c_str(), L"MAX") == 0))
1705 dValue = dTemp;
1706
1707 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1708
1709 nFieldsCount++;
1710 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001711 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001712 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001713
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001714 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1715 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001716
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001717 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1718 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001719 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001720 if (pContext->GetEventHandler()->m_pValue)
1721 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001722
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001724}
1725
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001726/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001727** within the specified range. */
1728
Lei Zhang945fdb72015-11-11 10:18:16 -08001729FX_BOOL CJS_PublicMethods::AFRange_Validate(
1730 IJS_Context* cc,
1731 const std::vector<CJS_Value>& params,
1732 CJS_Value& vRet,
1733 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001734 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001735 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001736
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737 if (params.size() != 4) {
1738 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1739 return FALSE;
1740 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001741
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001742 if (!pEvent->m_pValue)
1743 return FALSE;
1744 if (pEvent->Value().IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001745 return TRUE;
tsepezb4c9f3f2016-04-13 15:41:21 -07001746 double dEentValue =
1747 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001748 FX_BOOL bGreaterThan = params[0].ToBool();
1749 double dGreaterThan = params[1].ToDouble();
1750 FX_BOOL bLessThan = params[2].ToBool();
1751 double dLessThan = params[3].ToDouble();
1752 CFX_WideString swMsg;
1753
1754 if (bGreaterThan && bLessThan) {
1755 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
1756 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
1757 params[1].ToCFXWideString().c_str(),
1758 params[3].ToCFXWideString().c_str());
1759 } else if (bGreaterThan) {
1760 if (dEentValue < dGreaterThan)
1761 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
1762 params[1].ToCFXWideString().c_str());
1763 } else if (bLessThan) {
1764 if (dEentValue > dLessThan)
1765 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
1766 params[3].ToCFXWideString().c_str());
1767 }
1768
1769 if (!swMsg.IsEmpty()) {
1770 Alert(pContext, swMsg.c_str());
1771 pEvent->Rc() = FALSE;
1772 }
1773 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001774}
1775
Tom Sepezba038bc2015-10-08 12:03:00 -07001776FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001777 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001778 CJS_Value& vRet,
1779 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001780 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 if (params.size() != 1) {
1782 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1783 return FALSE;
1784 }
1785
Tom Sepez67fd5df2015-10-08 12:24:19 -07001786 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1787 CJS_Array nums(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001788
1789 CFX_WideString str = params[0].ToCFXWideString();
1790 CFX_WideString sPart;
1791
1792 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1793 str = L"0" + str;
1794
1795 int nIndex = 0;
1796 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1797 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001798 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001799 sPart += wc;
1800 } else {
1801 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001802 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001803 sPart = L"";
1804 nIndex++;
1805 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001806 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001807 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001808
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001809 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001810 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001811 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001812
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001813 if (nums.GetLength() > 0)
1814 vRet = nums;
1815 else
1816 vRet.SetNull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001817
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001818 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001819}