source: frontend/node_modules/whatwg-url/dist/percent-encoding.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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