| 1 | "use strict";
|
|---|
| 2 | const { isASCIIHex } = require("./infra");
|
|---|
| 3 | const { utf8Encode } = require("./encoding");
|
|---|
| 4 |
|
|---|
| 5 | // https://url.spec.whatwg.org/#percent-encode
|
|---|
| 6 | function 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
|
|---|
| 16 | function 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
|
|---|
| 39 | function percentDecodeString(input) {
|
|---|
| 40 | const bytes = utf8Encode(input);
|
|---|
| 41 | return percentDecodeBytes(bytes);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | // https://url.spec.whatwg.org/#c0-control-percent-encode-set
|
|---|
| 45 | function isC0ControlPercentEncode(c) {
|
|---|
| 46 | return c <= 0x1F || c > 0x7E;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | // https://url.spec.whatwg.org/#fragment-percent-encode-set
|
|---|
| 50 | const extraFragmentPercentEncodeSet = new Set([32, 34, 60, 62, 96]);
|
|---|
| 51 | function isFragmentPercentEncode(c) {
|
|---|
| 52 | return isC0ControlPercentEncode(c) || extraFragmentPercentEncodeSet.has(c);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | // https://url.spec.whatwg.org/#query-percent-encode-set
|
|---|
| 56 | const extraQueryPercentEncodeSet = new Set([32, 34, 35, 60, 62]);
|
|---|
| 57 | function isQueryPercentEncode(c) {
|
|---|
| 58 | return isC0ControlPercentEncode(c) || extraQueryPercentEncodeSet.has(c);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | // https://url.spec.whatwg.org/#special-query-percent-encode-set
|
|---|
| 62 | function isSpecialQueryPercentEncode(c) {
|
|---|
| 63 | return isQueryPercentEncode(c) || c === 39;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | // https://url.spec.whatwg.org/#path-percent-encode-set
|
|---|
| 67 | const extraPathPercentEncodeSet = new Set([63, 96, 123, 125]);
|
|---|
| 68 | function isPathPercentEncode(c) {
|
|---|
| 69 | return isQueryPercentEncode(c) || extraPathPercentEncodeSet.has(c);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | // https://url.spec.whatwg.org/#userinfo-percent-encode-set
|
|---|
| 73 | const extraUserinfoPercentEncodeSet =
|
|---|
| 74 | new Set([47, 58, 59, 61, 64, 91, 92, 93, 94, 124]);
|
|---|
| 75 | function isUserinfoPercentEncode(c) {
|
|---|
| 76 | return isPathPercentEncode(c) || extraUserinfoPercentEncodeSet.has(c);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | // https://url.spec.whatwg.org/#component-percent-encode-set
|
|---|
| 80 | const extraComponentPercentEncodeSet = new Set([36, 37, 38, 43, 44]);
|
|---|
| 81 | function 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
|
|---|
| 86 | const extraURLEncodedPercentEncodeSet = new Set([33, 39, 40, 41, 126]);
|
|---|
| 87 | function 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.
|
|---|
| 96 | function 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 |
|
|---|
| 111 | function 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
|
|---|
| 117 | function 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 |
|
|---|
| 129 | module.exports = {
|
|---|
| 130 | isC0ControlPercentEncode,
|
|---|
| 131 | isFragmentPercentEncode,
|
|---|
| 132 | isQueryPercentEncode,
|
|---|
| 133 | isSpecialQueryPercentEncode,
|
|---|
| 134 | isPathPercentEncode,
|
|---|
| 135 | isUserinfoPercentEncode,
|
|---|
| 136 | isURLEncodedPercentEncode,
|
|---|
| 137 | percentDecodeString,
|
|---|
| 138 | percentDecodeBytes,
|
|---|
| 139 | utf8PercentEncodeString,
|
|---|
| 140 | utf8PercentEncodeCodePoint
|
|---|
| 141 | };
|
|---|