From fb0bbc00cb872f777b1c3d8b65648266354eb686 Mon Sep 17 00:00:00 2001 From: Pauan Date: Fri, 23 Aug 2019 05:00:49 +0200 Subject: [PATCH] Adding ignoreBOM and fatal to TextDecoder (#1730) * Adding ignoreBOM and fatal to TextDecoder * Minor tweak to expose_text_processor * Adding in unit tests for BOM * Adding in comment for expose_text_decoder * Attempting to fix build failure * Temporarily disabling unit tests --- crates/cli-support/src/js/mod.rs | 17 +++++++++++------ tests/headless/strings.js | 7 +++++++ tests/headless/strings.rs | 5 +++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index f19d1e3fcab..da2eb4c8caf 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -1057,18 +1057,20 @@ impl<'a> Context<'a> { if !self.should_write_global("text_encoder") { return Ok(()); } - self.expose_text_processor("TextEncoder") + self.expose_text_processor("TextEncoder", "('utf-8')") } fn expose_text_decoder(&mut self) -> Result<(), Error> { if !self.should_write_global("text_decoder") { return Ok(()); } - self.expose_text_processor("TextDecoder")?; + // `ignoreBOM` is needed so that the BOM will be preserved when sending a string from Rust to JS + // `fatal` is needed to catch any weird encoding bugs when sending a string from Rust to JS + self.expose_text_processor("TextDecoder", "('utf-8', { ignoreBOM: true, fatal: true })")?; Ok(()) } - fn expose_text_processor(&mut self, s: &str) -> Result<(), Error> { + fn expose_text_processor(&mut self, s: &str, args: &str) -> Result<(), Error> { if self.config.mode.nodejs() { let name = self.import_name(&JsImport { name: JsImportName::Module { @@ -1077,7 +1079,8 @@ impl<'a> Context<'a> { }, fields: Vec::new(), })?; - self.global(&format!("let cached{} = new {}('utf-8');", s, name)); + self.global(&format!("let cached{} = new {}{};", s, name, args)); + } else if !self.config.mode.always_run_in_browser() { self.global(&format!( " @@ -1086,10 +1089,12 @@ impl<'a> Context<'a> { ", s )); - self.global(&format!("let cached{0} = new l{0}('utf-8');", s)); + self.global(&format!("let cached{0} = new l{0}{1};", s, args)); + } else { - self.global(&format!("let cached{0} = new {0}('utf-8');", s)); + self.global(&format!("let cached{0} = new {0}{1};", s, args)); } + Ok(()) } diff --git a/tests/headless/strings.js b/tests/headless/strings.js index f22b994b57b..3ef14370363 100644 --- a/tests/headless/strings.js +++ b/tests/headless/strings.js @@ -12,4 +12,11 @@ export function test_string_roundtrip(f) { test('a longer string'); test('a longer 💖 string'); + + // TODO re-enable this when Firefox 70 is released + //test('\uFEFFbar'); +} + +export function identity(s) { + return s; } diff --git a/tests/headless/strings.rs b/tests/headless/strings.rs index 9f2628cad32..39ec9aebd86 100644 --- a/tests/headless/strings.rs +++ b/tests/headless/strings.rs @@ -4,9 +4,14 @@ use wasm_bindgen_test::*; #[wasm_bindgen(module = "/tests/headless/strings.js")] extern "C" { fn test_string_roundtrip(c: &Closure String>); + + fn identity(s: &str) -> String; } #[wasm_bindgen_test] fn string_roundtrip() { test_string_roundtrip(&Closure::wrap(Box::new(|s| s))); + + // TODO re-enable this when Firefox 70 is released + //assert_eq!("\u{feff}bar", &identity("\u{feff}bar")); }