Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Run rustfmt on everything #357

Merged
merged 2 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions html5ever/benches/html5ever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ extern crate html5ever;
use std::fs;
use std::path::PathBuf;

use criterion::{Criterion, black_box};
use criterion::{black_box, Criterion};

use html5ever::tokenizer::{BufferQueue, TokenSink, Token, Tokenizer, TokenizerOpts, TokenSinkResult};
use html5ever::tendril::*;
use html5ever::tokenizer::{
BufferQueue, Token, TokenSink, TokenSinkResult, Tokenizer, TokenizerOpts,
};

struct Sink;

Expand All @@ -23,7 +25,6 @@ impl TokenSink for Sink {
}
}


fn run_bench(c: &mut Criterion, name: &str) {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("data/bench/");
Expand All @@ -32,7 +33,9 @@ fn run_bench(c: &mut Criterion, name: &str) {

// Read the file and treat it as an infinitely repeating sequence of characters.
let mut file_input = ByteTendril::new();
file.read_to_tendril(&mut file_input).ok().expect("can't read file");
file.read_to_tendril(&mut file_input)
.ok()
.expect("can't read file");
let file_input: StrTendril = file_input.try_reinterpret().unwrap();
let size = file_input.len();
let mut stream = file_input.chars().cycle();
Expand All @@ -51,22 +54,22 @@ fn run_bench(c: &mut Criterion, name: &str) {

let test_name = format!("html tokenizing {}", name);

c.bench_function(&test_name, move |b| b.iter(|| {
let mut tok = Tokenizer::new(Sink, Default::default());
let mut buffer = BufferQueue::new();
// We are doing clone inside the bench function, this is not ideal, but possibly
// necessary since our iterator consumes the underlying buffer.
for buf in input.clone().into_iter() {
buffer.push_back(buf);
c.bench_function(&test_name, move |b| {
b.iter(|| {
let mut tok = Tokenizer::new(Sink, Default::default());
let mut buffer = BufferQueue::new();
// We are doing clone inside the bench function, this is not ideal, but possibly
// necessary since our iterator consumes the underlying buffer.
for buf in input.clone().into_iter() {
buffer.push_back(buf);
let _ = tok.feed(&mut buffer);
}
let _ = tok.feed(&mut buffer);
}
let _ = tok.feed(&mut buffer);
tok.end();
}));
tok.end();
})
});
}



fn html5ever_benchmark(c: &mut Criterion) {
run_bench(c, "lipsum.html");
run_bench(c, "lipsum-zh.html");
Expand All @@ -77,4 +80,4 @@ fn html5ever_benchmark(c: &mut Criterion) {
}

criterion_group!(benches, html5ever_benchmark);
criterion_main!(benches);
criterion_main!(benches);
15 changes: 10 additions & 5 deletions html5ever/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use] extern crate quote;
#[macro_use] extern crate syn;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
extern crate proc_macro2;

use std::env;
Expand All @@ -26,9 +28,12 @@ fn main() {
println!("cargo:rerun-if-changed={}", input.display());

// We have stack overflows on Servo's CI.
let handle = Builder::new().stack_size(128 * 1024 * 1024).spawn(move || {
match_token::expand(&input, &output);
}).unwrap();
let handle = Builder::new()
.stack_size(128 * 1024 * 1024)
.spawn(move || {
match_token::expand(&input, &output);
})
.unwrap();

handle.join().unwrap();
}
98 changes: 68 additions & 30 deletions html5ever/examples/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
extern crate html5ever;
extern crate typed_arena;

use html5ever::{parse_document, QualName, Attribute, ExpandedName};
use html5ever::tendril::{TendrilSink, StrTendril};
use html5ever::interface::tree_builder::{TreeSink, QuirksMode, NodeOrText, ElementFlags};
use html5ever::interface::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
use html5ever::tendril::{StrTendril, TendrilSink};
use html5ever::{parse_document, Attribute, ExpandedName, QualName};
use std::borrow::Cow;
use std::cell::{Cell, RefCell};
use std::collections::HashSet;
Expand All @@ -32,7 +32,9 @@ fn html5ever_parse_slice_into_arena<'a>(bytes: &[u8], arena: Arena<'a>) -> Ref<'
document: arena.alloc(Node::new(NodeData::Document)),
quirks_mode: QuirksMode::NoQuirks,
};
parse_document(sink, Default::default()).from_utf8().one(bytes)
parse_document(sink, Default::default())
.from_utf8()
.one(bytes)
}

type Arena<'arena> = &'arena typed_arena::Arena<Node<'arena>>;
Expand Down Expand Up @@ -131,7 +133,10 @@ impl<'arena> Node<'arena> {
new_sibling.next_sibling.set(Some(self));
if let Some(previous_sibling) = self.previous_sibling.take() {
new_sibling.previous_sibling.set(Some(previous_sibling));
debug_assert!(ptr::eq::<Node>(previous_sibling.next_sibling.get().unwrap(), self));
debug_assert!(ptr::eq::<Node>(
previous_sibling.next_sibling.get().unwrap(),
self
));
previous_sibling.next_sibling.set(Some(new_sibling));
} else if let Some(parent) = self.parent.get() {
debug_assert!(ptr::eq::<Node>(parent.first_child.get().unwrap(), self));
Expand All @@ -147,19 +152,26 @@ impl<'arena> Sink<'arena> {
}

fn append_common<P, A>(&self, child: NodeOrText<Ref<'arena>>, previous: P, append: A)
where P: FnOnce() -> Option<Ref<'arena>>,
A: FnOnce(Ref<'arena>),
where
P: FnOnce() -> Option<Ref<'arena>>,
A: FnOnce(Ref<'arena>),
{
let new_node = match child {
NodeOrText::AppendText(text) => {
// Append to an existing Text node if we have one.
if let Some(&Node { data: NodeData::Text { ref contents }, .. }) = previous() {
if let Some(&Node {
data: NodeData::Text { ref contents },
..
}) = previous()
{
Copy link
Contributor Author

@vimpunk vimpunk Nov 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not happy about the placement of some of the opening brackets, like the one here. Maybe I could manually adjust these cases.

contents.borrow_mut().push_tendril(&text);
return
return;
}
self.new_node(NodeData::Text { contents: RefCell::new(text) })
}
NodeOrText::AppendNode(node) => node
self.new_node(NodeData::Text {
contents: RefCell::new(text),
})
},
NodeOrText::AppendNode(node) => node,
};

append(new_node)
Expand Down Expand Up @@ -196,22 +208,35 @@ impl<'arena> TreeSink for Sink<'arena> {
}

fn get_template_contents(&mut self, target: &Ref<'arena>) -> Ref<'arena> {
if let NodeData::Element { template_contents: Some(ref contents), .. } = target.data {
if let NodeData::Element {
template_contents: Some(ref contents),
..
} = target.data
{
contents
} else {
panic!("not a template element!")
}
}

fn is_mathml_annotation_xml_integration_point(&self, target: &Ref<'arena>) -> bool {
if let NodeData::Element { mathml_annotation_xml_integration_point, .. } = target.data {
if let NodeData::Element {
mathml_annotation_xml_integration_point,
..
} = target.data
{
mathml_annotation_xml_integration_point
} else {
panic!("not an element!")
}
}

fn create_element(&mut self, name: QualName, attrs: Vec<Attribute>, flags: ElementFlags) -> Ref<'arena> {
fn create_element(
&mut self,
name: QualName,
attrs: Vec<Attribute>,
flags: ElementFlags,
) -> Ref<'arena> {
self.new_node(NodeData::Element {
name: name,
attrs: RefCell::new(attrs),
Expand All @@ -221,7 +246,6 @@ impl<'arena> TreeSink for Sink<'arena> {
None
},
mathml_annotation_xml_integration_point: flags.mathml_annotation_xml_integration_point,

})
}

Expand All @@ -230,42 +254,51 @@ impl<'arena> TreeSink for Sink<'arena> {
}

fn create_pi(&mut self, target: StrTendril, data: StrTendril) -> Ref<'arena> {
self.new_node(NodeData::ProcessingInstruction { target: target, contents: data })
self.new_node(NodeData::ProcessingInstruction {
target: target,
contents: data,
})
}

fn append(&mut self, parent: &Ref<'arena>, child: NodeOrText<Ref<'arena>>) {
self.append_common(
child,
|| parent.last_child.get(),
|new_node| parent.append(new_node)
|new_node| parent.append(new_node),
)
}

fn append_before_sibling(&mut self, sibling: &Ref<'arena>, child: NodeOrText<Ref<'arena>>) {
self.append_common(
child,
|| sibling.previous_sibling.get(),
|new_node| sibling.insert_before(new_node)
|new_node| sibling.insert_before(new_node),
)
}

fn append_based_on_parent_node(&mut self, element: &Ref<'arena>,
prev_element: &Ref<'arena>, child: NodeOrText<Ref<'arena>>) {
fn append_based_on_parent_node(
&mut self,
element: &Ref<'arena>,
prev_element: &Ref<'arena>,
child: NodeOrText<Ref<'arena>>,
) {
if element.parent.get().is_some() {
self.append_before_sibling(element, child)
} else {
self.append(prev_element, child)
}
}

fn append_doctype_to_document(&mut self,
name: StrTendril,
public_id: StrTendril,
system_id: StrTendril) {
fn append_doctype_to_document(
&mut self,
name: StrTendril,
public_id: StrTendril,
system_id: StrTendril,
) {
self.document.append(self.new_node(NodeData::Doctype {
name: name,
public_id: public_id,
system_id: system_id
system_id: system_id,
}))
}

Expand All @@ -276,10 +309,15 @@ impl<'arena> TreeSink for Sink<'arena> {
panic!("not an element")
};

let existing_names = existing.iter().map(|e| e.name.clone()).collect::<HashSet<_>>();
existing.extend(attrs.into_iter().filter(|attr| {
!existing_names.contains(&attr.name)
}));
let existing_names = existing
.iter()
.map(|e| e.name.clone())
.collect::<HashSet<_>>();
existing.extend(
attrs
.into_iter()
.filter(|attr| !existing_names.contains(&attr.name)),
);
}

fn remove_from_parent(&mut self, target: &Ref<'arena>) {
Expand Down
14 changes: 8 additions & 6 deletions html5ever/examples/html2html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@

extern crate html5ever;

use std::io::{self, Write};
use std::default::Default;
use std::io::{self, Write};


use html5ever::{parse_document, serialize};
use html5ever::driver::ParseOpts;
use html5ever::rcdom::RcDom;
use html5ever::tendril::TendrilSink;
use html5ever::tree_builder::TreeBuilderOpts;
use html5ever::{parse_document, serialize};

fn main() {
let opts = ParseOpts {
Expand All @@ -42,8 +41,11 @@ fn main() {
.unwrap();

// The validator.nu HTML2HTML always prints a doctype at the very beginning.
io::stdout().write_all(b"<!DOCTYPE html>\n")
.ok().expect("writing DOCTYPE failed");
io::stdout()
.write_all(b"<!DOCTYPE html>\n")
.ok()
.expect("writing DOCTYPE failed");
serialize(&mut io::stdout(), &dom.document, Default::default())
.ok().expect("serialization failed");
.ok()
.expect("serialization failed");
}
4 changes: 2 additions & 2 deletions html5ever/examples/noop-tokenize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

extern crate html5ever;

use std::io;
use std::default::Default;
use std::io;

use html5ever::tokenizer::{BufferQueue, TokenSinkResult, TokenSink, Token, Tokenizer};
use html5ever::tendril::*;
use html5ever::tokenizer::{BufferQueue, Token, TokenSink, TokenSinkResult, Tokenizer};

struct Sink(Vec<Token>);

Expand Down
Loading