blob: 3c6d36fdd84ee19d80c84bd0a3e35982ed5b4ba1 [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
Tom Sepez37458412015-10-06 11:33:46 -07007#include "PublicMethods.h"
8
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
10
Tom Sepez37458412015-10-06 11:33:46 -070011#include "Field.h"
12#include "JS_Context.h"
13#include "JS_Define.h"
14#include "JS_EventHandler.h"
15#include "JS_Object.h"
16#include "JS_Runtime.h"
17#include "JS_Value.h"
18#include "color.h"
Dan Sinclair10cfea12015-11-16 13:09:00 -050019#include "core/include/fxcrt/fx_ext.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080020#include "fpdfsdk/include/fsdk_mgr.h" // For CPDFDoc_Environment.
21#include "fpdfsdk/include/javascript/IJavaScript.h"
Tom Sepez37458412015-10-06 11:33:46 -070022#include "resource.h"
23#include "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
Tom Sepezdfbf8e72015-10-14 14:17:26 -070054static const FX_WCHAR* const months[] = {L"Jan",
55 L"Feb",
56 L"Mar",
57 L"Apr",
58 L"May",
59 L"Jun",
60 L"Jul",
61 L"Aug",
62 L"Sep",
63 L"Oct",
64 L"Nov",
65 L"Dec"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080067static const FX_WCHAR* const fullmonths[] = {L"January",
68 L"February",
69 L"March",
70 L"April",
71 L"May",
72 L"June",
73 L"July",
74 L"August",
75 L"September",
76 L"October",
77 L"November",
78 L"December"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080FX_BOOL CJS_PublicMethods::IsNumber(const FX_WCHAR* string) {
81 CFX_WideString sTrim = StrTrim(string);
82 const FX_WCHAR* pTrim = sTrim.c_str();
83 const FX_WCHAR* p = pTrim;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 FX_BOOL bDot = FALSE;
86 FX_BOOL bKXJS = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 wchar_t c;
89 while ((c = *p)) {
90 if (c == '.' || c == ',') {
91 if (bDot)
92 return FALSE;
93 bDot = TRUE;
94 } else if (c == '-' || c == '+') {
95 if (p != pTrim)
96 return FALSE;
97 } else if (c == 'e' || c == 'E') {
98 if (bKXJS)
99 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 p++;
102 c = *p;
103 if (c == '+' || c == '-') {
104 bKXJS = TRUE;
105 } else {
106 return FALSE;
107 }
Lei Zhang9559b7a2015-12-21 11:12:20 -0800108 } else if (!FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 return FALSE;
110 }
111 p++;
112 }
113
114 return TRUE;
115}
116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117FX_BOOL CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
118 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':
126 return TRUE;
127 default:
128 return (c_Change == c_Mask);
129 }
130}
131
132FX_BOOL CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
133 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
155CFX_WideString CJS_PublicMethods::StrLTrim(const FX_WCHAR* pStr) {
156 while (*pStr && *pStr == L' ')
157 pStr++;
158
159 return pStr;
160}
161
162CFX_WideString CJS_PublicMethods::StrRTrim(const FX_WCHAR* pStr) {
163 const FX_WCHAR* p = pStr;
164 while (*p)
165 p++;
166 while (p > pStr && *(p - 1) == L' ')
167 p--;
168
169 return CFX_WideString(pStr, p - pStr);
170}
171
172CFX_WideString CJS_PublicMethods::StrTrim(const FX_WCHAR* pStr) {
173 return StrRTrim(StrLTrim(pStr).c_str());
174}
175
176CFX_ByteString CJS_PublicMethods::StrLTrim(const FX_CHAR* pStr) {
177 while (*pStr && *pStr == ' ')
178 pStr++;
179
180 return pStr;
181}
182
183CFX_ByteString CJS_PublicMethods::StrRTrim(const FX_CHAR* pStr) {
184 const FX_CHAR* p = pStr;
185 while (*p)
186 p++;
187 while (p > pStr && *(p - 1) == L' ')
188 p--;
189
190 return CFX_ByteString(pStr, p - pStr);
191}
192
193CFX_ByteString CJS_PublicMethods::StrTrim(const FX_CHAR* pStr) {
194 return StrRTrim(StrLTrim(pStr));
195}
196
Tom Sepez67fd5df2015-10-08 12:24:19 -0700197CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 CJS_Value val) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700199 CJS_Array StrArray(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 if (val.IsArrayObject()) {
201 val.ConvertToArray(StrArray);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700202 return StrArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 }
204 CFX_WideString wsStr = val.ToCFXWideString();
205 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
206 const char* p = (const char*)t;
207
208 int ch = ',';
209 int nIndex = 0;
210
211 while (*p) {
212 const char* pTemp = strchr(p, ch);
Lei Zhang997de612015-11-04 18:17:53 -0800213 if (!pTemp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700214 StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(p).c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 }
Lei Zhang997de612015-11-04 18:17:53 -0800217
218 char* pSub = new char[pTemp - p + 1];
219 strncpy(pSub, p, pTemp - p);
220 *(pSub + (pTemp - p)) = '\0';
221
222 StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(pSub).c_str()));
223 delete[] pSub;
224
225 nIndex++;
226 p = ++pTemp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 }
228 return StrArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229}
230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& string,
232 int nStart,
233 int& nSkip,
234 int nMaxStep) {
235 int nRet = 0;
236 nSkip = 0;
237 for (int i = nStart, sz = string.GetLength(); i < sz; i++) {
238 if (i - nStart > 10)
239 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 FX_WCHAR c = string.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800242 if (!FXSYS_iswdigit(c))
243 break;
244
245 nRet = nRet * 10 + FXSYS_toDecimalDigitWide(c);
246 nSkip = i - nStart + 1;
247 if (nSkip >= nMaxStep)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 break;
249 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 return nRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252}
253
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254CFX_WideString CJS_PublicMethods::ParseStringString(
255 const CFX_WideString& string,
256 int nStart,
257 int& nSkip) {
258 CFX_WideString swRet;
259 nSkip = 0;
260 for (int i = nStart, sz = string.GetLength(); i < sz; i++) {
261 FX_WCHAR c = string.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800262 if (!FXSYS_iswdigit(c))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800264
265 swRet += c;
266 nSkip = i - nStart + 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700270}
271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800273 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 int nYear = JS_GetYearFromTime(dt);
277 int nMonth = JS_GetMonthFromTime(dt) + 1;
278 int nDay = JS_GetDayFromTime(dt);
279 int nHour = JS_GetHourFromTime(dt);
280 int nMin = JS_GetMinFromTime(dt);
281 int nSec = JS_GetSecFromTime(dt);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 int number[3];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 int nSkip = 0;
286 int nLen = value.GetLength();
287 int nIndex = 0;
288 int i = 0;
289 while (i < nLen) {
290 if (nIndex > 2)
291 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 FX_WCHAR c = value.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -0800294 if (FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
296 i += nSkip;
297 } else {
298 i++;
299 }
300 }
301
302 if (nIndex == 2) {
303 // case2: month/day
304 // case3: day/month
305 if ((number[0] >= 1 && number[0] <= 12) &&
306 (number[1] >= 1 && number[1] <= 31)) {
307 nMonth = number[0];
308 nDay = number[1];
309 } else if ((number[0] >= 1 && number[0] <= 31) &&
310 (number[1] >= 1 && number[1] <= 12)) {
311 nDay = number[0];
312 nMonth = number[1];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700313 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700314
Lei Zhang9559b7a2015-12-21 11:12:20 -0800315 if (bWrongFormat)
316 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 } else if (nIndex == 3) {
318 // case1: year/month/day
319 // case2: month/day/year
320 // case3: day/month/year
Tom Sepez5ffacd62014-07-18 14:42:12 -0700321
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
323 (number[2] >= 1 && number[2] <= 31)) {
324 nYear = number[0];
325 nMonth = number[1];
326 nDay = number[2];
327 } else if ((number[0] >= 1 && number[0] <= 12) &&
328 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
329 nMonth = number[0];
330 nDay = number[1];
331 nYear = number[2];
332 } else if ((number[0] >= 1 && number[0] <= 31) &&
333 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
334 nDay = number[0];
335 nMonth = number[1];
336 nYear = number[2];
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700337 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700338
Lei Zhang9559b7a2015-12-21 11:12:20 -0800339 if (bWrongFormat)
340 *bWrongFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800342 if (bWrongFormat)
343 *bWrongFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 return dt;
345 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 CFX_WideString swTemp;
348 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
349 return JS_DateParse(swTemp.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700350}
351
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
353 const CFX_WideString& format,
Lei Zhang9559b7a2015-12-21 11:12:20 -0800354 bool* bWrongFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 double dt = JS_GetDateTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 if (format.IsEmpty() || value.IsEmpty())
358 return dt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700359
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 int nYear = JS_GetYearFromTime(dt);
361 int nMonth = JS_GetMonthFromTime(dt) + 1;
362 int nDay = JS_GetDayFromTime(dt);
363 int nHour = JS_GetHourFromTime(dt);
364 int nMin = JS_GetMinFromTime(dt);
365 int nSec = JS_GetSecFromTime(dt);
366
367 int nYearSub = 99; // nYear - 2000;
368
369 FX_BOOL bPm = FALSE;
370 FX_BOOL bExit = FALSE;
Lei Zhang9559b7a2015-12-21 11:12:20 -0800371 bool bBadFormat = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372
373 int i = 0;
374 int j = 0;
375
376 while (i < format.GetLength()) {
377 if (bExit)
378 break;
379
380 FX_WCHAR c = format.GetAt(i);
381 switch (c) {
382 case ':':
383 case '.':
384 case '-':
385 case '\\':
386 case '/':
387 i++;
388 j++;
389 break;
390
391 case 'y':
392 case 'm':
393 case 'd':
394 case 'H':
395 case 'h':
396 case 'M':
397 case 's':
398 case 't': {
399 int oldj = j;
400 int nSkip = 0;
401 int remaining = format.GetLength() - i - 1;
402
403 if (remaining == 0 || format.GetAt(i + 1) != c) {
404 switch (c) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700405 case 'y':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 i++;
407 j++;
408 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700409 case 'm':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 nMonth = ParseStringInteger(value, j, nSkip, 2);
411 i++;
412 j += nSkip;
413 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700414 case 'd':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 nDay = ParseStringInteger(value, j, nSkip, 2);
416 i++;
417 j += nSkip;
418 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700419 case 'H':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 nHour = ParseStringInteger(value, j, nSkip, 2);
421 i++;
422 j += nSkip;
423 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700424 case 'h':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 nHour = ParseStringInteger(value, j, nSkip, 2);
426 i++;
427 j += nSkip;
428 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700429 case 'M':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 nMin = ParseStringInteger(value, j, nSkip, 2);
431 i++;
432 j += nSkip;
433 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700434 case 's':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 nSec = ParseStringInteger(value, j, nSkip, 2);
436 i++;
437 j += nSkip;
438 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700439 case 't':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
441 i++;
442 j++;
443 break;
444 }
445 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
446 switch (c) {
447 case 'y':
448 nYear = ParseStringInteger(value, j, nSkip, 4);
449 i += 2;
450 j += nSkip;
451 break;
452 case 'm':
453 nMonth = ParseStringInteger(value, j, nSkip, 2);
454 i += 2;
455 j += nSkip;
456 break;
457 case 'd':
458 nDay = ParseStringInteger(value, j, nSkip, 2);
459 i += 2;
460 j += nSkip;
461 break;
462 case 'H':
463 nHour = ParseStringInteger(value, j, nSkip, 2);
464 i += 2;
465 j += nSkip;
466 break;
467 case 'h':
468 nHour = ParseStringInteger(value, j, nSkip, 2);
469 i += 2;
470 j += nSkip;
471 break;
472 case 'M':
473 nMin = ParseStringInteger(value, j, nSkip, 2);
474 i += 2;
475 j += nSkip;
476 break;
477 case 's':
478 nSec = ParseStringInteger(value, j, nSkip, 2);
479 i += 2;
480 j += nSkip;
481 break;
482 case 't':
483 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
484 value.GetAt(j + 1) == 'm');
485 i += 2;
486 j += 2;
487 break;
488 }
489 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
490 switch (c) {
491 case 'm': {
492 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
493 FX_BOOL bFind = FALSE;
494 for (int m = 0; m < 12; m++) {
495 if (sMonth.CompareNoCase(months[m]) == 0) {
496 nMonth = m + 1;
497 i += 3;
498 j += nSkip;
499 bFind = TRUE;
500 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700501 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502 }
503
504 if (!bFind) {
505 nMonth = ParseStringInteger(value, j, nSkip, 3);
506 i += 3;
507 j += nSkip;
508 }
509 } break;
510 case 'y':
511 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700512 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513 i += 3;
514 j += 3;
515 break;
516 }
517 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
518 switch (c) {
519 case 'y':
520 nYear = ParseStringInteger(value, j, nSkip, 4);
521 j += nSkip;
522 i += 4;
523 break;
524 case 'm': {
525 FX_BOOL bFind = FALSE;
526
527 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
528 sMonth.MakeLower();
529
530 for (int m = 0; m < 12; m++) {
531 CFX_WideString sFullMonths = fullmonths[m];
532 sFullMonths.MakeLower();
533
534 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
535 nMonth = m + 1;
536 i += 4;
537 j += nSkip;
538 bFind = TRUE;
539 break;
540 }
541 }
542
543 if (!bFind) {
544 nMonth = ParseStringInteger(value, j, nSkip, 4);
545 i += 4;
546 j += nSkip;
547 }
548 } break;
549 default:
550 i += 4;
551 j += 4;
552 break;
553 }
554 } else {
555 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800556 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557 bExit = TRUE;
558 }
559 i++;
560 j++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700561 }
Tom Sepez85386422014-07-23 10:28:37 -0700562
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 if (oldj == j) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800564 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 bExit = TRUE;
566 }
567 }
568
569 break;
570 default:
571 if (value.GetLength() <= j) {
572 bExit = TRUE;
573 } else if (format.GetAt(i) != value.GetAt(j)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800574 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 bExit = TRUE;
576 }
577
578 i++;
579 j++;
580 break;
581 }
582 }
583
584 if (bPm)
585 nHour += 12;
586
587 if (nYear >= 0 && nYear <= nYearSub)
588 nYear += 2000;
589
590 if (nMonth < 1 || nMonth > 12)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800591 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592
593 if (nDay < 1 || nDay > 31)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800594 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700595
596 if (nHour < 0 || nHour > 24)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800597 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598
599 if (nMin < 0 || nMin > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800600 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601
602 if (nSec < 0 || nSec > 60)
Lei Zhang9559b7a2015-12-21 11:12:20 -0800603 bBadFormat = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604
605 double dRet = 0;
606
Lei Zhang9559b7a2015-12-21 11:12:20 -0800607 if (bBadFormat) {
608 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 } else {
610 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
611 JS_MakeTime(nHour, nMin, nSec, 0));
612
613 if (JS_PortIsNan(dRet)) {
614 dRet = JS_DateParse(value.c_str());
615 }
616 }
617
618 if (JS_PortIsNan(dRet)) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800619 dRet = ParseNormalDate(value, &bBadFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700620 }
621
Lei Zhang9559b7a2015-12-21 11:12:20 -0800622 if (bWrongFormat)
623 *bWrongFormat = bBadFormat;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700624 return dRet;
625}
626
627CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
628 const CFX_WideString& format) {
629 CFX_WideString sRet = L"", sPart = L"";
630
631 int nYear = JS_GetYearFromTime(dDate);
632 int nMonth = JS_GetMonthFromTime(dDate) + 1;
633 int nDay = JS_GetDayFromTime(dDate);
634 int nHour = JS_GetHourFromTime(dDate);
635 int nMin = JS_GetMinFromTime(dDate);
636 int nSec = JS_GetSecFromTime(dDate);
637
638 int i = 0;
639 while (i < format.GetLength()) {
640 FX_WCHAR c = format.GetAt(i);
641 int remaining = format.GetLength() - i - 1;
642 sPart = L"";
643 switch (c) {
644 case 'y':
645 case 'm':
646 case 'd':
647 case 'H':
648 case 'h':
649 case 'M':
650 case 's':
651 case 't':
652 if (remaining == 0 || format.GetAt(i + 1) != c) {
653 switch (c) {
654 case 'y':
655 sPart += c;
656 break;
657 case 'm':
658 sPart.Format(L"%d", nMonth);
659 break;
660 case 'd':
661 sPart.Format(L"%d", nDay);
662 break;
663 case 'H':
664 sPart.Format(L"%d", nHour);
665 break;
666 case 'h':
667 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
668 break;
669 case 'M':
670 sPart.Format(L"%d", nMin);
671 break;
672 case 's':
673 sPart.Format(L"%d", nSec);
674 break;
675 case 't':
676 sPart += nHour > 12 ? 'p' : 'a';
677 break;
678 }
679 i++;
680 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
681 switch (c) {
682 case 'y':
683 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
684 break;
685 case 'm':
686 sPart.Format(L"%02d", nMonth);
687 break;
688 case 'd':
689 sPart.Format(L"%02d", nDay);
690 break;
691 case 'H':
692 sPart.Format(L"%02d", nHour);
693 break;
694 case 'h':
695 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
696 break;
697 case 'M':
698 sPart.Format(L"%02d", nMin);
699 break;
700 case 's':
701 sPart.Format(L"%02d", nSec);
702 break;
703 case 't':
704 sPart = nHour > 12 ? L"pm" : L"am";
705 break;
706 }
707 i += 2;
708 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
709 switch (c) {
710 case 'm':
711 i += 3;
712 if (nMonth > 0 && nMonth <= 12)
713 sPart += months[nMonth - 1];
714 break;
715 default:
716 i += 3;
717 sPart += c;
718 sPart += c;
719 sPart += c;
720 break;
721 }
722 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
723 switch (c) {
724 case 'y':
725 sPart.Format(L"%04d", nYear);
726 i += 4;
727 break;
728 case 'm':
729 i += 4;
730 if (nMonth > 0 && nMonth <= 12)
731 sPart += fullmonths[nMonth - 1];
732 break;
733 default:
734 i += 4;
735 sPart += c;
736 sPart += c;
737 sPart += c;
738 sPart += c;
739 break;
740 }
741 } else {
742 i++;
743 sPart += c;
744 }
745 break;
746 default:
747 i++;
748 sPart += c;
749 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700750 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700751
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700752 sRet += sPart;
753 }
754
755 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700756}
757
758/* -------------------------------------------------------------------------- */
759
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700760// function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
761// bCurrencyPrepend)
Tom Sepezba038bc2015-10-08 12:03:00 -0700762FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800763 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700764 CJS_Value& vRet,
765 CFX_WideString& sError) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700766#if _FX_OS_ != _FX_ANDROID_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700768 if (params.size() != 6) {
769 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
770 return FALSE;
771 }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700772
773 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
774 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700775 if (!pEvent->m_pValue)
776 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700777
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700778 CFX_WideString& Value = pEvent->Value();
779 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700780 if (strValue.IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700781 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782
783 int iDec = params[0].ToInt();
784 int iSepStyle = params[1].ToInt();
785 int iNegStyle = params[2].ToInt();
786 // params[3] is iCurrStyle, it's not used.
787 std::wstring wstrCurrency(params[4].ToCFXWideString().c_str());
788 FX_BOOL bCurrencyPrepend = params[5].ToBool();
789
790 if (iDec < 0)
791 iDec = -iDec;
792
793 if (iSepStyle < 0 || iSepStyle > 3)
794 iSepStyle = 0;
795
796 if (iNegStyle < 0 || iNegStyle > 3)
797 iNegStyle = 0;
798
799 //////////////////////////////////////////////////////
800 // for processing decimal places
801 strValue.Replace(",", ".");
802 double dValue = atof(strValue);
803 if (iDec > 0)
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700804 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700805
806 int iDec2;
807 int iNegative = 0;
808
809 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
810 if (strValue.IsEmpty()) {
811 dValue = 0;
812 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
813 if (strValue.IsEmpty()) {
814 strValue = "0";
815 iDec2 = 1;
816 }
817 }
818
819 if (iDec2 < 0) {
820 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
821 strValue = "0" + strValue;
822 }
823 iDec2 = 0;
824 }
825 int iMax = strValue.GetLength();
826 if (iDec2 > iMax) {
827 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
828 strValue += "0";
829 }
830 iMax = iDec2 + 1;
831 }
832 ///////////////////////////////////////////////////////
833 // for processing seperator style
834 if (iDec2 < iMax) {
835 if (iSepStyle == 0 || iSepStyle == 1) {
836 strValue.Insert(iDec2, '.');
837 iMax++;
838 } else if (iSepStyle == 2 || iSepStyle == 3) {
839 strValue.Insert(iDec2, ',');
840 iMax++;
841 }
842
843 if (iDec2 == 0)
844 strValue.Insert(iDec2, '0');
845 }
846 if (iSepStyle == 0 || iSepStyle == 2) {
847 char cSeperator;
848 if (iSepStyle == 0)
849 cSeperator = ',';
850 else
851 cSeperator = '.';
852
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700853 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 strValue.Insert(iDecPositive, cSeperator);
855 iMax++;
856 }
857 }
858
859 //////////////////////////////////////////////////////////////////////
860 // for processing currency string
861
862 Value = CFX_WideString::FromLocal(strValue);
863 std::wstring strValue2 = Value.c_str();
864
865 if (bCurrencyPrepend)
866 strValue2 = wstrCurrency + strValue2;
867 else
868 strValue2 = strValue2 + wstrCurrency;
869
870 /////////////////////////////////////////////////////////////////////////
871 // for processing negative style
872 if (iNegative) {
873 if (iNegStyle == 0) {
874 strValue2.insert(0, L"-");
875 }
876 if (iNegStyle == 2 || iNegStyle == 3) {
877 strValue2.insert(0, L"(");
878 strValue2.insert(strValue2.length(), L")");
879 }
880 if (iNegStyle == 1 || iNegStyle == 3) {
881 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700882 CJS_Array arColor(pRuntime);
883 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 vColElm = L"RGB";
885 arColor.SetElement(0, vColElm);
886 vColElm = 1;
887 arColor.SetElement(1, vColElm);
888 vColElm = 0;
889 arColor.SetElement(2, vColElm);
890
891 arColor.SetElement(3, vColElm);
892
Tom Sepez67fd5df2015-10-08 12:24:19 -0700893 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894 vProp.StartGetting();
895 vProp << arColor;
896 vProp.StartSetting();
897 fTarget->textColor(cc, vProp, sError); // red
898 }
899 }
900 } else {
901 if (iNegStyle == 1 || iNegStyle == 3) {
902 if (Field* fTarget = pEvent->Target_Field()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700903 CJS_Array arColor(pRuntime);
904 CJS_Value vColElm(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 vColElm = L"RGB";
906 arColor.SetElement(0, vColElm);
907 vColElm = 0;
908 arColor.SetElement(1, vColElm);
909 arColor.SetElement(2, vColElm);
910 arColor.SetElement(3, vColElm);
911
Tom Sepez67fd5df2015-10-08 12:24:19 -0700912 CJS_PropValue vProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700913 vProp.StartGetting();
914 fTarget->textColor(cc, vProp, sError);
915
Tom Sepez67fd5df2015-10-08 12:24:19 -0700916 CJS_Array aProp(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 vProp.ConvertToArray(aProp);
918
919 CPWL_Color crProp;
920 CPWL_Color crColor;
921 color::ConvertArrayToPWLColor(aProp, crProp);
922 color::ConvertArrayToPWLColor(arColor, crColor);
923
924 if (crColor != crProp) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700925 CJS_PropValue vProp2(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926 vProp2.StartGetting();
927 vProp2 << arColor;
928 vProp2.StartSetting();
929 fTarget->textColor(cc, vProp2, sError);
930 }
931 }
932 }
933 }
934 Value = strValue2.c_str();
935#endif
936 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700937}
938
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700939// function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
940// bCurrencyPrepend)
Lei Zhang945fdb72015-11-11 10:18:16 -0800941FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(
942 IJS_Context* cc,
943 const std::vector<CJS_Value>& params,
944 CJS_Value& vRet,
945 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700948
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949 if (params.size() < 2)
950 return FALSE;
951 int iSepStyle = params[1].ToInt();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700952
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 if (iSepStyle < 0 || iSepStyle > 3)
954 iSepStyle = 0;
955 if (!pEvent->m_pValue)
956 return FALSE;
957 CFX_WideString& val = pEvent->Value();
958 CFX_WideString& w_strChange = pEvent->Change();
959 CFX_WideString w_strValue = val;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700960
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961 if (pEvent->WillCommit()) {
962 CFX_WideString wstrChange = w_strChange;
963 CFX_WideString wstrValue = StrLTrim(w_strValue.c_str());
964 if (wstrValue.IsEmpty())
965 return TRUE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700966
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967 CFX_WideString swTemp = wstrValue;
968 swTemp.Replace(L",", L".");
969 if (!IsNumber(swTemp.c_str())) {
970 pEvent->Rc() = FALSE;
971 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
972 Alert(pContext, sError.c_str());
973 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700974 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 return TRUE; // it happens after the last keystroke and before validating,
976 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700977
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 std::wstring w_strValue2 = w_strValue.c_str();
979 std::wstring w_strChange2 = w_strChange.c_str();
980 std::wstring w_strSelected;
981 if (-1 != pEvent->SelStart())
982 w_strSelected = w_strValue2.substr(pEvent->SelStart(),
983 (pEvent->SelEnd() - pEvent->SelStart()));
Lei Zhangb9c31972015-08-11 14:09:35 -0700984 bool bHasSign = (w_strValue2.find('-') != std::wstring::npos) &&
985 (w_strSelected.find('-') == std::wstring::npos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 if (bHasSign) {
987 // can't insert "change" in front to sign postion.
988 if (pEvent->SelStart() == 0) {
989 FX_BOOL& bRc = pEvent->Rc();
990 bRc = FALSE;
991 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700992 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700994
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995 char cSep = L'.';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700996
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997 switch (iSepStyle) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700998 case 0:
999 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 cSep = L'.';
1001 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001002 case 2:
1003 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 cSep = L',';
1005 break;
1006 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001007
Lei Zhangb9c31972015-08-11 14:09:35 -07001008 bool bHasSep = (w_strValue2.find(cSep) != std::wstring::npos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 for (std::wstring::iterator it = w_strChange2.begin();
1010 it != w_strChange2.end(); it++) {
1011 if (*it == cSep) {
1012 if (bHasSep) {
1013 FX_BOOL& bRc = pEvent->Rc();
1014 bRc = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001015 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016 }
1017 bHasSep = TRUE;
1018 continue;
1019 }
1020 if (*it == L'-') {
1021 if (bHasSign) {
1022 FX_BOOL& bRc = pEvent->Rc();
1023 bRc = FALSE;
1024 return TRUE;
1025 }
Lei Zhang9559b7a2015-12-21 11:12:20 -08001026 // sign's position is not correct
1027 if (it != w_strChange2.begin()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028 FX_BOOL& bRc = pEvent->Rc();
1029 bRc = FALSE;
1030 return TRUE;
1031 }
1032 if (pEvent->SelStart() != 0) {
1033 FX_BOOL& bRc = pEvent->Rc();
1034 bRc = FALSE;
1035 return TRUE;
1036 }
1037 bHasSign = TRUE;
1038 continue;
1039 }
1040
Lei Zhang9559b7a2015-12-21 11:12:20 -08001041 if (!FXSYS_iswdigit(*it)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 FX_BOOL& bRc = pEvent->Rc();
1043 bRc = FALSE;
1044 return TRUE;
1045 }
1046 }
1047
1048 std::wstring w_prefix = w_strValue2.substr(0, pEvent->SelStart());
1049 std::wstring w_postfix;
1050 if (pEvent->SelEnd() < (int)w_strValue2.length())
1051 w_postfix = w_strValue2.substr(pEvent->SelEnd());
1052 w_strValue2 = w_prefix + w_strChange2 + w_postfix;
1053 w_strValue = w_strValue2.c_str();
1054 val = w_strValue;
1055 return TRUE;
1056}
1057
1058// function AFPercent_Format(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001059FX_BOOL CJS_PublicMethods::AFPercent_Format(
1060 IJS_Context* cc,
1061 const std::vector<CJS_Value>& params,
1062 CJS_Value& vRet,
1063 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001064#if _FX_OS_ != _FX_ANDROID_
1065 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067
1068 if (params.size() != 2) {
1069 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1070 return FALSE;
1071 }
1072 if (!pEvent->m_pValue)
1073 return FALSE;
1074
1075 CFX_WideString& Value = pEvent->Value();
1076 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1077 if (strValue.IsEmpty())
1078 return TRUE;
1079
1080 int iDec = params[0].ToInt();
1081 if (iDec < 0)
1082 iDec = -iDec;
1083
1084 int iSepStyle = params[1].ToInt();
1085 if (iSepStyle < 0 || iSepStyle > 3)
1086 iSepStyle = 0;
1087
1088 //////////////////////////////////////////////////////
1089 // for processing decimal places
1090 double dValue = atof(strValue);
1091 dValue *= 100;
1092 if (iDec > 0)
Lei Zhang9559b7a2015-12-21 11:12:20 -08001093 dValue += DOUBLE_CORRECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094
1095 int iDec2;
1096 int iNegative = 0;
1097 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1098 if (strValue.IsEmpty()) {
1099 dValue = 0;
1100 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1101 }
1102
1103 if (iDec2 < 0) {
1104 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1105 strValue = "0" + strValue;
1106 }
1107 iDec2 = 0;
1108 }
1109 int iMax = strValue.GetLength();
1110 if (iDec2 > iMax) {
1111 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1112 strValue += "0";
1113 }
1114 iMax = iDec2 + 1;
1115 }
1116 ///////////////////////////////////////////////////////
1117 // for processing seperator style
1118 if (iDec2 < iMax) {
1119 if (iSepStyle == 0 || iSepStyle == 1) {
1120 strValue.Insert(iDec2, '.');
1121 iMax++;
1122 } else if (iSepStyle == 2 || iSepStyle == 3) {
1123 strValue.Insert(iDec2, ',');
1124 iMax++;
1125 }
1126
1127 if (iDec2 == 0)
1128 strValue.Insert(iDec2, '0');
1129 }
1130 if (iSepStyle == 0 || iSepStyle == 2) {
1131 char cSeperator;
1132 if (iSepStyle == 0)
1133 cSeperator = ',';
1134 else
1135 cSeperator = '.';
1136
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001137 for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138 strValue.Insert(iDecPositive, cSeperator);
1139 iMax++;
1140 }
1141 }
1142 ////////////////////////////////////////////////////////////////////
1143 // negative mark
1144 if (iNegative)
1145 strValue = "-" + strValue;
1146 strValue += "%";
1147 Value = CFX_WideString::FromLocal(strValue);
1148#endif
1149 return TRUE;
1150}
1151// AFPercent_Keystroke(nDec, sepStyle)
Lei Zhang945fdb72015-11-11 10:18:16 -08001152FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(
1153 IJS_Context* cc,
1154 const std::vector<CJS_Value>& params,
1155 CJS_Value& vRet,
1156 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 return AFNumber_Keystroke(cc, params, vRet, sError);
1158}
1159
1160// function AFDate_FormatEx(cFormat)
Tom Sepezba038bc2015-10-08 12:03:00 -07001161FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001162 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 CJS_Value& vRet,
1164 CFX_WideString& sError) {
1165 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167
1168 if (params.size() != 1) {
1169 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1170 return FALSE;
1171 }
1172 if (!pEvent->m_pValue)
1173 return FALSE;
1174
1175 CFX_WideString& val = pEvent->Value();
1176 CFX_WideString strValue = val;
1177 if (strValue.IsEmpty())
1178 return TRUE;
1179
1180 CFX_WideString sFormat = params[0].ToCFXWideString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181 double dDate = 0.0f;
1182
1183 if (strValue.Find(L"GMT") != -1) {
1184 // for GMT format time
1185 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1186 dDate = MakeInterDate(strValue);
1187 } else {
Lei Zhang9559b7a2015-12-21 11:12:20 -08001188 dDate = MakeRegularDate(strValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189 }
1190
1191 if (JS_PortIsNan(dDate)) {
1192 CFX_WideString swMsg;
1193 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1194 sFormat.c_str());
1195 Alert(pContext, swMsg.c_str());
1196 return FALSE;
1197 }
1198
1199 val = MakeFormatDate(dDate, sFormat);
1200 return TRUE;
1201}
1202
1203double CJS_PublicMethods::MakeInterDate(CFX_WideString strValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 CFX_WideStringArray wsArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001205 CFX_WideString sTemp = L"";
Tom Sepez4246b002016-01-20 11:48:29 -08001206 for (int i = 0; i < strValue.GetLength(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 FX_WCHAR c = strValue.GetAt(i);
1208 if (c == L' ' || c == L':') {
1209 wsArray.Add(sTemp);
1210 sTemp = L"";
1211 continue;
1212 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213 sTemp += c;
1214 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001215 wsArray.Add(sTemp);
1216 if (wsArray.GetSize() != 8)
1217 return 0;
1218
Tom Sepez4246b002016-01-20 11:48:29 -08001219 int nMonth = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 sTemp = wsArray[1];
1221 if (sTemp.Compare(L"Jan") == 0)
1222 nMonth = 1;
Tom Sepez4246b002016-01-20 11:48:29 -08001223 else if (sTemp.Compare(L"Feb") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001224 nMonth = 2;
Tom Sepez4246b002016-01-20 11:48:29 -08001225 else if (sTemp.Compare(L"Mar") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226 nMonth = 3;
Tom Sepez4246b002016-01-20 11:48:29 -08001227 else if (sTemp.Compare(L"Apr") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001228 nMonth = 4;
Tom Sepez4246b002016-01-20 11:48:29 -08001229 else if (sTemp.Compare(L"May") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001230 nMonth = 5;
Tom Sepez4246b002016-01-20 11:48:29 -08001231 else if (sTemp.Compare(L"Jun") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232 nMonth = 6;
Tom Sepez4246b002016-01-20 11:48:29 -08001233 else if (sTemp.Compare(L"Jul") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234 nMonth = 7;
Tom Sepez4246b002016-01-20 11:48:29 -08001235 else if (sTemp.Compare(L"Aug") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001236 nMonth = 8;
Tom Sepez4246b002016-01-20 11:48:29 -08001237 else if (sTemp.Compare(L"Sep") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001238 nMonth = 9;
Tom Sepez4246b002016-01-20 11:48:29 -08001239 else if (sTemp.Compare(L"Oct") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001240 nMonth = 10;
Tom Sepez4246b002016-01-20 11:48:29 -08001241 else if (sTemp.Compare(L"Nov") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001242 nMonth = 11;
Tom Sepez4246b002016-01-20 11:48:29 -08001243 else if (sTemp.Compare(L"Dec") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001244 nMonth = 12;
1245
Tom Sepez4246b002016-01-20 11:48:29 -08001246 int nDay = FX_atof(wsArray[2]);
1247 int nHour = FX_atof(wsArray[3]);
1248 int nMin = FX_atof(wsArray[4]);
1249 int nSec = FX_atof(wsArray[5]);
1250 int nYear = FX_atof(wsArray[7]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1252 JS_MakeTime(nHour, nMin, nSec, 0));
Tom Sepez4246b002016-01-20 11:48:29 -08001253 if (JS_PortIsNan(dRet))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254 dRet = JS_DateParse(strValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001255
1256 return dRet;
1257}
1258
1259// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001260FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(
1261 IJS_Context* cc,
1262 const std::vector<CJS_Value>& params,
1263 CJS_Value& vRet,
1264 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267
1268 if (params.size() != 1) {
1269 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1270 return FALSE;
1271 }
1272
1273 if (pEvent->WillCommit()) {
1274 if (!pEvent->m_pValue)
1275 return FALSE;
1276 CFX_WideString strValue = pEvent->Value();
1277 if (strValue.IsEmpty())
1278 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001279
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001280 CFX_WideString sFormat = params[0].ToCFXWideString();
Lei Zhang9559b7a2015-12-21 11:12:20 -08001281 bool bWrongFormat = FALSE;
1282 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283 if (bWrongFormat || JS_PortIsNan(dRet)) {
1284 CFX_WideString swMsg;
1285 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1286 sFormat.c_str());
1287 Alert(pContext, swMsg.c_str());
1288 pEvent->Rc() = FALSE;
1289 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001290 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291 }
1292 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001293}
1294
Tom Sepezba038bc2015-10-08 12:03:00 -07001295FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001296 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297 CJS_Value& vRet,
1298 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001299 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1302 return FALSE;
1303 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001304
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305 int iIndex = params[0].ToInt();
1306 const FX_WCHAR* cFormats[] = {L"m/d",
1307 L"m/d/yy",
1308 L"mm/dd/yy",
1309 L"mm/yy",
1310 L"d-mmm",
1311 L"d-mmm-yy",
1312 L"dd-mmm-yy",
1313 L"yy-mm-dd",
1314 L"mmm-yy",
1315 L"mmmm-yy",
1316 L"mmm d, yyyy",
1317 L"mmmm d, yyyy",
1318 L"m/d/yy h:MM tt",
1319 L"m/d/yy HH:MM"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001320
Lei Zhanga0f67242015-08-17 15:39:30 -07001321 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1322 iIndex = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001323
Lei Zhang945fdb72015-11-11 10:18:16 -08001324 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001325 newParams.push_back(
1326 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327 return AFDate_FormatEx(cc, newParams, vRet, sError);
1328}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001329
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001330// AFDate_KeystrokeEx(cFormat)
Lei Zhang945fdb72015-11-11 10:18:16 -08001331FX_BOOL CJS_PublicMethods::AFDate_Keystroke(
1332 IJS_Context* cc,
1333 const std::vector<CJS_Value>& params,
1334 CJS_Value& vRet,
1335 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001336 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001337 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001338 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1339 return FALSE;
1340 }
1341
1342 int iIndex = params[0].ToInt();
1343 const FX_WCHAR* cFormats[] = {L"m/d",
1344 L"m/d/yy",
1345 L"mm/dd/yy",
1346 L"mm/yy",
1347 L"d-mmm",
1348 L"d-mmm-yy",
1349 L"dd-mmm-yy",
1350 L"yy-mm-dd",
1351 L"mmm-yy",
1352 L"mmmm-yy",
1353 L"mmm d, yyyy",
1354 L"mmmm d, yyyy",
1355 L"m/d/yy h:MM tt",
1356 L"m/d/yy HH:MM"};
1357
Lei Zhanga0f67242015-08-17 15:39:30 -07001358 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1359 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360
Lei Zhang945fdb72015-11-11 10:18:16 -08001361 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001362 newParams.push_back(
1363 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001364 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1365}
1366
1367// function AFTime_Format(ptf)
Tom Sepezba038bc2015-10-08 12:03:00 -07001368FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001369 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001370 CJS_Value& vRet,
1371 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001372 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1375 return FALSE;
1376 }
1377
1378 int iIndex = params[0].ToInt();
1379 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1380 L"h:MM:ss tt"};
1381
Lei Zhanga0f67242015-08-17 15:39:30 -07001382 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1383 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001384
Lei Zhang945fdb72015-11-11 10:18:16 -08001385 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001386 newParams.push_back(
1387 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388 return AFDate_FormatEx(cc, newParams, vRet, sError);
1389}
1390
Lei Zhang945fdb72015-11-11 10:18:16 -08001391FX_BOOL CJS_PublicMethods::AFTime_Keystroke(
1392 IJS_Context* cc,
1393 const std::vector<CJS_Value>& params,
1394 CJS_Value& vRet,
1395 CFX_WideString& sError) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001396 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001398 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1399 return FALSE;
1400 }
1401
1402 int iIndex = params[0].ToInt();
1403 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1404 L"h:MM:ss tt"};
1405
Lei Zhanga0f67242015-08-17 15:39:30 -07001406 if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
1407 iIndex = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408
Lei Zhang945fdb72015-11-11 10:18:16 -08001409 std::vector<CJS_Value> newParams;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001410 newParams.push_back(
1411 CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001412 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1413}
1414
Tom Sepezba038bc2015-10-08 12:03:00 -07001415FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001416 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001417 CJS_Value& vRet,
1418 CFX_WideString& sError) {
1419 return AFDate_FormatEx(cc, params, vRet, sError);
1420}
1421
Lei Zhang945fdb72015-11-11 10:18:16 -08001422FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(
1423 IJS_Context* cc,
1424 const std::vector<CJS_Value>& params,
1425 CJS_Value& vRet,
1426 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1428}
1429
1430// function AFSpecial_Format(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001431FX_BOOL CJS_PublicMethods::AFSpecial_Format(
1432 IJS_Context* cc,
1433 const std::vector<CJS_Value>& params,
1434 CJS_Value& vRet,
1435 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001436 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001437
1438 if (params.size() != 1) {
1439 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1440 return FALSE;
1441 }
1442
1443 std::string cFormat;
1444 int iIndex = params[0].ToInt();
1445
1446 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 if (!pEvent->m_pValue)
1448 return FALSE;
1449 CFX_WideString& Value = pEvent->Value();
1450 std::string strSrc = CFX_ByteString::FromUnicode(Value).c_str();
1451
1452 switch (iIndex) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001453 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001454 cFormat = "99999";
1455 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001456 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457 cFormat = "99999-9999";
1458 break;
1459 case 2: {
1460 std::string NumberStr;
1461 util::printx("9999999999", strSrc, NumberStr);
1462 if (NumberStr.length() >= 10)
1463 cFormat = "(999) 999-9999";
1464 else
1465 cFormat = "999-9999";
1466 break;
1467 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001468 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469 cFormat = "999-99-9999";
1470 break;
1471 }
1472
1473 std::string strDes;
1474 util::printx(cFormat, strSrc, strDes);
1475 Value = CFX_WideString::FromLocal(strDes.c_str());
1476 return TRUE;
1477}
1478
1479// function AFSpecial_KeystrokeEx(mask)
Lei Zhang945fdb72015-11-11 10:18:16 -08001480FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(
1481 IJS_Context* cc,
1482 const std::vector<CJS_Value>& params,
1483 CJS_Value& vRet,
1484 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001486 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1487
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001488 if (params.size() < 1) {
1489 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1490 return FALSE;
1491 }
1492
1493 if (!pEvent->m_pValue)
1494 return FALSE;
1495 CFX_WideString& valEvent = pEvent->Value();
1496
1497 CFX_WideString wstrMask = params[0].ToCFXWideString();
1498 if (wstrMask.IsEmpty())
1499 return TRUE;
1500
Lei Zhanga0f67242015-08-17 15:39:30 -07001501 const size_t wstrMaskLen = wstrMask.GetLength();
1502 const std::wstring wstrValue = valEvent.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503
1504 if (pEvent->WillCommit()) {
1505 if (wstrValue.empty())
1506 return TRUE;
Lei Zhanga0f67242015-08-17 15:39:30 -07001507 size_t iIndexMask = 0;
1508 for (const auto& w_Value : wstrValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509 if (!maskSatisfied(w_Value, wstrMask[iIndexMask]))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001510 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511 iIndexMask++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001512 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001513
Lei Zhanga0f67242015-08-17 15:39:30 -07001514 if (iIndexMask != wstrMaskLen ||
1515 (iIndexMask != wstrValue.size() && wstrMaskLen != 0)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516 Alert(
1517 pContext,
1518 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1519 pEvent->Rc() = FALSE;
1520 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001521 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001522 }
1523
1524 CFX_WideString& wideChange = pEvent->Change();
1525 std::wstring wChange = wideChange.c_str();
1526 if (wChange.empty())
1527 return TRUE;
1528
1529 int iIndexMask = pEvent->SelStart();
1530
Lei Zhanga0f67242015-08-17 15:39:30 -07001531 size_t combined_len = wstrValue.length() + wChange.length() -
1532 (pEvent->SelEnd() - pEvent->SelStart());
1533 if (combined_len > wstrMaskLen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001534 Alert(pContext,
1535 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1536 pEvent->Rc() = FALSE;
1537 return TRUE;
1538 }
1539
Lei Zhanga0f67242015-08-17 15:39:30 -07001540 if (iIndexMask >= wstrMaskLen && (!wChange.empty())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001541 Alert(pContext,
1542 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1543 pEvent->Rc() = FALSE;
1544 return TRUE;
1545 }
1546
1547 for (std::wstring::iterator it = wChange.begin(); it != wChange.end(); it++) {
Lei Zhanga0f67242015-08-17 15:39:30 -07001548 if (iIndexMask >= wstrMaskLen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001549 Alert(pContext,
1550 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1551 pEvent->Rc() = FALSE;
1552 return TRUE;
1553 }
1554 wchar_t w_Mask = wstrMask[iIndexMask];
1555 if (!isReservedMaskChar(w_Mask)) {
1556 *it = w_Mask;
1557 }
1558 wchar_t w_Change = *it;
1559 if (!maskSatisfied(w_Change, w_Mask)) {
1560 pEvent->Rc() = FALSE;
1561 return TRUE;
1562 }
1563 iIndexMask++;
1564 }
1565
1566 wideChange = wChange.c_str();
1567 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001568}
1569
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570// function AFSpecial_Keystroke(psf)
Lei Zhang945fdb72015-11-11 10:18:16 -08001571FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(
1572 IJS_Context* cc,
1573 const std::vector<CJS_Value>& params,
1574 CJS_Value& vRet,
1575 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001576 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001577 if (params.size() != 1) {
1578 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1579 return FALSE;
1580 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001581
Tom Sepez67fd5df2015-10-08 12:24:19 -07001582 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001583 if (!pEvent->m_pValue)
1584 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001585
1586 std::string cFormat;
1587 int iIndex = params[0].ToInt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001588 CFX_WideString& val = pEvent->Value();
1589 std::string strSrc = CFX_ByteString::FromUnicode(val).c_str();
1590 std::wstring wstrChange = pEvent->Change().c_str();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001591
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001592 switch (iIndex) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001593 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001594 cFormat = "99999";
1595 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001596 case 1:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001597 // cFormat = "99999-9999";
1598 cFormat = "999999999";
1599 break;
1600 case 2: {
1601 std::string NumberStr;
1602 util::printx("9999999999", strSrc, NumberStr);
1603 if (strSrc.length() + wstrChange.length() > 7)
1604 // cFormat = "(999) 999-9999";
1605 cFormat = "9999999999";
1606 else
1607 // cFormat = "999-9999";
1608 cFormat = "9999999";
1609 break;
1610 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001611 case 3:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001612 // cFormat = "999-99-9999";
1613 cFormat = "999999999";
1614 break;
1615 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001616
Lei Zhang945fdb72015-11-11 10:18:16 -08001617 std::vector<CJS_Value> params2;
Tom Sepez67fd5df2015-10-08 12:24:19 -07001618 params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001619 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001620}
1621
Tom Sepezba038bc2015-10-08 12:03:00 -07001622FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001623 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001624 CJS_Value& vRet,
1625 CFX_WideString& sError) {
1626 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001628
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001629 if (params.size() != 1) {
1630 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1631 return FALSE;
1632 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001633
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001634 CFX_WideString swValue;
Lei Zhang997de612015-11-04 18:17:53 -08001635 if (pEventHandler->m_pValue)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636 swValue = pEventHandler->Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001637
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001638 if (pEventHandler->WillCommit()) {
1639 vRet = swValue.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001640 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001641 }
1642
1643 CFX_WideString prefix, postfix;
1644
1645 if (pEventHandler->SelStart() >= 0)
1646 prefix = swValue.Mid(0, pEventHandler->SelStart());
1647 else
1648 prefix = L"";
1649
1650 if (pEventHandler->SelEnd() >= 0 &&
1651 pEventHandler->SelEnd() <= swValue.GetLength())
1652 postfix = swValue.Mid(pEventHandler->SelEnd(),
1653 swValue.GetLength() - pEventHandler->SelEnd());
1654 else
1655 postfix = L"";
1656
1657 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1658
1659 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001660}
1661
Tom Sepezba038bc2015-10-08 12:03:00 -07001662FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001663 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664 CJS_Value& vRet,
1665 CFX_WideString& sError) {
1666 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001667 ASSERT(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668
1669 if (params.size() != 2) {
1670 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1671 return FALSE;
1672 }
1673
1674 CFX_WideString sValue = params[0].ToCFXWideString();
1675 CFX_WideString sFormat = params[1].ToCFXWideString();
1676
Lei Zhang9559b7a2015-12-21 11:12:20 -08001677 double dDate = MakeRegularDate(sValue, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001678
1679 if (JS_PortIsNan(dDate)) {
1680 CFX_WideString swMsg;
1681 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1682 sFormat.c_str());
1683 Alert((CJS_Context*)cc, swMsg.c_str());
1684 return FALSE;
1685 }
1686
1687 vRet = dDate;
1688 return TRUE;
1689}
1690
Tom Sepezba038bc2015-10-08 12:03:00 -07001691FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001692 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001693 CJS_Value& vRet,
1694 CFX_WideString& sError) {
1695 if (params.size() != 3) {
1696 CJS_Context* pContext = (CJS_Context*)cc;
Lei Zhang96660d62015-12-14 18:27:25 -08001697 ASSERT(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001698
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001699 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1700 return FALSE;
1701 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001702
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1704 params[1].ToDouble(), params[2].ToDouble());
1705 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001706}
1707
Tom Sepezba038bc2015-10-08 12:03:00 -07001708FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001709 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001710 CJS_Value& vRet,
1711 CFX_WideString& sError) {
Tom Sepez4246b002016-01-20 11:48:29 -08001712 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713 if (params.size() != 1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001714 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1715 return FALSE;
1716 }
Tom Sepez4246b002016-01-20 11:48:29 -08001717 CFX_WideString ws = params[0].ToCFXWideString();
1718 ws.Replace(L",", L".");
1719 vRet = ws;
1720 vRet.MaybeCoerceToNumber();
1721 if (vRet.GetType() != CJS_Value::VT_number)
1722 vRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 return TRUE;
1724}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001725
Lei Zhang945fdb72015-11-11 10:18:16 -08001726FX_BOOL CJS_PublicMethods::AFSimple_Calculate(
1727 IJS_Context* cc,
1728 const std::vector<CJS_Value>& params,
1729 CJS_Value& vRet,
1730 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001732 if (params.size() != 2) {
1733 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1734 return FALSE;
1735 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001736
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737 CJS_Value params1 = params[1];
Tom Sepez39bfe122015-09-17 15:25:23 -07001738 if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001739 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1740 return FALSE;
1741 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001742
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001743 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001744 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001745 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001746
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001747 CFX_WideString sFunction = params[0].ToCFXWideString();
Tom Sepez67fd5df2015-10-08 12:24:19 -07001748 double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001749
Tom Sepez67fd5df2015-10-08 12:24:19 -07001750 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1751 CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001752 int nFieldsCount = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001753
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001754 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001755 CJS_Value jsValue(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001756 FieldNameArray.GetElement(i, jsValue);
1757 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001758
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001759 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1760 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1761 double dTemp = 0.0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001762 switch (pFormField->GetFieldType()) {
1763 case FIELDTYPE_TEXTFIELD:
1764 case FIELDTYPE_COMBOBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001765 CFX_WideString trimmed = pFormField->GetValue();
1766 trimmed.TrimRight();
1767 trimmed.TrimLeft();
1768 dTemp = FX_atof(trimmed);
1769 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001770 case FIELDTYPE_PUSHBUTTON: {
1771 dTemp = 0.0;
Tom Sepez4246b002016-01-20 11:48:29 -08001772 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001773 case FIELDTYPE_CHECKBOX:
1774 case FIELDTYPE_RADIOBUTTON: {
1775 dTemp = 0.0;
1776 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1777 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1778 if (pFormCtrl->IsChecked()) {
Tom Sepez4246b002016-01-20 11:48:29 -08001779 CFX_WideString trimmed = pFormCtrl->GetExportValue();
1780 trimmed.TrimRight();
1781 trimmed.TrimLeft();
1782 dTemp = FX_atof(trimmed);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001783 break;
Lei Zhang9559b7a2015-12-21 11:12:20 -08001784 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001785 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001786 }
Tom Sepez4246b002016-01-20 11:48:29 -08001787 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001788 case FIELDTYPE_LISTBOX: {
Tom Sepez4246b002016-01-20 11:48:29 -08001789 if (pFormField->CountSelectedItems() <= 1) {
1790 CFX_WideString trimmed = pFormField->GetValue();
1791 trimmed.TrimRight();
1792 trimmed.TrimLeft();
1793 dTemp = FX_atof(trimmed);
1794 }
1795 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001796 default:
1797 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001798 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001799
1800 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
1801 wcscmp(sFunction.c_str(), L"MAX") == 0))
1802 dValue = dTemp;
1803
1804 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
1805
1806 nFieldsCount++;
1807 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001808 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001809 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001810
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001811 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
1812 dValue /= nFieldsCount;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001813
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001814 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
1815 FXSYS_pow((double)10, (double)6);
Tom Sepez67fd5df2015-10-08 12:24:19 -07001816 CJS_Value jsValue(pRuntime, dValue);
foxit8b544ed2015-09-10 14:57:54 +08001817 if (pContext->GetEventHandler()->m_pValue)
1818 pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001819
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001820 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001821}
1822
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001823/* This function validates the current event to ensure that its value is
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001824** within the specified range. */
1825
Lei Zhang945fdb72015-11-11 10:18:16 -08001826FX_BOOL CJS_PublicMethods::AFRange_Validate(
1827 IJS_Context* cc,
1828 const std::vector<CJS_Value>& params,
1829 CJS_Value& vRet,
1830 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001831 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001832 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001833
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001834 if (params.size() != 4) {
1835 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1836 return FALSE;
1837 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001838
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001839 if (!pEvent->m_pValue)
1840 return FALSE;
1841 if (pEvent->Value().IsEmpty())
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001842 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001843 double dEentValue = atof(CFX_ByteString::FromUnicode(pEvent->Value()));
1844 FX_BOOL bGreaterThan = params[0].ToBool();
1845 double dGreaterThan = params[1].ToDouble();
1846 FX_BOOL bLessThan = params[2].ToBool();
1847 double dLessThan = params[3].ToDouble();
1848 CFX_WideString swMsg;
1849
1850 if (bGreaterThan && bLessThan) {
1851 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
1852 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
1853 params[1].ToCFXWideString().c_str(),
1854 params[3].ToCFXWideString().c_str());
1855 } else if (bGreaterThan) {
1856 if (dEentValue < dGreaterThan)
1857 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
1858 params[1].ToCFXWideString().c_str());
1859 } else if (bLessThan) {
1860 if (dEentValue > dLessThan)
1861 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
1862 params[3].ToCFXWideString().c_str());
1863 }
1864
1865 if (!swMsg.IsEmpty()) {
1866 Alert(pContext, swMsg.c_str());
1867 pEvent->Rc() = FALSE;
1868 }
1869 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001870}
1871
Tom Sepezba038bc2015-10-08 12:03:00 -07001872FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001873 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001874 CJS_Value& vRet,
1875 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001876 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001877 if (params.size() != 1) {
1878 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1879 return FALSE;
1880 }
1881
Tom Sepez67fd5df2015-10-08 12:24:19 -07001882 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
1883 CJS_Array nums(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001884
1885 CFX_WideString str = params[0].ToCFXWideString();
1886 CFX_WideString sPart;
1887
1888 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
1889 str = L"0" + str;
1890
1891 int nIndex = 0;
1892 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
1893 FX_WCHAR wc = str.GetAt(i);
Lei Zhang9559b7a2015-12-21 11:12:20 -08001894 if (FXSYS_iswdigit(wc)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001895 sPart += wc;
1896 } else {
1897 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001898 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001899 sPart = L"";
1900 nIndex++;
1901 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001902 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001903 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001904
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001905 if (sPart.GetLength() > 0) {
Tom Sepez67fd5df2015-10-08 12:24:19 -07001906 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001907 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001908
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001909 if (nums.GetLength() > 0)
1910 vRet = nums;
1911 else
1912 vRet.SetNull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001913
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001914 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001915}