From 3d3ecb52b2dbcf74c22ad1aba0253f8beb3c76fe Mon Sep 17 00:00:00 2001 From: Evan Lloyd New-Schmidt Date: Tue, 15 Aug 2023 16:19:16 -0400 Subject: [PATCH] Minify whitespace between elements Signed-off-by: Evan Lloyd New-Schmidt --- src/html.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/html.rs b/src/html.rs index 8d75cc6..fa85ce5 100644 --- a/src/html.rs +++ b/src/html.rs @@ -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) { @@ -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) {