blob: fa7b5d4d3627b12563c7d6fa842bb162550bd89f [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()) {
ochanga0a3bc32016-05-12 15:22:48 -0700906 CFX_WideString swTemp = StrTrim(wstrValue);
907 if (swTemp.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 return TRUE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700909
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910 swTemp.Replace(L",", L".");
911 if (!IsNumber(swTemp.c_str())) {
912 pEvent->Rc() = FALSE;
913 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
914 Alert(pContext, sError.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700915 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 return TRUE; // it happens after the last keystroke and before validating,
917 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700918
thestigcf03f8e2016-05-09 12:36:18 -0700919 CFX_WideString wstrSelected;
920 if (pEvent->SelStart() != -1) {
921 wstrSelected = wstrValue.Mid(pEvent->SelStart(),
922 pEvent->SelEnd() - pEvent->SelStart());
923 }
924
925 bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926 if (bHasSign) {
927 // can't insert "change" in front to sign postion.
928 if (pEvent->SelStart() == 0) {
929 FX_BOOL& bRc = pEvent->Rc();
930 bRc = FALSE;
931 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700932 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700933 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700934
thestigcf03f8e2016-05-09 12:36:18 -0700935 int iSepStyle = params[1].ToInt();
936 if (iSepStyle < 0 || iSepStyle > 3)
937 iSepStyle = 0;
938 const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L',';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700939
thestigcf03f8e2016-05-09 12:36:18 -0700940 bool bHasSep = wstrValue.Find(cSep) != -1;
941 for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) {
942 if (wstrChange[i] == cSep) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943 if (bHasSep) {
944 FX_BOOL& bRc = pEvent->Rc();
945 bRc = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700946 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 }
948 bHasSep = TRUE;
949 continue;
950 }
thestigcf03f8e2016-05-09 12:36:18 -0700951 if (wstrChange[i] == L'-') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952 if (bHasSign) {
953 FX_BOOL& bRc = pEvent->Rc();
954 bRc = FALSE;
955 return TRUE;
956 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800957 // sign's position is not correct
thestigcf03f8e2016-05-09 12:36:18 -0700958 if (i != 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 FX_BOOL& bRc = pEvent->Rc();
960 bRc = FALSE;
961 return TRUE;
962 }
963 if (pEvent->SelStart() != 0) {
964 FX_BOOL& bRc = pEvent->Rc();
965 bRc = FALSE;
966 return TRUE;
967 }
968 bHasSign = TRUE;
969 continue;
970 }
971
thestigcf03f8e2016-05-09 12:36:18 -0700972 if (!FXSYS_iswdigit(wstrChange[i])) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 FX_BOOL& bRc = pEvent->Rc();
974 bRc = FALSE;
975 return TRUE;
976 }
977 }
978
thestigcf03f8e2016-05-09 12:36:18 -0700979 CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart());
980 CFX_WideString wpostfix;
981 if (pEvent->SelEnd() < wstrValue.GetLength())
982 wpostfix = wstrValue.Mid(pEvent->SelEnd());
983 val = wprefix + wstrChange + wpostfix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 return TRUE;
985}
986
987// function AFPercent_Format(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -0800988FX_BOOL CJS_PublicMethods::AFPercent_Format(
989 IJS_Context* cc,
990 const std::vector<CJS_Value>& params,
991 CJS_Value& vRet,
992 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993#if _FX_OS_ != _FX_ANDROID_
994 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996
997 if (params.size() != 2) {
998 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
999 return FALSE;
1000 }
1001 if (!pEvent->m_pValue)
1002 return FALSE;
1003
1004 CFX_WideString& Value = pEvent->Value();
1005 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1006 if (strValue.IsEmpty())
1007 return TRUE;
1008
1009 int iDec = params[0].ToInt();
1010 if (iDec < 0)
1011 iDec = -iDec;
1012
1013 int iSepStyle = params[1].ToInt();
1014 if (iSepStyle < 0 || iSepStyle > 3)
1015 iSepStyle = 0;
1016
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017 // for processing decimal places
tsepezb4c9f3f2016-04-13 15:41:21 -07001018 double dValue = atof(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 dValue *= 100;
1020 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001021 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022
1023 int iDec2;
1024 int iNegative = 0;
1025 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1026 if (strValue.IsEmpty()) {
1027 dValue = 0;
1028 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1029 }
1030
1031 if (iDec2 < 0) {
1032 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1033 strValue = "0" + strValue;
1034 }
1035 iDec2 = 0;
1036 }
1037 int iMax = strValue.GetLength();
1038 if (iDec2 > iMax) {
1039 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1040 strValue += "0";
1041 }
1042 iMax = iDec2 + 1;
1043 }
dsinclair64376be2016-03-31 20:03:24 -07001044
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045 // for processing seperator style
1046 if (iDec2 < iMax) {
1047 if (iSepStyle == 0 || iSepStyle == 1) {
1048 strValue.Insert(iDec2, '.');
1049 iMax++;
1050 } else if (iSepStyle == 2 || iSepStyle == 3) {
1051 strValue.Insert(iDec2, ',');
1052 iMax++;
1053 }
1054
1055 if (iDec2 == 0)
1056 strValue.Insert(iDec2, '0');
1057 }
1058 if (iSepStyle == 0 || iSepStyle == 2) {
1059 char cSeperator;
1060 if (iSepStyle == 0)
1061 cSeperator = ',';
1062 else
1063 cSeperator = '.';
1064
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001065 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 strValue.Insert(iDecPositive, cSeperator);
1067 iMax++;
1068 }
1069 }
dsinclair64376be2016-03-31 20:03:24 -07001070
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071 // negative mark
1072 if (iNegative)
1073 strValue = "-" + strValue;
1074 strValue += "%";
tsepez4c3debb2016-04-08 12:20:38 -07001075 Value = CFX_WideString::FromLocal(strValue.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076#endif
1077 return TRUE;
1078}
1079// AFPercent_Keystroke(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001080FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(
1081 IJS_Context* cc,
1082 const std::vector<CJS_Value>& params,
1083 CJS_Value& vRet,
1084 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085 return AFNumber_Keystroke(cc, params, vRet, sError);
1086}
1087
1088// function AFDate_FormatEx(cFormat)
Tom Sepezba038bc2015-10-08 12:03:00 -07001089FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001090 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001091 CJS_Value& vRet,
1092 CFX_WideString& sError) {
1093 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095
1096 if (params.size() != 1) {
1097 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1098 return FALSE;
1099 }
1100 if (!pEvent->m_pValue)
1101 return FALSE;
1102
1103 CFX_WideString& val = pEvent->Value();
1104 CFX_WideString strValue = val;
1105 if (strValue.IsEmpty())
1106 return TRUE;
1107
1108 CFX_WideString sFormat = params[0].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001109 double dDate = 0.0f;
1110
1111 if (strValue.Find(L"GMT") != -1) {
1112 // for GMT format time
1113 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1114 dDate = MakeInterDate(strValue);
1115 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001116 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117 }
1118
1119 if (JS_PortIsNan(dDate)) {
1120 CFX_WideString swMsg;
1121 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1122 sFormat.c_str());
1123 Alert(pContext, swMsg.c_str());
1124 return FALSE;
1125 }
1126
1127 val = MakeFormatDate(dDate, sFormat);
1128 return TRUE;
1129}
1130
tsepez745611b2016-04-12 16:46:34 -07001131double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
Tom Sepezab277682016-02-17 10:07:21 -08001132 std::vector<CFX_WideString> wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001134 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 FX_WCHAR c = strValue.GetAt(i);
1136 if (c == L' ' || c == L':') {
Tom Sepezab277682016-02-17 10:07:21 -08001137 wsArray.push_back(sTemp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138 sTemp = L"";
1139 continue;
1140 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 sTemp += c;
1142 }
Tom Sepezab277682016-02-17 10:07:21 -08001143 wsArray.push_back(sTemp);
1144 if (wsArray.size() != 8)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145 return 0;
1146
Tom Sepez4246b002016-01-20 11:48:29 -08001147 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 sTemp = wsArray[1];
1149 if (sTemp.Compare(L"Jan") == 0)
1150 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001151 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001152 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001153 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001155 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001157 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001159 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001161 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001163 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001165 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001167 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001169 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001171 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 nMonth = 12;
1173
tsepez4c3debb2016-04-08 12:20:38 -07001174 int nDay = FX_atof(wsArray[2].AsStringC());
1175 int nHour = FX_atof(wsArray[3].AsStringC());
1176 int nMin = FX_atof(wsArray[4].AsStringC());
1177 int nSec = FX_atof(wsArray[5].AsStringC());
1178 int nYear = FX_atof(wsArray[7].AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1180 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001181 if (JS_PortIsNan(dRet))
tsepez018935c2016-04-15 13:15:12 -07001182 dRet = JS_DateParse(strValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183
1184 return dRet;
1185}
1186
1187// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001188FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(
1189 IJS_Context* cc,
1190 const std::vector<CJS_Value>& params,
1191 CJS_Value& vRet,
1192 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001194 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195
1196 if (params.size() != 1) {
1197 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1198 return FALSE;
1199 }
1200
1201 if (pEvent->WillCommit()) {
1202 if (!pEvent->m_pValue)
1203 return FALSE;
1204 CFX_WideString strValue = pEvent->Value();
1205 if (strValue.IsEmpty())
1206 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001207
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001208 CFX_WideString sFormat = params[0].ToCFXWideString();
Lei Zhang9559b7a2015-12-21 11:12:20 -08001209 bool bWrongFormat = FALSE;
1210 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001211 if (bWrongFormat || JS_PortIsNan(dRet)) {
1212 CFX_WideString swMsg;
1213 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1214 sFormat.c_str());
1215 Alert(pContext, swMsg.c_str());
1216 pEvent->Rc() = FALSE;
1217 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001218 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 }
1220 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001221}
1222
Tom Sepezba038bc2015-10-08 12:03:00 -07001223FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001224 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 CJS_Value& vRet,
1226 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001227 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001228 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1230 return FALSE;
1231 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001232
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233 int iIndex = params[0].ToInt();
1234 const FX_WCHAR* cFormats[] = {L"m/d",
1235 L"m/d/yy",
1236 L"mm/dd/yy",
1237 L"mm/yy",
1238 L"d-mmm",
1239 L"d-mmm-yy",
1240 L"dd-mmm-yy",
1241 L"yy-mm-dd",
1242 L"mmm-yy",
1243 L"mmmm-yy",
1244 L"mmm d, yyyy",
1245 L"mmmm d, yyyy",
1246 L"m/d/yy h:MM tt",
1247 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001248
Lei Zhanga0f67242015-08-17 15:39:30 -07001249 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1250 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001251
Lei Zhang945fdb72015-11-11 10:18:16 -08001252 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001253 newParams.push_back(
1254 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001255 return AFDate_FormatEx(cc, newParams, vRet, sError);
1256}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001257
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001259FX_BOOL CJS_PublicMethods::AFDate_Keystroke(
1260 IJS_Context* cc,
1261 const std::vector<CJS_Value>& params,
1262 CJS_Value& vRet,
1263 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001264 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1267 return FALSE;
1268 }
1269
1270 int iIndex = params[0].ToInt();
1271 const FX_WCHAR* cFormats[] = {L"m/d",
1272 L"m/d/yy",
1273 L"mm/dd/yy",
1274 L"mm/yy",
1275 L"d-mmm",
1276 L"d-mmm-yy",
1277 L"dd-mmm-yy",
1278 L"yy-mm-dd",
1279 L"mmm-yy",
1280 L"mmmm-yy",
1281 L"mmm d, yyyy",
1282 L"mmmm d, yyyy",
1283 L"m/d/yy h:MM tt",
1284 L"m/d/yy HH:MM"};
1285
Lei Zhanga0f67242015-08-17 15:39:30 -07001286 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1287 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288
Lei Zhang945fdb72015-11-11 10:18:16 -08001289 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001290 newParams.push_back(
1291 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1293}
1294
1295// function AFTime_Format(ptf)
Tom Sepezba038bc2015-10-08 12:03:00 -07001296FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001297 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001298 CJS_Value& vRet,
1299 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001300 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1303 return FALSE;
1304 }
1305
1306 int iIndex = params[0].ToInt();
1307 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1308 L"h:MM:ss tt"};
1309
Lei Zhanga0f67242015-08-17 15:39:30 -07001310 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1311 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312
Lei Zhang945fdb72015-11-11 10:18:16 -08001313 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001314 newParams.push_back(
1315 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001316 return AFDate_FormatEx(cc, newParams, vRet, sError);
1317}
1318
Lei Zhang945fdb72015-11-11 10:18:16 -08001319FX_BOOL CJS_PublicMethods::AFTime_Keystroke(
1320 IJS_Context* cc,
1321 const std::vector<CJS_Value>& params,
1322 CJS_Value& vRet,
1323 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001324 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001325 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1327 return FALSE;
1328 }
1329
1330 int iIndex = params[0].ToInt();
1331 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1332 L"h:MM:ss tt"};
1333
Lei Zhanga0f67242015-08-17 15:39:30 -07001334 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1335 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001336
Lei Zhang945fdb72015-11-11 10:18:16 -08001337 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001338 newParams.push_back(
1339 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001340 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1341}
1342
Tom Sepezba038bc2015-10-08 12:03:00 -07001343FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001344 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001345 CJS_Value& vRet,
1346 CFX_WideString& sError) {
1347 return AFDate_FormatEx(cc, params, vRet, sError);
1348}
1349
Lei Zhang945fdb72015-11-11 10:18:16 -08001350FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(
1351 IJS_Context* cc,
1352 const std::vector<CJS_Value>& params,
1353 CJS_Value& vRet,
1354 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001355 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1356}
1357
1358// function AFSpecial_Format(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001359FX_BOOL CJS_PublicMethods::AFSpecial_Format(
1360 IJS_Context* cc,
1361 const std::vector<CJS_Value>& params,
1362 CJS_Value& vRet,
1363 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001364 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001365
1366 if (params.size() != 1) {
1367 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1368 return FALSE;
1369 }
1370
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001371 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 if (!pEvent->m_pValue)
1373 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374
tsepez4f1f41f2016-03-28 14:13:16 -07001375 CFX_WideString wsSource = pEvent->Value();
1376 CFX_WideString wsFormat;
1377 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001378 case 0:
tsepez4f1f41f2016-03-28 14:13:16 -07001379 wsFormat = L"99999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001381 case 1:
tsepez4f1f41f2016-03-28 14:13:16 -07001382 wsFormat = L"99999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001384 case 2:
1385 if (util::printx(L"9999999999", wsSource).GetLength() >= 10)
1386 wsFormat = L"(999) 999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387 else
tsepez4f1f41f2016-03-28 14:13:16 -07001388 wsFormat = L"999-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001389 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001390 case 3:
tsepez4f1f41f2016-03-28 14:13:16 -07001391 wsFormat = L"999-99-9999";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001392 break;
1393 }
1394
tsepez4f1f41f2016-03-28 14:13:16 -07001395 pEvent->Value() = util::printx(wsFormat, wsSource);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396 return TRUE;
1397}
1398
1399// function AFSpecial_KeystrokeEx(mask)
Lei Zhang945fdb72015-11-11 10:18:16 -08001400FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(
1401 IJS_Context* cc,
1402 const std::vector<CJS_Value>& params,
1403 CJS_Value& vRet,
1404 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1407
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408 if (params.size() < 1) {
1409 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1410 return FALSE;
1411 }
1412
1413 if (!pEvent->m_pValue)
1414 return FALSE;
1415 CFX_WideString& valEvent = pEvent->Value();
1416
1417 CFX_WideString wstrMask = params[0].ToCFXWideString();
1418 if (wstrMask.IsEmpty())
1419 return TRUE;
1420
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421 if (pEvent->WillCommit()) {
thestigcf03f8e2016-05-09 12:36:18 -07001422 if (valEvent.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 return TRUE;
thestigcf03f8e2016-05-09 12:36:18 -07001424
1425 FX_STRSIZE iIndexMask = 0;
1426 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) {
1427 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001428 break;
1429 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001430
thestigcf03f8e2016-05-09 12:36:18 -07001431 if (iIndexMask != wstrMask.GetLength() ||
1432 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001433 Alert(
1434 pContext,
1435 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1436 pEvent->Rc() = FALSE;
1437 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001438 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001439 }
1440
1441 CFX_WideString& wideChange = pEvent->Change();
thestigcf03f8e2016-05-09 12:36:18 -07001442 if (wideChange.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443 return TRUE;
1444
thestigcf03f8e2016-05-09 12:36:18 -07001445 CFX_WideString wChange = wideChange;
1446 FX_STRSIZE iIndexMask = pEvent->SelStart();
1447 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() +
1448 pEvent->SelStart() - pEvent->SelEnd();
1449 if (combined_len > wstrMask.GetLength()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001450 Alert(pContext,
1451 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1452 pEvent->Rc() = FALSE;
1453 return TRUE;
1454 }
1455
thestigcf03f8e2016-05-09 12:36:18 -07001456 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457 Alert(pContext,
1458 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1459 pEvent->Rc() = FALSE;
1460 return TRUE;
1461 }
1462
thestigcf03f8e2016-05-09 12:36:18 -07001463 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) {
1464 if (iIndexMask >= wstrMask.GetLength()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001465 Alert(pContext,
1466 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1467 pEvent->Rc() = FALSE;
1468 return TRUE;
1469 }
thestigcf03f8e2016-05-09 12:36:18 -07001470 FX_WCHAR wMask = wstrMask[iIndexMask];
1471 if (!isReservedMaskChar(wMask))
1472 wChange.SetAt(i, wMask);
1473
1474 if (!maskSatisfied(wChange[i], wMask)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475 pEvent->Rc() = FALSE;
1476 return TRUE;
1477 }
1478 iIndexMask++;
1479 }
thestigcf03f8e2016-05-09 12:36:18 -07001480 wideChange = wChange;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001481 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001482}
1483
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001484// function AFSpecial_Keystroke(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001485FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(
1486 IJS_Context* cc,
1487 const std::vector<CJS_Value>& params,
1488 CJS_Value& vRet,
1489 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491 if (params.size() != 1) {
1492 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1493 return FALSE;
1494 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001495
Tom Sepez67fd5df2015-10-08 12:24:19 -07001496 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001497 if (!pEvent->m_pValue)
1498 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001499
thestigcf03f8e2016-05-09 12:36:18 -07001500 const char* cFormat = "";
1501 switch (params[0].ToInt()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001502 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503 cFormat = "99999";
1504 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001505 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001506 cFormat = "999999999";
1507 break;
tsepez4f1f41f2016-03-28 14:13:16 -07001508 case 2:
thestigcf03f8e2016-05-09 12:36:18 -07001509 if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001510 cFormat = "9999999999";
1511 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512 cFormat = "9999999";
1513 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001514 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515 cFormat = "999999999";
1516 break;
1517 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001518
Lei Zhang945fdb72015-11-11 10:18:16 -08001519 std::vector<CJS_Value> params2;
thestigcf03f8e2016-05-09 12:36:18 -07001520 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001522}
1523
Tom Sepezba038bc2015-10-08 12:03:00 -07001524FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001525 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001526 CJS_Value& vRet,
1527 CFX_WideString& sError) {
1528 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001529 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001530
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001531 if (params.size() != 1) {
1532 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1533 return FALSE;
1534 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001535
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001536 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001537 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001539
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001540 if (pEventHandler->WillCommit()) {
1541 vRet = swValue.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001542 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 }
1544
1545 CFX_WideString prefix, postfix;
1546
1547 if (pEventHandler->SelStart() >= 0)
1548 prefix = swValue.Mid(0, pEventHandler->SelStart());
1549 else
1550 prefix = L"";
1551
1552 if (pEventHandler->SelEnd() >= 0 &&
1553 pEventHandler->SelEnd() <= swValue.GetLength())
1554 postfix = swValue.Mid(pEventHandler->SelEnd(),
1555 swValue.GetLength() - pEventHandler->SelEnd());
1556 else
1557 postfix = L"";
1558
1559 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1560
1561 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001562}
1563
Tom Sepezba038bc2015-10-08 12:03:00 -07001564FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001565 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566 CJS_Value& vRet,
1567 CFX_WideString& sError) {
1568 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001569 ASSERT(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570
1571 if (params.size() != 2) {
1572 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1573 return FALSE;
1574 }
1575
1576 CFX_WideString sValue = params[0].ToCFXWideString();
1577 CFX_WideString sFormat = params[1].ToCFXWideString();
1578
Lei Zhang9559b7a2015-12-21 11:12:20 -08001579 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001580
1581 if (JS_PortIsNan(dDate)) {
1582 CFX_WideString swMsg;
1583 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1584 sFormat.c_str());
1585 Alert((CJS_Context*)cc, swMsg.c_str());
1586 return FALSE;
1587 }
1588
1589 vRet = dDate;
1590 return TRUE;
1591}
1592
Tom Sepezba038bc2015-10-08 12:03:00 -07001593FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001594 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001595 CJS_Value& vRet,
1596 CFX_WideString& sError) {
1597 if (params.size() != 3) {
1598 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001599 ASSERT(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001600
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001601 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1602 return FALSE;
1603 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001604
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001605 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1606 params[1].ToDouble(), params[2].ToDouble());
1607 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001608}
1609
Tom Sepezba038bc2015-10-08 12:03:00 -07001610FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001611 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001612 CJS_Value& vRet,
1613 CFX_WideString& sError) {
Tom Sepez4246b002016-01-20 11:48:29 -08001614 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001615 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001616 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1617 return FALSE;
1618 }
Tom Sepez4246b002016-01-20 11:48:29 -08001619 CFX_WideString ws = params[0].ToCFXWideString();
1620 ws.Replace(L",", L".");
tsepezbd9748d2016-04-13 21:40:19 -07001621 vRet = ws.c_str();
Tom Sepez4246b002016-01-20 11:48:29 -08001622 vRet.MaybeCoerceToNumber();
1623 if (vRet.GetType() != CJS_Value::VT_number)
1624 vRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001625 return TRUE;
1626}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001627
Lei Zhang945fdb72015-11-11 10:18:16 -08001628FX_BOOL CJS_PublicMethods::AFSimple_Calculate(
1629 IJS_Context* cc,
1630 const std::vector<CJS_Value>& params,
1631 CJS_Value& vRet,
1632 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001633 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001634 if (params.size() != 2) {
1635 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1636 return FALSE;
1637 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001638
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001639 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001640 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001641 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1642 return FALSE;
1643 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001644
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001647 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001648
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001649 CFX_WideString sFunction = params[0].ToCFXWideString();
Tom Sepez67fd5df2015-10-08 12:24:19 -07001650 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001651
Tom Sepez67fd5df2015-10-08 12:24:19 -07001652 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1653 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001654 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001655
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001656 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001657 CJS_Value jsValue(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001658 FieldNameArray.GetElement(i, jsValue);
1659 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001660
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001661 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1662 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1663 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664 switch (pFormField->GetFieldType()) {
1665 case FIELDTYPE_TEXTFIELD:
1666 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001667 CFX_WideString trimmed = pFormField->GetValue();
1668 trimmed.TrimRight();
1669 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001670 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001671 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001672 case FIELDTYPE_PUSHBUTTON: {
1673 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001674 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001675 case FIELDTYPE_CHECKBOX:
1676 case FIELDTYPE_RADIOBUTTON: {
1677 dTemp = 0.0;
1678 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1679 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1680 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001681 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1682 trimmed.TrimRight();
1683 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001684 dTemp = FX_atof(trimmed.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001685 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001686 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001687 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001688 }
Tom Sepez4246b002016-01-20 11:48:29 -08001689 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001690 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001691 if (pFormField->CountSelectedItems() <= 1) {
1692 CFX_WideString trimmed = pFormField->GetValue();
1693 trimmed.TrimRight();
1694 trimmed.TrimLeft();
tsepez4c3debb2016-04-08 12:20:38 -07001695 dTemp = FX_atof(trimmed.AsStringC());
Tom Sepez4246b002016-01-20 11:48:29 -08001696 }
1697 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001698 default:
1699 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001700 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001701
1702 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1703 wcscmp(sFunction.c_str(), L"MAX") == 0))
1704 dValue = dTemp;
1705
1706 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1707
1708 nFieldsCount++;
1709 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001710 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001711 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001712
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1714 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001715
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001716 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1717 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001718 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001719 if (pContext->GetEventHandler()->m_pValue)
1720 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001721
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001722 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001723}
1724
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001725/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001726** within the specified range. */
1727
Lei Zhang945fdb72015-11-11 10:18:16 -08001728FX_BOOL CJS_PublicMethods::AFRange_Validate(
1729 IJS_Context* cc,
1730 const std::vector<CJS_Value>& params,
1731 CJS_Value& vRet,
1732 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001733 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001734 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001735
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001736 if (params.size() != 4) {
1737 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1738 return FALSE;
1739 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001740
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001741 if (!pEvent->m_pValue)
1742 return FALSE;
1743 if (pEvent->Value().IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001744 return TRUE;
tsepezb4c9f3f2016-04-13 15:41:21 -07001745 double dEentValue =
1746 atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001747 FX_BOOL bGreaterThan = params[0].ToBool();
1748 double dGreaterThan = params[1].ToDouble();
1749 FX_BOOL bLessThan = params[2].ToBool();
1750 double dLessThan = params[3].ToDouble();
1751 CFX_WideString swMsg;
1752
1753 if (bGreaterThan && bLessThan) {
1754 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
1755 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
1756 params[1].ToCFXWideString().c_str(),
1757 params[3].ToCFXWideString().c_str());
1758 } else if (bGreaterThan) {
1759 if (dEentValue < dGreaterThan)
1760 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
1761 params[1].ToCFXWideString().c_str());
1762 } else if (bLessThan) {
1763 if (dEentValue > dLessThan)
1764 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
1765 params[3].ToCFXWideString().c_str());
1766 }
1767
1768 if (!swMsg.IsEmpty()) {
1769 Alert(pContext, swMsg.c_str());
1770 pEvent->Rc() = FALSE;
1771 }
1772 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001773}
1774
Tom Sepezba038bc2015-10-08 12:03:00 -07001775FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001776 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001777 CJS_Value& vRet,
1778 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001779 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001780 if (params.size() != 1) {
1781 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1782 return FALSE;
1783 }
1784
Tom Sepez67fd5df2015-10-08 12:24:19 -07001785 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1786 CJS_Array nums(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001787
1788 CFX_WideString str = params[0].ToCFXWideString();
1789 CFX_WideString sPart;
1790
1791 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1792 str = L"0" + str;
1793
1794 int nIndex = 0;
1795 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1796 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001797 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001798 sPart += wc;
1799 } else {
1800 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001801 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001802 sPart = L"";
1803 nIndex++;
1804 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001805 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001806 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001807
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001808 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001809 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001810 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001811
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812 if (nums.GetLength() > 0)
1813 vRet = nums;
1814 else
1815 vRet.SetNull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001816
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001817 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001818}