Current section

86 Versions

Jump to

Compare versions

10 files changed
+219 additions
-102 deletions
  @@ -5,13 +5,14 @@
5 5 [![Hex.pm](https://img.shields.io/hexpm/dt/floki.svg)](https://hex.pm/packages/floki)
6 6 [![Inline docs](http://inch-ci.org/github/philss/floki.svg?branch=master)](http://inch-ci.org/github/philss/floki)
7 7
8 - Floki is a simple HTML parser that enables search using query selectors like jQuery or CSS.
8 + Floki is a simple HTML parser that enables search for nodes using CSS selectors.
9 9
10 - You can search elements by class, tag name and id.
10 + You can perform searches using classes, attributes, tag names and IDs.
11 + You can also combine selectors and use groups, like: `"a.foo[data-action='bar'], .baz.zaz"`.
11 12
12 13 [Check the documentation](http://hexdocs.pm/floki).
13 14
14 - ## Example
15 + ## Usage
15 16
16 17 Assuming that you have the following HTML:
17 18
  @@ -21,6 +22,7 @@ Assuming that you have the following HTML:
21 22 <body>
22 23 <section id="content">
23 24 <p class="headline">Floki</p>
25 + <span class="headline">Enables search using CSS selectors</span>
24 26 <a href="http://github.com/philss/floki">Github page</a>
25 27 <span data-model="user">philss</span>
26 28 </section>
  @@ -33,12 +35,12 @@ Here are some queries that you can perform (with return examples):
33 35
34 36 ```elixir
35 37 Floki.find(html, "#content")
36 - # => {"section", [{"id", "content"}],
38 + # => [{"section", [{"id", "content"}],
37 39 # => [{"p", [{"class", "headline"}], ["Floki"]},
38 - # => {"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]}]}
40 + # => {"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]}]}]
39 41
40 42
41 - Floki.find(html, ".headline") # returns a list with the `p` element
43 + Floki.find(html, "p.headline")
42 44 # => [{"p", [{"class", "headline"}], ["Floki"]}]
43 45
44 46
  @@ -47,6 +49,11 @@ Floki.find(html, "a")
47 49 # => {"a", [{"href", "https://hex.pm/packages/floki"}], ["Hex package"]}]
48 50
49 51
52 + Floki.find(html, "a[href^=https]")
53 + # => [{"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]},
54 + # => {"a", [{"href", "https://hex.pm/packages/floki"}], ["Hex package"]}]
55 +
56 +
50 57 Floki.find(html, "#content a")
51 58 # => [{"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]}]
52 59
  @@ -91,14 +98,14 @@ You can install Floki by adding a dependency to your mix file (mix.exs):
91 98 ```elixir
92 99 defp deps do
93 100 [
94 - {:floki, "~> 0.4"}
101 + {:floki, "~> 0.5"}
95 102 ]
96 103 end
97 104 ```
98 105
99 106 After that, run `mix deps.get`.
100 107
101 - ## API
108 + ## More about the API
102 109
103 110 To parse a HTML document, try:
104 111
  @@ -129,7 +136,7 @@ Floki.attribute(html, ".example", "class") # href or src are good possibilities
129 136 # => ["example"]
130 137 ```
131 138
132 - You can also get attributes from elements that you already have:
139 + You can get attributes from elements that you already have:
133 140
134 141 ```elixir
135 142 Floki.find(html, ".example")
  @@ -1,8 +1,7 @@
1 1 {<<"app">>,<<"floki">>}.
2 2 {<<"build_tools">>,[<<"mix">>]}.
3 - {<<"contributors">>,[<<"Philip Sampaio Silva">>]}.
4 3 {<<"description">>,
5 - <<"A HTML parser and searcher.\n\nYou can search inside HTML documents using CSS like selectors.">>}.
4 + <<"A HTML parser and searcher.\n\nYou can search inside HTML documents using CSS selectors.">>}.
6 5 {<<"elixir">>,<<">= 1.0.0">>}.
7 6 {<<"files">>,
8 7 [<<"lib/floki.ex">>,<<"lib/floki/attribute_selector.ex">>,
  @@ -16,10 +15,11 @@
16 15 {<<"links">>,
17 16 [{<<"Docs">>,<<"http://hexdocs.pm/floki">>},
18 17 {<<"GitHub">>,<<"https://github.com/philss/floki">>}]}.
18 + {<<"maintainers">>,[<<"Philip Sampaio Silva">>]}.
19 19 {<<"name">>,<<"floki">>}.
20 20 {<<"requirements">>,
21 21 [{<<"mochiweb">>,
22 22 [{<<"app">>,<<"mochiweb">>},
23 23 {<<"optional">>,false},
24 24 {<<"requirement">>,<<"~> 2.12.2">>}]}]}.
25 - {<<"version">>,<<"0.4.1">>}.
25 + {<<"version">>,<<"0.5.0">>}.
  @@ -78,8 +78,7 @@ defmodule Floki do
78 78 end
79 79
80 80 @doc """
81 - Finds elements inside a HTML tree or string.
82 - You can search by class, tag name or id.
81 + Find elements inside a HTML tree or string.
83 82
84 83 ## Examples
85 84
  @@ -97,10 +96,10 @@ defmodule Floki do
97 96 @spec find(binary | html_tree, binary) :: html_tree
98 97
99 98 def find(html, selector) when is_binary(html) do
100 - Floki.Parser.parse(html) |> Finder.find(selector)
99 + parse(html) |> Finder.find(selector)
101 100 end
102 - def find(html, selector) do
103 - Finder.find(html, selector)
101 + def find(html_tree, selector) do
102 + Finder.find(html_tree, selector)
104 103 end
105 104
106 105 @doc """
  @@ -1,8 +1,16 @@
1 1 defmodule Floki.AttributeSelector do
2 + @moduledoc """
3 + It is very similar to the `Selector` module, but is specialized in attributes
4 + and attribute selectors.
5 + """
6 +
2 7 alias Floki.AttributeSelector
3 8
4 9 defstruct match_type: nil, attribute: nil, value: nil
5 10
11 + @doc """
12 + Returns if attributes of a node matches with a given attribute selector.
13 + """
6 14 def match?(attributes, s = %AttributeSelector{match_type: nil, value: nil}) do
7 15 attribute_present?(s.attribute, attributes)
8 16 end
  @@ -1,3 +1,19 @@
1 1 defmodule Floki.Combinator do
2 + @moduledoc """
3 + Represents the conjunction of a combinator with its selector.
4 +
5 + Combinators can have the following match types:
6 +
7 + - descendant;
8 + e.g.: "a b"
9 + - child;
10 + e.g.: "a > b"
11 + - adjacent sibling;
12 + e.g.: "a + b"
13 + - general sibling;
14 + e.g.: "a ~ b"
15 +
16 + """
17 +
2 18 defstruct match_type: nil, selector: nil
3 19 end
Loading more files…