uri.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // Ranges of valid hexadecimal characters
  6. var v0 = 48, v9 = 57, vA = 65, vZ = 90, va = 97, vz = 122;
  7. var nonHexWideCharValues =
  8. [
  9. // These characters are just outside the boundaries for hexadecimal characters
  10. v0 - 1, v9 + 1,
  11. vA - 1, vZ + 1,
  12. va - 1, vz + 1,
  13. // Some of these non-ASCII characters may map to a hexadecimal digit. However, we still should not accept them since they
  14. // are not hexadecimal characters in the ASCII set.
  15. 65296, 65297, 65298, 65299, 65300, 65301, 65302, 65303,
  16. 65304, 65305, 65313, 65314, 65315, 65316, 65317, 65318,
  17. 65345, 65346, 65347, 65348, 65349, 65350
  18. ];
  19. writeLine("First character invalid in %-encoded URI component:");
  20. for (var i in nonHexWideCharValues)
  21. {
  22. var testResult = " " + nonHexWideCharValues[i] + ": ";
  23. try
  24. {
  25. decodeURIComponent("%" + String.fromCharCode(nonHexWideCharValues[i]) + "0");
  26. testResult += "Fail (no exception)";
  27. }
  28. catch (ex)
  29. {
  30. testResult += "Pass (" + ex.name + ": " + ex.message + ")";
  31. }
  32. writeLine(testResult);
  33. }
  34. writeLine("");
  35. writeLine("Second character invalid in %-encoded URI component:");
  36. for (var i in nonHexWideCharValues)
  37. {
  38. var testResult = " " + nonHexWideCharValues[i] + ": ";
  39. try
  40. {
  41. decodeURIComponent("%0" + String.fromCharCode(nonHexWideCharValues[i]));
  42. testResult += "Fail (no exception)";
  43. }
  44. catch (ex)
  45. {
  46. testResult += "Pass (" + ex.name + ": " + ex.message + ")";
  47. }
  48. writeLine(testResult);
  49. }
  50. writeLine("");
  51. // Helpers
  52. function writeLine(str)
  53. {
  54. WScript.Echo("" + str);
  55. }