Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

percent-encoding.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use strict";
  2. const { isASCIIHex } = require("./infra");
  3. const { utf8Encode } = require("./encoding");
  4. // https://url.spec.whatwg.org/#percent-encode
  5. function percentEncode(c) {
  6. let hex = c.toString(16).toUpperCase();
  7. if (hex.length === 1) {
  8. hex = `0${hex}`;
  9. }
  10. return `%${hex}`;
  11. }
  12. // https://url.spec.whatwg.org/#percent-decode
  13. function percentDecodeBytes(input) {
  14. const output = new Uint8Array(input.byteLength);
  15. let outputIndex = 0;
  16. for (let i = 0; i < input.byteLength; ++i) {
  17. const byte = input[i];
  18. if (byte !== 0x25) {
  19. output[outputIndex++] = byte;
  20. } else if (byte === 0x25 && (!isASCIIHex(input[i + 1]) || !isASCIIHex(input[i + 2]))) {
  21. output[outputIndex++] = byte;
  22. } else {
  23. const bytePoint = parseInt(String.fromCodePoint(input[i + 1], input[i + 2]), 16);
  24. output[outputIndex++] = bytePoint;
  25. i += 2;
  26. }
  27. }
  28. return output.slice(0, outputIndex);
  29. }
  30. // https://url.spec.whatwg.org/#string-percent-decode
  31. function percentDecodeString(input) {
  32. const bytes = utf8Encode(input);
  33. return percentDecodeBytes(bytes);
  34. }
  35. // https://url.spec.whatwg.org/#c0-control-percent-encode-set
  36. function isC0ControlPercentEncode(c) {
  37. return c <= 0x1F || c > 0x7E;
  38. }
  39. // https://url.spec.whatwg.org/#fragment-percent-encode-set
  40. const extraFragmentPercentEncodeSet = new Set([32, 34, 60, 62, 96]);
  41. function isFragmentPercentEncode(c) {
  42. return isC0ControlPercentEncode(c) || extraFragmentPercentEncodeSet.has(c);
  43. }
  44. // https://url.spec.whatwg.org/#query-percent-encode-set
  45. const extraQueryPercentEncodeSet = new Set([32, 34, 35, 60, 62]);
  46. function isQueryPercentEncode(c) {
  47. return isC0ControlPercentEncode(c) || extraQueryPercentEncodeSet.has(c);
  48. }
  49. // https://url.spec.whatwg.org/#special-query-percent-encode-set
  50. function isSpecialQueryPercentEncode(c) {
  51. return isQueryPercentEncode(c) || c === 39;
  52. }
  53. // https://url.spec.whatwg.org/#path-percent-encode-set
  54. const extraPathPercentEncodeSet = new Set([63, 96, 123, 125]);
  55. function isPathPercentEncode(c) {
  56. return isQueryPercentEncode(c) || extraPathPercentEncodeSet.has(c);
  57. }
  58. // https://url.spec.whatwg.org/#userinfo-percent-encode-set
  59. const extraUserinfoPercentEncodeSet =
  60. new Set([47, 58, 59, 61, 64, 91, 92, 93, 94, 124]);
  61. function isUserinfoPercentEncode(c) {
  62. return isPathPercentEncode(c) || extraUserinfoPercentEncodeSet.has(c);
  63. }
  64. // https://url.spec.whatwg.org/#component-percent-encode-set
  65. const extraComponentPercentEncodeSet = new Set([36, 37, 38, 43, 44]);
  66. function isComponentPercentEncode(c) {
  67. return isUserinfoPercentEncode(c) || extraComponentPercentEncodeSet.has(c);
  68. }
  69. // https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set
  70. const extraURLEncodedPercentEncodeSet = new Set([33, 39, 40, 41, 126]);
  71. function isURLEncodedPercentEncode(c) {
  72. return isComponentPercentEncode(c) || extraURLEncodedPercentEncodeSet.has(c);
  73. }
  74. // https://url.spec.whatwg.org/#code-point-percent-encode-after-encoding
  75. // https://url.spec.whatwg.org/#utf-8-percent-encode
  76. // Assuming encoding is always utf-8 allows us to trim one of the logic branches. TODO: support encoding.
  77. // The "-Internal" variant here has code points as JS strings. The external version used by other files has code points
  78. // as JS numbers, like the rest of the codebase.
  79. function utf8PercentEncodeCodePointInternal(codePoint, percentEncodePredicate) {
  80. const bytes = utf8Encode(codePoint);
  81. let output = "";
  82. for (const byte of bytes) {
  83. // Our percentEncodePredicate operates on bytes, not code points, so this is slightly different from the spec.
  84. if (!percentEncodePredicate(byte)) {
  85. output += String.fromCharCode(byte);
  86. } else {
  87. output += percentEncode(byte);
  88. }
  89. }
  90. return output;
  91. }
  92. function utf8PercentEncodeCodePoint(codePoint, percentEncodePredicate) {
  93. return utf8PercentEncodeCodePointInternal(String.fromCodePoint(codePoint), percentEncodePredicate);
  94. }
  95. // https://url.spec.whatwg.org/#string-percent-encode-after-encoding
  96. // https://url.spec.whatwg.org/#string-utf-8-percent-encode
  97. function utf8PercentEncodeString(input, percentEncodePredicate, spaceAsPlus = false) {
  98. let output = "";
  99. for (const codePoint of input) {
  100. if (spaceAsPlus && codePoint === " ") {
  101. output += "+";
  102. } else {
  103. output += utf8PercentEncodeCodePointInternal(codePoint, percentEncodePredicate);
  104. }
  105. }
  106. return output;
  107. }
  108. module.exports = {
  109. isC0ControlPercentEncode,
  110. isFragmentPercentEncode,
  111. isQueryPercentEncode,
  112. isSpecialQueryPercentEncode,
  113. isPathPercentEncode,
  114. isUserinfoPercentEncode,
  115. isURLEncodedPercentEncode,
  116. percentDecodeString,
  117. percentDecodeBytes,
  118. utf8PercentEncodeString,
  119. utf8PercentEncodeCodePoint
  120. };