3 * Copyright(c) 2016 Douglas Christopher Wilson
14 module.exports = encodeUrl
17 * RegExp to match non-URL code points, *after* encoding (i.e. not including "%")
18 * and including invalid escape sequences.
22 var ENCODE_CHARS_REGEXP = /(?:[^\x21\x25\x26-\x3B\x3D\x3F-\x5B\x5D\x5F\x61-\x7A\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g
25 * RegExp to match unmatched surrogate pair.
29 var UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g
32 * String to replace unmatched surrogate pair with.
36 var UNMATCHED_SURROGATE_PAIR_REPLACE = '$1\uFFFD$2'
39 * Encode a URL to a percent-encoded form, excluding already-encoded sequences.
41 * This function will take an already-encoded URL and encode all the non-URL
42 * code points. This function will not encode the "%" character unless it is
43 * not part of a valid sequence (`%20` will be left as-is, but `%foo` will
44 * be encoded as `%25foo`).
46 * This encode is meant to be "safe" and does not throw errors. It will try as
47 * hard as it can to properly encode the given URL, including replacing any raw,
48 * unpaired surrogate pairs with the Unicode replacement character prior to
56 function encodeUrl (url) {
58 .replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)
59 .replace(ENCODE_CHARS_REGEXP, encodeURI)