diff --git a/crates/nu_plugin_query/src/nu/mod.rs b/crates/nu_plugin_query/src/nu/mod.rs index 80c0bf8571..fcd5eaa4c3 100644 --- a/crates/nu_plugin_query/src/nu/mod.rs +++ b/crates/nu_plugin_query/src/nu/mod.rs @@ -73,7 +73,7 @@ impl Plugin for Query { pub fn web_examples() -> Vec { vec![PluginExample { - example: "http get https://phoronix.com | query web --query 'header'".into(), + example: "http get https://phoronix.com | query web --query 'header' | flatten".into(), description: "Retrieve all `
` elements from phoronix.com website".into(), result: None, }, PluginExample { @@ -83,7 +83,7 @@ pub fn web_examples() -> Vec { result: None }, PluginExample { - example: "http get https://www.nushell.sh | query web --query 'h2, h2 + p' | group 2 | each {rotate --ccw tagline description} | flatten".into(), + example: "http get https://www.nushell.sh | query web --query 'h2, h2 + p' | each {str join} | group 2 | each {rotate --ccw tagline description} | flatten".into(), description: "Pass multiple css selectors to extract several elements within single query, group the query results together and rotate them to create a table".into(), result: None, }, diff --git a/crates/nu_plugin_query/src/query_web.rs b/crates/nu_plugin_query/src/query_web.rs index 9318ea9dd7..ad6eca4f97 100644 --- a/crates/nu_plugin_query/src/query_web.rs +++ b/crates/nu_plugin_query/src/query_web.rs @@ -260,10 +260,11 @@ fn execute_selector_query( false => doc .select(&css(query_string, inspect)) .map(|selection| { - Value::string( + Value::list( selection .text() - .fold("".to_string(), |acc, x| format!("{acc}{x}")), + .map(|text| Value::string(text, span)) + .collect(), span, ) }) @@ -293,6 +294,8 @@ mod tests { "#; + const NESTED_TEXT: &str = r#"

Hello there, World

"#; + #[test] fn test_first_child_is_not_empty() { assert!(!execute_selector_query( @@ -316,6 +319,35 @@ mod tests { ); let config = nu_protocol::Config::default(); let out = item.into_string("\n", &config); - assert_eq!("[Coffee]".to_string(), out) + assert_eq!("[[Coffee]]".to_string(), out) + } + + #[test] + fn test_nested_text_nodes() { + let item = execute_selector_query( + NESTED_TEXT, + "p:first-child", + false, + false, + Span::test_data(), + ); + let out = item + .as_list() + .unwrap() + .iter() + .map(|matches| { + matches + .as_list() + .unwrap() + .iter() + .map(|text_nodes| text_nodes.as_string().unwrap()) + .collect::>() + }) + .collect::>>(); + + assert_eq!( + out, + vec![vec!["Hello there, ".to_string(), "World".to_string()]], + ); } }