Skip to content

Commit 3209283

Browse files
authored
Add parse functions that return attributes as maps (#91)
* Add parse functions that return attributes as maps This is in line with Floki's new feature, that was introduced in philss/floki#467 The initial idea is from philss/floki#463 * Improve error handling * Add a Rust CI workflow to check fmt and clippy * Add missing guard clause * Remove unused module and rename function * Omit "self::" to refer to local module
1 parent 2d5e843 commit 3209283

File tree

8 files changed

+354
-192
lines changed

8 files changed

+354
-192
lines changed

.github/workflows/rust-ci.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Rust CI
2+
on:
3+
push:
4+
branches:
5+
- master
6+
paths:
7+
- "native/**"
8+
pull_request:
9+
paths:
10+
- "native/**"
11+
workflow_dispatch:
12+
13+
jobs:
14+
lint-rust:
15+
name: Lint Rust
16+
runs-on: ubuntu-20.04
17+
strategy:
18+
matrix:
19+
manifest:
20+
- native/html5ever_nif/Cargo.toml
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
25+
- uses: dtolnay/rust-toolchain@stable
26+
with:
27+
components: rustfmt, clippy
28+
29+
- uses: Swatinem/rust-cache@v2
30+
with:
31+
workspaces: |
32+
native/html5ever_nif
33+
34+
- name: run rustfmt
35+
run: cargo fmt --manifest-path=${{ matrix.manifest }} --all -- --check
36+
37+
- name: run clippy
38+
run: cargo clippy --manifest-path=${{ matrix.manifest }} -- -Dwarnings

lib/html5ever.ex

+33-21
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,30 @@ defmodule Html5ever do
3737
]}
3838
3939
"""
40-
def parse(html) do
41-
parse_dirty(html)
40+
def parse(html) when is_binary(html) do
41+
Html5ever.Native.parse(html, false)
42+
end
43+
44+
@doc """
45+
Same as `parse/1`, but with attributes as maps.
46+
47+
This is going to remove duplicated attributes, keeping the ones
48+
that appear first.
49+
50+
## Example
51+
52+
iex> Html5ever.parse_with_attributes_as_maps(
53+
...> "<!doctype html><html><body><h1 class=title>Hello world</h1></body></html>"
54+
...> )
55+
{:ok,
56+
[
57+
{:doctype, "html", "", ""},
58+
{"html", %{}, [{"head", %{}, []}, {"body", %{}, [{"h1", %{"class" => "title"}, ["Hello world"]}]}]}
59+
]}
60+
61+
"""
62+
def parse_with_attributes_as_maps(html) when is_binary(html) do
63+
Html5ever.Native.parse(html, true)
4264
end
4365

4466
@doc """
@@ -92,27 +114,17 @@ defmodule Html5ever do
92114
}}
93115
94116
"""
95-
def flat_parse(html) do
96-
flat_parse_dirty(html)
117+
def flat_parse(html) when is_binary(html) do
118+
Html5ever.Native.flat_parse(html, false)
97119
end
98120

99-
defp parse_dirty(html) do
100-
case Html5ever.Native.parse_sync(html) do
101-
{:html5ever_nif_result, :ok, result} ->
102-
{:ok, result}
103-
104-
{:html5ever_nif_result, :error, err} ->
105-
{:error, err}
106-
end
107-
end
108-
109-
defp flat_parse_dirty(html) do
110-
case Html5ever.Native.flat_parse_sync(html) do
111-
{:html5ever_nif_result, :ok, result} ->
112-
{:ok, result}
121+
@doc """
122+
Same as `flat_parse/1`, but with attributes as maps.
113123
114-
{:html5ever_nif_result, :error, err} ->
115-
{:error, err}
116-
end
124+
This is going to remove duplicated attributes, keeping the ones
125+
that appear first.
126+
"""
127+
def flat_parse_with_attributes_as_maps(html) when is_binary(html) do
128+
Html5ever.Native.flat_parse(html, true)
117129
end
118130
end

lib/html5ever/native.ex

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ defmodule Html5ever.Native do
1919
System.get_env("HTML5EVER_BUILD") in ["1", "true"] or env_config[:build_from_source],
2020
version: version
2121

22-
def parse_sync(_binary), do: err()
23-
def parse_dirty(_binary), do: err()
24-
def flat_parse_sync(_binary), do: err()
25-
def flat_parse_dirty(_binary), do: err()
22+
def parse(_binary, _attrs_as_maps), do: err()
23+
def flat_parse(_binary, _attrs_as_maps), do: err()
2624

2725
defp err, do: :erlang.nif_error(:nif_not_loaded)
2826
end

native/html5ever_nif/Cargo.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/html5ever_nif/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ markup5ever = "0.11"
1717

1818
tendril = "0.4"
1919
lazy_static = "1.4"
20+
21+
thiserror = "1"

0 commit comments

Comments
 (0)