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