diff --git a/src/js.rs b/src/js.rs index 465edac3..7d957f74 100644 --- a/src/js.rs +++ b/src/js.rs @@ -49,8 +49,8 @@ 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. + /// 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)] @@ -66,6 +66,15 @@ extern "C" { #[wasm_bindgen(js_name = encodeURI)] pub fn encode_uri(decoded: &str) -> JsString; + /// The encodeURIComponent() function encodes a Uniform Resource Identifier (URI) component + /// by replacing each instance of certain characters by one, two, three, or four escape sequences + /// representing the UTF-8 encoding of the character + /// (will only be four escape sequences for characters composed of two "surrogate" characters). + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent + #[wasm_bindgen(js_name = encodeURIComponent)] + pub fn encode_uri_component(decoded: &str) -> JsString; + /// The `eval()` function evaluates JavaScript code represented as a string. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index b365c6ce..632898e0 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -96,6 +96,28 @@ fn encode_uri() { .test(); } +#[test] +fn encode_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::encode_uri_component("?x=шеллы"); + assert_eq!(String::from(x), "%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"); + } + "#, + ) + .test(); +} + #[test] fn eval() { project()