John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/javascript/PublicMethods.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
Lei Zhang | 375a864 | 2016-01-11 11:59:17 -0800 | [diff] [blame] | 9 | #include <algorithm> |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 10 | #include <iomanip> |
dsinclair | 992ecf7 | 2016-12-14 05:45:57 -0800 | [diff] [blame] | 11 | #include <limits> |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 12 | #include <sstream> |
| 13 | #include <string> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 14 | #include <vector> |
Lei Zhang | 375a864 | 2016-01-11 11:59:17 -0800 | [diff] [blame] | 15 | |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 16 | #include "core/fpdfdoc/cpdf_interform.h" |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 17 | #include "core/fxcrt/fx_ext.h" |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 18 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 19 | #include "fpdfsdk/cpdfsdk_interform.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 20 | #include "fpdfsdk/javascript/Field.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 21 | #include "fpdfsdk/javascript/JS_Define.h" |
| 22 | #include "fpdfsdk/javascript/JS_EventHandler.h" |
| 23 | #include "fpdfsdk/javascript/JS_Object.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 24 | #include "fpdfsdk/javascript/JS_Value.h" |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 25 | #include "fpdfsdk/javascript/cjs_event_context.h" |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 26 | #include "fpdfsdk/javascript/cjs_runtime.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 27 | #include "fpdfsdk/javascript/color.h" |
| 28 | #include "fpdfsdk/javascript/resource.h" |
| 29 | #include "fpdfsdk/javascript/util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 30 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 31 | #define DOUBLE_CORRECT 0.000000000000001 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 32 | |
| 33 | BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format) |
| 35 | JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke) |
| 36 | JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format) |
| 37 | JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke) |
| 38 | JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx) |
| 39 | JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx) |
| 40 | JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format) |
| 41 | JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke) |
| 42 | JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx) |
| 43 | JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx) |
| 44 | JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format) |
| 45 | JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke) |
| 46 | JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format) |
| 47 | JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke) |
| 48 | JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx) |
| 49 | JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple) |
| 50 | JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber) |
| 51 | JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate) |
| 52 | JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate) |
| 53 | JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange) |
| 54 | JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx) |
| 55 | JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 56 | END_JS_STATIC_GLOBAL_FUN() |
| 57 | |
| 58 | IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods) |
| 59 | |
tsepez | 745611b | 2016-04-12 16:46:34 -0700 | [diff] [blame] | 60 | namespace { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 61 | |
tsepez | 745611b | 2016-04-12 16:46:34 -0700 | [diff] [blame] | 62 | const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr", |
| 63 | L"May", L"Jun", L"Jul", L"Aug", |
| 64 | L"Sep", L"Oct", L"Nov", L"Dec"}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 65 | |
tsepez | 745611b | 2016-04-12 16:46:34 -0700 | [diff] [blame] | 66 | const FX_WCHAR* const fullmonths[] = {L"January", L"February", L"March", |
| 67 | L"April", L"May", L"June", |
| 68 | L"July", L"August", L"September", |
| 69 | L"October", L"November", L"December"}; |
| 70 | |
| 71 | CFX_ByteString StrTrim(const CFX_ByteString& pStr) { |
| 72 | CFX_ByteString result(pStr); |
| 73 | result.TrimLeft(' '); |
| 74 | result.TrimRight(' '); |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | CFX_WideString StrTrim(const CFX_WideString& pStr) { |
| 79 | CFX_WideString result(pStr); |
| 80 | result.TrimLeft(' '); |
| 81 | result.TrimRight(' '); |
| 82 | return result; |
| 83 | } |
| 84 | |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 85 | void AlertIfPossible(CJS_EventContext* pContext, const FX_WCHAR* swMsg) { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 86 | CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv(); |
| 87 | if (pFormFillEnv) |
| 88 | pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3); |
tsepez | e1e7bd0 | 2016-08-08 13:03:16 -0700 | [diff] [blame] | 89 | } |
| 90 | |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 91 | #if _FX_OS_ != _FX_ANDROID_ |
| 92 | CFX_ByteString CalculateString(double dValue, |
| 93 | int iDec, |
| 94 | int* iDec2, |
| 95 | bool* bNegative) { |
| 96 | *bNegative = dValue < 0; |
| 97 | if (*bNegative) |
| 98 | dValue = -dValue; |
dsinclair | 992ecf7 | 2016-12-14 05:45:57 -0800 | [diff] [blame] | 99 | |
| 100 | // Make sure the number of precision characters will fit. |
| 101 | if (iDec > std::numeric_limits<double>::digits10) |
| 102 | iDec = std::numeric_limits<double>::digits10; |
| 103 | |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 104 | std::stringstream ss; |
| 105 | ss << std::fixed << std::setprecision(iDec) << dValue; |
| 106 | std::string stringValue = ss.str(); |
| 107 | size_t iDecimalPos = stringValue.find("."); |
| 108 | *iDec2 = iDecimalPos == std::string::npos ? stringValue.size() |
| 109 | : static_cast<int>(iDecimalPos); |
| 110 | return CFX_ByteString(stringValue.c_str()); |
| 111 | } |
| 112 | #endif |
| 113 | |
tsepez | 745611b | 2016-04-12 16:46:34 -0700 | [diff] [blame] | 114 | } // namespace |
| 115 | |
| 116 | bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) { |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 117 | CFX_WideString sTrim = StrTrim(str); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | const FX_WCHAR* pTrim = sTrim.c_str(); |
| 119 | const FX_WCHAR* p = pTrim; |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 120 | bool bDot = false; |
| 121 | bool bKXJS = false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 122 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | wchar_t c; |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 124 | while ((c = *p) != L'\0') { |
| 125 | if (c == L'.' || c == L',') { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | if (bDot) |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 127 | return false; |
| 128 | bDot = true; |
| 129 | } else if (c == L'-' || c == L'+') { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 130 | if (p != pTrim) |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 131 | return false; |
| 132 | } else if (c == L'e' || c == L'E') { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 133 | if (bKXJS) |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 134 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 135 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | p++; |
| 137 | c = *p; |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 138 | if (c == L'+' || c == L'-') { |
| 139 | bKXJS = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 140 | } else { |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 141 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 142 | } |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 143 | } else if (!FXSYS_iswdigit(c)) { |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 144 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 145 | } |
| 146 | p++; |
| 147 | } |
| 148 | |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 149 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 152 | bool CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 153 | switch (c_Mask) { |
| 154 | case L'9': |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 155 | return FXSYS_iswdigit(c_Change); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | case L'A': |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 157 | return FXSYS_iswalpha(c_Change); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 158 | case L'O': |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 159 | return FXSYS_iswalnum(c_Change); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 160 | case L'X': |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 161 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 162 | default: |
| 163 | return (c_Change == c_Mask); |
| 164 | } |
| 165 | } |
| 166 | |
Wei Li | 614d20a | 2016-03-15 13:55:12 -0700 | [diff] [blame] | 167 | bool CJS_PublicMethods::isReservedMaskChar(wchar_t ch) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 168 | return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X'; |
| 169 | } |
| 170 | |
| 171 | double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction, |
| 172 | double dValue1, |
| 173 | double dValue2) { |
| 174 | if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 || |
| 175 | FXSYS_wcsicmp(sFuction, L"SUM") == 0) { |
| 176 | return dValue1 + dValue2; |
| 177 | } |
| 178 | if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) { |
| 179 | return dValue1 * dValue2; |
| 180 | } |
| 181 | if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) { |
Lei Zhang | 375a864 | 2016-01-11 11:59:17 -0800 | [diff] [blame] | 182 | return std::min(dValue1, dValue2); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | } |
| 184 | if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) { |
Lei Zhang | 375a864 | 2016-01-11 11:59:17 -0800 | [diff] [blame] | 185 | return std::max(dValue1, dValue2); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | } |
| 187 | return dValue1; |
| 188 | } |
| 189 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 190 | CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 191 | CJS_Value val) { |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 192 | CJS_Array StrArray; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | if (val.IsArrayObject()) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 194 | val.ConvertToArray(pRuntime, StrArray); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 195 | return StrArray; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 197 | CFX_WideString wsStr = val.ToCFXWideString(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 198 | CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr); |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 199 | const char* p = t.c_str(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 200 | |
| 201 | int ch = ','; |
| 202 | int nIndex = 0; |
| 203 | |
| 204 | while (*p) { |
| 205 | const char* pTemp = strchr(p, ch); |
Lei Zhang | 997de61 | 2015-11-04 18:17:53 -0800 | [diff] [blame] | 206 | if (!pTemp) { |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 207 | StrArray.SetElement( |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 208 | pRuntime, nIndex, |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 209 | CJS_Value(pRuntime, StrTrim(CFX_ByteString(p)).c_str())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 211 | } |
Lei Zhang | 997de61 | 2015-11-04 18:17:53 -0800 | [diff] [blame] | 212 | |
| 213 | char* pSub = new char[pTemp - p + 1]; |
| 214 | strncpy(pSub, p, pTemp - p); |
| 215 | *(pSub + (pTemp - p)) = '\0'; |
| 216 | |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 217 | StrArray.SetElement( |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 218 | pRuntime, nIndex, |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 219 | CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub)).c_str())); |
Lei Zhang | 997de61 | 2015-11-04 18:17:53 -0800 | [diff] [blame] | 220 | delete[] pSub; |
| 221 | |
| 222 | nIndex++; |
| 223 | p = ++pTemp; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 224 | } |
| 225 | return StrArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 228 | int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 229 | int nStart, |
| 230 | int& nSkip, |
| 231 | int nMaxStep) { |
| 232 | int nRet = 0; |
| 233 | nSkip = 0; |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 234 | for (int i = nStart, sz = str.GetLength(); i < sz; i++) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 235 | if (i - nStart > 10) |
| 236 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 237 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 238 | FX_WCHAR c = str.GetAt(i); |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 239 | if (!FXSYS_iswdigit(c)) |
| 240 | break; |
| 241 | |
Dan Sinclair | 1c91537 | 2016-03-03 17:12:58 -0500 | [diff] [blame] | 242 | nRet = nRet * 10 + FXSYS_toDecimalDigit(c); |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 243 | nSkip = i - nStart + 1; |
| 244 | if (nSkip >= nMaxStep) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 245 | break; |
| 246 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 247 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 248 | return nRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 251 | CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str, |
| 252 | int nStart, |
| 253 | int& nSkip) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 254 | CFX_WideString swRet; |
| 255 | nSkip = 0; |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 256 | for (int i = nStart, sz = str.GetLength(); i < sz; i++) { |
| 257 | FX_WCHAR c = str.GetAt(i); |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 258 | if (!FXSYS_iswdigit(c)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 259 | break; |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 260 | |
| 261 | swRet += c; |
| 262 | nSkip = i - nStart + 1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 263 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 264 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | return swRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 268 | double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value, |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 269 | bool* bWrongFormat) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | double dt = JS_GetDateTime(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 271 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 272 | int nYear = JS_GetYearFromTime(dt); |
| 273 | int nMonth = JS_GetMonthFromTime(dt) + 1; |
| 274 | int nDay = JS_GetDayFromTime(dt); |
| 275 | int nHour = JS_GetHourFromTime(dt); |
| 276 | int nMin = JS_GetMinFromTime(dt); |
| 277 | int nSec = JS_GetSecFromTime(dt); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 278 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 279 | int number[3]; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 280 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | int nSkip = 0; |
| 282 | int nLen = value.GetLength(); |
| 283 | int nIndex = 0; |
| 284 | int i = 0; |
| 285 | while (i < nLen) { |
| 286 | if (nIndex > 2) |
| 287 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 288 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 289 | FX_WCHAR c = value.GetAt(i); |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 290 | if (FXSYS_iswdigit(c)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 291 | number[nIndex++] = ParseStringInteger(value, i, nSkip, 4); |
| 292 | i += nSkip; |
| 293 | } else { |
| 294 | i++; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (nIndex == 2) { |
| 299 | // case2: month/day |
| 300 | // case3: day/month |
| 301 | if ((number[0] >= 1 && number[0] <= 12) && |
| 302 | (number[1] >= 1 && number[1] <= 31)) { |
| 303 | nMonth = number[0]; |
| 304 | nDay = number[1]; |
| 305 | } else if ((number[0] >= 1 && number[0] <= 31) && |
| 306 | (number[1] >= 1 && number[1] <= 12)) { |
| 307 | nDay = number[0]; |
| 308 | nMonth = number[1]; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 309 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 310 | |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 311 | if (bWrongFormat) |
| 312 | *bWrongFormat = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 313 | } else if (nIndex == 3) { |
| 314 | // case1: year/month/day |
| 315 | // case2: month/day/year |
| 316 | // case3: day/month/year |
Tom Sepez | 5ffacd6 | 2014-07-18 14:42:12 -0700 | [diff] [blame] | 317 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 318 | if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) && |
| 319 | (number[2] >= 1 && number[2] <= 31)) { |
| 320 | nYear = number[0]; |
| 321 | nMonth = number[1]; |
| 322 | nDay = number[2]; |
| 323 | } else if ((number[0] >= 1 && number[0] <= 12) && |
| 324 | (number[1] >= 1 && number[1] <= 31) && number[2] > 31) { |
| 325 | nMonth = number[0]; |
| 326 | nDay = number[1]; |
| 327 | nYear = number[2]; |
| 328 | } else if ((number[0] >= 1 && number[0] <= 31) && |
| 329 | (number[1] >= 1 && number[1] <= 12) && number[2] > 31) { |
| 330 | nDay = number[0]; |
| 331 | nMonth = number[1]; |
| 332 | nYear = number[2]; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 333 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 334 | |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 335 | if (bWrongFormat) |
| 336 | *bWrongFormat = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 337 | } else { |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 338 | if (bWrongFormat) |
| 339 | *bWrongFormat = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 340 | return dt; |
| 341 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 342 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 343 | CFX_WideString swTemp; |
| 344 | swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec); |
tsepez | 018935c | 2016-04-15 13:15:12 -0700 | [diff] [blame] | 345 | return JS_DateParse(swTemp); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 348 | double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value, |
| 349 | const CFX_WideString& format, |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 350 | bool* bWrongFormat) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 351 | double dt = JS_GetDateTime(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 352 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 353 | if (format.IsEmpty() || value.IsEmpty()) |
| 354 | return dt; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 355 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 356 | int nYear = JS_GetYearFromTime(dt); |
| 357 | int nMonth = JS_GetMonthFromTime(dt) + 1; |
| 358 | int nDay = JS_GetDayFromTime(dt); |
| 359 | int nHour = JS_GetHourFromTime(dt); |
| 360 | int nMin = JS_GetMinFromTime(dt); |
| 361 | int nSec = JS_GetSecFromTime(dt); |
| 362 | |
| 363 | int nYearSub = 99; // nYear - 2000; |
| 364 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 365 | bool bPm = false; |
| 366 | bool bExit = false; |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 367 | bool bBadFormat = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 368 | |
| 369 | int i = 0; |
| 370 | int j = 0; |
| 371 | |
| 372 | while (i < format.GetLength()) { |
| 373 | if (bExit) |
| 374 | break; |
| 375 | |
| 376 | FX_WCHAR c = format.GetAt(i); |
| 377 | switch (c) { |
| 378 | case ':': |
| 379 | case '.': |
| 380 | case '-': |
| 381 | case '\\': |
| 382 | case '/': |
| 383 | i++; |
| 384 | j++; |
| 385 | break; |
| 386 | |
| 387 | case 'y': |
| 388 | case 'm': |
| 389 | case 'd': |
| 390 | case 'H': |
| 391 | case 'h': |
| 392 | case 'M': |
| 393 | case 's': |
| 394 | case 't': { |
| 395 | int oldj = j; |
| 396 | int nSkip = 0; |
| 397 | int remaining = format.GetLength() - i - 1; |
| 398 | |
| 399 | if (remaining == 0 || format.GetAt(i + 1) != c) { |
| 400 | switch (c) { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 401 | case 'y': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 402 | i++; |
| 403 | j++; |
| 404 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 405 | case 'm': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 406 | nMonth = ParseStringInteger(value, j, nSkip, 2); |
| 407 | i++; |
| 408 | j += nSkip; |
| 409 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 410 | case 'd': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 411 | nDay = ParseStringInteger(value, j, nSkip, 2); |
| 412 | i++; |
| 413 | j += nSkip; |
| 414 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 415 | case 'H': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 416 | nHour = ParseStringInteger(value, j, nSkip, 2); |
| 417 | i++; |
| 418 | j += nSkip; |
| 419 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 420 | case 'h': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 421 | nHour = ParseStringInteger(value, j, nSkip, 2); |
| 422 | i++; |
| 423 | j += nSkip; |
| 424 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 425 | case 'M': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 426 | nMin = ParseStringInteger(value, j, nSkip, 2); |
| 427 | i++; |
| 428 | j += nSkip; |
| 429 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 430 | case 's': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 431 | nSec = ParseStringInteger(value, j, nSkip, 2); |
| 432 | i++; |
| 433 | j += nSkip; |
| 434 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 435 | case 't': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 436 | bPm = (j < value.GetLength() && value.GetAt(j) == 'p'); |
| 437 | i++; |
| 438 | j++; |
| 439 | break; |
| 440 | } |
| 441 | } else if (remaining == 1 || format.GetAt(i + 2) != c) { |
| 442 | switch (c) { |
| 443 | case 'y': |
| 444 | nYear = ParseStringInteger(value, j, nSkip, 4); |
| 445 | i += 2; |
| 446 | j += nSkip; |
| 447 | break; |
| 448 | case 'm': |
| 449 | nMonth = ParseStringInteger(value, j, nSkip, 2); |
| 450 | i += 2; |
| 451 | j += nSkip; |
| 452 | break; |
| 453 | case 'd': |
| 454 | nDay = ParseStringInteger(value, j, nSkip, 2); |
| 455 | i += 2; |
| 456 | j += nSkip; |
| 457 | break; |
| 458 | case 'H': |
| 459 | nHour = ParseStringInteger(value, j, nSkip, 2); |
| 460 | i += 2; |
| 461 | j += nSkip; |
| 462 | break; |
| 463 | case 'h': |
| 464 | nHour = ParseStringInteger(value, j, nSkip, 2); |
| 465 | i += 2; |
| 466 | j += nSkip; |
| 467 | break; |
| 468 | case 'M': |
| 469 | nMin = ParseStringInteger(value, j, nSkip, 2); |
| 470 | i += 2; |
| 471 | j += nSkip; |
| 472 | break; |
| 473 | case 's': |
| 474 | nSec = ParseStringInteger(value, j, nSkip, 2); |
| 475 | i += 2; |
| 476 | j += nSkip; |
| 477 | break; |
| 478 | case 't': |
| 479 | bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' && |
| 480 | value.GetAt(j + 1) == 'm'); |
| 481 | i += 2; |
| 482 | j += 2; |
| 483 | break; |
| 484 | } |
| 485 | } else if (remaining == 2 || format.GetAt(i + 3) != c) { |
| 486 | switch (c) { |
| 487 | case 'm': { |
| 488 | CFX_WideString sMonth = ParseStringString(value, j, nSkip); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 489 | bool bFind = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 490 | for (int m = 0; m < 12; m++) { |
| 491 | if (sMonth.CompareNoCase(months[m]) == 0) { |
| 492 | nMonth = m + 1; |
| 493 | i += 3; |
| 494 | j += nSkip; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 495 | bFind = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 496 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 497 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | if (!bFind) { |
| 501 | nMonth = ParseStringInteger(value, j, nSkip, 3); |
| 502 | i += 3; |
| 503 | j += nSkip; |
| 504 | } |
| 505 | } break; |
| 506 | case 'y': |
| 507 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 508 | default: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 509 | i += 3; |
| 510 | j += 3; |
| 511 | break; |
| 512 | } |
| 513 | } else if (remaining == 3 || format.GetAt(i + 4) != c) { |
| 514 | switch (c) { |
| 515 | case 'y': |
| 516 | nYear = ParseStringInteger(value, j, nSkip, 4); |
| 517 | j += nSkip; |
| 518 | i += 4; |
| 519 | break; |
| 520 | case 'm': { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 521 | bool bFind = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 522 | |
| 523 | CFX_WideString sMonth = ParseStringString(value, j, nSkip); |
| 524 | sMonth.MakeLower(); |
| 525 | |
| 526 | for (int m = 0; m < 12; m++) { |
| 527 | CFX_WideString sFullMonths = fullmonths[m]; |
| 528 | sFullMonths.MakeLower(); |
| 529 | |
| 530 | if (sFullMonths.Find(sMonth.c_str(), 0) != -1) { |
| 531 | nMonth = m + 1; |
| 532 | i += 4; |
| 533 | j += nSkip; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 534 | bFind = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 535 | break; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (!bFind) { |
| 540 | nMonth = ParseStringInteger(value, j, nSkip, 4); |
| 541 | i += 4; |
| 542 | j += nSkip; |
| 543 | } |
| 544 | } break; |
| 545 | default: |
| 546 | i += 4; |
| 547 | j += 4; |
| 548 | break; |
| 549 | } |
| 550 | } else { |
| 551 | if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) { |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 552 | bBadFormat = true; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 553 | bExit = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 554 | } |
| 555 | i++; |
| 556 | j++; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 557 | } |
Tom Sepez | 8538642 | 2014-07-23 10:28:37 -0700 | [diff] [blame] | 558 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 559 | if (oldj == j) { |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 560 | bBadFormat = true; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 561 | bExit = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | break; |
| 566 | default: |
| 567 | if (value.GetLength() <= j) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 568 | bExit = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 569 | } else if (format.GetAt(i) != value.GetAt(j)) { |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 570 | bBadFormat = true; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 571 | bExit = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | i++; |
| 575 | j++; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (bPm) |
| 581 | nHour += 12; |
| 582 | |
| 583 | if (nYear >= 0 && nYear <= nYearSub) |
| 584 | nYear += 2000; |
| 585 | |
| 586 | if (nMonth < 1 || nMonth > 12) |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 587 | bBadFormat = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 588 | |
| 589 | if (nDay < 1 || nDay > 31) |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 590 | bBadFormat = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 591 | |
| 592 | if (nHour < 0 || nHour > 24) |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 593 | bBadFormat = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 594 | |
| 595 | if (nMin < 0 || nMin > 60) |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 596 | bBadFormat = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 597 | |
| 598 | if (nSec < 0 || nSec > 60) |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 599 | bBadFormat = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 600 | |
| 601 | double dRet = 0; |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 602 | if (bBadFormat) { |
| 603 | dRet = ParseNormalDate(value, &bBadFormat); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 604 | } else { |
| 605 | dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay), |
| 606 | JS_MakeTime(nHour, nMin, nSec, 0)); |
tsepez | 018935c | 2016-04-15 13:15:12 -0700 | [diff] [blame] | 607 | if (JS_PortIsNan(dRet)) |
| 608 | dRet = JS_DateParse(value); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 609 | } |
| 610 | |
tsepez | 018935c | 2016-04-15 13:15:12 -0700 | [diff] [blame] | 611 | if (JS_PortIsNan(dRet)) |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 612 | dRet = ParseNormalDate(value, &bBadFormat); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 613 | |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 614 | if (bWrongFormat) |
| 615 | *bWrongFormat = bBadFormat; |
tsepez | 018935c | 2016-04-15 13:15:12 -0700 | [diff] [blame] | 616 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 617 | return dRet; |
| 618 | } |
| 619 | |
| 620 | CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate, |
| 621 | const CFX_WideString& format) { |
| 622 | CFX_WideString sRet = L"", sPart = L""; |
| 623 | |
| 624 | int nYear = JS_GetYearFromTime(dDate); |
| 625 | int nMonth = JS_GetMonthFromTime(dDate) + 1; |
| 626 | int nDay = JS_GetDayFromTime(dDate); |
| 627 | int nHour = JS_GetHourFromTime(dDate); |
| 628 | int nMin = JS_GetMinFromTime(dDate); |
| 629 | int nSec = JS_GetSecFromTime(dDate); |
| 630 | |
| 631 | int i = 0; |
| 632 | while (i < format.GetLength()) { |
| 633 | FX_WCHAR c = format.GetAt(i); |
| 634 | int remaining = format.GetLength() - i - 1; |
| 635 | sPart = L""; |
| 636 | switch (c) { |
| 637 | case 'y': |
| 638 | case 'm': |
| 639 | case 'd': |
| 640 | case 'H': |
| 641 | case 'h': |
| 642 | case 'M': |
| 643 | case 's': |
| 644 | case 't': |
| 645 | if (remaining == 0 || format.GetAt(i + 1) != c) { |
| 646 | switch (c) { |
| 647 | case 'y': |
| 648 | sPart += c; |
| 649 | break; |
| 650 | case 'm': |
| 651 | sPart.Format(L"%d", nMonth); |
| 652 | break; |
| 653 | case 'd': |
| 654 | sPart.Format(L"%d", nDay); |
| 655 | break; |
| 656 | case 'H': |
| 657 | sPart.Format(L"%d", nHour); |
| 658 | break; |
| 659 | case 'h': |
| 660 | sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour); |
| 661 | break; |
| 662 | case 'M': |
| 663 | sPart.Format(L"%d", nMin); |
| 664 | break; |
| 665 | case 's': |
| 666 | sPart.Format(L"%d", nSec); |
| 667 | break; |
| 668 | case 't': |
| 669 | sPart += nHour > 12 ? 'p' : 'a'; |
| 670 | break; |
| 671 | } |
| 672 | i++; |
| 673 | } else if (remaining == 1 || format.GetAt(i + 2) != c) { |
| 674 | switch (c) { |
| 675 | case 'y': |
| 676 | sPart.Format(L"%02d", nYear - (nYear / 100) * 100); |
| 677 | break; |
| 678 | case 'm': |
| 679 | sPart.Format(L"%02d", nMonth); |
| 680 | break; |
| 681 | case 'd': |
| 682 | sPart.Format(L"%02d", nDay); |
| 683 | break; |
| 684 | case 'H': |
| 685 | sPart.Format(L"%02d", nHour); |
| 686 | break; |
| 687 | case 'h': |
| 688 | sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour); |
| 689 | break; |
| 690 | case 'M': |
| 691 | sPart.Format(L"%02d", nMin); |
| 692 | break; |
| 693 | case 's': |
| 694 | sPart.Format(L"%02d", nSec); |
| 695 | break; |
| 696 | case 't': |
| 697 | sPart = nHour > 12 ? L"pm" : L"am"; |
| 698 | break; |
| 699 | } |
| 700 | i += 2; |
| 701 | } else if (remaining == 2 || format.GetAt(i + 3) != c) { |
| 702 | switch (c) { |
| 703 | case 'm': |
| 704 | i += 3; |
| 705 | if (nMonth > 0 && nMonth <= 12) |
| 706 | sPart += months[nMonth - 1]; |
| 707 | break; |
| 708 | default: |
| 709 | i += 3; |
| 710 | sPart += c; |
| 711 | sPart += c; |
| 712 | sPart += c; |
| 713 | break; |
| 714 | } |
| 715 | } else if (remaining == 3 || format.GetAt(i + 4) != c) { |
| 716 | switch (c) { |
| 717 | case 'y': |
| 718 | sPart.Format(L"%04d", nYear); |
| 719 | i += 4; |
| 720 | break; |
| 721 | case 'm': |
| 722 | i += 4; |
| 723 | if (nMonth > 0 && nMonth <= 12) |
| 724 | sPart += fullmonths[nMonth - 1]; |
| 725 | break; |
| 726 | default: |
| 727 | i += 4; |
| 728 | sPart += c; |
| 729 | sPart += c; |
| 730 | sPart += c; |
| 731 | sPart += c; |
| 732 | break; |
| 733 | } |
| 734 | } else { |
| 735 | i++; |
| 736 | sPart += c; |
| 737 | } |
| 738 | break; |
| 739 | default: |
| 740 | i++; |
| 741 | sPart += c; |
| 742 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 743 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 744 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 745 | sRet += sPart; |
| 746 | } |
| 747 | |
| 748 | return sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 751 | // function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency, |
| 752 | // bCurrencyPrepend) |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 753 | bool CJS_PublicMethods::AFNumber_Format(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 754 | const std::vector<CJS_Value>& params, |
| 755 | CJS_Value& vRet, |
| 756 | CFX_WideString& sError) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 757 | #if _FX_OS_ != _FX_ANDROID_ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 758 | if (params.size() != 6) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 759 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 760 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 761 | } |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 762 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 763 | CJS_EventHandler* pEvent = |
| 764 | pRuntime->GetCurrentEventContext()->GetEventHandler(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 765 | if (!pEvent->m_pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 766 | return false; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 767 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 768 | CFX_WideString& Value = pEvent->Value(); |
| 769 | CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 770 | if (strValue.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 771 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 772 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 773 | int iDec = params[0].ToInt(pRuntime); |
| 774 | int iSepStyle = params[1].ToInt(pRuntime); |
| 775 | int iNegStyle = params[2].ToInt(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 776 | // params[3] is iCurrStyle, it's not used. |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 777 | CFX_WideString wstrCurrency = params[4].ToCFXWideString(pRuntime); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 778 | bool bCurrencyPrepend = params[5].ToBool(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 779 | |
| 780 | if (iDec < 0) |
| 781 | iDec = -iDec; |
| 782 | |
| 783 | if (iSepStyle < 0 || iSepStyle > 3) |
| 784 | iSepStyle = 0; |
| 785 | |
| 786 | if (iNegStyle < 0 || iNegStyle > 3) |
| 787 | iNegStyle = 0; |
| 788 | |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 789 | // Processing decimal places |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 790 | strValue.Replace(",", "."); |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 791 | double dValue = atof(strValue.c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 792 | if (iDec > 0) |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 793 | dValue += DOUBLE_CORRECT; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 794 | |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 795 | // Calculating number string |
| 796 | bool bNegative; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 797 | int iDec2; |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 798 | strValue = CalculateString(dValue, iDec, &iDec2, &bNegative); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 799 | if (strValue.IsEmpty()) { |
| 800 | dValue = 0; |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 801 | strValue = CalculateString(dValue, iDec, &iDec2, &bNegative); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 802 | if (strValue.IsEmpty()) { |
| 803 | strValue = "0"; |
| 804 | iDec2 = 1; |
| 805 | } |
| 806 | } |
| 807 | |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 808 | // Processing separator style |
| 809 | if (iDec2 < strValue.GetLength()) { |
| 810 | if (iSepStyle == 2 || iSepStyle == 3) |
| 811 | strValue.Replace(".", ","); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 812 | |
| 813 | if (iDec2 == 0) |
| 814 | strValue.Insert(iDec2, '0'); |
| 815 | } |
| 816 | if (iSepStyle == 0 || iSepStyle == 2) { |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 817 | char cSeparator; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 818 | if (iSepStyle == 0) |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 819 | cSeparator = ','; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 820 | else |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 821 | cSeparator = '.'; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 822 | |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 823 | for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) |
| 824 | strValue.Insert(iDecPositive, cSeparator); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 825 | } |
| 826 | |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 827 | // Processing currency string |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 828 | Value = CFX_WideString::FromLocal(strValue.AsStringC()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 829 | |
| 830 | if (bCurrencyPrepend) |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 831 | Value = wstrCurrency + Value; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 832 | else |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 833 | Value = Value + wstrCurrency; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 834 | |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 835 | // Processing negative style |
| 836 | if (bNegative) { |
| 837 | if (iNegStyle == 0) |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 838 | Value = L"-" + Value; |
npm | 49c5928 | 2016-11-15 15:14:04 -0800 | [diff] [blame] | 839 | else if (iNegStyle == 2 || iNegStyle == 3) |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 840 | Value = L"(" + Value + L")"; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 841 | if (iNegStyle == 1 || iNegStyle == 3) { |
| 842 | if (Field* fTarget = pEvent->Target_Field()) { |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 843 | CJS_Array arColor; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 844 | CJS_Value vColElm(pRuntime); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 845 | vColElm = CJS_Value(pRuntime, L"RGB"); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 846 | arColor.SetElement(pRuntime, 0, vColElm); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 847 | vColElm = CJS_Value(pRuntime, 1); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 848 | arColor.SetElement(pRuntime, 1, vColElm); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 849 | vColElm = CJS_Value(pRuntime, 0); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 850 | arColor.SetElement(pRuntime, 2, vColElm); |
| 851 | arColor.SetElement(pRuntime, 3, vColElm); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 852 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 853 | CJS_PropValue vProp(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 854 | vProp.StartGetting(); |
| 855 | vProp << arColor; |
| 856 | vProp.StartSetting(); |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 857 | fTarget->textColor(pRuntime, vProp, sError); // red |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | } else { |
| 861 | if (iNegStyle == 1 || iNegStyle == 3) { |
| 862 | if (Field* fTarget = pEvent->Target_Field()) { |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 863 | CJS_Array arColor; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 864 | CJS_Value vColElm(pRuntime); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 865 | vColElm = CJS_Value(pRuntime, L"RGB"); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 866 | arColor.SetElement(pRuntime, 0, vColElm); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 867 | vColElm = CJS_Value(pRuntime, 0); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 868 | arColor.SetElement(pRuntime, 1, vColElm); |
| 869 | arColor.SetElement(pRuntime, 2, vColElm); |
| 870 | arColor.SetElement(pRuntime, 3, vColElm); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 871 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 872 | CJS_PropValue vProp(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 873 | vProp.StartGetting(); |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 874 | fTarget->textColor(pRuntime, vProp, sError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 875 | |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 876 | CJS_Array aProp; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 877 | vProp.GetJSValue()->ConvertToArray(pRuntime, aProp); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 878 | |
| 879 | CPWL_Color crProp; |
| 880 | CPWL_Color crColor; |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 881 | color::ConvertArrayToPWLColor(pRuntime, aProp, &crProp); |
| 882 | color::ConvertArrayToPWLColor(pRuntime, arColor, &crColor); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 883 | |
| 884 | if (crColor != crProp) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 885 | CJS_PropValue vProp2(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 886 | vProp2.StartGetting(); |
| 887 | vProp2 << arColor; |
| 888 | vProp2.StartSetting(); |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 889 | fTarget->textColor(pRuntime, vProp2, sError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | } |
| 893 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 894 | #endif |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 895 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 898 | // function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency, |
| 899 | // bCurrencyPrepend) |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 900 | bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 901 | const std::vector<CJS_Value>& params, |
| 902 | CJS_Value& vRet, |
| 903 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 904 | if (params.size() < 2) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 905 | return false; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 906 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 907 | CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); |
| 908 | CJS_EventHandler* pEvent = pContext->GetEventHandler(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 909 | if (!pEvent->m_pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 910 | return false; |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 911 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 912 | CFX_WideString& val = pEvent->Value(); |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 913 | CFX_WideString& wstrChange = pEvent->Change(); |
| 914 | CFX_WideString wstrValue = val; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 915 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 916 | if (pEvent->WillCommit()) { |
ochang | a0a3bc3 | 2016-05-12 15:22:48 -0700 | [diff] [blame] | 917 | CFX_WideString swTemp = StrTrim(wstrValue); |
| 918 | if (swTemp.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 919 | return true; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 920 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 921 | swTemp.Replace(L",", L"."); |
| 922 | if (!IsNumber(swTemp.c_str())) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 923 | pEvent->Rc() = false; |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 924 | sError = JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE); |
tsepez | e1e7bd0 | 2016-08-08 13:03:16 -0700 | [diff] [blame] | 925 | AlertIfPossible(pContext, sError.c_str()); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 926 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 927 | return true; // it happens after the last keystroke and before validating, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 928 | } |
Tom Sepez | 4f7bc04 | 2015-04-27 12:06:58 -0700 | [diff] [blame] | 929 | |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 930 | CFX_WideString wstrSelected; |
| 931 | if (pEvent->SelStart() != -1) { |
| 932 | wstrSelected = wstrValue.Mid(pEvent->SelStart(), |
| 933 | pEvent->SelEnd() - pEvent->SelStart()); |
| 934 | } |
| 935 | |
| 936 | bool bHasSign = wstrValue.Find(L'-') != -1 && wstrSelected.Find(L'-') == -1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 937 | if (bHasSign) { |
| 938 | // can't insert "change" in front to sign postion. |
| 939 | if (pEvent->SelStart() == 0) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 940 | bool& bRc = pEvent->Rc(); |
| 941 | bRc = false; |
| 942 | return true; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 943 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 944 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 945 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 946 | int iSepStyle = params[1].ToInt(pRuntime); |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 947 | if (iSepStyle < 0 || iSepStyle > 3) |
| 948 | iSepStyle = 0; |
| 949 | const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L','; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 950 | |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 951 | bool bHasSep = wstrValue.Find(cSep) != -1; |
| 952 | for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) { |
| 953 | if (wstrChange[i] == cSep) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 954 | if (bHasSep) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 955 | bool& bRc = pEvent->Rc(); |
| 956 | bRc = false; |
| 957 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 958 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 959 | bHasSep = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 960 | continue; |
| 961 | } |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 962 | if (wstrChange[i] == L'-') { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 963 | if (bHasSign) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 964 | bool& bRc = pEvent->Rc(); |
| 965 | bRc = false; |
| 966 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 967 | } |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 968 | // sign's position is not correct |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 969 | if (i != 0) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 970 | bool& bRc = pEvent->Rc(); |
| 971 | bRc = false; |
| 972 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 973 | } |
| 974 | if (pEvent->SelStart() != 0) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 975 | bool& bRc = pEvent->Rc(); |
| 976 | bRc = false; |
| 977 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 978 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 979 | bHasSign = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 980 | continue; |
| 981 | } |
| 982 | |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 983 | if (!FXSYS_iswdigit(wstrChange[i])) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 984 | bool& bRc = pEvent->Rc(); |
| 985 | bRc = false; |
| 986 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 990 | CFX_WideString wprefix = wstrValue.Mid(0, pEvent->SelStart()); |
| 991 | CFX_WideString wpostfix; |
| 992 | if (pEvent->SelEnd() < wstrValue.GetLength()) |
| 993 | wpostfix = wstrValue.Mid(pEvent->SelEnd()); |
| 994 | val = wprefix + wstrChange + wpostfix; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 995 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | // function AFPercent_Format(nDec, sepStyle) |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 999 | bool CJS_PublicMethods::AFPercent_Format(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1000 | const std::vector<CJS_Value>& params, |
| 1001 | CJS_Value& vRet, |
| 1002 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1003 | #if _FX_OS_ != _FX_ANDROID_ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1004 | if (params.size() != 2) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1005 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1006 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1007 | } |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1008 | |
| 1009 | CJS_EventHandler* pEvent = |
| 1010 | pRuntime->GetCurrentEventContext()->GetEventHandler(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1011 | if (!pEvent->m_pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1012 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1013 | |
| 1014 | CFX_WideString& Value = pEvent->Value(); |
| 1015 | CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value)); |
| 1016 | if (strValue.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1017 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1018 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1019 | int iDec = params[0].ToInt(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1020 | if (iDec < 0) |
| 1021 | iDec = -iDec; |
| 1022 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1023 | int iSepStyle = params[1].ToInt(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1024 | if (iSepStyle < 0 || iSepStyle > 3) |
| 1025 | iSepStyle = 0; |
| 1026 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1027 | // for processing decimal places |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 1028 | double dValue = atof(strValue.c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1029 | dValue *= 100; |
| 1030 | if (iDec > 0) |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 1031 | dValue += DOUBLE_CORRECT; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1032 | |
| 1033 | int iDec2; |
| 1034 | int iNegative = 0; |
| 1035 | strValue = fcvt(dValue, iDec, &iDec2, &iNegative); |
| 1036 | if (strValue.IsEmpty()) { |
| 1037 | dValue = 0; |
| 1038 | strValue = fcvt(dValue, iDec, &iDec2, &iNegative); |
| 1039 | } |
| 1040 | |
| 1041 | if (iDec2 < 0) { |
| 1042 | for (int iNum = 0; iNum < abs(iDec2); iNum++) { |
| 1043 | strValue = "0" + strValue; |
| 1044 | } |
| 1045 | iDec2 = 0; |
| 1046 | } |
| 1047 | int iMax = strValue.GetLength(); |
| 1048 | if (iDec2 > iMax) { |
| 1049 | for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) { |
| 1050 | strValue += "0"; |
| 1051 | } |
| 1052 | iMax = iDec2 + 1; |
| 1053 | } |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 1054 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1055 | // for processing seperator style |
| 1056 | if (iDec2 < iMax) { |
| 1057 | if (iSepStyle == 0 || iSepStyle == 1) { |
| 1058 | strValue.Insert(iDec2, '.'); |
| 1059 | iMax++; |
| 1060 | } else if (iSepStyle == 2 || iSepStyle == 3) { |
| 1061 | strValue.Insert(iDec2, ','); |
| 1062 | iMax++; |
| 1063 | } |
| 1064 | |
| 1065 | if (iDec2 == 0) |
| 1066 | strValue.Insert(iDec2, '0'); |
| 1067 | } |
| 1068 | if (iSepStyle == 0 || iSepStyle == 2) { |
| 1069 | char cSeperator; |
| 1070 | if (iSepStyle == 0) |
| 1071 | cSeperator = ','; |
| 1072 | else |
| 1073 | cSeperator = '.'; |
| 1074 | |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 1075 | for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1076 | strValue.Insert(iDecPositive, cSeperator); |
| 1077 | iMax++; |
| 1078 | } |
| 1079 | } |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 1080 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1081 | // negative mark |
| 1082 | if (iNegative) |
| 1083 | strValue = "-" + strValue; |
| 1084 | strValue += "%"; |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 1085 | Value = CFX_WideString::FromLocal(strValue.AsStringC()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1086 | #endif |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1087 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1088 | } |
| 1089 | // AFPercent_Keystroke(nDec, sepStyle) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1090 | bool CJS_PublicMethods::AFPercent_Keystroke( |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1091 | CJS_Runtime* pRuntime, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 1092 | const std::vector<CJS_Value>& params, |
| 1093 | CJS_Value& vRet, |
| 1094 | CFX_WideString& sError) { |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1095 | return AFNumber_Keystroke(pRuntime, params, vRet, sError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | // function AFDate_FormatEx(cFormat) |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1099 | bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1100 | const std::vector<CJS_Value>& params, |
| 1101 | CJS_Value& vRet, |
| 1102 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1103 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1104 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1105 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1106 | } |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1107 | |
| 1108 | CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); |
| 1109 | CJS_EventHandler* pEvent = pContext->GetEventHandler(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1110 | if (!pEvent->m_pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1111 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1112 | |
| 1113 | CFX_WideString& val = pEvent->Value(); |
| 1114 | CFX_WideString strValue = val; |
| 1115 | if (strValue.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1116 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1117 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1118 | CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1119 | double dDate = 0.0f; |
| 1120 | |
| 1121 | if (strValue.Find(L"GMT") != -1) { |
| 1122 | // for GMT format time |
| 1123 | // such as "Tue Aug 11 14:24:16 GMT+08002009" |
| 1124 | dDate = MakeInterDate(strValue); |
| 1125 | } else { |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 1126 | dDate = MakeRegularDate(strValue, sFormat, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | if (JS_PortIsNan(dDate)) { |
| 1130 | CFX_WideString swMsg; |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1131 | swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1132 | sFormat.c_str()); |
tsepez | e1e7bd0 | 2016-08-08 13:03:16 -0700 | [diff] [blame] | 1133 | AlertIfPossible(pContext, swMsg.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1134 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | val = MakeFormatDate(dDate, sFormat); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1138 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
tsepez | 745611b | 2016-04-12 16:46:34 -0700 | [diff] [blame] | 1141 | double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) { |
Tom Sepez | ab27768 | 2016-02-17 10:07:21 -0800 | [diff] [blame] | 1142 | std::vector<CFX_WideString> wsArray; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1143 | CFX_WideString sTemp = L""; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1144 | for (int i = 0; i < strValue.GetLength(); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1145 | FX_WCHAR c = strValue.GetAt(i); |
| 1146 | if (c == L' ' || c == L':') { |
Tom Sepez | ab27768 | 2016-02-17 10:07:21 -0800 | [diff] [blame] | 1147 | wsArray.push_back(sTemp); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1148 | sTemp = L""; |
| 1149 | continue; |
| 1150 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1151 | sTemp += c; |
| 1152 | } |
Tom Sepez | ab27768 | 2016-02-17 10:07:21 -0800 | [diff] [blame] | 1153 | wsArray.push_back(sTemp); |
| 1154 | if (wsArray.size() != 8) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1155 | return 0; |
| 1156 | |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1157 | int nMonth = 1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1158 | sTemp = wsArray[1]; |
| 1159 | if (sTemp.Compare(L"Jan") == 0) |
| 1160 | nMonth = 1; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1161 | else if (sTemp.Compare(L"Feb") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1162 | nMonth = 2; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1163 | else if (sTemp.Compare(L"Mar") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1164 | nMonth = 3; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1165 | else if (sTemp.Compare(L"Apr") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1166 | nMonth = 4; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1167 | else if (sTemp.Compare(L"May") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1168 | nMonth = 5; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1169 | else if (sTemp.Compare(L"Jun") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1170 | nMonth = 6; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1171 | else if (sTemp.Compare(L"Jul") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1172 | nMonth = 7; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1173 | else if (sTemp.Compare(L"Aug") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1174 | nMonth = 8; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1175 | else if (sTemp.Compare(L"Sep") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1176 | nMonth = 9; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1177 | else if (sTemp.Compare(L"Oct") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1178 | nMonth = 10; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1179 | else if (sTemp.Compare(L"Nov") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1180 | nMonth = 11; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1181 | else if (sTemp.Compare(L"Dec") == 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1182 | nMonth = 12; |
| 1183 | |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 1184 | int nDay = FX_atof(wsArray[2].AsStringC()); |
| 1185 | int nHour = FX_atof(wsArray[3].AsStringC()); |
| 1186 | int nMin = FX_atof(wsArray[4].AsStringC()); |
| 1187 | int nSec = FX_atof(wsArray[5].AsStringC()); |
| 1188 | int nYear = FX_atof(wsArray[7].AsStringC()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1189 | double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay), |
| 1190 | JS_MakeTime(nHour, nMin, nSec, 0)); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1191 | if (JS_PortIsNan(dRet)) |
tsepez | 018935c | 2016-04-15 13:15:12 -0700 | [diff] [blame] | 1192 | dRet = JS_DateParse(strValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1193 | |
| 1194 | return dRet; |
| 1195 | } |
| 1196 | |
| 1197 | // AFDate_KeystrokeEx(cFormat) |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1198 | bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1199 | const std::vector<CJS_Value>& params, |
| 1200 | CJS_Value& vRet, |
| 1201 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1202 | if (params.size() != 1) { |
| 1203 | sError = L"AFDate_KeystrokeEx's parameters' size r not correct"; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1204 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1207 | CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); |
| 1208 | CJS_EventHandler* pEvent = pContext->GetEventHandler(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1209 | if (pEvent->WillCommit()) { |
| 1210 | if (!pEvent->m_pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1211 | return false; |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1212 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1213 | CFX_WideString strValue = pEvent->Value(); |
| 1214 | if (strValue.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1215 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1216 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1217 | CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1218 | bool bWrongFormat = false; |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 1219 | double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1220 | if (bWrongFormat || JS_PortIsNan(dRet)) { |
| 1221 | CFX_WideString swMsg; |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1222 | swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1223 | sFormat.c_str()); |
tsepez | e1e7bd0 | 2016-08-08 13:03:16 -0700 | [diff] [blame] | 1224 | AlertIfPossible(pContext, swMsg.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1225 | pEvent->Rc() = false; |
| 1226 | return true; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1227 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1228 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1229 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1232 | bool CJS_PublicMethods::AFDate_Format(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1233 | const std::vector<CJS_Value>& params, |
| 1234 | CJS_Value& vRet, |
| 1235 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1236 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1237 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1238 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1239 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1240 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1241 | int iIndex = params[0].ToInt(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1242 | const FX_WCHAR* cFormats[] = {L"m/d", |
| 1243 | L"m/d/yy", |
| 1244 | L"mm/dd/yy", |
| 1245 | L"mm/yy", |
| 1246 | L"d-mmm", |
| 1247 | L"d-mmm-yy", |
| 1248 | L"dd-mmm-yy", |
| 1249 | L"yy-mm-dd", |
| 1250 | L"mmm-yy", |
| 1251 | L"mmmm-yy", |
| 1252 | L"mmm d, yyyy", |
| 1253 | L"mmmm d, yyyy", |
| 1254 | L"m/d/yy h:MM tt", |
| 1255 | L"m/d/yy HH:MM"}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1256 | |
Lei Zhang | a0f6724 | 2015-08-17 15:39:30 -0700 | [diff] [blame] | 1257 | if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats))) |
| 1258 | iIndex = 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1259 | |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 1260 | std::vector<CJS_Value> newParams; |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1261 | newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex])); |
| 1262 | return AFDate_FormatEx(pRuntime, newParams, vRet, sError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1263 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 1264 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1265 | // AFDate_KeystrokeEx(cFormat) |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1266 | bool CJS_PublicMethods::AFDate_Keystroke(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1267 | const std::vector<CJS_Value>& params, |
| 1268 | CJS_Value& vRet, |
| 1269 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1270 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1271 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1272 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1275 | int iIndex = params[0].ToInt(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1276 | const FX_WCHAR* cFormats[] = {L"m/d", |
| 1277 | L"m/d/yy", |
| 1278 | L"mm/dd/yy", |
| 1279 | L"mm/yy", |
| 1280 | L"d-mmm", |
| 1281 | L"d-mmm-yy", |
| 1282 | L"dd-mmm-yy", |
| 1283 | L"yy-mm-dd", |
| 1284 | L"mmm-yy", |
| 1285 | L"mmmm-yy", |
| 1286 | L"mmm d, yyyy", |
| 1287 | L"mmmm d, yyyy", |
| 1288 | L"m/d/yy h:MM tt", |
| 1289 | L"m/d/yy HH:MM"}; |
| 1290 | |
Lei Zhang | a0f6724 | 2015-08-17 15:39:30 -0700 | [diff] [blame] | 1291 | if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats))) |
| 1292 | iIndex = 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1293 | |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 1294 | std::vector<CJS_Value> newParams; |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1295 | newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex])); |
| 1296 | return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | // function AFTime_Format(ptf) |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1300 | bool CJS_PublicMethods::AFTime_Format(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1301 | const std::vector<CJS_Value>& params, |
| 1302 | CJS_Value& vRet, |
| 1303 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1304 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1305 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1306 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1309 | int iIndex = params[0].ToInt(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1310 | const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", |
| 1311 | L"h:MM:ss tt"}; |
| 1312 | |
Lei Zhang | a0f6724 | 2015-08-17 15:39:30 -0700 | [diff] [blame] | 1313 | if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats))) |
| 1314 | iIndex = 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1315 | |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 1316 | std::vector<CJS_Value> newParams; |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1317 | newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex])); |
| 1318 | return AFDate_FormatEx(pRuntime, newParams, vRet, sError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1321 | bool CJS_PublicMethods::AFTime_Keystroke(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1322 | const std::vector<CJS_Value>& params, |
| 1323 | CJS_Value& vRet, |
| 1324 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1325 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1326 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1327 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1330 | int iIndex = params[0].ToInt(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1331 | const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", |
| 1332 | L"h:MM:ss tt"}; |
| 1333 | |
Lei Zhang | a0f6724 | 2015-08-17 15:39:30 -0700 | [diff] [blame] | 1334 | if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats))) |
| 1335 | iIndex = 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1336 | |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 1337 | std::vector<CJS_Value> newParams; |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1338 | newParams.push_back(CJS_Value(pRuntime, cFormats[iIndex])); |
| 1339 | return AFDate_KeystrokeEx(pRuntime, newParams, vRet, sError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1342 | bool CJS_PublicMethods::AFTime_FormatEx(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1343 | const std::vector<CJS_Value>& params, |
| 1344 | CJS_Value& vRet, |
| 1345 | CFX_WideString& sError) { |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1346 | return AFDate_FormatEx(pRuntime, params, vRet, sError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1349 | bool CJS_PublicMethods::AFTime_KeystrokeEx(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1350 | const std::vector<CJS_Value>& params, |
| 1351 | CJS_Value& vRet, |
| 1352 | CFX_WideString& sError) { |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1353 | return AFDate_KeystrokeEx(pRuntime, params, vRet, sError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | // function AFSpecial_Format(psf) |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1357 | bool CJS_PublicMethods::AFSpecial_Format(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1358 | const std::vector<CJS_Value>& params, |
| 1359 | CJS_Value& vRet, |
| 1360 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1361 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1362 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1363 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1366 | CJS_EventHandler* pEvent = |
| 1367 | pRuntime->GetCurrentEventContext()->GetEventHandler(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1368 | if (!pEvent->m_pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1369 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1370 | |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 1371 | CFX_WideString wsSource = pEvent->Value(); |
| 1372 | CFX_WideString wsFormat; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1373 | switch (params[0].ToInt(pRuntime)) { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1374 | case 0: |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 1375 | wsFormat = L"99999"; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1376 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1377 | case 1: |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 1378 | wsFormat = L"99999-9999"; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1379 | break; |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 1380 | case 2: |
| 1381 | if (util::printx(L"9999999999", wsSource).GetLength() >= 10) |
| 1382 | wsFormat = L"(999) 999-9999"; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1383 | else |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 1384 | wsFormat = L"999-9999"; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1385 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1386 | case 3: |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 1387 | wsFormat = L"999-99-9999"; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1388 | break; |
| 1389 | } |
| 1390 | |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 1391 | pEvent->Value() = util::printx(wsFormat, wsSource); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1392 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | // function AFSpecial_KeystrokeEx(mask) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1396 | bool CJS_PublicMethods::AFSpecial_KeystrokeEx( |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1397 | CJS_Runtime* pRuntime, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 1398 | const std::vector<CJS_Value>& params, |
| 1399 | CJS_Value& vRet, |
| 1400 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1401 | if (params.size() < 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1402 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1403 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1406 | CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); |
| 1407 | CJS_EventHandler* pEvent = pContext->GetEventHandler(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1408 | if (!pEvent->m_pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1409 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1410 | |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1411 | CFX_WideString& valEvent = pEvent->Value(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1412 | CFX_WideString wstrMask = params[0].ToCFXWideString(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1413 | if (wstrMask.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1414 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1415 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1416 | if (pEvent->WillCommit()) { |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1417 | if (valEvent.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1418 | return true; |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1419 | |
| 1420 | FX_STRSIZE iIndexMask = 0; |
| 1421 | for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) { |
| 1422 | if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask])) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1423 | break; |
| 1424 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 1425 | |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1426 | if (iIndexMask != wstrMask.GetLength() || |
| 1427 | (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) { |
tsepez | e1e7bd0 | 2016-08-08 13:03:16 -0700 | [diff] [blame] | 1428 | AlertIfPossible( |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1429 | pContext, JSGetStringFromID(IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1430 | pEvent->Rc() = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1431 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1432 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | CFX_WideString& wideChange = pEvent->Change(); |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1436 | if (wideChange.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1437 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1438 | |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1439 | CFX_WideString wChange = wideChange; |
| 1440 | FX_STRSIZE iIndexMask = pEvent->SelStart(); |
| 1441 | FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() + |
| 1442 | pEvent->SelStart() - pEvent->SelEnd(); |
| 1443 | if (combined_len > wstrMask.GetLength()) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1444 | AlertIfPossible(pContext, |
| 1445 | JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1446 | pEvent->Rc() = false; |
| 1447 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1450 | if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1451 | AlertIfPossible(pContext, |
| 1452 | JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1453 | pEvent->Rc() = false; |
| 1454 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1457 | for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) { |
| 1458 | if (iIndexMask >= wstrMask.GetLength()) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1459 | AlertIfPossible(pContext, |
| 1460 | JSGetStringFromID(IDS_STRING_JSPARAM_TOOLONG).c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1461 | pEvent->Rc() = false; |
| 1462 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1463 | } |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1464 | FX_WCHAR wMask = wstrMask[iIndexMask]; |
| 1465 | if (!isReservedMaskChar(wMask)) |
| 1466 | wChange.SetAt(i, wMask); |
| 1467 | |
| 1468 | if (!maskSatisfied(wChange[i], wMask)) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1469 | pEvent->Rc() = false; |
| 1470 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1471 | } |
| 1472 | iIndexMask++; |
| 1473 | } |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1474 | wideChange = wChange; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1475 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1478 | // function AFSpecial_Keystroke(psf) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1479 | bool CJS_PublicMethods::AFSpecial_Keystroke( |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1480 | CJS_Runtime* pRuntime, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 1481 | const std::vector<CJS_Value>& params, |
| 1482 | CJS_Value& vRet, |
| 1483 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1484 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1485 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1486 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1487 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1488 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1489 | CJS_EventHandler* pEvent = |
| 1490 | pRuntime->GetCurrentEventContext()->GetEventHandler(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1491 | if (!pEvent->m_pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1492 | return false; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 1493 | |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1494 | const char* cFormat = ""; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1495 | switch (params[0].ToInt(pRuntime)) { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1496 | case 0: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1497 | cFormat = "99999"; |
| 1498 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1499 | case 1: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1500 | cFormat = "999999999"; |
| 1501 | break; |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 1502 | case 2: |
thestig | cf03f8e | 2016-05-09 12:36:18 -0700 | [diff] [blame] | 1503 | if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1504 | cFormat = "9999999999"; |
| 1505 | else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1506 | cFormat = "9999999"; |
| 1507 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1508 | case 3: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1509 | cFormat = "999999999"; |
| 1510 | break; |
| 1511 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 1512 | |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 1513 | std::vector<CJS_Value> params2; |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1514 | params2.push_back(CJS_Value(pRuntime, cFormat)); |
| 1515 | return AFSpecial_KeystrokeEx(pRuntime, params2, vRet, sError); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1518 | bool CJS_PublicMethods::AFMergeChange(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1519 | const std::vector<CJS_Value>& params, |
| 1520 | CJS_Value& vRet, |
| 1521 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1522 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1523 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1524 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1525 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1526 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1527 | CJS_EventHandler* pEventHandler = |
| 1528 | pRuntime->GetCurrentEventContext()->GetEventHandler(); |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1529 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1530 | CFX_WideString swValue; |
Lei Zhang | 997de61 | 2015-11-04 18:17:53 -0800 | [diff] [blame] | 1531 | if (pEventHandler->m_pValue) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1532 | swValue = pEventHandler->Value(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1533 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1534 | if (pEventHandler->WillCommit()) { |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 1535 | vRet = CJS_Value(pRuntime, swValue.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1536 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | CFX_WideString prefix, postfix; |
| 1540 | |
| 1541 | if (pEventHandler->SelStart() >= 0) |
| 1542 | prefix = swValue.Mid(0, pEventHandler->SelStart()); |
| 1543 | else |
| 1544 | prefix = L""; |
| 1545 | |
| 1546 | if (pEventHandler->SelEnd() >= 0 && |
| 1547 | pEventHandler->SelEnd() <= swValue.GetLength()) |
| 1548 | postfix = swValue.Mid(pEventHandler->SelEnd(), |
| 1549 | swValue.GetLength() - pEventHandler->SelEnd()); |
| 1550 | else |
| 1551 | postfix = L""; |
| 1552 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 1553 | vRet = |
| 1554 | CJS_Value(pRuntime, (prefix + pEventHandler->Change() + postfix).c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1555 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1558 | bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1559 | const std::vector<CJS_Value>& params, |
| 1560 | CJS_Value& vRet, |
| 1561 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1562 | if (params.size() != 2) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1563 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1564 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1567 | CFX_WideString sValue = params[0].ToCFXWideString(pRuntime); |
| 1568 | CFX_WideString sFormat = params[1].ToCFXWideString(pRuntime); |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 1569 | double dDate = MakeRegularDate(sValue, sFormat, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1570 | if (JS_PortIsNan(dDate)) { |
| 1571 | CFX_WideString swMsg; |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1572 | swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1573 | sFormat.c_str()); |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1574 | AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1575 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 1578 | vRet = CJS_Value(pRuntime, dDate); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1579 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1582 | bool CJS_PublicMethods::AFSimple(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1583 | const std::vector<CJS_Value>& params, |
| 1584 | CJS_Value& vRet, |
| 1585 | CFX_WideString& sError) { |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 1586 | if (params.size() != 3) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1587 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1588 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1589 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1590 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1591 | vRet = CJS_Value(pRuntime, static_cast<double>(AF_Simple( |
| 1592 | params[0].ToCFXWideString(pRuntime).c_str(), |
| 1593 | params[1].ToDouble(pRuntime), |
| 1594 | params[2].ToDouble(pRuntime)))); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 1595 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1596 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1599 | bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1600 | const std::vector<CJS_Value>& params, |
| 1601 | CJS_Value& vRet, |
| 1602 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1603 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1604 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1605 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1606 | } |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 1607 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1608 | CFX_WideString ws = params[0].ToCFXWideString(pRuntime); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1609 | ws.Replace(L",", L"."); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 1610 | vRet = CJS_Value(pRuntime, ws.c_str()); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1611 | vRet.MaybeCoerceToNumber(pRuntime); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1612 | if (vRet.GetType() != CJS_Value::VT_number) |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 1613 | vRet = CJS_Value(pRuntime, 0); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1614 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1615 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1616 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1617 | bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1618 | const std::vector<CJS_Value>& params, |
| 1619 | CJS_Value& vRet, |
| 1620 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1621 | if (params.size() != 2) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1622 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1623 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1624 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1625 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1626 | CJS_Value params1 = params[1]; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 1627 | if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1628 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1629 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1630 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1631 | |
dsinclair | 4526faf | 2016-10-11 10:54:49 -0700 | [diff] [blame] | 1632 | CPDFSDK_InterForm* pReaderInterForm = |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1633 | pRuntime->GetFormFillEnv()->GetInterForm(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1634 | CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1635 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1636 | CFX_WideString sFunction = params[0].ToCFXWideString(pRuntime); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 1637 | double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1638 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 1639 | CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1640 | int nFieldsCount = 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1641 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1642 | for (int i = 0, isz = FieldNameArray.GetLength(pRuntime); i < isz; i++) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 1643 | CJS_Value jsValue(pRuntime); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1644 | FieldNameArray.GetElement(pRuntime, i, jsValue); |
| 1645 | CFX_WideString wsFieldName = jsValue.ToCFXWideString(pRuntime); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1646 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1647 | for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) { |
| 1648 | if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) { |
| 1649 | double dTemp = 0.0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1650 | switch (pFormField->GetFieldType()) { |
| 1651 | case FIELDTYPE_TEXTFIELD: |
| 1652 | case FIELDTYPE_COMBOBOX: { |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1653 | CFX_WideString trimmed = pFormField->GetValue(); |
| 1654 | trimmed.TrimRight(); |
| 1655 | trimmed.TrimLeft(); |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 1656 | dTemp = FX_atof(trimmed.AsStringC()); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1657 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1658 | case FIELDTYPE_PUSHBUTTON: { |
| 1659 | dTemp = 0.0; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1660 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1661 | case FIELDTYPE_CHECKBOX: |
| 1662 | case FIELDTYPE_RADIOBUTTON: { |
| 1663 | dTemp = 0.0; |
| 1664 | for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) { |
| 1665 | if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) { |
| 1666 | if (pFormCtrl->IsChecked()) { |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1667 | CFX_WideString trimmed = pFormCtrl->GetExportValue(); |
| 1668 | trimmed.TrimRight(); |
| 1669 | trimmed.TrimLeft(); |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 1670 | dTemp = FX_atof(trimmed.AsStringC()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1671 | break; |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 1672 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1673 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1674 | } |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1675 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1676 | case FIELDTYPE_LISTBOX: { |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1677 | if (pFormField->CountSelectedItems() <= 1) { |
| 1678 | CFX_WideString trimmed = pFormField->GetValue(); |
| 1679 | trimmed.TrimRight(); |
| 1680 | trimmed.TrimLeft(); |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 1681 | dTemp = FX_atof(trimmed.AsStringC()); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 1682 | } |
| 1683 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1684 | default: |
| 1685 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1686 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1687 | |
| 1688 | if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 || |
| 1689 | wcscmp(sFunction.c_str(), L"MAX") == 0)) |
| 1690 | dValue = dTemp; |
| 1691 | |
| 1692 | dValue = AF_Simple(sFunction.c_str(), dValue, dTemp); |
| 1693 | |
| 1694 | nFieldsCount++; |
| 1695 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1696 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1697 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1698 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1699 | if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0) |
| 1700 | dValue /= nFieldsCount; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1701 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1702 | dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) / |
| 1703 | FXSYS_pow((double)10, (double)6); |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1704 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 1705 | CJS_Value jsValue(pRuntime, dValue); |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1706 | CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); |
foxit | 8b544ed | 2015-09-10 14:57:54 +0800 | [diff] [blame] | 1707 | if (pContext->GetEventHandler()->m_pValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1708 | pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString(pRuntime); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1709 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1710 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 1713 | /* This function validates the current event to ensure that its value is |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1714 | ** within the specified range. */ |
| 1715 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1716 | bool CJS_PublicMethods::AFRange_Validate(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1717 | const std::vector<CJS_Value>& params, |
| 1718 | CJS_Value& vRet, |
| 1719 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1720 | if (params.size() != 4) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1721 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1722 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1723 | } |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1724 | CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1725 | CJS_EventHandler* pEvent = pContext->GetEventHandler(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1726 | if (!pEvent->m_pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1727 | return false; |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1728 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1729 | if (pEvent->Value().IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1730 | return true; |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1731 | |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 1732 | double dEentValue = |
| 1733 | atof(CFX_ByteString::FromUnicode(pEvent->Value()).c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1734 | bool bGreaterThan = params[0].ToBool(pRuntime); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1735 | double dGreaterThan = params[1].ToDouble(pRuntime); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1736 | bool bLessThan = params[2].ToBool(pRuntime); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1737 | double dLessThan = params[3].ToDouble(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1738 | CFX_WideString swMsg; |
| 1739 | |
| 1740 | if (bGreaterThan && bLessThan) { |
| 1741 | if (dEentValue < dGreaterThan || dEentValue > dLessThan) |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1742 | swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE1).c_str(), |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1743 | params[1].ToCFXWideString(pRuntime).c_str(), |
| 1744 | params[3].ToCFXWideString(pRuntime).c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1745 | } else if (bGreaterThan) { |
| 1746 | if (dEentValue < dGreaterThan) |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1747 | swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE2).c_str(), |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1748 | params[1].ToCFXWideString(pRuntime).c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1749 | } else if (bLessThan) { |
| 1750 | if (dEentValue > dLessThan) |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1751 | swMsg.Format(JSGetStringFromID(IDS_STRING_JSRANGE3).c_str(), |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1752 | params[3].ToCFXWideString(pRuntime).c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | if (!swMsg.IsEmpty()) { |
tsepez | e1e7bd0 | 2016-08-08 13:03:16 -0700 | [diff] [blame] | 1756 | AlertIfPossible(pContext, swMsg.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1757 | pEvent->Rc() = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1758 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1759 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1762 | bool CJS_PublicMethods::AFExtractNums(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1763 | const std::vector<CJS_Value>& params, |
| 1764 | CJS_Value& vRet, |
| 1765 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1766 | if (params.size() != 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 1767 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1768 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1771 | CFX_WideString str = params[0].ToCFXWideString(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1772 | if (str.GetAt(0) == L'.' || str.GetAt(0) == L',') |
| 1773 | str = L"0" + str; |
| 1774 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame^] | 1775 | CFX_WideString sPart; |
| 1776 | CJS_Array nums; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1777 | int nIndex = 0; |
| 1778 | for (int i = 0, sz = str.GetLength(); i < sz; i++) { |
| 1779 | FX_WCHAR wc = str.GetAt(i); |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 1780 | if (FXSYS_iswdigit(wc)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1781 | sPart += wc; |
| 1782 | } else { |
| 1783 | if (sPart.GetLength() > 0) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1784 | nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1785 | sPart = L""; |
| 1786 | nIndex++; |
| 1787 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 1788 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1789 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1790 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1791 | if (sPart.GetLength() > 0) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1792 | nums.SetElement(pRuntime, nIndex, CJS_Value(pRuntime, sPart.c_str())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1793 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1794 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 1795 | if (nums.GetLength(pRuntime) > 0) |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 1796 | vRet = CJS_Value(pRuntime, nums); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1797 | else |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 1798 | vRet.SetNull(pRuntime); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1799 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1800 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1801 | } |