Skip to content

Commit

Permalink
Formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdm committed Aug 7, 2024
1 parent 6a60698 commit 6165801
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 70 deletions.
4 changes: 3 additions & 1 deletion html5ever/examples/tokenize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ impl TokenSink for TokenPrinter {
/// In this example we implement the TokenSink trait in such a way that each token is printed.
/// If a there's an error while processing a token it is printed as well.
fn main() {
let sink = TokenPrinter { in_char_run: Cell::new(false) };
let sink = TokenPrinter {
in_char_run: Cell::new(false),
};

// Read HTML from standard input
let mut chunk = ByteTendril::new();
Expand Down
2 changes: 1 addition & 1 deletion html5ever/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<Sink: TreeSink> TendrilSink<tendril::fmt::UTF8> for Parser<Sink> {

type Output = Sink::Output;

fn finish(mut self) -> Self::Output {
fn finish(self) -> Self::Output {
// FIXME: Properly support </script> somehow.
while let TokenizerResult::Script(_) = self.tokenizer.feed(&self.input_buffer) {}
assert!(self.input_buffer.is_empty());
Expand Down
10 changes: 4 additions & 6 deletions html5ever/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
// except according to those terms.

macro_rules! unwrap_or_else {
($opt:expr, $else_block:block) => {
{
let Some(x) = $opt else { $else_block };
x
}
};
($opt:expr, $else_block:block) => {{
let Some(x) = $opt else { $else_block };
x
}};
}

macro_rules! unwrap_or_return {
Expand Down
32 changes: 16 additions & 16 deletions html5ever/src/tokenizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ impl<Sink: TokenSink> Tokenizer<Sink> {
if self.reconsume.get() {
self.reconsume.set(false);
Some(self.current_char.get())

} else {
input
.next()
Expand Down Expand Up @@ -321,12 +320,7 @@ impl<Sink: TokenSink> Tokenizer<Sink> {
// BufferQueue::eat.
//
// NB: this doesn't set the current input character.
fn eat(
&self,
input: &BufferQueue,
pat: &str,
eq: fn(&u8, &u8) -> bool,
) -> Option<bool> {
fn eat(&self, input: &BufferQueue, pat: &str, eq: fn(&u8, &u8) -> bool) -> Option<bool> {
if self.ignore_lf.get() {
self.ignore_lf.set(false);
if self.peek(input) == Some('\n') {
Expand Down Expand Up @@ -491,7 +485,10 @@ impl<Sink: TokenSink> Tokenizer<Sink> {

fn have_appropriate_end_tag(&self) -> bool {
match self.last_start_tag_name.borrow().as_ref() {
Some(last) => (self.current_tag_kind.get() == EndTag) && (**self.current_tag_name.borrow() == **last),
Some(last) => {
(self.current_tag_kind.get() == EndTag)
&& (**self.current_tag_name.borrow() == **last)
},
None => false,
}
}
Expand Down Expand Up @@ -1469,16 +1466,23 @@ impl<Sink: TokenSink> Tokenizer<Sink> {
}

fn dump_profile(&self) {
let mut results: Vec<(states::State, u64)> =
self.state_profile.borrow().iter().map(|(s, t)| (*s, *t)).collect();
let mut results: Vec<(states::State, u64)> = self
.state_profile
.borrow()
.iter()
.map(|(s, t)| (*s, *t))
.collect();
results.sort_by(|&(_, x), &(_, y)| y.cmp(&x));

let total: u64 = results
.iter()
.map(|&(_, t)| t)
.fold(0, ::std::ops::Add::add);
println!("\nTokenizer profile, in nanoseconds");
println!("\n{:12} total in token sink", self.time_in_sink.get());
println!(
"\n{:12} total in token sink",
self.time_in_sink.get()
);
println!("\n{:12} total in tokenizer", total);

for (k, v) in results.into_iter() {
Expand Down Expand Up @@ -1629,11 +1633,7 @@ mod test {
impl TokenSink for LinesMatch {
type Handle = ();

fn process_token(
&self,
token: Token,
line_number: u64,
) -> TokenSinkResult<Self::Handle> {
fn process_token(&self, token: Token, line_number: u64) -> TokenSinkResult<Self::Handle> {
match token {
CharacterTokens(b) => {
self.current_str.borrow_mut().push_slice(&b);
Expand Down
61 changes: 41 additions & 20 deletions html5ever/src/tree_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::tokenizer::states as tok_state;
use crate::tokenizer::{Doctype, EndTag, StartTag, Tag, TokenSink, TokenSinkResult};

use std::borrow::Cow::Borrowed;
use std::cell::{Cell, RefCell, Ref, RefMut};
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::VecDeque;
use std::iter::{Enumerate, Rev};
use std::mem;
Expand Down Expand Up @@ -274,9 +274,18 @@ where
_ => (),
}
}
self.head_elem.borrow().as_ref().map(|h| tracer.trace_handle(h));
self.form_elem.borrow().as_ref().map(|h| tracer.trace_handle(h));
self.context_elem.borrow().as_ref().map(|h| tracer.trace_handle(h));
self.head_elem
.borrow()
.as_ref()
.map(|h| tracer.trace_handle(h));
self.form_elem
.borrow()
.as_ref()
.map(|h| tracer.trace_handle(h));
self.context_elem
.borrow()
.as_ref()
.map(|h| tracer.trace_handle(h));
}

#[allow(dead_code)]
Expand Down Expand Up @@ -451,11 +460,7 @@ where
{
type Handle = Handle;

fn process_token(
&self,
token: tokenizer::Token,
line_number: u64,
) -> TokenSinkResult<Handle> {
fn process_token(&self, token: tokenizer::Token, line_number: u64) -> TokenSinkResult<Handle> {
if line_number != self.current_line.get() {
self.sink.set_current_line(line_number);
}
Expand Down Expand Up @@ -546,10 +551,10 @@ struct ActiveFormattingView<'a, Handle: 'a> {
data: Ref<'a, Vec<FormatEntry<Handle>>>,
}

impl <'a, Handle: 'a> ActiveFormattingView<'a, Handle> {
impl<'a, Handle: 'a> ActiveFormattingView<'a, Handle> {
fn iter(&'a self) -> impl Iterator<Item = (usize, &'a Handle, &'a Tag)> + 'a {
ActiveFormattingIter {
iter: self.data.iter().enumerate().rev()
iter: self.data.iter().enumerate().rev(),
}
}
}
Expand Down Expand Up @@ -625,10 +630,13 @@ where
}

fn position_in_active_formatting(&self, element: &Handle) -> Option<usize> {
self.active_formatting.borrow().iter().position(|n| match n {
&Marker => false,
&Element(ref handle, _) => self.sink.same_node(handle, element),
})
self.active_formatting
.borrow()
.iter()
.position(|n| match n {
&Marker => false,
&Element(ref handle, _) => self.sink.same_node(handle, element),
})
}

fn set_quirks_mode(&self, mode: QuirksMode) {
Expand Down Expand Up @@ -659,7 +667,9 @@ where
//§ END

fn current_node(&self) -> Ref<Handle> {
Ref::map(self.open_elems.borrow(), |elems| elems.last().expect("no current element"))
Ref::map(self.open_elems.borrow(), |elems| {
elems.last().expect("no current element")
})
}

fn adjusted_current_node(&self) -> Ref<Handle> {
Expand Down Expand Up @@ -819,7 +829,8 @@ where
tag.attrs.clone(),
);
self.open_elems.borrow_mut()[node_index] = new_element.clone();
self.active_formatting.borrow_mut()[node_formatting_index] = Element(new_element.clone(), tag);
self.active_formatting.borrow_mut()[node_formatting_index] =
Element(new_element.clone(), tag);
node = new_element;

// 13.8.
Expand Down Expand Up @@ -902,7 +913,11 @@ where
}

fn pop(&self) -> Handle {
let elem = self.open_elems.borrow_mut().pop().expect("no current element");
let elem = self
.open_elems
.borrow_mut()
.pop()
.expect("no current element");
self.sink.pop(&elem);
elem
}
Expand Down Expand Up @@ -1386,7 +1401,9 @@ where
}

let elem = self.insert_element(Push, ns!(html), tag.name.clone(), tag.attrs.clone());
self.active_formatting.borrow_mut().push(Element(elem.clone(), tag));
self.active_formatting
.borrow_mut()
.push(Element(elem.clone(), tag));
elem
}

Expand Down Expand Up @@ -1671,7 +1688,11 @@ where
}

fn foreign_start_tag(&self, mut tag: Tag) -> ProcessResult<Handle> {
let current_ns = self.sink.elem_name(&self.adjusted_current_node()).ns.clone();
let current_ns = self
.sink
.elem_name(&self.adjusted_current_node())
.ns
.clone();
match current_ns {
ns!(mathml) => self.adjust_mathml_attributes(&mut tag),
ns!(svg) => {
Expand Down
5 changes: 4 additions & 1 deletion markup5ever/util/buffer_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ impl BufferQueue {
}

pub fn swap_with(&self, other: &BufferQueue) {
mem::swap(&mut *self.buffers.borrow_mut(), &mut *other.buffers.borrow_mut());
mem::swap(
&mut *self.buffers.borrow_mut(),
&mut *other.buffers.borrow_mut(),
);
}
}

Expand Down
7 changes: 1 addition & 6 deletions rcdom/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,7 @@ impl TreeSink for RcDom {
};
}

fn create_element(
&self,
name: QualName,
attrs: Vec<Attribute>,
flags: ElementFlags,
) -> Handle {
fn create_element(&self, name: QualName, attrs: Vec<Attribute>, flags: ElementFlags) -> Handle {
Node::new(NodeData::Element {
name,
attrs: RefCell::new(attrs),
Expand Down
11 changes: 4 additions & 7 deletions rcdom/tests/html-tree-sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ impl TreeSink for LineCountingDOM {
self.rcdom.elem_name(target)
}

fn create_element(
&self,
name: QualName,
attrs: Vec<Attribute>,
flags: ElementFlags,
) -> Handle {
self.line_vec.borrow_mut().push((name.clone(), self.current_line.get()));
fn create_element(&self, name: QualName, attrs: Vec<Attribute>, flags: ElementFlags) -> Handle {
self.line_vec
.borrow_mut()
.push((name.clone(), self.current_line.get()));
self.rcdom.create_element(name, attrs, flags)
}

Expand Down
2 changes: 1 addition & 1 deletion rcdom/tests/xml-tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
use serde_json::{Map, Value};
use std::borrow::Cow::Borrowed;
use std::cell::RefCell;
use std::env;
use std::ffi::OsStr;
use std::io::Read;
use std::path::Path;
use std::env;

use util::find_tests::foreach_xml5lib_test;
use util::runner::Test;
Expand Down
4 changes: 3 additions & 1 deletion xml5ever/examples/xml_tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ impl TokenSink for TokenPrinter {
}

fn main() {
let sink = TokenPrinter { in_char_run: Cell::new(false) };
let sink = TokenPrinter {
in_char_run: Cell::new(false),
};
let mut input = ByteTendril::new();
io::stdin().read_to_tendril(&mut input).unwrap();
let input_buffer = BufferQueue::default();
Expand Down
26 changes: 20 additions & 6 deletions xml5ever/src/tokenizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,10 @@ impl<Sink: TokenSink> XmlTokenizer<Sink> {
fn emit_current_tag(&self) {
self.finish_attribute();

let qname = process_qname(replace(&mut *self.current_tag_name.borrow_mut(), StrTendril::new()));
let qname = process_qname(replace(
&mut *self.current_tag_name.borrow_mut(),
StrTendril::new(),
));

match self.current_tag_kind.get() {
StartTag | EmptyTag => {},
Expand Down Expand Up @@ -463,7 +466,8 @@ impl<Sink: TokenSink> XmlTokenizer<Sink> {
fn consume_char_ref(&self, addnl_allowed: Option<char>) {
// NB: The char ref tokenizer assumes we have an additional allowed
// character iff we're tokenizing in an attribute value.
*self.char_ref_tokenizer.borrow_mut() = Some(Box::new(CharRefTokenizer::new(addnl_allowed)));
*self.char_ref_tokenizer.borrow_mut() =
Some(Box::new(CharRefTokenizer::new(addnl_allowed)));
}

fn emit_eof(&self) {
Expand Down Expand Up @@ -1116,16 +1120,23 @@ impl<Sink: TokenSink> XmlTokenizer<Sink> {

#[cfg(not(for_c))]
fn dump_profile(&self) {
let mut results: Vec<(states::XmlState, u64)> =
self.state_profile.borrow().iter().map(|(s, t)| (*s, *t)).collect();
let mut results: Vec<(states::XmlState, u64)> = self
.state_profile
.borrow()
.iter()
.map(|(s, t)| (*s, *t))
.collect();
results.sort_by(|&(_, x), &(_, y)| y.cmp(&x));

let total: u64 = results
.iter()
.map(|&(_, t)| t)
.fold(0, ::std::ops::Add::add);
debug!("\nTokenizer profile, in nanoseconds");
debug!("\n{:12} total in token sink", self.time_in_sink.get());
debug!(
"\n{:12} total in token sink",
self.time_in_sink.get()
);
debug!("\n{:12} total in tokenizer", total);

for (k, v) in results.into_iter() {
Expand Down Expand Up @@ -1249,7 +1260,10 @@ impl<Sink: TokenSink> XmlTokenizer<Sink> {
self.current_attr_name.borrow_mut().clear();
self.current_attr_value.borrow_mut().clear();
} else {
let qname = process_qname(replace(&mut self.current_attr_name.borrow_mut(), StrTendril::new()));
let qname = process_qname(replace(
&mut self.current_attr_name.borrow_mut(),
StrTendril::new(),
));
let attr = Attribute {
name: qname.clone(),
value: replace(&mut self.current_attr_value.borrow_mut(), StrTendril::new()),
Expand Down
Loading

0 comments on commit 6165801

Please sign in to comment.