From 4a640f2e179773b7350d6680904f5b0b54b3bd99 Mon Sep 17 00:00:00 2001 From: Mahmadhadi Date: Mon, 28 Sep 2026 14:36:40 +0530 Subject: [PATCH] fix: correctly percent encode UTF-8 bytes (#15595) --- .../string/base/percent-encode/lib/main.js | 141 +++++++++--------- .../string/base/percent-encode/test/test.js | 91 +++++------ 2 files changed, 119 insertions(+), 113 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/percent-encode/lib/main.js b/lib/node_modules/@stdlib/string/base/percent-encode/lib/main.js index cad0ac487884..24a68b4607b0 100644 --- a/lib/node_modules/@stdlib/string/base/percent-encode/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/percent-encode/lib/main.js @@ -1,60 +1,58 @@ /** -* @license Apache-2.0 -* -* Copyright (c) 2022 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; + * @license Apache-2.0 + * + * Copyright (c) 2022 The Stdlib Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +"use strict"; // MODULES // -var utf16ToUTF8Array = require( '@stdlib/string/utf16-to-utf8-array' ); - +var utf16ToUTF8Array = require("@stdlib/string/utf16-to-utf8-array"); // VARIABLES // // Character codes: -var UNDERSCORE = 95|0; -var PERIOD = 46|0; -var HYPHEN = 45|0; -var TILDE = 126|0; -var ZERO = 48|0; -var NINE = 57|0; -var A = 65|0; -var Z = 90|0; -var a = 97|0; -var z = 122|0; - +var UNDERSCORE = 95 | 0; +var PERIOD = 46 | 0; +var HYPHEN = 45 | 0; +var TILDE = 126 | 0; +var ZERO = 48 | 0; +var NINE = 57 | 0; +var A = 65 | 0; +var Z = 90 | 0; +var a = 97 | 0; +var z = 122 | 0; // MAIN // /** -* Percent-encodes a UTF-16 encoded string according to [RFC 3986][1]. -* -* [1]: https://tools.ietf.org/html/rfc3986#section-2.1 -* -* @param {string} str - string to percent-encode -* @returns {string} percent-encoded string -* -* @example -* var str1 = 'Ladies + Gentlemen'; -* -* var str2 = percentEncode( str1 ); -* // returns 'Ladies%20%2B%20Gentlemen' -*/ -function percentEncode( str ) { + * Percent-encodes a UTF-16 encoded string according to [RFC 3986][1]. + * + * [1]: https://tools.ietf.org/html/rfc3986#section-2.1 + * + * @param {string} str - string to percent-encode + * @returns {string} percent-encoded string + * + * @example + * var str1 = 'Ladies + Gentlemen'; + * + * var str2 = percentEncode( str1 ); + * // returns 'Ladies%20%2B%20Gentlemen' + */ +function percentEncode(str) { var byte; var out; var len; @@ -62,39 +60,42 @@ function percentEncode( str ) { var i; // Convert to an array of octets using UTF-8 encoding (see https://tools.ietf.org/html/rfc3986#section-2.5): - buf = utf16ToUTF8Array( str ); + buf = utf16ToUTF8Array(str); // Encode the string... len = buf.length; - out = ''; - for ( i = 0; i < len; i++ ) { - byte = buf[ i ]; - if ( - // ASCII Digits: - ( byte >= ZERO && byte <= NINE ) || - - // ASCII uppercase letters: - ( byte >= A && byte <= Z ) || - - // ASCII lowercase letters: - ( byte >= a && byte <= z ) || - - // ASCII unreserved characters (see https://tools.ietf.org/html/rfc3986#section-2.3): - byte === HYPHEN || - byte === PERIOD || - byte === UNDERSCORE || - byte === TILDE - ) { - out += str.charAt( i ); - } else { - // Convert an octet to hexadecimal and uppercase according to the RFC (see https://tools.ietf.org/html/rfc3986#section-2.1): - out += '%' + byte.toString( 16 ).toUpperCase(); + out = ""; + for (i = 0; i < len; i++) { + byte = buf[i]; + // Encode the string... + len = buf.length; + out = ""; + for (i = 0; i < len; i++) { + byte = buf[i]; + if ( + // ASCII Digits: + (byte >= ZERO && byte <= NINE) || + // ASCII uppercase letters: + (byte >= A && byte <= Z) || + // ASCII lowercase letters: + (byte >= a && byte <= z) || + // ASCII unreserved characters (see https://tools.ietf.org/html/rfc3986#section-2.3): + byte === HYPHEN || + byte === PERIOD || + byte === UNDERSCORE || + byte === TILDE + ) { + out += String.fromCharCode(byte); + } else { + // Convert an octet to hexadecimal and uppercase according to the RFC (see https://tools.ietf.org/html/rfc3986#section-2.1): + var hex = byte.toString(16).toUpperCase(); + out += "%" + (hex.length === 1 ? "0" : "") + hex; + } } } return out; } - // EXPORTS // module.exports = percentEncode; diff --git a/lib/node_modules/@stdlib/string/base/percent-encode/test/test.js b/lib/node_modules/@stdlib/string/base/percent-encode/test/test.js index 42cbb3bb2929..46a77f4baa57 100644 --- a/lib/node_modules/@stdlib/string/base/percent-encode/test/test.js +++ b/lib/node_modules/@stdlib/string/base/percent-encode/test/test.js @@ -1,38 +1,37 @@ /** -* @license Apache-2.0 -* -* Copyright (c) 2022 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * @license Apache-2.0 + * + * Copyright (c) 2022 The Stdlib Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -'use strict'; +"use strict"; // MODULES // -var tape = require( 'tape' ); -var percentEncode = require( './../lib' ); - +var tape = require("tape"); +var percentEncode = require("./../lib"); // TESTS // -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof percentEncode, 'function', 'main export is a function' ); +tape("main export is a function", function test(t) { + t.ok(true, __filename); + t.strictEqual(typeof percentEncode, "function", "main export is a function"); t.end(); }); -tape( 'the function percent-encodes a string', function test( t ) { +tape("the function percent-encodes a string", function test(t) { var expected; var values; var out; @@ -40,30 +39,36 @@ tape( 'the function percent-encodes a string', function test( t ) { // Some test values are from https://dev.twitter.com/oauth/overview/percent-encoding-parameters. values = [ - 'Ladies + Gentlemen', - 'An encoded string!', - 'Dogs, Cats & Mice', - '☃', - 'æ', - '𐐷', - '()', - 'https://en.wikipedia.org/wiki/Mode_(statistics)' + "Ladies + Gentlemen", + "An encoded string!", + "Dogs, Cats & Mice", + "☃", + "æ", + "𐐷", + "()", + "https://en.wikipedia.org/wiki/Mode_(statistics)", + "\t", + "\r", + "\é1", ]; expected = [ - 'Ladies%20%2B%20Gentlemen', - 'An%20encoded%20string%21', - 'Dogs%2C%20Cats%20%26%20Mice', - '%E2%98%83', - '%C3%A6', - '%F0%90%90%B7', - '%28%29', - 'https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMode_%28statistics%29' + "Ladies%20%2B%20Gentlemen", + "An%20encoded%20string%21", + "Dogs%2C%20Cats%20%26%20Mice", + "%E2%98%83", + "%C3%A6", + "%F0%90%90%B7", + "%28%29", + "https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMode_%28statistics%29", + "%09", + "%0D", + "%C3%A91", ]; - for ( i = 0; i < values.length; i++ ) { - out = percentEncode( values[ i ] ); - t.strictEqual( out, expected[ i ], 'returns expected value for '+values[i] ); + for (i = 0; i < values.length; i++) { + out = percentEncode(values[i]); + t.strictEqual(out, expected[i], "returns expected value for " + values[i]); } t.end(); });