From 0f07dd9048625365acd236e1be75088c73efc7a4 Mon Sep 17 00:00:00 2001 From: Alexander Kryvomaz Date: Sun, 1 Jul 2018 15:53:44 +0300 Subject: [PATCH] bindings for decodeURIComponent --- src/js.rs | 7 +++++++ tests/all/js_globals/mod.rs | 28 +++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index e10d8d88..465edac3 100644 --- a/src/js.rs +++ b/src/js.rs @@ -49,6 +49,13 @@ extern "C" { #[wasm_bindgen(catch, js_name = decodeURI)] pub fn decode_uri(encoded: &str) -> Result; + /// The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) + /// component previously created by encodeURIComponent or by a similar routine. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent + #[wasm_bindgen(catch, js_name = decodeURIComponent)] + pub fn decode_uri_component(encoded: &str) -> Result; + /// The `encodeURI()` function encodes a Uniform Resource Identifier (URI) /// by replacing each instance of certain characters by one, two, three, or /// four escape sequences representing the UTF-8 encoding of the character diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index e33d9d6d..b365c6ce 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -6,8 +6,8 @@ mod Array; mod ArrayIterator; mod Boolean; mod Date; -mod Function; mod Error; +mod Function; mod JsString; mod Map; mod MapIterator; @@ -47,6 +47,32 @@ fn decode_uri() { .test(); } +#[test] +fn decode_uri_component() { + project() + .file( + "src/lib.rs", + r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js; + + #[wasm_bindgen] + pub fn test() { + let x = js::decode_uri_component("%3Fx%3Dtest") + .ok() + .expect("should decode URI OK"); + assert_eq!(String::from(x), "?x=test"); + + assert!(js::decode_uri_component("%E0%A4%A").is_err()); + } + "#, + ) + .test(); +} + #[test] #[cfg(feature = "std")] fn encode_uri() {