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

query with numerous selectors at once #6

Closed
liabri opened this issue Oct 19, 2022 · 9 comments
Closed

query with numerous selectors at once #6

liabri opened this issue Oct 19, 2022 · 9 comments
Labels
enhancement New feature or request

Comments

@liabri
Copy link

liabri commented Oct 19, 2022

Hello, I would like to query_all all the headers (regardless of size) present, as the order matters. If I individually search for each header (h1, h2, h3 etc.) I will get them ordered based on their size, h1 first, h2 second and so on. I've looked through the docs and cannot seem to find a way to do this, so if one is indeed not present I suggest the following syntax.

fn query_all(&self, selector: Vec<&Selector>) -> Vec<Element>
so usage would be something like:
dom.query_all(vec![&Selector::from("h1"), &Selector::from("h2"), ...]);

@lomirus lomirus added the enhancement New feature or request label Oct 19, 2022
@lomirus
Copy link
Owner

lomirus commented Oct 19, 2022

Thanks for your advice, It is a considerable feature.

However, my usage would be a little different from yours. I prefer this way:

dom.query_all(&Selector::from("h1, h2, h3"))

Because:

  • Using a vector of selectors would be ambigious, Users may be confused about if it will select elements of "h1 and h2" or "h1 or h2";
  • Using , to split selectors is a more natural way as you code in css selector;
  • It can be more concise than the vector way.

This feature may be added in a few days when I am available. BTW, it is also a good timing for me to refactor the selector as I has been always wanting to change the type of the returned value of Selector::from to Result<Selector, Err> for better error handling. So it needs another several more days to release the next version.


Edit: Seems it needs me a lot of work to refactor the selector return value. So maybe this feature will be postponed.

@liabri
Copy link
Author

liabri commented Oct 19, 2022

I'm happy to hear this. As for the actual implementation I was hesitating between the two, but I went for what I initially proposed to avoid having to change the Selector mostly (and semantics as I mention below).

To address your first point, I do not see how an "h1 and h2" could possibly exist, a Node can only be one type so it is clear that it would be an OR relationship (unless I'm missing something that is x)). My only concern with the way you put it is that semantically it could mean there is an element of type "h1, h2", now I don't think theres an actual tag with a comma in it, but yeah semantic consistency 🤷 x) As for it being more concise I agree, concision is indeed nice!

@lomirus
Copy link
Owner

lomirus commented Oct 20, 2022

  1. It's truly a bad example I used above. In fact, I mean, selectors can use not only the tag name but also the class name, like:
dom.query_all(vec![&Selector::from(".question"), &Selector::from(".answer"), ...]);
  1. I don't think there is need to consider the <h1,h2> as they are not standard. Besides, if you are familiar with the JavaScript & CSS, you will find it's already a standard in web. For example, I create a HTML file like this:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <style>
        h1, h2 {
            color: red;
        }
    </style>
    <h1>asd</h1>
    <h2>qwe</h2>
</body>
</html>

For the selector h1, h2 in CSS , it affects both h1 and h2:

For the selector in JavaScript, the image below shows how it works:

@liabri
Copy link
Author

liabri commented Oct 20, 2022

I completely forget a Selector can be more than a tag, you do have a point there. Although the web does use such syntax (and this project is mostly geared towards web use) it still does not sit well with me. I have no experience in JavaScript and find it quite bizarre the syntax used there, for some reason in CSS it makes more sense, maybe because it's a different type of language, however it may just be me x) I suppose it wouldn't be too far-stretching to follow this syntax because of that, though I wouldn't necessarily call it idiomatic rust personally.

To confirm: AND operations would be marked with spaces (such as .a .b) whilst OR operations would be marked with commas (such as h1, h2 or .a, .b) ? If it was completely up to me I would convey AND operations just as they are, but OR operations using the initial syntax I proposed, my reasoning is that .a .b may be considered as a single selector if you think about it, however .a, .b is made up of two separate selectors, which in Rust (to me at least) should be independent structures i.e. Selector.

@lomirus
Copy link
Owner

lomirus commented Oct 20, 2022 via email

@liabri
Copy link
Author

liabri commented Oct 20, 2022

I'm referring to the creation of a Selector. If OR operations are Selector::from("h1, h2") in your proposition, how would an AND operation be (i.e. selection of the element·s containing all selectors to clarify)? I assumed it'd be Select::from(".a .b"), separation by space.

@lomirus
Copy link
Owner

lomirus commented Oct 21, 2022 via email

@liabri
Copy link
Author

liabri commented Oct 21, 2022

I see, well it all depends if you want to do it from a web perspective or not I suppose. I don't know what would be more preferred by the general user of this library x) If you stick with the web ways, separation by comma definitely seems like the way to go.

@lomirus
Copy link
Owner

lomirus commented Oct 22, 2022

Solved in #7

@lomirus lomirus closed this as completed Oct 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants