Packages
floki
0.20.0
0.38.4
0.38.3
0.38.2
0.38.1
0.38.0
0.37.1
0.37.0
0.36.3
0.36.2
0.36.1
0.36.0
0.35.4
0.35.3
0.35.2
0.35.1
0.35.0
0.34.3
0.34.2
0.34.1
0.34.0
0.33.1
0.33.0
0.32.1
0.32.0
0.31.0
0.30.1
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.1
0.23.0
0.22.0
0.21.0
0.20.4
0.20.3
0.20.2
0.20.1
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.17.2
0.17.1
0.17.0
0.16.0
0.15.0
0.14.0
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.1
0.10.0
0.9.0
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.1
0.6.0
0.5.0
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.1
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Floki is a simple HTML parser that enables search for nodes using CSS selectors.
Current section
86 Versions
Jump to
Current section
86 Versions
Compare versions
10
files changed
+108
additions
-67
deletions
| @@ -70,7 +70,7 @@ Add Floki to your `mix.exs`: | |
| 70 70 | ```elixir |
| 71 71 | defp deps do |
| 72 72 | [ |
| 73 | - {:floki, "~> 0.19.0"} |
| 73 | + {:floki, "~> 0.20.0"} |
| 74 74 | ] |
| 75 75 | end |
| 76 76 | ``` |
| @@ -105,7 +105,7 @@ After setup Rust, you need to add `html5ever` NIF to your dependency list: | |
| 105 105 | ```elixir |
| 106 106 | defp deps do |
| 107 107 | [ |
| 108 | - {:floki, "~> 0.19.0"}, |
| 108 | + {:floki, "~> 0.20.0"}, |
| 109 109 | {:html5ever, "~> 0.5.0"} |
| 110 110 | ] |
| 111 111 | end |
| @@ -36,4 +36,4 @@ | |
| 36 36 | {<<"optional">>,false}, |
| 37 37 | {<<"repository">>,<<"hexpm">>}, |
| 38 38 | {<<"requirement">>,<<"~> 2.15">>}]]}. |
| 39 | - {<<"version">>,<<"0.19.3">>}. |
| 39 | + {<<"version">>,<<"0.20.0">>}. |
| @@ -77,16 +77,28 @@ defmodule Floki do | |
| 77 77 | Note that the resultant HTML may be different from the original one. |
| 78 78 | Spaces after tags and doctypes are ignored. |
| 79 79 | |
| 80 | + Floki.raw_html/2 accepts a keyword list of options. Currently, the |
| 81 | + only supported option is `:encode`, which can be set to `true` or `false`. |
| 82 | + |
| 83 | + You can also control the encoding behaviour at the application level via |
| 84 | + `config :floki, :encode_raw_html, true | false` |
| 85 | + |
| 86 | + |
| 80 87 | ## Examples |
| 81 88 | |
| 82 89 | iex> Floki.parse(~s(<div class="wrapper">my content</div>)) |> Floki.raw_html |
| 83 90 | ~s(<div class="wrapper">my content</div>) |
| 84 91 | |
| 92 | + iex> Floki.parse(~s(<div class="wrapper">10 > 5</div>)) |> Floki.raw_html(encode: true) |
| 93 | + ~s(<div class="wrapper">10 > 5</div>) |
| 94 | + |
| 95 | + iex> Floki.parse(~s(<div class="wrapper">10 > 5</div>)) |> Floki.raw_html(encode: false) |
| 96 | + ~s(<div class="wrapper">10 > 5</div>) |
| 85 97 | """ |
| 86 98 | |
| 87 | - @spec raw_html(html_tree | binary) :: binary |
| 99 | + @spec raw_html(html_tree | binary, keyword) :: binary |
| 88 100 | |
| 89 | - defdelegate raw_html(html_tree), to: Floki.RawHTML |
| 101 | + defdelegate raw_html(html_tree, options \\ []), to: Floki.RawHTML |
| 90 102 | |
| 91 103 | @doc """ |
| 92 104 | Find elements inside a HTML tree or string. |
| @@ -185,9 +197,8 @@ defmodule Floki do | |
| 185 197 | |
| 186 198 | tree = add_nodes_to_tree(tree, mutated_nodes) |
| 187 199 | |
| 188 | - tree.nodes |
| 189 | - |> Map.values() |
| 190 | - |> Enum.filter(fn actual_node -> is_nil(actual_node.parent_node_id) end) |
| 200 | + tree.root_nodes_ids |
| 201 | + |> Enum.map(fn id -> Map.get(tree.nodes, id) end) |
| 191 202 | |> Enum.map(fn html_node -> HTMLTree.to_tuple(tree, html_node) end) |
| 192 203 | end |
| @@ -34,10 +34,7 @@ defmodule Floki.FilterOut do | |
| 34 34 | |
| 35 35 | case html_tree do |
| 36 36 | tree when is_tuple(tree) -> |
| 37 | - case html_as_tuples do |
| 38 | - [] -> [] |
| 39 | - [head | _] -> head |
| 40 | - end |
| 37 | + first_tuple(html_as_tuples) |
| 41 38 | |
| 42 39 | trees when is_list(trees) -> |
| 43 40 | html_as_tuples |
| @@ -45,6 +42,9 @@ defmodule Floki.FilterOut do | |
| 45 42 | end |
| 46 43 | end |
| 47 44 | |
| 45 | + defp first_tuple([]), do: [] |
| 46 | + defp first_tuple([head | _rest]), do: head |
| 47 | + |
| 48 48 | defp filter({nodetext, _, _}, selector) when nodetext === selector, do: false |
| 49 49 | defp filter({nodetext, _}, selector) when nodetext === selector, do: false |
| 50 50 | defp filter(_, _), do: true |
| @@ -204,7 +204,7 @@ defmodule Floki.HTMLTree do | |
| 204 204 | {:ok, length(html_tree.node_ids)} |
| 205 205 | end |
| 206 206 | |
| 207 | - def member?(html_tree, %{node_id: node_id} = html_node) do |
| 207 | + def member?(html_tree, html_node = %{node_id: node_id}) do |
| 208 208 | a_node = Map.get(html_tree.nodes, node_id) |
| 209 209 | |
| 210 210 | {:ok, a_node === html_node} |
| @@ -222,7 +222,7 @@ defmodule Floki.HTMLTree do | |
| 222 222 | defp do_reduce(tree, {:suspend, acc}, fun), do: {:suspended, acc, &do_reduce(tree, &1, fun)} |
| 223 223 | defp do_reduce(%HTMLTree{node_ids: []}, {:cont, acc}, _fun), do: {:done, acc} |
| 224 224 | |
| 225 | - defp do_reduce(%HTMLTree{node_ids: [h | t]} = html_tree, {:cont, acc}, fun) do |
| 225 | + defp do_reduce(html_tree = %HTMLTree{node_ids: [h | t]}, {:cont, acc}, fun) do |
| 226 226 | tree = %{html_tree | node_ids: t} |
| 227 227 | head_node = Map.get(html_tree.nodes, h) |
| 228 228 | do_reduce(tree, fun.(head_node, acc), fun) |
Loading more files…