Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(encoding): count TextEncoder progress in UTF-16 units

ldm06d9125f3fb34
diff --git a/moli-renderer-v8/src/context_bootstrap/constructors/text_codecs.rs b/moli-renderer-v8/src/context_bootstrap/constructors/text_codecs.rs
index 8a51dceed..6bcca5cfd 100644
--- a/moli-renderer-v8/src/context_bootstrap/constructors/text_codecs.rs
+++ b/moli-renderer-v8/src/context_bootstrap/constructors/text_codecs.rs
@@ -276,7 +276,7 @@ fn text_encoder_encode_into_callback<'s>(
break;
}
written += ch_len;
- read_chars += 1;
+ read_chars += ch.len_utf16();
}
if written > 0 {
diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs
index 47566f449..41c466c2d 100644
--- a/moli-renderer-v8/src/script_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs
@@ -2171,6 +2171,7 @@ mod svg_filter_interfaces;
mod svg_number_validation;
mod svg_root_factory_receivers;
mod svg_switch_mpath_interfaces;
+mod text_encoder_utf16_progress;
mod time_ranges;
mod url_components;
mod webgl_interfaces;
diff --git a/moli-renderer-v8/src/script_vm/tests/text_encoder_utf16_progress.rs b/moli-renderer-v8/src/script_vm/tests/text_encoder_utf16_progress.rs
new file mode 100644
index 000000000..e727bf24a
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/text_encoder_utf16_progress.rs
@@ -0,0 +1,20 @@
+use super::*;
+
+#[test]
+fn text_encoder_reports_utf16_progress_at_complete_scalar_boundaries() {
+ let mut vm = new_parsed_test_vm(
+ "https://text-encoder-utf16-progress.test/",
+ "<!doctype html><body></body>",
+ );
+ assert_eq!(vm.eval(r#"(() => {
+ const encoder=new TextEncoder();
+ for(const [text,size,read,written] of [['A๐Ÿ˜€B',0,0,0],['A๐Ÿ˜€B',1,1,1],['A๐Ÿ˜€B',4,1,1],['A๐Ÿ˜€B',5,3,5],['A๐Ÿ˜€B',6,4,6],['๐Ÿ˜€',4,2,4],['\ud800',3,1,3]]) {
+ const storage=new Uint8Array(size+4).fill(99),dest=storage.subarray(2,2+size);
+ const result=encoder.encodeInto(text,dest),expected=encoder.encode(text).slice(0,written);
+ if(result.read!==read||result.written!==written||storage[0]!==99||storage[1]!==99||storage.at(-1)!==99)throw Error('progress '+text+':'+size);
+ for(let i=0;i<written;i++)if(dest[i]!==expected[i])throw Error('bytes');
+ for(let i=written;i<size;i++)if(dest[i]!==99)throw Error('suffix');
+ }
+ return 'ok';
+})()"#).expect("native regression"), "ok");
+}