Truly independent web browser
LibWeb: Read a plainly written HTML tag in one go
Andreas Kling02e9e1dd0220
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/code_units.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/code_units.rsindex fe40159f8f9..d17956877b9 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/code_units.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/code_units.rs@@ -229,6 +229,13 @@ impl CodeUnits {(Self::Utf16(string), Utf16View::Latin1(code_units)) => {string.extend(code_units.iter().copied().map(u16::from));}+ // NB: Input stored as UTF-16 is mostly code units that fit in a byte, which keeps this at a byte each+ // and in the buffer it already has.+ (Self::Latin1(string), Utf16View::Utf16(code_units))+ if code_units.iter().all(|&code_unit| code_unit <= 0xFF) =>+ {+ string.extend(code_units.iter().map(|&code_unit| code_unit as u8));+ }(Self::Latin1(string), Utf16View::Utf16(code_units)) => {let mut widened = Vec::with_capacity(string.len() + code_units.len());widened.extend(string.iter().copied().map(u16::from));diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/fragment.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/fragment.rsindex 97efcc52c48..3012cf1dac7 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/fragment.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/fragment.rs@@ -189,15 +189,6 @@ fn equals_ascii<T: CodeUnit>(code_units: &[T], ascii: &[u8]) -> bool {.all(|(&code_unit, &byte)| code_unit.into() == u16::from(byte))}-/// The bytes of a name the scanner has read, which is ASCII, if it fits in `buffer`.-fn name_bytes<'a, T: CodeUnit>(name: &[T], buffer: &'a mut [u8; 32]) -> Option<&'a [u8]> {- let bytes = buffer.get_mut(..name.len())?;- for (byte, &code_unit) in bytes.iter_mut().zip(name) {- *byte = code_unit.into() as u8;- }- Some(bytes)-}-fn scan<T: CodeUnit>(input: &[T], tape: &mut RustHtmlFragmentTape) -> bool {tape.operations.clear();tape.attributes.clear();@@ -410,7 +401,7 @@ impl<'a, T: CodeUnit> Scanner<'a, T> {fn scan_supported_tag(&mut self) -> Option<TagId> {let name = self.scan_name();- let tag = tag_id_for_name(name_bytes(name, &mut [0; 32])?);+ let tag = tag_id_for_name(scan::ascii_name_bytes(name, &mut [0; 32])?);(tag != TagId::UNKNOWN && SUPPORTED_TAGS.contains(tag)).then_some(tag)}@@ -460,14 +451,18 @@ impl<'a, T: CodeUnit> Scanner<'a, T> {fn known_attribute_name_identity<T: CodeUnit>(name: &[T]) -> usize {#[cfg(not(test))]{- name_bytes(name, &mut [0; 32])+ scan::ascii_name_bytes(name, &mut [0; 32]).and_then(known_attribute_name).map_or(0, |name| name.raw_identity())}// NB: There are no fly strings in the standalone tests, which only tell known names from other names.#[cfg(test)]{- usize::from(name_bytes(name, &mut [0; 32]).and_then(known_attribute_name).is_some())+ usize::from(+ scan::ascii_name_bytes(name, &mut [0; 32])+ .and_then(known_attribute_name)+ .is_some(),+ )}}diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/scan.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/scan.rsindex 35a8cdd480f..8ccd738281f 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/scan.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/scan.rs@@ -14,6 +14,7 @@//! which is the shape the compiler turns into vector instructions for whatever the target CPU has.use crate::token::Position;+use ak::Utf16View;/// A code unit of the input stream, in either of the forms it is stored in.pub trait CodeUnit: Copy + Default + Eq + Into<u16> + From<u8> {@@ -28,6 +29,8 @@ pub trait CodeUnit: Copy + Default + Eq + Into<u16> + From<u8> {/// # Safety/// `BLOCK_SIZE` code units must be readable at `code_units`.unsafe fn surrogates_in_block(code_units: *const Self) -> u32;++ fn view(code_units: &[Self]) -> Utf16View<'_>;}impl CodeUnit for u8 {@@ -40,6 +43,11 @@ impl CodeUnit for u8 {unsafe fn surrogates_in_block(_: *const u8) -> u32 {0}++ #[inline(always)]+ fn view(code_units: &[u8]) -> Utf16View<'_> {+ Utf16View::Latin1(code_units)+ }}impl CodeUnit for u16 {@@ -52,6 +60,11 @@ impl CodeUnit for u16 {unsafe fn surrogates_in_block(code_units: *const u16) -> u32 {unsafe { block::surrogates(code_units) }}++ #[inline(always)]+ fn view(code_units: &[u16]) -> Utf16View<'_> {+ Utf16View::Utf16(code_units)+ }}use block::BLOCK_SIZE;@@ -245,6 +258,16 @@ fn find_first<T: CodeUnit, const N: usize>(code_units: &[T], stops: [u8; N], seeoffset + stop as usize}+/// The bytes of `name`, which has to be ASCII, if they fit in `buffer`.+#[inline(always)]+pub fn ascii_name_bytes<'a, T: CodeUnit, const N: usize>(name: &[T], buffer: &'a mut [u8; N]) -> Option<&'a [u8]> {+ let bytes = buffer.get_mut(..name.len())?;+ for (byte, &code_unit) in bytes.iter_mut().zip(name) {+ *byte = code_unit.into() as u8;+ }+ Some(bytes)+}+/// A run of code units at the start of some input.#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub struct Run {diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rsindex 6ce3df6f753..d22f49fdead 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs@@ -278,6 +278,134 @@ fn to_ascii_lowercase(c: u32) -> u32 {if is_ascii_upper_alpha(c) { c + 0x20 } else { c }}+/// Reads the tag whose "<" ends right before `offset` into `token`, and returns the offset after its ">", if the tag+/// is as plain as `HtmlTokenizer::try_scan_tag` describes and all of it is in `input`.+fn scan_tag<T: scan::CodeUnit>(input: &[T], mut offset: usize, token: &mut Token) -> Option<usize> {+ // NB: U+0000 NULL, which nothing here takes, stands in for the end of the input.+ let at = |offset: usize| -> u16 { input.get(offset).map_or(0, |&code_unit| code_unit.into()) };+ let is_tag_whitespace = |code_unit: u16| matches!(code_unit, 0x09 | 0x0A | 0x0C | 0x20);+ let is_tag_end = |code_unit: u16| matches!(code_unit, 0x09 | 0x0A | 0x0C | 0x20 | 0x2F | 0x3E);++ let is_end_tag = at(offset) == 0x2F;+ if is_end_tag {+ offset += 1;+ }++ let name_start = offset;+ if !matches!(at(offset), 0x61..=0x7A) {+ return None;+ }+ offset += 1;+ while matches!(at(offset), 0x61..=0x7A | 0x30..=0x39 | 0x2D) {+ offset += 1;+ }+ if !is_tag_end(at(offset)) {+ return None;+ }+ let mut name_buffer = [0; 64];+ let name = Utf16View::Latin1(scan::ascii_name_bytes(&input[name_start..offset], &mut name_buffer)?);+ *token = Token {+ token_type: if is_end_tag {+ TokenType::EndTag+ } else {+ TokenType::StartTag+ },+ code_point: 0,+ payload: TokenPayload::Tag {+ tag_name: HtmlName::from_code_units(name),+ tag_id: crate::token::TagId::for_code_units(name),+ self_closing: false,+ had_duplicate_attribute: false,+ attributes: crate::token::AttributeList::new(),+ },+ start_offset: SourceOffset::new(name_start),+ end_offset: SourceOffset::NONE,+ };+ let TokenPayload::Tag {+ attributes,+ self_closing,+ ..+ } = &mut token.payload+ else {+ unreachable!();+ };++ loop {+ let after_whitespace = offset;+ while is_tag_whitespace(at(offset)) {+ offset += 1;+ }+ match at(offset) {+ 0x3E => return Some(offset + 1),+ 0x2F if !is_end_tag && at(offset + 1) == 0x3E => {+ *self_closing = true;+ return Some(offset + 2);+ }+ _ => {}+ }+ // NB: An attribute comes after whitespace, and an end tag with attributes is a parse error.+ if is_end_tag || offset == after_whitespace {+ return None;+ }++ let name_start = offset;+ while matches!(at(offset), 0x61..=0x7A | 0x30..=0x39 | 0x2D | 0x2E | 0x3A | 0x5F) {+ offset += 1;+ }+ if offset == name_start || !(is_tag_end(at(offset)) || at(offset) == 0x3D) {+ return None;+ }+ let name = Utf16View::Latin1(scan::ascii_name_bytes(&input[name_start..offset], &mut name_buffer)?);+ attributes.start_new_attribute();+ let attribute = attributes.last_mut().unwrap();+ attribute.local_name = HtmlName::from_code_units(name);+ attribute.name_start_offset = SourceOffset::new(name_start);+ attribute.name_end_offset = SourceOffset::new(offset);++ let mut after_name = offset;+ while is_tag_whitespace(at(after_name)) {+ after_name += 1;+ }+ if at(after_name) != 0x3D {+ // NB: The attribute has no value, and the whitespace after it is that before what comes next.+ continue;+ }+ offset = after_name + 1;+ while is_tag_whitespace(at(offset)) {+ offset += 1;+ }++ let quote = at(offset);+ attribute.value_start_offset = SourceOffset::new(offset);+ if quote == 0x22 || quote == 0x27 {+ offset += 1;+ let length = scan::attribute_value_run(&input[offset..], quote as u8);+ if at(offset + length) != quote {+ return None;+ }+ attribute.value.push_slice(T::view(&input[offset..offset + length]));+ offset += length + 1;+ if !is_tag_end(at(offset)) {+ return None;+ }+ } else {+ let value_start = offset;+ loop {+ match at(offset) {+ 0x09 | 0x0A | 0x0C | 0x20 | 0x3E => break,+ 0x00 | 0x0D | 0x22 | 0x26 | 0x27 | 0x3C | 0x3D | 0x60 | 0xD800..=0xDFFF => return None,+ _ => offset += 1,+ }+ }+ if offset == value_start {+ return None;+ }+ attribute.value.push_slice(T::view(&input[value_start..offset]));+ }+ attribute.value_end_offset = SourceOffset::new(offset);+ }+}+#[inline]fn is_whitespace(c: u32) -> bool {matches!(c, 0x09 | 0x0A | 0x0C | 0x20)@@ -784,6 +912,27 @@ impl HtmlTokenizer {self.current_offset = offset;}+ /// OPTIMIZATION: Most tags are written the same plain way: a lowercase name, attributes with lowercase names and+ /// values that hold no character references, and nothing out of place. Such a tag is read here in+ /// one go, right after its "<" in the data state, into the token that the tag open state and the+ /// states after it would have made of it one code point at a time. Any other tag is left to them,+ /// with nothing consumed.+ fn try_scan_tag(&mut self) -> bool {+ let limit = self.fast_scan_limit();+ let offset = self.current_offset;+ let end = match &self.input {+ CodeUnits::Latin1(code_units) => scan_tag(&code_units[..limit], offset, &mut self.current_token),+ CodeUnits::Utf16(code_units) => scan_tag(&code_units[..limit], offset, &mut self.current_token),+ };+ let Some(end) = end else {+ self.current_token = Token::default();+ return false;+ };+ self.prev_offset = end - 1;+ self.current_offset = end;+ true+ }+fn consume_current_builder(&mut self) -> CodeUnits {self.current_builder.take()}@@ -1052,6 +1201,9 @@ impl HtmlTokenizer {}Some(0x3C) => {// '<'+ if self.try_scan_tag() {+ return self.emit_current_token_direct();+ }self.state = State::TagOpen;continue;}