| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "fpdfsdk/javascript/PublicMethods.h" | 7 #include "fpdfsdk/javascript/PublicMethods.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 return result; | 70 return result; |
| 71 } | 71 } |
| 72 | 72 |
| 73 CFX_WideString StrTrim(const CFX_WideString& pStr) { | 73 CFX_WideString StrTrim(const CFX_WideString& pStr) { |
| 74 CFX_WideString result(pStr); | 74 CFX_WideString result(pStr); |
| 75 result.TrimLeft(' '); | 75 result.TrimLeft(' '); |
| 76 result.TrimRight(' '); | 76 result.TrimRight(' '); |
| 77 return result; | 77 return result; |
| 78 } | 78 } |
| 79 | 79 |
| 80 void AlertIfPossible(CJS_Context* pContext, const FX_WCHAR* swMsg) { |
| 81 CPDFDoc_Environment* pApp = pContext->GetReaderApp(); |
| 82 if (pApp) |
| 83 pApp->JS_appAlert(swMsg, nullptr, 0, 3); |
| 84 } |
| 85 |
| 80 } // namespace | 86 } // namespace |
| 81 | 87 |
| 82 bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) { | 88 bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) { |
| 83 CFX_WideString sTrim = StrTrim(str); | 89 CFX_WideString sTrim = StrTrim(str); |
| 84 const FX_WCHAR* pTrim = sTrim.c_str(); | 90 const FX_WCHAR* pTrim = sTrim.c_str(); |
| 85 const FX_WCHAR* p = pTrim; | 91 const FX_WCHAR* p = pTrim; |
| 86 bool bDot = false; | 92 bool bDot = false; |
| 87 bool bKXJS = false; | 93 bool bKXJS = false; |
| 88 | 94 |
| 89 wchar_t c; | 95 wchar_t c; |
| (...skipping 816 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 906 | 912 |
| 907 if (pEvent->WillCommit()) { | 913 if (pEvent->WillCommit()) { |
| 908 CFX_WideString swTemp = StrTrim(wstrValue); | 914 CFX_WideString swTemp = StrTrim(wstrValue); |
| 909 if (swTemp.IsEmpty()) | 915 if (swTemp.IsEmpty()) |
| 910 return TRUE; | 916 return TRUE; |
| 911 | 917 |
| 912 swTemp.Replace(L",", L"."); | 918 swTemp.Replace(L",", L"."); |
| 913 if (!IsNumber(swTemp.c_str())) { | 919 if (!IsNumber(swTemp.c_str())) { |
| 914 pEvent->Rc() = FALSE; | 920 pEvent->Rc() = FALSE; |
| 915 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); | 921 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); |
| 916 Alert(pContext, sError.c_str()); | 922 AlertIfPossible(pContext, sError.c_str()); |
| 917 } | 923 } |
| 918 return TRUE; // it happens after the last keystroke and before validating, | 924 return TRUE; // it happens after the last keystroke and before validating, |
| 919 } | 925 } |
| 920 | 926 |
| 921 CFX_WideString wstrSelected; | 927 CFX_WideString wstrSelected; |
| 922 if (pEvent->SelStart() != -1) { | 928 if (pEvent->SelStart() != -1) { |
| 923 wstrSelected = wstrValue.Mid(pEvent->SelStart(), | 929 wstrSelected = wstrValue.Mid(pEvent->SelStart(), |
| 924 pEvent->SelEnd() - pEvent->SelStart()); | 930 pEvent->SelEnd() - pEvent->SelStart()); |
| 925 } | 931 } |
| 926 | 932 |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1115 // such as "Tue Aug 11 14:24:16 GMT+08002009" | 1121 // such as "Tue Aug 11 14:24:16 GMT+08002009" |
| 1116 dDate = MakeInterDate(strValue); | 1122 dDate = MakeInterDate(strValue); |
| 1117 } else { | 1123 } else { |
| 1118 dDate = MakeRegularDate(strValue, sFormat, nullptr); | 1124 dDate = MakeRegularDate(strValue, sFormat, nullptr); |
| 1119 } | 1125 } |
| 1120 | 1126 |
| 1121 if (JS_PortIsNan(dDate)) { | 1127 if (JS_PortIsNan(dDate)) { |
| 1122 CFX_WideString swMsg; | 1128 CFX_WideString swMsg; |
| 1123 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(), | 1129 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(), |
| 1124 sFormat.c_str()); | 1130 sFormat.c_str()); |
| 1125 Alert(pContext, swMsg.c_str()); | 1131 AlertIfPossible(pContext, swMsg.c_str()); |
| 1126 return FALSE; | 1132 return FALSE; |
| 1127 } | 1133 } |
| 1128 | 1134 |
| 1129 val = MakeFormatDate(dDate, sFormat); | 1135 val = MakeFormatDate(dDate, sFormat); |
| 1130 return TRUE; | 1136 return TRUE; |
| 1131 } | 1137 } |
| 1132 | 1138 |
| 1133 double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) { | 1139 double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) { |
| 1134 std::vector<CFX_WideString> wsArray; | 1140 std::vector<CFX_WideString> wsArray; |
| 1135 CFX_WideString sTemp = L""; | 1141 CFX_WideString sTemp = L""; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1207 if (strValue.IsEmpty()) | 1213 if (strValue.IsEmpty()) |
| 1208 return TRUE; | 1214 return TRUE; |
| 1209 | 1215 |
| 1210 CFX_WideString sFormat = params[0].ToCFXWideString(); | 1216 CFX_WideString sFormat = params[0].ToCFXWideString(); |
| 1211 bool bWrongFormat = FALSE; | 1217 bool bWrongFormat = FALSE; |
| 1212 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat); | 1218 double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat); |
| 1213 if (bWrongFormat || JS_PortIsNan(dRet)) { | 1219 if (bWrongFormat || JS_PortIsNan(dRet)) { |
| 1214 CFX_WideString swMsg; | 1220 CFX_WideString swMsg; |
| 1215 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(), | 1221 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(), |
| 1216 sFormat.c_str()); | 1222 sFormat.c_str()); |
| 1217 Alert(pContext, swMsg.c_str()); | 1223 AlertIfPossible(pContext, swMsg.c_str()); |
| 1218 pEvent->Rc() = FALSE; | 1224 pEvent->Rc() = FALSE; |
| 1219 return TRUE; | 1225 return TRUE; |
| 1220 } | 1226 } |
| 1221 } | 1227 } |
| 1222 return TRUE; | 1228 return TRUE; |
| 1223 } | 1229 } |
| 1224 | 1230 |
| 1225 FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc, | 1231 FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc, |
| 1226 const std::vector<CJS_Value>& params, | 1232 const std::vector<CJS_Value>& params, |
| 1227 CJS_Value& vRet, | 1233 CJS_Value& vRet, |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1425 return TRUE; | 1431 return TRUE; |
| 1426 | 1432 |
| 1427 FX_STRSIZE iIndexMask = 0; | 1433 FX_STRSIZE iIndexMask = 0; |
| 1428 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) { | 1434 for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) { |
| 1429 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask])) | 1435 if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask])) |
| 1430 break; | 1436 break; |
| 1431 } | 1437 } |
| 1432 | 1438 |
| 1433 if (iIndexMask != wstrMask.GetLength() || | 1439 if (iIndexMask != wstrMask.GetLength() || |
| 1434 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) { | 1440 (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) { |
| 1435 Alert( | 1441 AlertIfPossible( |
| 1436 pContext, | 1442 pContext, |
| 1437 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str()); | 1443 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str()); |
| 1438 pEvent->Rc() = FALSE; | 1444 pEvent->Rc() = FALSE; |
| 1439 } | 1445 } |
| 1440 return TRUE; | 1446 return TRUE; |
| 1441 } | 1447 } |
| 1442 | 1448 |
| 1443 CFX_WideString& wideChange = pEvent->Change(); | 1449 CFX_WideString& wideChange = pEvent->Change(); |
| 1444 if (wideChange.IsEmpty()) | 1450 if (wideChange.IsEmpty()) |
| 1445 return TRUE; | 1451 return TRUE; |
| 1446 | 1452 |
| 1447 CFX_WideString wChange = wideChange; | 1453 CFX_WideString wChange = wideChange; |
| 1448 FX_STRSIZE iIndexMask = pEvent->SelStart(); | 1454 FX_STRSIZE iIndexMask = pEvent->SelStart(); |
| 1449 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() + | 1455 FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() + |
| 1450 pEvent->SelStart() - pEvent->SelEnd(); | 1456 pEvent->SelStart() - pEvent->SelEnd(); |
| 1451 if (combined_len > wstrMask.GetLength()) { | 1457 if (combined_len > wstrMask.GetLength()) { |
| 1452 Alert(pContext, | 1458 AlertIfPossible( |
| 1453 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); | 1459 pContext, |
| 1460 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); |
| 1454 pEvent->Rc() = FALSE; | 1461 pEvent->Rc() = FALSE; |
| 1455 return TRUE; | 1462 return TRUE; |
| 1456 } | 1463 } |
| 1457 | 1464 |
| 1458 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) { | 1465 if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) { |
| 1459 Alert(pContext, | 1466 AlertIfPossible( |
| 1460 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); | 1467 pContext, |
| 1468 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); |
| 1461 pEvent->Rc() = FALSE; | 1469 pEvent->Rc() = FALSE; |
| 1462 return TRUE; | 1470 return TRUE; |
| 1463 } | 1471 } |
| 1464 | 1472 |
| 1465 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) { | 1473 for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) { |
| 1466 if (iIndexMask >= wstrMask.GetLength()) { | 1474 if (iIndexMask >= wstrMask.GetLength()) { |
| 1467 Alert(pContext, | 1475 AlertIfPossible( |
| 1468 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); | 1476 pContext, |
| 1477 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); |
| 1469 pEvent->Rc() = FALSE; | 1478 pEvent->Rc() = FALSE; |
| 1470 return TRUE; | 1479 return TRUE; |
| 1471 } | 1480 } |
| 1472 FX_WCHAR wMask = wstrMask[iIndexMask]; | 1481 FX_WCHAR wMask = wstrMask[iIndexMask]; |
| 1473 if (!isReservedMaskChar(wMask)) | 1482 if (!isReservedMaskChar(wMask)) |
| 1474 wChange.SetAt(i, wMask); | 1483 wChange.SetAt(i, wMask); |
| 1475 | 1484 |
| 1476 if (!maskSatisfied(wChange[i], wMask)) { | 1485 if (!maskSatisfied(wChange[i], wMask)) { |
| 1477 pEvent->Rc() = FALSE; | 1486 pEvent->Rc() = FALSE; |
| 1478 return TRUE; | 1487 return TRUE; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1577 | 1586 |
| 1578 CFX_WideString sValue = params[0].ToCFXWideString(); | 1587 CFX_WideString sValue = params[0].ToCFXWideString(); |
| 1579 CFX_WideString sFormat = params[1].ToCFXWideString(); | 1588 CFX_WideString sFormat = params[1].ToCFXWideString(); |
| 1580 | 1589 |
| 1581 double dDate = MakeRegularDate(sValue, sFormat, nullptr); | 1590 double dDate = MakeRegularDate(sValue, sFormat, nullptr); |
| 1582 | 1591 |
| 1583 if (JS_PortIsNan(dDate)) { | 1592 if (JS_PortIsNan(dDate)) { |
| 1584 CFX_WideString swMsg; | 1593 CFX_WideString swMsg; |
| 1585 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(), | 1594 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(), |
| 1586 sFormat.c_str()); | 1595 sFormat.c_str()); |
| 1587 Alert((CJS_Context*)cc, swMsg.c_str()); | 1596 AlertIfPossible((CJS_Context*)cc, swMsg.c_str()); |
| 1588 return FALSE; | 1597 return FALSE; |
| 1589 } | 1598 } |
| 1590 | 1599 |
| 1591 vRet = dDate; | 1600 vRet = dDate; |
| 1592 return TRUE; | 1601 return TRUE; |
| 1593 } | 1602 } |
| 1594 | 1603 |
| 1595 FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc, | 1604 FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc, |
| 1596 const std::vector<CJS_Value>& params, | 1605 const std::vector<CJS_Value>& params, |
| 1597 CJS_Value& vRet, | 1606 CJS_Value& vRet, |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1761 if (dEentValue < dGreaterThan) | 1770 if (dEentValue < dGreaterThan) |
| 1762 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(), | 1771 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(), |
| 1763 params[1].ToCFXWideString().c_str()); | 1772 params[1].ToCFXWideString().c_str()); |
| 1764 } else if (bLessThan) { | 1773 } else if (bLessThan) { |
| 1765 if (dEentValue > dLessThan) | 1774 if (dEentValue > dLessThan) |
| 1766 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(), | 1775 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(), |
| 1767 params[3].ToCFXWideString().c_str()); | 1776 params[3].ToCFXWideString().c_str()); |
| 1768 } | 1777 } |
| 1769 | 1778 |
| 1770 if (!swMsg.IsEmpty()) { | 1779 if (!swMsg.IsEmpty()) { |
| 1771 Alert(pContext, swMsg.c_str()); | 1780 AlertIfPossible(pContext, swMsg.c_str()); |
| 1772 pEvent->Rc() = FALSE; | 1781 pEvent->Rc() = FALSE; |
| 1773 } | 1782 } |
| 1774 return TRUE; | 1783 return TRUE; |
| 1775 } | 1784 } |
| 1776 | 1785 |
| 1777 FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc, | 1786 FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc, |
| 1778 const std::vector<CJS_Value>& params, | 1787 const std::vector<CJS_Value>& params, |
| 1779 CJS_Value& vRet, | 1788 CJS_Value& vRet, |
| 1780 CFX_WideString& sError) { | 1789 CFX_WideString& sError) { |
| 1781 CJS_Context* pContext = (CJS_Context*)cc; | 1790 CJS_Context* pContext = (CJS_Context*)cc; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1813 CJS_Value(pRuntime, sPart.c_str())); | 1822 CJS_Value(pRuntime, sPart.c_str())); |
| 1814 } | 1823 } |
| 1815 | 1824 |
| 1816 if (nums.GetLength() > 0) | 1825 if (nums.GetLength() > 0) |
| 1817 vRet = CJS_Value(pRuntime, nums); | 1826 vRet = CJS_Value(pRuntime, nums); |
| 1818 else | 1827 else |
| 1819 vRet.SetNull(); | 1828 vRet.SetNull(); |
| 1820 | 1829 |
| 1821 return TRUE; | 1830 return TRUE; |
| 1822 } | 1831 } |
| OLD | NEW |