Skip to content

Commit fb0bbc0

Browse files
Pauanalexcrichton
authored andcommitted
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
1 parent e39e850 commit fb0bbc0

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

crates/cli-support/src/js/mod.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1057,18 +1057,20 @@ impl<'a> Context<'a> {
10571057
if !self.should_write_global("text_encoder") {
10581058
return Ok(());
10591059
}
1060-
self.expose_text_processor("TextEncoder")
1060+
self.expose_text_processor("TextEncoder", "('utf-8')")
10611061
}
10621062

10631063
fn expose_text_decoder(&mut self) -> Result<(), Error> {
10641064
if !self.should_write_global("text_decoder") {
10651065
return Ok(());
10661066
}
1067-
self.expose_text_processor("TextDecoder")?;
1067+
// `ignoreBOM` is needed so that the BOM will be preserved when sending a string from Rust to JS
1068+
// `fatal` is needed to catch any weird encoding bugs when sending a string from Rust to JS
1069+
self.expose_text_processor("TextDecoder", "('utf-8', { ignoreBOM: true, fatal: true })")?;
10681070
Ok(())
10691071
}
10701072

1071-
fn expose_text_processor(&mut self, s: &str) -> Result<(), Error> {
1073+
fn expose_text_processor(&mut self, s: &str, args: &str) -> Result<(), Error> {
10721074
if self.config.mode.nodejs() {
10731075
let name = self.import_name(&JsImport {
10741076
name: JsImportName::Module {
@@ -1077,7 +1079,8 @@ impl<'a> Context<'a> {
10771079
},
10781080
fields: Vec::new(),
10791081
})?;
1080-
self.global(&format!("let cached{} = new {}('utf-8');", s, name));
1082+
self.global(&format!("let cached{} = new {}{};", s, name, args));
1083+
10811084
} else if !self.config.mode.always_run_in_browser() {
10821085
self.global(&format!(
10831086
"
@@ -1086,10 +1089,12 @@ impl<'a> Context<'a> {
10861089
",
10871090
s
10881091
));
1089-
self.global(&format!("let cached{0} = new l{0}('utf-8');", s));
1092+
self.global(&format!("let cached{0} = new l{0}{1};", s, args));
1093+
10901094
} else {
1091-
self.global(&format!("let cached{0} = new {0}('utf-8');", s));
1095+
self.global(&format!("let cached{0} = new {0}{1};", s, args));
10921096
}
1097+
10931098
Ok(())
10941099
}
10951100

tests/headless/strings.js

+7
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ export function test_string_roundtrip(f) {
1212

1313
test('a longer string');
1414
test('a longer 💖 string');
15+
16+
// TODO re-enable this when Firefox 70 is released
17+
//test('\uFEFFbar');
18+
}
19+
20+
export function identity(s) {
21+
return s;
1522
}

tests/headless/strings.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ use wasm_bindgen_test::*;
44
#[wasm_bindgen(module = "/tests/headless/strings.js")]
55
extern "C" {
66
fn test_string_roundtrip(c: &Closure<dyn Fn(String) -> String>);
7+
8+
fn identity(s: &str) -> String;
79
}
810

911
#[wasm_bindgen_test]
1012
fn string_roundtrip() {
1113
test_string_roundtrip(&Closure::wrap(Box::new(|s| s)));
14+
15+
// TODO re-enable this when Firefox 70 is released
16+
//assert_eq!("\u{feff}bar", &identity("\u{feff}bar"));
1217
}

0 commit comments

Comments
 (0)