From c06a6a764ea568be5faade665302aa23ca64304c Mon Sep 17 00:00:00 2001 From: eri Date: Tue, 30 Jul 2024 12:18:21 +0200 Subject: DevTools: Inline text and clean whitespace (#32884) * feat: inline text contents Signed-off-by: eri * feat: filter whitespace only nodes Signed-off-by: eri * chore: cleanup Signed-off-by: eri * fix: url fix Signed-off-by: eri * fix: review fixes Signed-off-by: eri Signed-off-by: Martin Robinson Co-authored-by: Martin Robinson --------- Signed-off-by: eri Signed-off-by: Martin Robinson Co-authored-by: Martin Robinson --- components/script/devtools.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'components/script/devtools.rs') diff --git a/components/script/devtools.rs b/components/script/devtools.rs index 597ed25dfd5..14f792555a4 100644 --- a/components/script/devtools.rs +++ b/components/script/devtools.rs @@ -19,6 +19,7 @@ use crate::dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyl use crate::dom::bindings::codegen::Bindings::DOMRectBinding::DOMRectMethods; use crate::dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods; use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods; +use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeConstants; use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; use crate::dom::bindings::conversions::{jsstring_to_str, ConversionResult, FromJSValConvertible}; use crate::dom::bindings::inheritance::Castable; @@ -125,7 +126,43 @@ pub fn handle_get_children( match find_node_by_unique_id(documents, pipeline, &node_id) { None => reply.send(None).unwrap(), Some(parent) => { - let children = parent.children().map(|child| child.summarize()).collect(); + let is_whitespace = |node: &NodeInfo| { + node.node_type == NodeConstants::TEXT_NODE && + node.node_value + .as_ref() + .map_or(true, |v| v.trim().is_empty()) + }; + + let inline: Vec<_> = parent + .children() + .map(|child| { + let window = window_from_node(&*child); + let Some(elem) = child.downcast::() else { + return false; + }; + let computed_style = window.GetComputedStyle(elem, None); + let display = computed_style.Display(); + display == "inline" + }) + .collect(); + + let children: Vec<_> = parent + .children() + .enumerate() + .filter_map(|(i, child)| { + // Filter whitespace only text nodes that are not inline level + // https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_and_edit_html/index.html#whitespace-only-text-nodes + let prev_inline = i > 0 && inline[i - 1]; + let next_inline = i < inline.len() - 1 && inline[i + 1]; + + let info = child.summarize(); + if !is_whitespace(&info) { + return Some(info); + } + + (prev_inline && next_inline).then_some(info) + }) + .collect(); reply.send(Some(children)).unwrap(); }, -- cgit v1.2.3