@depup/whatwg-url 16.0.1-depup.0

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