Skip to content

Commit

Permalink
feat: add htmls() and outer_htmls() for getting combined html of all …
Browse files Browse the repository at this point in the history
…elements, feature by #12
  • Loading branch information
fefit committed Sep 29, 2022
1 parent 158dc63 commit 1cee95c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

0.2.1 版本后各个接口方法已经基本趋于稳定,后面将不会做大的调整。

## [0.5.6] - 2022-09-29

### 增加

- 增加 `htmls()``outer_htmls()` 方法,相比于 `html()``outer_html()` 仅获取第一个元素、此两方法获取所有元素的 `html`

## [0.5.5] - 2022-08-23

### 增加
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "visdom"
version = "0.5.5"
version = "0.5.6"
edition = "2018"
description = "A html document syntax and operation library, use APIs similar to jquery, easy to use for web scraping and confused html."
keywords = ["html", "scrape", "jquery", "query", "selector"]
Expand Down
61 changes: 59 additions & 2 deletions src/mesdoc/interface/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2907,7 +2907,7 @@ impl<'a> Elements<'a> {
/// let html = r##"
/// <html>
/// <head>
/// <title>document</title>
/// <title>set_text()</title>
/// </head>
/// <body>
/// <dl>
Expand Down Expand Up @@ -2949,7 +2949,7 @@ impl<'a> Elements<'a> {
/// let html = r##"
/// <html>
/// <head>
/// <title>document</title>
/// <title>html()</title>
/// </head>
/// <body>
/// <dl>
Expand Down Expand Up @@ -2978,6 +2978,34 @@ impl<'a> Elements<'a> {
String::from("")
}

/// Get the combined html of all the elements in Elements.
///
/// ```
/// use visdom::Vis;
/// use visdom::types::BoxDynError;
/// fn main()-> Result<(), BoxDynError>{
/// let html = r##"
/// <html>
/// <head>
/// <title>htmls()</title>
/// </head>
/// <body>
/// <div><span class="div1span">span1</span></div>
/// <div><span class="div2span">span2</span></div>
/// </body>
/// </html>
/// "##;
/// let doc = Vis::load(html)?;
/// let divs = doc.find("div");
/// assert_eq!(divs.htmls(), r#"<span class="div1span">span1</span><span class="div2span">span2</span>"#);
/// assert_eq!(doc.find("p").htmls(), "");
/// Ok(())
/// }
/// ```
pub fn htmls(&self) -> String {
self.map(|_, ele| ele.html()).join("")
}

/// Set the html to content of each element in Elements.
///
/// ```
Expand Down Expand Up @@ -3051,6 +3079,35 @@ impl<'a> Elements<'a> {
}
String::from("")
}

/// Get the combined outer html of all the elements in Elements.
///
/// ```
/// use visdom::Vis;
/// use visdom::types::BoxDynError;
/// fn main()-> Result<(), BoxDynError>{
/// let html = r##"
/// <html>
/// <head>
/// <title>outer_htmls()</title>
/// </head>
/// <body>
/// <div><span class="div1span">span1</span></div>
/// <div><span class="div2span">span2</span></div>
/// </body>
/// </html>
/// "##;
/// let doc = Vis::load(html)?;
/// let divs = doc.find("div");
/// assert_eq!(divs.outer_htmls(), r#"<div><span class="div1span">span1</span></div><div><span class="div2span">span2</span></div>"#);
/// assert_eq!(doc.find("p").outer_htmls(), "");
/// Ok(())
/// }
/// ```
pub fn outer_htmls(&self) -> String {
self.map(|_, ele| ele.outer_html()).join("")
}

cfg_feat_text! {
/// pub fn `texts`
/// get the text node of each element
Expand Down
28 changes: 28 additions & 0 deletions tests/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ fn test_inner_html() -> Result {
Ok(())
}

#[test]
fn test_inner_htmls() -> Result {
let inner_html = "abc<span>def</span>ghj";
let code = format!("<div>{}</div><div>{}</div>", inner_html, inner_html);
let root = Vis::load(&code)?;
assert_eq!(root.find("div").eq(0).htmls(), inner_html);
assert_eq!(
root.find("div").htmls(),
format!("{}{}", inner_html, inner_html)
);
assert_eq!(root.find("p").htmls(), "");
Ok(())
}

#[test]
fn test_outer_html() -> Result {
let inner_html = "abc<span>def</span>ghj";
Expand All @@ -272,6 +286,20 @@ fn test_outer_html() -> Result {
Ok(())
}

#[test]
fn test_outer_htmls() -> Result {
let inner_html = "abc<span>def</span>ghj";
let code = format!("<div>{}</div><div>{}</div>", inner_html, inner_html);
let root = Vis::load(&code)?;
assert_eq!(
root.find("div").eq(0).outer_htmls(),
format!("<div>{}</div>", inner_html)
);
assert_eq!(root.find("div").outer_htmls(), code);
assert_eq!(root.find("p").outer_htmls(), "");
Ok(())
}

#[test]
#[cfg(feature = "text")]
fn test_texts() -> Result {
Expand Down

0 comments on commit 1cee95c

Please sign in to comment.