Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(html2md): omit hidden serialized page state

lanyue-llk06d6a57d7290
diff --git a/moli-html2md/README.md b/moli-html2md/README.md
index c85505ef2..133f4be75 100644
--- a/moli-html2md/README.md
+++ b/moli-html2md/README.md
@@ -87,8 +87,9 @@ default is `false`, which collapses ordinary HTML whitespace and moves boundary
spaces outside code delimiters. Fenced blocks always preserve nonempty code's
spaces and blank lines. Empty blocks still separate surrounding text.
-This is a structural content dump. It does not evaluate CSS, visibility, layout,
-or JavaScript, and cannot preserve all HTML presentation (for example, arbitrary
+This is a structural content dump. It omits non-rendered serialized state blobs
+but retains content in collapsed panels and inactive tabs. It does not evaluate
+stylesheets, layout, or JavaScript, and cannot preserve all HTML presentation (for example, arbitrary
ordered-list numbering or table spanning). DOM snapshots used by Moli's optional
strip/base/frame transformations remain the responsibility of the caller.
diff --git a/moli-html2md/src/lib.rs b/moli-html2md/src/lib.rs
index c14d26876..2562a5125 100644
--- a/moli-html2md/src/lib.rs
+++ b/moli-html2md/src/lib.rs
@@ -4,6 +4,7 @@ mod machine;
mod options;
mod output;
mod table;
+mod visibility;
mod writer;
pub use converter::{Converter, convert};
diff --git a/moli-html2md/src/machine.rs b/moli-html2md/src/machine.rs
index 236beec17..62ff7cd9e 100644
--- a/moli-html2md/src/machine.rs
+++ b/moli-html2md/src/machine.rs
@@ -1,5 +1,6 @@
use crate::output::Output;
use crate::table::Table;
+use crate::visibility::nonrendered_serialized_state;
use crate::writer::{Style, Writer, longest_run};
use crate::{Dom, NodeKind, Options};
@@ -198,6 +199,9 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
NodeKind::Other => return,
NodeKind::Element(tag) => tag,
};
+ if nonrendered_serialized_state(self.dom, node) {
+ return;
+ }
if let Some(table) = self.tables.last_mut() {
table.visit_element(tag);
}
@@ -380,6 +384,9 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
if depth >= self.options.max_depth {
return;
}
+ if nonrendered_serialized_state(self.dom, node) {
+ return;
+ }
match self.dom.node_kind(node) {
NodeKind::Text(text) => self.raw.push_str(text),
// Inline code normalizes line endings to spaces. Keep explicit
diff --git a/moli-html2md/src/visibility.rs b/moli-html2md/src/visibility.rs
new file mode 100644
index 000000000..ef8b763f8
--- /dev/null
+++ b/moli-html2md/src/visibility.rs
@@ -0,0 +1,49 @@
+use crate::{Dom, NodeKind};
+
+/// A hidden leaf containing a serialized page-state object is not reader text.
+/// Keep other hidden subtrees: inactive tabs and collapsed details can contain
+/// the page's primary data and belong in a structural content dump.
+pub(crate) fn nonrendered_serialized_state<D: Dom + ?Sized>(dom: &D, node: D::NodeId) -> bool {
+ if !matches!(dom.node_kind(node), NodeKind::Element(_)) {
+ return false;
+ }
+ let hidden = dom.attribute(node, "hidden").is_some()
+ || dom
+ .attribute(node, "style")
+ .is_some_and(inline_display_none);
+ if !hidden {
+ return false;
+ }
+ let Some(child) = dom.first_child(node) else {
+ return false;
+ };
+ if dom.next_sibling(child).is_some() {
+ return false;
+ }
+ let NodeKind::Text(text) = dom.node_kind(child) else {
+ return false;
+ };
+ let trimmed = text.trim();
+ trimmed.len() >= 512
+ && ((trimmed.starts_with('{') && trimmed.ends_with('}'))
+ || (trimmed.starts_with('[') && trimmed.ends_with(']')))
+}
+
+fn inline_display_none(style: &str) -> bool {
+ let mut display = None;
+ for declaration in style.split(';') {
+ let Some((property, value)) = declaration.split_once(':') else {
+ continue;
+ };
+ if !property.trim().eq_ignore_ascii_case("display") {
+ continue;
+ }
+ let value = value.trim().to_ascii_lowercase();
+ let important = value.trim_end().ends_with("!important");
+ let value = value.trim_end_matches("!important").trim();
+ if !display.is_some_and(|(_, previous_important)| previous_important && !important) {
+ display = Some((value == "none", important));
+ }
+ }
+ display.is_some_and(|(hidden, _)| hidden)
+}
diff --git a/moli-html2md/tests/conversion.rs b/moli-html2md/tests/conversion.rs
index 4453a93c5..13d0740c0 100644
--- a/moli-html2md/tests/conversion.rs
+++ b/moli-html2md/tests/conversion.rs
@@ -85,6 +85,44 @@ impl Dom for Tree {
}
}
+#[test]
+fn hidden_page_state_is_omitted_without_losing_inactive_tab_content() {
+ let mut dom = Tree::new();
+ let page = dom.element(0, "main");
+ dom.leaf(page, "h1", "Article title");
+ let state: &'static str =
+ Box::leak(format!("{{\"state\":\"{}\"}}", "x".repeat(512)).into_boxed_str());
+ let hidden = dom.leaf(page, "div", state);
+ dom.attr(hidden, "style", "color:red; DISPLAY : none !important");
+ let hidden_attribute = dom.leaf(page, "p", "Collapsed details");
+ dom.attr(hidden_attribute, "hidden", "");
+ let visible_override = dom.leaf(page, "p", "Visible details");
+ dom.attr(visible_override, "style", "display:none; display:block");
+ let important_hidden = dom.leaf(page, "p", "Important hidden details");
+ dom.attr(
+ important_hidden,
+ "style",
+ "display:none!important; display:block",
+ );
+ let table = dom.element(page, "table");
+ let header = dom.element(table, "tr");
+ dom.leaf(header, "th", "Name");
+ let hidden_row = dom.element(table, "tr");
+ dom.attr(hidden_row, "hidden", "");
+ dom.leaf(hidden_row, "td", "Hidden row");
+ let row = dom.element(table, "tr");
+ dom.leaf(row, "td", "Visible row");
+
+ let text = Converter::default().convert(&dom, 0);
+ assert!(text.contains("Article title"));
+ assert!(text.contains("Visible details"));
+ assert!(text.contains("Visible row"));
+ assert!(!text.contains(state));
+ assert!(text.contains("Collapsed details"));
+ assert!(text.contains("Important hidden details"));
+ assert!(text.contains("Hidden row"));
+}
+
#[test]
fn coalesces_styles_without_changing_the_tree() {
let mut dom = Tree::new();