Truly independent web browser
LibWeb: Keep one-byte HTML tokenizer input at one byte per code unit
Andreas Kling571832d3319c
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/input.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/input.rsnew file mode 100644index 00000000000..87fc9b68872--- /dev/null+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/input.rs@@ -0,0 +1,179 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++/// The code units of a tokenizer's input stream.+///+/// Most documents only have code units below U+0100, and are kept at one byte per code unit. The stream is widened+/// the first time it is given a code unit that doesn't fit. Both forms are indexed by code unit, so offsets into the+/// stream stay the same when that happens.+#[derive(Clone, Debug)]+pub enum Input {+ Latin1(Vec<u8>),+ Utf16(Vec<u16>),+}++/// A borrowed run of input code units.+#[derive(Clone, Copy, Debug)]+pub enum InputSlice<'a> {+ Latin1(&'a [u8]),+ Utf16(&'a [u16]),+}++impl Default for Input {+ fn default() -> Self {+ Self::Latin1(Vec::new())+ }+}++impl From<Vec<u8>> for Input {+ fn from(code_units: Vec<u8>) -> Self {+ Self::Latin1(code_units)+ }+}++impl From<Vec<u16>> for Input {+ fn from(code_units: Vec<u16>) -> Self {+ Self::Utf16(code_units)+ }+}++impl Input {+ #[inline(always)]+ pub fn len(&self) -> usize {+ match self {+ Self::Latin1(code_units) => code_units.len(),+ Self::Utf16(code_units) => code_units.len(),+ }+ }++ #[inline(always)]+ pub fn is_empty(&self) -> bool {+ self.len() == 0+ }++ /// The code unit at `offset`, which must be inside the input.+ #[inline(always)]+ pub fn code_unit_at(&self, offset: usize) -> u16 {+ match self {+ Self::Latin1(code_units) => u16::from(code_units[offset]),+ Self::Utf16(code_units) => code_units[offset],+ }+ }++ #[inline(always)]+ pub fn get(&self, offset: usize) -> Option<u16> {+ match self {+ Self::Latin1(code_units) => code_units.get(offset).copied().map(u16::from),+ Self::Utf16(code_units) => code_units.get(offset).copied(),+ }+ }++ pub fn slice_from(&self, start: usize) -> InputSlice<'_> {+ match self {+ Self::Latin1(code_units) => InputSlice::Latin1(&code_units[start..]),+ Self::Utf16(code_units) => InputSlice::Utf16(&code_units[start..]),+ }+ }++ pub fn append(&mut self, code_units: InputSlice<'_>) {+ self.insert(self.len(), code_units);+ }++ pub fn insert(&mut self, offset: usize, code_units: InputSlice<'_>) {+ if self.is_empty() {+ *self = code_units.to_input();+ return;+ }+ match (&mut *self, code_units) {+ (Self::Latin1(input), InputSlice::Latin1(code_units)) => {+ input.splice(offset..offset, code_units.iter().copied());+ }+ (Self::Utf16(input), InputSlice::Latin1(code_units)) => {+ input.splice(offset..offset, code_units.iter().copied().map(u16::from));+ }+ (Self::Utf16(input), InputSlice::Utf16(code_units)) => {+ input.splice(offset..offset, code_units.iter().copied());+ }+ (Self::Latin1(input), InputSlice::Utf16(code_units)) => {+ let mut widened = Vec::with_capacity(input.len() + code_units.len());+ widened.extend(input[..offset].iter().copied().map(u16::from));+ widened.extend_from_slice(code_units);+ widened.extend(input[offset..].iter().copied().map(u16::from));+ *self = Self::Utf16(widened);+ }+ }+ }++ pub fn to_utf16(&self) -> Vec<u16> {+ self.slice_from(0).to_utf16()+ }+}++impl InputSlice<'_> {+ pub fn len(&self) -> usize {+ match self {+ Self::Latin1(code_units) => code_units.len(),+ Self::Utf16(code_units) => code_units.len(),+ }+ }++ pub fn is_empty(&self) -> bool {+ self.len() == 0+ }++ pub fn to_utf16(&self) -> Vec<u16> {+ match self {+ Self::Latin1(code_units) => code_units.iter().copied().map(u16::from).collect(),+ Self::Utf16(code_units) => code_units.to_vec(),+ }+ }++ fn to_input(self) -> Input {+ match self {+ Self::Latin1(code_units) => Input::Latin1(code_units.to_vec()),+ Self::Utf16(code_units) => Input::Utf16(code_units.to_vec()),+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn code_units(input: &str) -> Vec<u16> {+ input.encode_utf16().collect()+ }++ #[test]+ fn one_byte_input_stays_one_byte() {+ let mut input = Input::default();+ input.append(InputSlice::Latin1(b"<p>"));+ input.append(InputSlice::Latin1(b"caf\xe9"));+ input.insert(3, InputSlice::Latin1(b"un "));+ assert!(matches!(input, Input::Latin1(_)));+ assert_eq!(input.to_utf16(), code_units("<p>un caf\u{e9}"));+ assert_eq!(input.code_unit_at(9), 0xe9);+ assert_eq!(input.get(10), None);+ }++ #[test]+ fn wide_code_units_widen_the_input_in_place() {+ let mut input = Input::default();+ input.append(InputSlice::Latin1(b"<p>caf\xe9</p>"));+ input.insert(7, InputSlice::Utf16(&code_units(" \u{2014} \u{1f600}")));+ assert!(matches!(input, Input::Utf16(_)));+ assert_eq!(input.to_utf16(), code_units("<p>caf\u{e9} \u{2014} \u{1f600}</p>"));+ input.append(InputSlice::Latin1(b"\xe9"));+ assert_eq!(input.code_unit_at(input.len() - 1), 0xe9);+ }++ #[test]+ fn empty_input_takes_the_form_of_what_it_is_given() {+ let mut input = Input::from(Vec::<u16>::new());+ input.append(InputSlice::Latin1(b"abc"));+ assert!(matches!(input, Input::Latin1(_)));+ }+}diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rsindex 7fdccff4413..fc615485c99 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs@@ -5,6 +5,7 @@*/pub mod entities;+pub mod input;mod known_names;pub mod meta_charset_scanner;pub mod parser;@@ -12,6 +13,7 @@ pub mod preload_scanner;pub mod token;pub mod tokenizer;+use input::InputSlice;use std::ptr;use token::Attribute;use token::Position;@@ -129,17 +131,13 @@ fn position_to_ffi(pos: &Position) -> (u64, u64) {/// Create a new Rust HTML tokenizer directly from a UTF-16 code unit buffer.#[unsafe(no_mangle)]pub extern "C" fn rust_html_tokenizer_create_from_utf16(input: ak::Span<'_, u16>) -> *mut RustFfiTokenizerHandle {- let code_units = input.as_slice().to_vec();-- make_handle(HtmlTokenizer::new(code_units))+ make_handle(HtmlTokenizer::new(input.as_slice().to_vec()))}-/// Create a new Rust HTML tokenizer directly from an ASCII byte buffer.+/// Create a new Rust HTML tokenizer directly from a Latin-1 byte buffer.#[unsafe(no_mangle)]pub extern "C" fn rust_html_tokenizer_create_from_latin1(input: ak::Span<'_, u8>) -> *mut RustFfiTokenizerHandle {- let code_units = input.as_slice().iter().map(|byte| u16::from(*byte)).collect();-- make_handle(HtmlTokenizer::new(code_units))+ make_handle(HtmlTokenizer::new(input.as_slice().to_vec()))}fn make_handle(tokenizer: HtmlTokenizer) -> *mut RustFfiTokenizerHandle {@@ -331,11 +329,12 @@ pub extern "C" fn rust_html_tokenizer_insert_utf16_input(let Some(handle) = handle else {return;};- let code_units = input.as_slice();- handle.tokenizer.insert_input_at_insertion_point(code_units);+ handle+ .tokenizer+ .insert_input_at_insertion_point(InputSlice::Utf16(input.as_slice()));}-/// Insert ASCII input at the current insertion point.+/// Insert Latin-1 input at the current insertion point.#[unsafe(no_mangle)]pub extern "C" fn rust_html_tokenizer_insert_latin1_input(handle: Option<&mut RustFfiTokenizerHandle>,@@ -344,8 +343,9 @@ pub extern "C" fn rust_html_tokenizer_insert_latin1_input(let Some(handle) = handle else {return;};- let input = input.as_slice();- handle.tokenizer.insert_latin1_input_at_insertion_point(input);+ handle+ .tokenizer+ .insert_input_at_insertion_point(InputSlice::Latin1(input.as_slice()));}/// Append UTF-16 input to the tokenizer input stream.@@ -357,11 +357,10 @@ pub extern "C" fn rust_html_tokenizer_append_utf16_input(let Some(handle) = handle else {return;};- let code_units = input.as_slice();- handle.tokenizer.append_input(code_units);+ handle.tokenizer.append_input(InputSlice::Utf16(input.as_slice()));}-/// Append ASCII input to the tokenizer input stream.+/// Append Latin-1 input to the tokenizer input stream.#[unsafe(no_mangle)]pub extern "C" fn rust_html_tokenizer_append_latin1_input(handle: Option<&mut RustFfiTokenizerHandle>,@@ -370,8 +369,7 @@ pub extern "C" fn rust_html_tokenizer_append_latin1_input(let Some(handle) = handle else {return;};- let input = input.as_slice();- handle.tokenizer.append_latin1_input(input);+ handle.tokenizer.append_input(InputSlice::Latin1(input.as_slice()));}/// Get the tokenizer input that has not been consumed yet.@@ -384,7 +382,7 @@ pub extern "C" fn rust_html_tokenizer_unparsed_input(let (Some(handle), Some(out_ptr), Some(out_len)) = (handle, out_ptr, out_len) else {return;};- handle.last_unparsed_input = handle.tokenizer.unparsed_input().encode_utf16().collect();+ handle.last_unparsed_input = handle.tokenizer.unparsed_input().to_utf16();*out_ptr = handle.last_unparsed_input.as_ptr();*out_len = handle.last_unparsed_input.len();}diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/meta_charset_scanner.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/meta_charset_scanner.rsindex 8ce8aef0574..71cf660493f 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/meta_charset_scanner.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/meta_charset_scanner.rs@@ -7,6 +7,7 @@//! Finds a document's encoding declaration before the HTML parser starts, by tokenizing the document's first bytes as//! the parser will: the prescan proposed in https://github.com/whatwg/html/issues/6962.+use crate::input::InputSlice;use crate::known_names::attribute_name;use crate::known_names::tag_name;use crate::preload_scanner::tokenizer_state_after_start_tag;@@ -56,7 +57,7 @@ pub struct MetaCharsetScanner {impl MetaCharsetScanner {pub fn new(scripting_enabled: bool) -> Self {// The bytes arrive in pieces, so the end of the ones so far isn't the end of the input until the caller says so.- let mut tokenizer = HtmlTokenizer::new(Vec::new());+ let mut tokenizer = HtmlTokenizer::new(Vec::<u8>::new());tokenizer.set_input_stream_closed(false);Self {tokenizer,@@ -83,7 +84,7 @@ impl MetaCharsetScanner {}// Bytes before a declaration only ever matter as ASCII, which every encoding a declaration can name reads the// same way, so reading them as Latin-1 tokenizes them as the parser will.- self.tokenizer.append_latin1_input(bytes);+ self.tokenizer.append_input(InputSlice::Latin1(bytes));if input_complete {self.tokenizer.set_input_stream_closed(true);}diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser/tests.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser/tests.rsindex 2d38c2bbe04..f67157f477b 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/parser/tests.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser/tests.rs@@ -16,6 +16,7 @@ use super::test_host::FakeInsertion;use super::test_host::FakeInsertionCall;use super::test_host::FakeNodeKind;use crate::RustFfiTokenizerHandle;+use crate::input::InputSlice;use crate::make_handle;use crate::rust_html_tokenizer_destroy;use crate::tokenizer::HtmlTokenizer;@@ -32,7 +33,7 @@ impl ScriptCreatedParser {test_host::reset();let mut parser = Self {parser: rust_html_parser_create(),- tokenizer: make_handle(HtmlTokenizer::new(Vec::new())),+ tokenizer: make_handle(HtmlTokenizer::new(Vec::<u8>::new())),};parser.tokenizer().set_input_stream_closed(false);parser.tokenizer().update_insertion_point();@@ -59,7 +60,8 @@ impl ScriptCreatedParser {fn write(&mut self, markup: &str) {let code_units: Vec<u16> = markup.encode_utf16().collect();- self.tokenizer().insert_input_at_insertion_point(&code_units);+ self.tokenizer()+ .insert_input_at_insertion_point(InputSlice::Utf16(&code_units));self.run(true);}diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/preload_scanner.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/preload_scanner.rsindex c244e78e375..90476a43dc6 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/preload_scanner.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/preload_scanner.rs@@ -5,6 +5,7 @@*/use crate::RustFfiTokenizerHandle;+use crate::input::InputSlice;use crate::known_names::attribute_name;use crate::known_names::tag_name;use crate::token::Attribute;@@ -85,7 +86,7 @@ pub struct RustFfiPreloadScanner {impl RustFfiPreloadScanner {fn new(scripting_enabled: bool) -> Self {- let mut tokenizer = HtmlTokenizer::new(Vec::new());+ let mut tokenizer = HtmlTokenizer::new(Vec::<u8>::new());// More input can arrive, so the scanner waits at the end of what it has for the rest of a token.tokenizer.set_input_stream_closed(false);RustFfiPreloadScanner {@@ -100,7 +101,7 @@ impl RustFfiPreloadScanner {*self = Self::new(self.scripting_enabled);}- fn append_input(&mut self, code_units: &[u16]) {+ fn append_input(&mut self, code_units: InputSlice<'_>) {self.tokenizer.append_input(code_units);}@@ -209,7 +210,7 @@ pub(crate) fn scan_utf16(mut callback: impl FnMut(&RustFfiPreloadScannerEntry) -> bool,) {let mut scanner = RustFfiPreloadScanner::new(scripting_enabled);- scanner.append_input(input);+ scanner.append_input(InputSlice::Utf16(input));scanner.tokenizer.set_input_stream_closed(true);scanner.scan(&mut callback);}@@ -1027,13 +1028,17 @@ mod tests {let mut scanner = RustFfiPreloadScanner::new(true);// The input runs out inside a tag, and then inside the text of a script.- scanner.append_input(&code_units(r#"<img src="./a.png"><img sr"#));+ scanner.append_input(InputSlice::Utf16(&code_units(r#"<img src="./a.png"><img sr"#)));assert_eq!(scan_urls(&mut scanner), vec!["./a.png"]);- scanner.append_input(&code_units(r#"c="./b.png"><script>var trap = '<img src="./tr"#));+ scanner.append_input(InputSlice::Utf16(&code_units(+ r#"c="./b.png"><script>var trap = '<img src="./tr"#,+ )));assert_eq!(scan_urls(&mut scanner), vec!["./b.png"]);- scanner.append_input(&code_units(r#"ap.png">';</script><img src="./c.png">"#));+ scanner.append_input(InputSlice::Utf16(&code_units(+ r#"ap.png">';</script><img src="./c.png">"#,+ )));assert_eq!(scan_urls(&mut scanner), vec!["./c.png"]);}@@ -1043,26 +1048,26 @@ mod tests {parser_tokenizer.set_input_stream_closed(false);let (input, continues) = parser_tokenizer.input_for_speculative_scan();- assert_eq!(input, code_units("<img src=a>"));+ assert_eq!(input.to_utf16(), code_units("<img src=a>"));assert!(!continues);- parser_tokenizer.append_input(&code_units("<img src=b>"));+ parser_tokenizer.append_input(InputSlice::Utf16(&code_units("<img src=b>")));let (input, continues) = parser_tokenizer.input_for_speculative_scan();- assert_eq!(input, code_units("<img src=b>"));+ assert_eq!(input.to_utf16(), code_units("<img src=b>"));assert!(continues);// Once the parser gets past the scanned input, the scan starts over where the parser is.while parser_tokenizer.next_token(false, false).is_some() {}- parser_tokenizer.append_input(&code_units("<img src=c>"));+ parser_tokenizer.append_input(InputSlice::Utf16(&code_units("<img src=c>")));let (input, continues) = parser_tokenizer.input_for_speculative_scan();- assert_eq!(input, code_units("<img src=c>"));+ assert_eq!(input.to_utf16(), code_units("<img src=c>"));assert!(continues);- parser_tokenizer.append_input(&code_units("<img src=d><img src=e>"));+ parser_tokenizer.append_input(InputSlice::Utf16(&code_units("<img src=d><img src=e>")));while parser_tokenizer.next_token(false, false).is_some() {}parser_tokenizer.parser_did_run();- parser_tokenizer.append_input(&code_units("<img src=f>"));+ parser_tokenizer.append_input(InputSlice::Utf16(&code_units("<img src=f>")));let (input, continues) = parser_tokenizer.input_for_speculative_scan();- assert_eq!(input, code_units("<img src=f>"));+ assert_eq!(input.to_utf16(), code_units("<img src=f>"));assert!(!continues);}@@ -1075,13 +1080,13 @@ mod tests {parser_tokenizer.update_insertion_point();let (input, _) = parser_tokenizer.input_for_speculative_scan();- assert_eq!(input, code_units("<img src=a>"));+ assert_eq!(input.to_utf16(), code_units("<img src=a>"));// Input that document.write() inserts at the insertion point comes before the scanned input, so the scan has// to take it in too.- parser_tokenizer.insert_input_at_insertion_point(&code_units("<img src=w>"));+ parser_tokenizer.insert_input_at_insertion_point(InputSlice::Utf16(&code_units("<img src=w>")));let (input, continues) = parser_tokenizer.input_for_speculative_scan();- assert_eq!(input, code_units("<img src=w><img src=a>"));+ assert_eq!(input.to_utf16(), code_units("<img src=w><img src=a>"));assert!(!continues);}}diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rsindex 78331d1f04f..15ff6ee1bb8 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs@@ -7,6 +7,8 @@use std::collections::VecDeque;use crate::entities::NamedCharacterReferenceMatcher;+use crate::input::Input;+use crate::input::InputSlice;use crate::token::Attribute;use crate::token::DoctypeData;use crate::token::HtmlName;@@ -209,7 +211,7 @@ impl State {pub struct HtmlTokenizer {pub state: State,return_state: State,- pub input: Vec<u16>,+ pub input: Input,pub current_offset: usize,prev_offset: usize,current_token: Token,@@ -344,12 +346,12 @@ fn push_code_point(buf: &mut String, cp: u32) {}impl HtmlTokenizer {- /// Create a new tokenizer for the given UTF-16 input.- pub fn new(input: Vec<u16>) -> Self {+ /// Create a new tokenizer for the given input.+ pub fn new(input: impl Into<Input>) -> Self {HtmlTokenizer {state: State::Data,return_state: State::Data,- input,+ input: input.into(),current_offset: 0,prev_offset: 0,current_token: Token::default(),@@ -411,18 +413,14 @@ impl HtmlTokenizer {.is_some_and(|insertion_point| self.current_offset >= insertion_point)}- pub fn append_input(&mut self, code_units: &[u16]) {- self.input.extend_from_slice(code_units);+ pub fn append_input(&mut self, code_units: InputSlice<'_>) {+ self.input.append(code_units);}- pub fn append_latin1_input(&mut self, bytes: &[u8]) {- self.input.extend(bytes.iter().map(|byte| u16::from(*byte)));- }-- pub fn insert_input_at_insertion_point(&mut self, code_units: &[u16]) {+ pub fn insert_input_at_insertion_point(&mut self, code_units: InputSlice<'_>) {if let Some(ip) = self.insertion_point {let ip = ip.min(self.input.len());- self.input.splice(ip..ip, code_units.iter().copied());+ self.input.insert(ip, code_units);self.insertion_point = Some(ip + code_units.len());self.forget_speculative_scan_end_before(ip);for old_insertion_point in &mut self.old_insertion_points {@@ -435,36 +433,20 @@ impl HtmlTokenizer {}}- pub fn insert_latin1_input_at_insertion_point(&mut self, bytes: &[u8]) {- if let Some(ip) = self.insertion_point {- let ip = ip.min(self.input.len());- self.input.splice(ip..ip, bytes.iter().map(|byte| u16::from(*byte)));- self.insertion_point = Some(ip + bytes.len());- self.forget_speculative_scan_end_before(ip);- for old_insertion_point in &mut self.old_insertion_points {- if let Some(old_ip) = old_insertion_point- && ip <= *old_ip- {- *old_ip += bytes.len();- }- }- }- }-- pub fn unparsed_input(&self) -> String {- String::from_utf16_lossy(&self.input[self.current_offset..])+ pub fn unparsed_input(&self) -> InputSlice<'_> {+ self.input.slice_from(self.current_offset)}/// The input a speculative HTML parser's preload scanner hasn't been given yet, and whether the scanner can go on/// from where it stopped. It can't once this tokenizer has got past that point, or input was inserted before it;/// the input then runs from the current position, and the scanner has to start over there.- pub fn input_for_speculative_scan(&mut self) -> (&[u16], bool) {+ pub fn input_for_speculative_scan(&mut self) -> (InputSlice<'_>, bool) {let (start, continues) = match self.speculative_scan_end {Some(end) if end >= self.current_offset => (end, true),_ => (self.current_offset, false),};self.speculative_scan_end = Some(self.input.len());- (&self.input[start..], continues)+ (self.input.slice_from(start), continues)}/// Makes the next speculative scan start over at the current position.@@ -504,7 +486,7 @@ impl HtmlTokenizer {.speculative_scan_end.filter(|end| *end == self.input.len()).map(|_| 0);- self.input = Vec::new();+ self.input = Input::default();self.current_offset = 0;self.prev_offset = 0;let last_position = *self.source_positions.last().unwrap_or(&Position::default());@@ -546,10 +528,10 @@ impl HtmlTokenizer {#[inline(always)]fn code_point_at(&self, offset: usize) -> Option<(u32, usize)> {let limit = self.fast_scan_limit();- let first = *self.input.get(offset)?;+ let first = self.input.get(offset)?;if is_high_surrogate(first) {if offset + 1 < limit {- let second = self.input[offset + 1];+ let second = self.input.code_unit_at(offset + 1);if (0xDC00..=0xDFFF).contains(&second) {let code_point = 0x10000 + ((u32::from(first) - 0xD800) << 10) + (u32::from(second) - 0xDC00);return Some((code_point, 2));@@ -603,7 +585,7 @@ impl HtmlTokenizer {};for _ in 0..count {self.prev_offset = self.current_offset;- let code_point = self.input[self.current_offset];+ let code_point = self.input.code_unit_at(self.current_offset);debug_assert!(code_point < 0x80);if code_point == 0x0A {line += 1;@@ -671,7 +653,7 @@ impl HtmlTokenizer {self.sync_source_positions();return None;}- let cp = u32::from(self.input[self.current_offset]);+ let cp = u32::from(self.input.code_unit_at(self.current_offset));if cp == 0x3C || cp == 0x26 || cp == 0x00 || cp == 0x0D || cp >= 0x80 {self.sync_source_positions();return None;@@ -705,7 +687,7 @@ impl HtmlTokenizer {let mut offset = start_offset;let mut contains_non_whitespace = false;while offset < input_len {- let code_point = u32::from(self.input[offset]);+ let code_point = u32::from(self.input.code_unit_at(offset));if code_point == 0x3C|| code_point == 0x26|| code_point == 0x00@@ -725,7 +707,8 @@ impl HtmlTokenizer {}output.reserve(offset - start_offset);- for &code_point in &self.input[start_offset..offset] {+ for offset in start_offset..offset {+ let code_point = self.input.code_unit_at(offset);// SAFETY: The scan above only accepts ASCII code points.unsafe { output.as_mut_vec().push(code_point as u8) };if code_point == 0x0A {@@ -748,7 +731,7 @@ impl HtmlTokenizer {if self.current_offset >= self.fast_scan_limit() {return false;}- let code_point = u32::from(self.input[self.current_offset]);+ let code_point = u32::from(self.input.code_unit_at(self.current_offset));code_point != 0x3C && code_point != 0x26 && code_point != 0x00 && code_point != 0x0D && code_point < 0x80}@@ -945,7 +928,8 @@ impl HtmlTokenizer {}if self.current_offset < self.input.len()- && (self.input[self.current_offset] == 0x0D || is_high_surrogate(self.input[self.current_offset]))+ && (self.input.code_unit_at(self.current_offset) == 0x0D+ || is_high_surrogate(self.input.code_unit_at(self.current_offset)))&& self.current_offset + 1 == ip{return true;@@ -960,7 +944,8 @@ impl HtmlTokenizer {return true;}- (self.input[self.current_offset] == 0x0D || is_high_surrogate(self.input[self.current_offset]))+ (self.input.code_unit_at(self.current_offset) == 0x0D+ || is_high_surrogate(self.input.code_unit_at(self.current_offset)))&& self.current_offset + 1 >= self.input.len()}@@ -1228,7 +1213,7 @@ impl HtmlTokenizer {let mut off = start_offset;let input_len = self.fast_scan_limit();while off < input_len {- let next = self.input[off];+ let next = self.input.code_unit_at(off);if next < b'a' as u16 || next > b'z' as u16 {break;}@@ -1330,7 +1315,7 @@ impl HtmlTokenizer {let mut off = start_offset;let input_len = self.fast_scan_limit();while off < input_len {- let next = self.input[off];+ let next = self.input.code_unit_at(off);let ok = (next >= b'a' as u16 && next <= b'z' as u16)|| (next >= b'0' as u16 && next <= b'9' as u16)|| next == b'-' as u16@@ -1466,7 +1451,7 @@ impl HtmlTokenizer {let mut off = start_offset;let input_len = self.fast_scan_limit();while off < input_len {- let next = self.input[off];+ let next = self.input.code_unit_at(off);if !(0x20..0x80).contains(&next) || next == 0x22 || next == 0x26 {break;}@@ -2696,7 +2681,7 @@ impl HtmlTokenizer {}let limit = self.input.len();while self.current_offset < limit {- let cp = u32::from(self.input[self.current_offset]);+ let cp = u32::from(self.input.code_unit_at(self.current_offset));if !self.entity_matcher.try_consume_code_point(cp) {break;}@@ -3451,9 +3436,9 @@ mod tests {tokenizer.parser_did_run();assert_eq!(tokenizer.current_offset, tokenizer.input.len());- assert_eq!(tokenizer.input, code_units("<p a=\"&tw"));+ assert_eq!(tokenizer.input.to_utf16(), code_units("<p a=\"&tw"));- tokenizer.append_input(&code_units("s-checkout-success=4.1.0\">"));+ tokenizer.append_input(InputSlice::Utf16(&code_units("s-checkout-success=4.1.0\">")));tokenizer.set_input_stream_closed(true);let token = tokenizer.next_token(false, false).expect("start tag token");