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

Cannot search with multiple selectors #18

Closed
sllvn opened this issue Sep 3, 2015 · 2 comments
Closed

Cannot search with multiple selectors #18

sllvn opened this issue Sep 3, 2015 · 2 comments

Comments

@sllvn
Copy link

sllvn commented Sep 3, 2015

With @html markup from test/floki_test.exs:

<html>
<head>
<title>Test</title>
</head>
<body>
  <div class="content">
    <a href="http://google.com" class="js-google js-cool">Google</a>
    <a href="http://elixir-lang.org" class="js-elixir js-cool">Elixir lang</a>
    <a href="http://java.com" class="js-java">Java</a>
  </div>
</body>
</html>

I am trying to search for an element with multiple classes (on the same element) with the selector .js-cool.js-elixir but Floki is not finding them. However, if I add a space between the classes, as if the second was a descendent of the first, .js-cool .js-elixir, it does return what I'm looking for but should not (as js-elixir is not descendent of js-cool).

Test demonstrating the error:

test "find elements with given multiple classes" do
  class_selector = ".js-cool.js-elixir"

  assert Floki.find(@html, class_selector) == [
    {"a", [
        {"href", "http://elixir-lang.org"},
        {"class", "js-elixir js-cool"}],
      ["Elixir lang"]}
  ]
end
philss added a commit that referenced this issue Sep 17, 2015
This is a huge refactor in the `Floki.find/2` function that enables more
complex searches using a mix of selectors. You can mix selectors
like you would normally do in other tools like jQuery or to apply rules
using CSS selectors.

Examples of queries now supported:
- "a.foo"
- ".foo.bar"
- ".baz[data='something']"
- "[title][href$='.html']"
- "a b.foo c"

To archive this, it was necessary to write a tokenizer and a parser for
the inputted selector. It's quite easy to understand after read this
article by Andrea Leopardi (@whatyouhide) about tokenizing and parsing
in Elixir:
http://andrealeopardi.com/posts/tokenizing-and-parsing-in-elixir-using-leex-and-yecc/

The tokenizer partially covers the specs of CSS3 selectors, that you can
find at http://www.w3.org/TR/css3-selectors/

Knowning issues:
- There is no support for pseudo-selectors;
- The only combinator supported is descendant combinator;
- If there is a group of selectors in the same query, and two selectors
  matches the same node, this node will appear twice in the resultant
  list.

Closes #18 and #20.
@philss
Copy link
Owner

philss commented Sep 17, 2015

@licyeus Please check the new released version "0.4.0". It provides this selector and much more.

@sllvn
Copy link
Author

sllvn commented Sep 17, 2015

Thanks much 🎆; looking at diffs looks to cover my use cases. However, I'm running into errors compiling 0.4. Created #25 for that.

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