Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selector with comma support #8

Closed
l4l opened this issue Nov 3, 2019 · 4 comments
Closed

Selector with comma support #8

l4l opened this issue Nov 3, 2019 · 4 comments

Comments

@l4l
Copy link

l4l commented Nov 3, 2019

For now, it seems that comma in #[html(selector = "...")] is parsed but not supported. I think it should at least panic as non-supported operation, and of course it's better be implemented. Here's the sample snippet:

use unhtml::{self, FromHtml};
use unhtml_derive::*;

#[derive(FromHtml)]
struct Test {
    #[html(selector = "div,p", attr = "inner")]
    text: String,
}

fn main() {
    let test = Vec::<Test>::from_html(
        r#"
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Title</title>
        </head>
        <body>
            <div>Some text</div>
            <p>Some other text</p>
        </body>
        </html>
        "#,
    )
    .unwrap();

    assert_eq!(test.len(), 2);
    assert_eq!(test[0].text, "Some text");
    assert_eq!(test[1].text, "Some other text");
}
@l4l
Copy link
Author

l4l commented Nov 4, 2019

Hmm, it's seems that comma is supported, though it's only works with struct Test { texts: Vec<String> } rather than with Vec<Test>::from_html. Is it expected behavior in example below then?

@Hexilee
Copy link
Owner

Hexilee commented Nov 4, 2019

Maybe the document should be updated...

<Vec<T> as FromHtml>::from_html is just a default implementation:

pub type ElemIter<'b, 'a> = &'b mut (dyn Iterator<Item = ElementRef<'a>> + 'b);

pub trait FromHtml: Sized {
    fn from_elements(select: ElemIter) -> Result<Self>;
    fn from_html(html: &str) -> Result<Self> {
        Self::from_elements(
            &mut Html::parse_document(html).select(&Selector::parse(":root").unwrap()),
        )
    }
}

While <Vec<T> as FromHtml>::from_elements is implemented:

impl<T> FromHtml for Vec<T>
where
    T: FromHtml,
{
    fn from_elements(select: ElemIter) -> Result<Self> {
        let mut ret = vec![];
        for elem in select {
            ret.push(vec![elem].into_iter().element()?)
        }
        Ok(ret)
    }
}

So, you can select only one element when straightly using <Vec<T> as FromHtml>::from_html.

@Hexilee
Copy link
Owner

Hexilee commented Nov 4, 2019

It is an expected behaviour.

@l4l
Copy link
Author

l4l commented Nov 4, 2019

I see, thanks!

@l4l l4l closed this as completed Nov 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants