Minify whitespace between elements

Signed-off-by: Evan Lloyd New-Schmidt <evan@new-schmidt.com>
This commit is contained in:
Evan Lloyd New-Schmidt 2023-08-15 16:19:16 -04:00 committed by Evan Lloyd New-Schmidt
parent 81783695d5
commit 3d3ecb52b2

View file

@ -134,6 +134,8 @@ pub fn simplify_html(document: &mut Html, lang: &str) {
remove_attrs(document);
final_expansions(document);
remove_toplevel_whitespace(document);
}
fn remove_ids(document: &mut Html, ids: impl IntoIterator<Item = NodeId>) {
@ -158,6 +160,29 @@ fn remove_non_element_nodes(document: &mut Html) {
remove_ids(document, to_remove.drain(..));
}
fn remove_toplevel_whitespace(document: &mut Html) {
let mut to_remove = Vec::new();
let parent = document.tree.root();
for el in parent.children() {
let Some(t) = el.value().as_text() else {
continue;
};
if t.chars().all(char::is_whitespace) {
to_remove.push(el.id());
}
}
trace!(
"Removing {} whitespace text nodes children from {:?}",
to_remove.len(),
parent.value(),
);
remove_ids(document, to_remove.drain(..));
}
fn remove_empty_sections(document: &mut Html) {
let mut to_remove = Vec::new();
for el in document.select(&HEADERS) {