Skip to content

Commit

Permalink
Send-parameterizable rewriter.
Browse files Browse the repository at this point in the history
  • Loading branch information
orium committed Sep 24, 2024
1 parent 384f617 commit 5feceab
Show file tree
Hide file tree
Showing 30 changed files with 985 additions and 277 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ include = [
autotests = false
edition = "2021"

[lib]
# Disable libtest to make sure criterion can parse the command line flags.
# See https://bheisler.github.io/criterion.rs/book/faq.html and https://github.com/rust-lang/rust/issues/47241.
bench = false

[features]
debug_trace = []
integration_test = []
Expand Down Expand Up @@ -55,6 +60,7 @@ hashbrown = { version = "0.13.1", features = ["serde"] }
serde = "1.0.126"
serde_derive = "1.0.19"
serde_json = "1.0.65"
static_assertions = "1.1.0"
rand = "0.8.5"
rustc-test = "0.3.1"
itertools = "0.10.1"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
})
],
..Settings::default()
..Settings::new()
},
|c: &[u8]| output.extend_from_slice(c)
);
Expand Down
6 changes: 3 additions & 3 deletions benches/cases/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use lol_html::*;
define_group!(
"Parsing",
[
("Tag scanner", Settings::default()),
("Tag scanner", Settings::new()),
(
"Lexer",
// NOTE: this switches parser to the lexer mode and doesn't
// trigger token production for anything, except doctype. So,
// we can get relatively fair comparison.
Settings {
document_content_handlers: vec![doctype!(noop_handler!())],
..Settings::default()
..Settings::new()
}
),
(
Expand All @@ -23,7 +23,7 @@ define_group!(
// incoming chunks to produce correct text chunk rewritable units.
Settings {
document_content_handlers: vec![doc_text!(noop_handler!())],
..Settings::default()
..Settings::new()
}
)
]
Expand Down
4 changes: 2 additions & 2 deletions benches/cases/rewriting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ define_group!(

Ok(())
})],
..Settings::default()
..Settings::new()
}
),
(
Expand All @@ -24,7 +24,7 @@ define_group!(

Ok(())
})],
..Settings::default()
..Settings::new()
}
)
]
Expand Down
10 changes: 5 additions & 5 deletions benches/cases/selector_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ define_group!(
"Match-all selector",
Settings {
element_content_handlers: vec![element!("*", noop_handler!())],
..Settings::default()
..Settings::new()
}
),
(
"Tag name selector",
Settings {
element_content_handlers: vec![element!("div", noop_handler!())],
..Settings::default()
..Settings::new()
}
),
(
"Class selector",
Settings {
element_content_handlers: vec![element!(".note", noop_handler!())],
..Settings::default()
..Settings::new()
}
),
(
"Attribute selector",
Settings {
element_content_handlers: vec![element!("[href]", noop_handler!())],
..Settings::default()
..Settings::new()
}
),
(
Expand All @@ -43,7 +43,7 @@ define_group!(
element!("div img", noop_handler!()),
element!("div.note span", noop_handler!())
],
..Settings::default()
..Settings::new()
}
)
]
Expand Down
18 changes: 9 additions & 9 deletions c-api/src/rewriter_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<F> ExternHandler<F> {
}

macro_rules! add_handler {
($handlers:ident, $self:ident.$ty:ident) => {{
($handlers:ident, $el_ty:ident, $self:ident.$ty:ident) => {{
if let Some(handler) = $self.$ty.func {
// NOTE: the closure actually holds a reference to the content
// handler object, but since we pass the object to the C side this
Expand All @@ -41,7 +41,7 @@ macro_rules! add_handler {

$handlers =
$handlers.$ty(
move |arg: &mut _| match unsafe { handler(arg, user_data) } {
move |arg: &mut $el_ty| match unsafe { handler(arg, user_data) } {
RewriterDirective::Continue => Ok(()),
RewriterDirective::Stop => Err("The rewriter has been stopped.".into()),
},
Expand All @@ -61,10 +61,10 @@ impl ExternDocumentContentHandlers {
pub fn as_safe_document_content_handlers(&self) -> DocumentContentHandlers {
let mut handlers = DocumentContentHandlers::default();

add_handler!(handlers, self.doctype);
add_handler!(handlers, self.comments);
add_handler!(handlers, self.text);
add_handler!(handlers, self.end);
add_handler!(handlers, Doctype, self.doctype);
add_handler!(handlers, Comment, self.comments);
add_handler!(handlers, TextChunk, self.text);
add_handler!(handlers, DocumentEnd, self.end);

handlers
}
Expand All @@ -80,9 +80,9 @@ impl ExternElementContentHandlers {
pub fn as_safe_element_content_handlers(&self) -> ElementContentHandlers {
let mut handlers = ElementContentHandlers::default();

add_handler!(handlers, self.element);
add_handler!(handlers, self.comments);
add_handler!(handlers, self.text);
add_handler!(handlers, Element, self.element);
add_handler!(handlers, Comment, self.comments);
add_handler!(handlers, TextChunk, self.text);

handlers
}
Expand Down
2 changes: 1 addition & 1 deletion examples/defer_scripts/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
Ok(())
}
)],
..Settings::default()
..Settings::new()
},
output_sink,
);
Expand Down
2 changes: 1 addition & 1 deletion examples/mixed_content_rewriter/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn main() {
}
),
],
..Settings::default()
..Settings::new()
},
output_sink,
);
Expand Down
8 changes: 3 additions & 5 deletions fuzz/test_case/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use std::ffi::{CStr, CString};

use encoding_rs::*;
use lol_html::html_content::ContentType;
use lol_html::{
comments, doc_comments, doc_text, element, text, HtmlRewriter, MemorySettings, Settings,
};
use lol_html::{comments, doc_comments, doc_text, element, text, HtmlRewriter, MemorySettings, Settings};

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

Expand Down Expand Up @@ -103,7 +101,7 @@ fn get_random_selector() -> &'static str {
}

fn run_rewriter_iter(data: &[u8], selector: &str, encoding: &'static Encoding) -> () {
let mut rewriter = HtmlRewriter::new(
let mut rewriter: HtmlRewriter<_> = HtmlRewriter::new(
Settings {
enable_esi_tags: true,
element_content_handlers: vec![
Expand Down Expand Up @@ -178,7 +176,7 @@ fn run_rewriter_iter(data: &[u8], selector: &str, encoding: &'static Encoding) -
}),
],
encoding: encoding.try_into().unwrap(),
memory_settings: MemorySettings::default(),
memory_settings: MemorySettings::new(),
strict: false,
adjust_charset_on_meta_tag: false,
},
Expand Down
Loading

0 comments on commit 5feceab

Please sign in to comment.