-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Percent decode #1361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+175
−21
Merged
Percent decode #1361
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c48c4ec
Percent decode
slowcheetah 485c871
Percent decode
slowcheetah 75fdae9
Percent decode
slowcheetah 598d3de
Percent decode
slowcheetah b2ddb51
Percent decode
slowcheetah 3f75274
Percent decode
slowcheetah f012003
Percent decode
slowcheetah ba501bf
Percent decode
slowcheetah b3e76ef
Percent decode
slowcheetah 5eaf948
Percent decode
slowcheetah a476cc5
Percent decode
slowcheetah 177312e
Optimization & fixes
slowcheetah db0ef53
Methods refactoring
slowcheetah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| 'use strict'; | ||
| // TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env` | ||
| require('../modules/es.array.iterator'); | ||
| require('../modules/es.string.from-code-point'); | ||
| var $ = require('../internals/export'); | ||
| var globalThis = require('../internals/global-this'); | ||
| var safeGetBuiltIn = require('../internals/safe-get-built-in'); | ||
| var getBuiltIn = require('../internals/get-built-in'); | ||
| var call = require('../internals/function-call'); | ||
| var uncurryThis = require('../internals/function-uncurry-this'); | ||
| var DESCRIPTORS = require('../internals/descriptors'); | ||
|
|
@@ -43,10 +45,12 @@ var NativeRequest = safeGetBuiltIn('Request'); | |
| var Headers = safeGetBuiltIn('Headers'); | ||
| var RequestPrototype = NativeRequest && NativeRequest.prototype; | ||
| var HeadersPrototype = Headers && Headers.prototype; | ||
| var RegExp = globalThis.RegExp; | ||
| var TypeError = globalThis.TypeError; | ||
| var decodeURIComponent = globalThis.decodeURIComponent; | ||
| var encodeURIComponent = globalThis.encodeURIComponent; | ||
| var fromCharCode = String.fromCharCode; | ||
| var fromCodePoint = getBuiltIn('String', 'fromCodePoint'); | ||
| var $isNaN = isNaN; | ||
| var $parseInt = parseInt; | ||
| var charAt = uncurryThis(''.charAt); | ||
| var join = uncurryThis([].join); | ||
| var push = uncurryThis([].push); | ||
|
|
@@ -55,33 +59,123 @@ var shift = uncurryThis([].shift); | |
| var splice = uncurryThis([].splice); | ||
| var split = uncurryThis(''.split); | ||
| var stringSlice = uncurryThis(''.slice); | ||
| var exec = uncurryThis(/./.exec); | ||
|
|
||
| var plus = /\+/g; | ||
| var sequences = Array(4); | ||
| var FALLBACK_REPLACER = '\uFFFD'; | ||
| var VALID_HEX = /^[0-9a-f]+$/i; | ||
|
|
||
| var percentSequence = function (bytes) { | ||
| return sequences[bytes - 1] || (sequences[bytes - 1] = RegExp('((?:%[\\da-f]{2}){' + bytes + '})', 'gi')); | ||
| var parseHexOctet = function (string, start) { | ||
| var substr = stringSlice(string, start, start + 2); | ||
| if (!exec(VALID_HEX, substr)) return NaN; | ||
|
|
||
| return $parseInt(substr, 16); | ||
| }; | ||
|
|
||
| var getLeadingOnes = function (octet) { | ||
| var count = 0; | ||
| for (var mask = 0x80; mask > 0 && (octet & mask) !== 0; mask >>= 1) { | ||
| count++; | ||
| } | ||
| return count; | ||
| }; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome -) |
||
|
|
||
| var percentDecode = function (sequence) { | ||
| try { | ||
| return decodeURIComponent(sequence); | ||
| } catch (error) { | ||
| return sequence; | ||
| var utf8Decode = function (octets) { | ||
| var codePoint = null; | ||
|
|
||
| switch (octets.length) { | ||
| case 1: | ||
| codePoint = octets[0]; | ||
| break; | ||
| case 2: | ||
| codePoint = (octets[0] & 0x1F) << 6 | (octets[1] & 0x3F); | ||
| break; | ||
| case 3: | ||
| codePoint = (octets[0] & 0x0F) << 12 | (octets[1] & 0x3F) << 6 | (octets[2] & 0x3F); | ||
| break; | ||
| case 4: | ||
| codePoint = (octets[0] & 0x07) << 18 | (octets[1] & 0x3F) << 12 | (octets[2] & 0x3F) << 6 | (octets[3] & 0x3F); | ||
| break; | ||
| } | ||
|
|
||
| return codePoint > 0x10FFFF ? null : codePoint; | ||
| }; | ||
|
|
||
| var deserialize = function (it) { | ||
| var result = replace(it, plus, ' '); | ||
| var bytes = 4; | ||
| try { | ||
| return decodeURIComponent(result); | ||
| } catch (error) { | ||
| while (bytes) { | ||
| result = replace(result, percentSequence(bytes--), percentDecode); | ||
| var decode = function (input) { | ||
| input = replace(input, plus, ' '); | ||
| var length = input.length; | ||
| var result = ''; | ||
| var i = 0; | ||
|
|
||
| while (i < length) { | ||
| var decodedChar = charAt(input, i); | ||
|
|
||
| if (decodedChar === '%') { | ||
| if (charAt(input, i + 1) === '%' || i + 3 > length) { | ||
| result += '%'; | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| var octet = parseHexOctet(input, i + 1); | ||
|
|
||
| if ($isNaN(octet)) { | ||
| result += decodedChar; | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| i += 2; | ||
| var byteSequenceLength = getLeadingOnes(octet); | ||
|
|
||
| if (byteSequenceLength === 0) { | ||
| decodedChar = fromCharCode(octet); | ||
| } else { | ||
| if (byteSequenceLength === 1 || byteSequenceLength > 4) { | ||
| result += FALLBACK_REPLACER; | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| var octets = [octet]; | ||
| var sequenceIndex = 1; | ||
|
|
||
| while (sequenceIndex < byteSequenceLength) { | ||
| i++; | ||
| if (i + 3 > length || charAt(input, i) !== '%') break; | ||
|
|
||
| var nextByte = parseHexOctet(input, i + 1); | ||
|
|
||
| if ($isNaN(nextByte)) { | ||
| i += 3; | ||
| break; | ||
| } | ||
| if (nextByte > 191 || nextByte < 128) break; | ||
|
|
||
| push(octets, nextByte); | ||
| i += 2; | ||
| sequenceIndex++; | ||
| } | ||
|
|
||
| if (octets.length !== byteSequenceLength) { | ||
| result += FALLBACK_REPLACER; | ||
| continue; | ||
| } | ||
|
|
||
| var codePoint = utf8Decode(octets); | ||
| if (codePoint === null) { | ||
| result += FALLBACK_REPLACER; | ||
| } else { | ||
| decodedChar = fromCodePoint(codePoint); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
|
|
||
| result += decodedChar; | ||
| i++; | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| var find = /[!'()~]|%20/g; | ||
|
|
@@ -174,8 +268,8 @@ URLSearchParamsState.prototype = { | |
| if (attribute.length) { | ||
| entry = split(attribute, '='); | ||
| push(entries, { | ||
| key: deserialize(shift(entry)), | ||
| value: deserialize(join(entry, '=')) | ||
| key: decode(shift(entry)), | ||
| value: decode(join(entry, '=')) | ||
| }); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.