bindings for decodeURIComponent

This commit is contained in:
Alexander Kryvomaz 2018-07-01 15:53:44 +03:00
parent f850a6fafc
commit 0f07dd9048
2 changed files with 34 additions and 1 deletions

View File

@ -49,6 +49,13 @@ extern "C" {
#[wasm_bindgen(catch, js_name = decodeURI)]
pub fn decode_uri(encoded: &str) -> Result<JsString, JsValue>;
/// 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<JsString, JsValue>;
/// 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

View File

@ -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() {