Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ignoreBOM and fatal to TextDecoder #1730

Merged
merged 6 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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!(
"
Expand All @@ -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(())
}

Expand Down
7 changes: 7 additions & 0 deletions tests/headless/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions tests/headless/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ use wasm_bindgen_test::*;
#[wasm_bindgen(module = "/tests/headless/strings.js")]
extern "C" {
fn test_string_roundtrip(c: &Closure<dyn Fn(String) -> 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"));
}