diff options
author | Joshua Reusch <jreusch4@gmail.com> | 2024-08-13 21:40:56 +0200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-08-14 11:26:26 +0200 |
commit | f668f195a455739a88721622d57111948fa6435a (patch) | |
tree | cc427b96198c37d3e22ca2772bfbe6cf7f107572 | |
parent | fab4557cdbcd8405b4d255ff7ca845ee9a6f8085 (diff) | |
download | gleam_stdlib-f668f195a455739a88721622d57111948fa6435a.tar.gz gleam_stdlib-f668f195a455739a88721622d57111948fa6435a.zip |
add test, make erlang and javascript implementation agree #672
-rw-r--r-- | src/gleam_stdlib.mjs | 11 | ||||
-rw-r--r-- | test/gleam/uri_test.gleam | 3 |
2 files changed, 11 insertions, 3 deletions
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 157f66d..3296b67 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -479,6 +479,10 @@ export function map_insert(key, value, map) { } function unsafe_percent_decode(string) { + return decodeURIComponent(string || ""); +} + +function unsafe_percent_decode_query(string) { return decodeURIComponent((string || "").replace("+", " ")); } @@ -491,7 +495,7 @@ export function percent_decode(string) { } export function percent_encode(string) { - return encodeURIComponent(string); + return encodeURIComponent(string).replace("%2B", "+"); } export function parse_query(query) { @@ -500,7 +504,10 @@ export function parse_query(query) { for (const section of query.split("&")) { const [key, value] = section.split("="); if (!key) continue; - pairs.push([unsafe_percent_decode(key), unsafe_percent_decode(value)]); + + const decodedKey = unsafe_percent_decode_query(key) + const decodedValue = unsafe_percent_decode_query(value) + pairs.push([decodedKey, decodedValue]) } return new Ok(List.fromArray(pairs)); } catch { diff --git a/test/gleam/uri_test.gleam b/test/gleam/uri_test.gleam index 2d2732a..267c0d4 100644 --- a/test/gleam/uri_test.gleam +++ b/test/gleam/uri_test.gleam @@ -345,7 +345,8 @@ const percent_codec_fixtures = [ #("?", "%3F"), #("'", "'"), #("(", "("), #(")", ")"), #("[", "%5B"), #("@", "%40"), #("/", "%2F"), #("\\", "%5C"), #("&", "%26"), #("#", "%23"), #("=", "%3D"), #("~", "~"), #("ñ", "%C3%B1"), #("-", "-"), #("_", "_"), - #(".", "."), #("*", "*"), #("100% great", "100%25%20great"), + #(".", "."), #("*", "*"), #("+", "+"), + #("100% great+fun", "100%25%20great+fun"), ] // Allowed chars |