|
| 1 | +import { warn } from '../core/utils' |
| 2 | + |
| 3 | +/** |
| 4 | + * Encoding Rules (␣ = Space) |
| 5 | + * - Path: ␣ " < > # ? { } |
| 6 | + * - Query: ␣ " < > # & = |
| 7 | + * - Hash: ␣ " < > ` |
| 8 | + * |
| 9 | + * On top of that, the RFC3986 (https://tools.ietf.org/html/rfc3986#section-2.2) |
| 10 | + * defines some extra characters to be encoded. Most browsers do not encode them |
| 11 | + * in encodeURI https://github.com/whatwg/url/issues/369, so it may be safer to |
| 12 | + * also encode `!'()*`. Leaving un-encoded only ASCII alphanumeric(`a-zA-Z0-9`) |
| 13 | + * plus `-._~`. This extra safety should be applied to query by patching the |
| 14 | + * string returned by encodeURIComponent encodeURI also encodes `[\]^`. `\` |
| 15 | + * should be encoded to avoid ambiguity. Browsers (IE, FF, C) transform a `\` |
| 16 | + * into a `/` if directly typed in. The _backtick_ (`````) should also be |
| 17 | + * encoded everywhere because some browsers like FF encode it when directly |
| 18 | + * written while others don't. Safari and IE don't encode ``"<>{}``` in hash. |
| 19 | + */ |
| 20 | +// const EXTRA_RESERVED_RE = /[!'()*]/g |
| 21 | +// const encodeReservedReplacer = (c: string) => '%' + c.charCodeAt(0).toString(16) |
| 22 | + |
| 23 | +const HASH_RE = /#/g // %23 |
| 24 | +const AMPERSAND_RE = /&/g // %26 |
| 25 | +export const SLASH_RE = /\//g // %2F |
| 26 | +const EQUAL_RE = /=/g // %3D |
| 27 | +const IM_RE = /\?/g // %3F |
| 28 | +export const PLUS_RE = /\+/g // %2B |
| 29 | +/** |
| 30 | + * NOTE: It's not clear to me if we should encode the + symbol in queries, it |
| 31 | + * seems to be less flexible than not doing so and I can't find out the legacy |
| 32 | + * systems requiring this for regular requests like text/html. In the standard, |
| 33 | + * the encoding of the plus character is only mentioned for |
| 34 | + * application/x-www-form-urlencoded |
| 35 | + * (https://url.spec.whatwg.org/#urlencoded-parsing) and most browsers seems lo |
| 36 | + * leave the plus character as is in queries. To be more flexible, we allow the |
| 37 | + * plus character on the query, but it can also be manually encoded by the user. |
| 38 | + * |
| 39 | + * Resources: |
| 40 | + * - https://url.spec.whatwg.org/#urlencoded-parsing |
| 41 | + * - https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20 |
| 42 | + */ |
| 43 | + |
| 44 | +const ENC_BRACKET_OPEN_RE = /%5B/g // [ |
| 45 | +const ENC_BRACKET_CLOSE_RE = /%5D/g // ] |
| 46 | +const ENC_CARET_RE = /%5E/g // ^ |
| 47 | +const ENC_BACKTICK_RE = /%60/g // ` |
| 48 | +const ENC_CURLY_OPEN_RE = /%7B/g // { |
| 49 | +const ENC_PIPE_RE = /%7C/g // | |
| 50 | +const ENC_CURLY_CLOSE_RE = /%7D/g // } |
| 51 | +const ENC_SPACE_RE = /%20/g // } |
| 52 | + |
| 53 | +/** |
| 54 | + * Encode characters that need to be encoded on the path, search and hash |
| 55 | + * sections of the URL. |
| 56 | + * |
| 57 | + * @internal |
| 58 | + * @param text - string to encode |
| 59 | + * @returns encoded string |
| 60 | + */ |
| 61 | +export function commonEncode(text: string | number | null | undefined): string { |
| 62 | + return text == null |
| 63 | + ? '' |
| 64 | + : encodeURI('' + text) |
| 65 | + .replace(ENC_PIPE_RE, '|') |
| 66 | + .replace(ENC_BRACKET_OPEN_RE, '[') |
| 67 | + .replace(ENC_BRACKET_CLOSE_RE, ']') |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Encode characters that need to be encoded on the hash section of the URL. |
| 72 | + * |
| 73 | + * @param text - string to encode |
| 74 | + * @returns encoded string |
| 75 | + */ |
| 76 | +export function encodeHash(text: string): string { |
| 77 | + return commonEncode(text) |
| 78 | + .replace(ENC_CURLY_OPEN_RE, '{') |
| 79 | + .replace(ENC_CURLY_CLOSE_RE, '}') |
| 80 | + .replace(ENC_CARET_RE, '^') |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Encode characters that need to be encoded query values on the query |
| 85 | + * section of the URL. |
| 86 | + * |
| 87 | + * @param text - string to encode |
| 88 | + * @returns encoded string |
| 89 | + */ |
| 90 | +export function encodeQueryValue(text: string | number): string { |
| 91 | + return ( |
| 92 | + commonEncode(text) |
| 93 | + // Encode the space as +, encode the + to differentiate it from the space |
| 94 | + .replace(PLUS_RE, '%2B') |
| 95 | + .replace(ENC_SPACE_RE, '+') |
| 96 | + .replace(HASH_RE, '%23') |
| 97 | + .replace(AMPERSAND_RE, '%26') |
| 98 | + .replace(ENC_BACKTICK_RE, '`') |
| 99 | + .replace(ENC_CURLY_OPEN_RE, '{') |
| 100 | + .replace(ENC_CURLY_CLOSE_RE, '}') |
| 101 | + .replace(ENC_CARET_RE, '^') |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Like `encodeQueryValue` but also encodes the `=` character. |
| 107 | + * |
| 108 | + * @param text - string to encode |
| 109 | + */ |
| 110 | +export function encodeQueryKey(text: string | number): string { |
| 111 | + return encodeQueryValue(text).replace(EQUAL_RE, '%3D') |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Encode characters that need to be encoded on the path section of the URL. |
| 116 | + * |
| 117 | + * @param text - string to encode |
| 118 | + * @returns encoded string |
| 119 | + */ |
| 120 | +export function encodePath(text: string | number | null | undefined): string { |
| 121 | + return commonEncode(text).replace(HASH_RE, '%23').replace(IM_RE, '%3F') |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Encode characters that need to be encoded on the path section of the URL as a |
| 126 | + * param. This function encodes everything {@link encodePath} does plus the |
| 127 | + * slash (`/`) character. If `text` is `null` or `undefined`, returns an empty |
| 128 | + * string instead. |
| 129 | + * |
| 130 | + * @param text - string to encode |
| 131 | + * @returns encoded string |
| 132 | + */ |
| 133 | +export function encodeParam(text: string | number | null | undefined): string { |
| 134 | + return encodePath(text).replace(SLASH_RE, '%2F') |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Decode text using `decodeURIComponent`. Returns the original text if it |
| 139 | + * fails. |
| 140 | + * |
| 141 | + * @param text - string to decode |
| 142 | + * @returns decoded string |
| 143 | + */ |
| 144 | +export function decode(text: string | number): string |
| 145 | +export function decode(text: null | undefined): null |
| 146 | +export function decode(text: string | number | null | undefined): string | null |
| 147 | +export function decode( |
| 148 | + text: string | number | null | undefined |
| 149 | +): string | null { |
| 150 | + if (text == null) return null |
| 151 | + try { |
| 152 | + return decodeURIComponent('' + text) |
| 153 | + } catch (err) { |
| 154 | + if (process.env.NODE_ENV !== 'production') { |
| 155 | + warn(`Error decoding "${text}". Using original value`) |
| 156 | + } |
| 157 | + } |
| 158 | + return '' + text |
| 159 | +} |
0 commit comments