MCPcopy Create free account
hub / github.com/witheve/Eve / encode

Function encode

src/commonmark.js:3229–3278  ·  view source on GitHub ↗
(string, exclude, keepEscaped)

Source from the content-addressed store, hash-verified

3227// - keepEscaped - don't encode '%' in a correct escape sequence (default: true)
3228//
3229function encode(string, exclude, keepEscaped) {
3230 var i, l, code, nextCode, cache,
3231 result = '';
3232
3233 if (typeof exclude !== 'string') {
3234 // encode(string, keepEscaped)
3235 keepEscaped = exclude;
3236 exclude = encode.defaultChars;
3237 }
3238
3239 if (typeof keepEscaped === 'undefined') {
3240 keepEscaped = true;
3241 }
3242
3243 cache = getEncodeCache(exclude);
3244
3245 for (i = 0, l = string.length; i < l; i++) {
3246 code = string.charCodeAt(i);
3247
3248 if (keepEscaped && code === 0x25 /* % */ && i + 2 < l) {
3249 if (/^[0-9a-f]{2}$/i.test(string.slice(i + 1, i + 3))) {
3250 result += string.slice(i, i + 3);
3251 i += 2;
3252 continue;
3253 }
3254 }
3255
3256 if (code < 128) {
3257 result += cache[code];
3258 continue;
3259 }
3260
3261 if (code >= 0xD800 && code <= 0xDFFF) {
3262 if (code >= 0xD800 && code <= 0xDBFF && i + 1 < l) {
3263 nextCode = string.charCodeAt(i + 1);
3264 if (nextCode >= 0xDC00 && nextCode <= 0xDFFF) {
3265 result += encodeURIComponent(string[i] + string[i + 1]);
3266 i++;
3267 continue;
3268 }
3269 }
3270 result += '%EF%BF%BD';
3271 continue;
3272 }
3273
3274 result += encodeURIComponent(string[i]);
3275 }
3276
3277 return result;
3278}
3279
3280encode.defaultChars = ";/?:@&=+$,-_.!~*'()#";
3281encode.componentChars = "-_.!~*'()";

Callers 1

normalizeURIFunction · 0.85

Calls 2

getEncodeCacheFunction · 0.85
testMethod · 0.45

Tested by

no test coverage detected