├── .formatter.exs
├── .github
├── dependabot.yml
└── workflows
│ ├── ci.yml
│ ├── release.yml
│ └── rust-ci.yml
├── .gitignore
├── CHANGELOG.md
├── LICENSE-APACHE
├── LICENSE-MIT
├── README.md
├── RELEASE_CHECKLIST.md
├── lib
├── html5ever.ex
└── html5ever
│ └── native.ex
├── mix.exs
├── mix.lock
├── native
└── html5ever_nif
│ ├── .cargo
│ └── config.toml
│ ├── .gitignore
│ ├── Cargo.lock
│ ├── Cargo.toml
│ └── src
│ ├── common.rs
│ ├── flat_dom.rs
│ └── lib.rs
├── priv
└── test_data
│ ├── drudgereport.html
│ └── example.html
└── test
├── html5ever_test.exs
└── test_helper.exs
/.formatter.exs:
--------------------------------------------------------------------------------
1 | [
2 | inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}"]
3 | ]
4 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: mix
4 | directory: "/"
5 | schedule:
6 | interval: daily
7 | open-pull-requests-limit: 5
8 |
9 | - package-ecosystem: cargo
10 | directory: "/native/html5ever_nif"
11 | schedule:
12 | interval: daily
13 | open-pull-requests-limit: 5
14 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | pull_request:
5 | push:
6 | branches:
7 | - master
8 |
9 | jobs:
10 | mix_test:
11 | runs-on: ubuntu-22.04
12 | env:
13 | MIX_ENV: test
14 | HTML5EVER_BUILD: "true"
15 |
16 | name: Elixir ${{ matrix.pair.elixir }} / OTP ${{ matrix.pair.otp }}
17 |
18 | strategy:
19 | fail-fast: false
20 | matrix:
21 | include:
22 | - pair:
23 | elixir: 1.13.4
24 | otp: "24.3"
25 | - pair:
26 | elixir: 1.16.1
27 | otp: "26.2"
28 | lint: lint
29 | steps:
30 | - uses: actions/checkout@v4
31 |
32 | - uses: erlef/setup-beam@v1
33 | with:
34 | otp-version: ${{ matrix.pair.otp }}
35 | elixir-version: ${{ matrix.pair.elixir }}
36 |
37 | - name: Install minimal stable Rust toolchain
38 | uses: dtolnay/rust-toolchain@stable
39 |
40 | - name: Install Dependencies
41 | run: mix deps.get
42 |
43 | - run: mix format --check-formatted
44 | if: ${{ matrix.lint }}
45 |
46 | - run: mix deps.unlock --check-unused
47 | if: ${{ matrix.lint }}
48 |
49 | - run: mix deps.compile
50 |
51 | - run: mix compile --warnings-as-errors
52 | if: ${{ matrix.lint }}
53 |
54 | - run: mix test
55 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Build precompiled NIFs
2 |
3 | permissions:
4 | id-token: write
5 | attestations: write
6 | contents: write
7 |
8 | on:
9 | push:
10 | branches:
11 | - main
12 | - master
13 | paths:
14 | # Just run on main branch if "native" path changed.
15 | - "native/**"
16 | # Also run if this file changes.
17 | - ".github/workflows/release.yml"
18 | tags:
19 | # Tags will always run.
20 | - "*"
21 | pull_request:
22 | paths:
23 | # In PRs we only run if this file changes.
24 | - ".github/workflows/release.yml"
25 | workflow_dispatch:
26 |
27 | jobs:
28 | build_release:
29 | name: NIF ${{ matrix.nif }} - ${{ matrix.job.target }} (${{ matrix.job.os }})
30 | runs-on: ${{ matrix.job.os }}
31 | strategy:
32 | fail-fast: false
33 | matrix:
34 | nif: ["2.15"]
35 | job:
36 | - { target: arm-unknown-linux-gnueabihf , os: ubuntu-20.04 , use-cross: true }
37 | - { target: aarch64-unknown-linux-gnu , os: ubuntu-20.04 , use-cross: true }
38 | - { target: aarch64-unknown-linux-musl , os: ubuntu-20.04 , use-cross: true }
39 | - { target: aarch64-apple-darwin , os: macos-13 }
40 | - { target: riscv64gc-unknown-linux-gnu , os: ubuntu-20.04 , use-cross: true }
41 | - { target: x86_64-apple-darwin , os: macos-13 }
42 | - { target: x86_64-unknown-linux-gnu , os: ubuntu-20.04 }
43 | - { target: x86_64-unknown-linux-musl , os: ubuntu-20.04 , use-cross: true }
44 | - { target: x86_64-pc-windows-gnu , os: windows-2019 }
45 | - { target: x86_64-pc-windows-msvc , os: windows-2019 }
46 |
47 | steps:
48 | - name: Checkout source code
49 | uses: actions/checkout@v4
50 |
51 | - name: Extract project version
52 | shell: bash
53 | run: |
54 | # Get the project version from mix.exs
55 | echo "PROJECT_VERSION=$(sed -n 's/^ @version "\(.*\)"/\1/p' mix.exs | head -n1)" >> $GITHUB_ENV
56 |
57 | - name: Install Rust toolchain
58 | uses: dtolnay/rust-toolchain@stable
59 | with:
60 | toolchain: stable
61 | target: ${{ matrix.job.target }}
62 |
63 | - name: Build the project
64 | id: build-crate
65 | uses: philss/rustler-precompiled-action@v1.1.4
66 | with:
67 | project-name: html5ever_nif
68 | project-version: ${{ env.PROJECT_VERSION }}
69 | target: ${{ matrix.job.target }}
70 | nif-version: ${{ matrix.nif }}
71 | use-cross: ${{ matrix.job.use-cross }}
72 | project-dir: "native/html5ever_nif"
73 |
74 | - name: Artifact attestation
75 | uses: actions/attest-build-provenance@v1
76 | with:
77 | subject-path: ${{ steps.build-crate.outputs.file-path }}
78 |
79 | - name: Artifact upload
80 | uses: actions/upload-artifact@v4
81 | with:
82 | name: ${{ steps.build-crate.outputs.file-name }}
83 | path: ${{ steps.build-crate.outputs.file-path }}
84 |
85 | - name: Write SHA256 to the summary
86 | run: |
87 | echo "SHA256 for this artifact:" >> $GITHUB_STEP_SUMMARY
88 | echo "${{ steps.build-crate.outputs.file-sha256 }} ${{ steps.build-crate.outputs.file-name }}" >> $GITHUB_STEP_SUMMARY
89 |
90 | - name: Publish archives and packages
91 | uses: softprops/action-gh-release@v2
92 | with:
93 | files: |
94 | ${{ steps.build-crate.outputs.file-path }}
95 | if: startsWith(github.ref, 'refs/tags/')
96 |
--------------------------------------------------------------------------------
/.github/workflows/rust-ci.yml:
--------------------------------------------------------------------------------
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-22.04
17 | strategy:
18 | matrix:
19 | manifest:
20 | - native/html5ever_nif/Cargo.toml
21 |
22 | steps:
23 | - uses: actions/checkout@v4
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
39 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # The directory Mix will write compiled artifacts to.
2 | /_build
3 |
4 | # If you run "mix test --cover", coverage assets end up here.
5 | /cover
6 |
7 | # The directory Mix downloads your dependencies sources to.
8 | /deps
9 |
10 | # Where 3rd-party dependencies like ExDoc output generated docs.
11 | /doc
12 |
13 | # Ignore .fetch files in case you like to edit your project deps locally.
14 | /.fetch
15 |
16 | # If the VM crashes, it generates a dump, let's ignore it too.
17 | erl_crash.dump
18 |
19 | # Also ignore archive artifacts (built via "mix archive.build").
20 | *.ez
21 |
22 | /priv/native
23 |
24 | /native/*/target
25 |
26 | # The checksum files for precompiled NIFs
27 | checksum-*.exs
28 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 | All notable changes to this project will be documented in this file.
3 |
4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6 |
7 | ## [Unreleased]
8 |
9 | ## [0.16.1] - 2024-05-15
10 |
11 | ### Fixed
12 |
13 | - Fix parsing of HTML documents that may start with a comment or an XML doc tag.
14 |
15 | ## [0.16.0] - 2024-03-25
16 |
17 | ### Fixed
18 |
19 | - Fix parsing of comments in `parse/1`.
20 | - Avoid panic when parsing content with the "template" tag.
21 |
22 | ### Removed
23 |
24 | - Drop support for Elixir 1.12
25 |
26 | ## [0.15.0] - 2023-06-16
27 |
28 | ### Added
29 |
30 | - Add two new functions to parse documents:
31 |
32 | * `parse_with_attributes_as_maps/1`
33 | * `flat_parse_with_attributes_as_maps/1`
34 |
35 | And as the names suggest, it returns a document tree with attributes as maps,
36 | instead of lists of pairs. These functions are useful to match node attributes,
37 | since the order of attributes does not matter must of the times.
38 |
39 | ### Fixed
40 |
41 | - Use dirty CPU scheduler for all functions. For some reason we were using a
42 | normal scheduler, but this could cause instability.
43 |
44 | With a dirty scheduler we can parse medium to big files without worry about
45 | lengthy work. Please read https://www.erlang.org/doc/man/erl_nif.html#lengthy_work
46 | for further information.
47 |
48 | ### Removed
49 |
50 | - Remove support for Elixir 1.11.
51 |
52 | ## [0.14.3] - 2023-05-26
53 |
54 | ### Added
55 |
56 | - Add precompilation target for Linux running on RISC-V 64 bits machines.
57 | This is useful for projects using Nerves.
58 |
59 | Note that this is going to require `rustler_precompiled` v0.6 or above, since
60 | the that version includes RISC-V on Linux as defaults.
61 |
62 | - Add support for OTP 26 by updating the `rustler-sys` package.
63 |
64 | ## [0.14.2] - 2023-05-20
65 |
66 | ### Added
67 |
68 | - Add precompilation target for Linux running on ARM64 machines (both musl and gnu ABI).
69 | This is useful for projects using Nerves.
70 |
71 | Note that this is going to require `rustler_precompiled` v0.6 or above, since
72 | the that version includes ARM64 on Linux as defaults.
73 |
74 | ## [0.14.1] - 2023-05-20
75 |
76 | ### Added
77 |
78 | - Add support for `rustler_precompiled` v0.6.
79 |
80 | ### Changed
81 |
82 | - Update Rustler version in the crate from `v0.26` to `v0.28`.
83 | This shouldn't break anything, but would require the installation of rustler `v0.28`
84 | if needed in the Elixir side.
85 |
86 | - Change the Rust edition to 2021 (it was 2018). This shouldn't change any behaviour.
87 |
88 | ## [0.14.0] - 2022-11-04
89 |
90 | ### Changed
91 |
92 | - Require `rustler_precompiled` equal or above `v0.5.2` - thanks [@Benjamin-Philip](https://github.com/Benjamin-Philip).
93 | - Use `Application.compile_env/3` instead of `Application.get_env/3` in the native module.
94 |
95 | ## [0.13.1] - 2022-06-24
96 |
97 | ### Fixed
98 |
99 | - Fix the precompilation build for targets using `cross` by adding a `Cross.toml`
100 | file with a setting telling to read the `RUSTLER_NIF_VERSION` env var from the host machine.
101 |
102 | ## [0.13.0] - 2022-04-28
103 |
104 | ### Changed
105 |
106 | - Bump requirement for `rustler_precompiled` to `~> v0.4`. This is needed to avoid installing Rustler by default.
107 | - Bump `html5ever` (Rust crate) to `v0.26.0`.
108 |
109 | ## [0.12.0] - 2022-03-14
110 |
111 | ### Changed
112 |
113 | - Start using [`rustler_precompiled`](https://hex.pm/packages/rustler_precompiled) as
114 | dependency.
115 |
116 | ## [0.11.0] - 2021-12-15
117 |
118 | ### Security
119 |
120 | - Add checksum verification of precompiled NIF files before extracting
121 | them to the correct location. This is to avoid supply chain attacks.
122 | With this change we added a new mix task to download all the files
123 | and generate the checksum before publishing the package. Additionally
124 | the user can download only the local NIF file with the checksum.
125 | See the `RELEASE_CHECKLIST.md` file for details on how we ensure this
126 | works correctly.
127 |
128 | ### Removed
129 |
130 | - Remove support for Elixir 1.10 and below. This is to keep a policy of
131 | supporting the latest three Elixir versions.
132 |
133 | ### Changed
134 |
135 | - Switch from thread pool to being a dirty NIF. This prevents the
136 | resulting term from having to be sent between processes, and therefore
137 | prevents an extra copy from having to be performed.
138 | - In the FlatSink implementation for the NIF, track children in a pool
139 | instead of allocating new vectors for every node. This significantly
140 | reduces allocator pressure while parsing, and improves performance.
141 | - When converting a parsed FlatSink into its term representation,
142 | use a common child node stack instead of allocating a new one for every
143 | node. This significantly reduces allocator pressure while creating terms,
144 | and improves performance.
145 | - Start using LTO for the NIF compilation. This reduces the build size
146 | and improves performance.
147 |
148 | ### Fixed
149 |
150 | - Fix the target selection when using `TARGET_*` env vars on macOS.
151 |
152 | ## [0.10.1] - 2021-11-24
153 |
154 | ### Fixed
155 |
156 | - It provides a precompiled NIF for ARM 64 bits running on Linux. This
157 | is needed for Raspberry PI 4.
158 |
159 | ## [0.10.0] - 2021-11-24
160 |
161 | ### Added
162 |
163 | - Add the ability to download precompiled NIFs. We provide compiled
164 | NIF files in our GitHub releases page (from GitHub Actions) and the
165 | lib will try to download the correct NIF respecting the OS, NIF version
166 | and architecture of your build machine. This also works for Nerves
167 | projects that compiles to different targets. This way the Rust toolchain
168 | is not needed for most of people using this project.
169 |
170 | ### Fixed
171 |
172 | - Fix compilation on macOS.
173 |
174 | ## [0.9.0] - 2021-10-02
175 |
176 | ### Added
177 |
178 | - Add support for OTP 24. This was achieved by updating Rustler to v0.22.
179 |
180 | [Unreleased]: https://github.com/rusterlium/html5ever_elixir/compare/v0.16.1...HEAD
181 | [0.16.1]: https://github.com/rusterlium/html5ever_elixir/compare/v0.16.0...v0.16.1
182 | [0.16.0]: https://github.com/rusterlium/html5ever_elixir/compare/v0.15.0...v0.16.0
183 | [0.15.0]: https://github.com/rusterlium/html5ever_elixir/compare/v0.14.3...v0.15.0
184 | [0.14.3]: https://github.com/rusterlium/html5ever_elixir/compare/v0.14.2...v0.14.3
185 | [0.14.2]: https://github.com/rusterlium/html5ever_elixir/compare/v0.14.1...v0.14.2
186 | [0.14.1]: https://github.com/rusterlium/html5ever_elixir/compare/v0.14.0...v0.14.1
187 | [0.14.0]: https://github.com/rusterlium/html5ever_elixir/compare/v0.13.1...v0.14.0
188 | [0.13.1]: https://github.com/rusterlium/html5ever_elixir/compare/v0.13.0...v0.13.1
189 | [0.13.0]: https://github.com/rusterlium/html5ever_elixir/compare/v0.12.0...v0.13.0
190 | [0.12.0]: https://github.com/rusterlium/html5ever_elixir/compare/v0.11.0...v0.12.0
191 | [0.11.0]: https://github.com/rusterlium/html5ever_elixir/compare/v0.10.1...v0.11.0
192 | [0.10.1]: https://github.com/rusterlium/html5ever_elixir/compare/v0.10.0...v0.10.1
193 | [0.10.0]: https://github.com/rusterlium/html5ever_elixir/compare/v0.9.0...v0.10.0
194 | [0.9.0]: https://github.com/rusterlium/html5ever_elixir/releases/tag/v0.9.0
195 |
--------------------------------------------------------------------------------
/LICENSE-APACHE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/LICENSE-MIT:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 hansihe
2 |
3 | Permission is hereby granted, free of charge, to any
4 | person obtaining a copy of this software and associated
5 | documentation files (the "Software"), to deal in the
6 | Software without restriction, including without
7 | limitation the rights to use, copy, modify, merge,
8 | publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software
10 | is furnished to do so, subject to the following
11 | conditions:
12 |
13 | The above copyright notice and this permission notice
14 | shall be included in all copies or substantial portions
15 | of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 | DEALINGS IN THE SOFTWARE.
26 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Html5ever binding for Elixir
2 |
3 | [](https://github.com/rusterlium/html5ever_elixir/actions/workflows/ci.yml)
4 |
5 | NIF binding of [html5ever](https://github.com/servo/html5ever) using [Rustler](https://github.com/rusterlium/rustler).
6 |
7 | It is currently functional with basic features.
8 |
9 | ## Installation
10 |
11 | The package can be installed by adding `html5ever` to your list of dependencies in `mix.exs`:
12 |
13 | ```elixir
14 | def deps do
15 | [{:html5ever, "~> 0.16.0"}]
16 | end
17 | ```
18 |
19 | Or with [`Mix.install/1`](https://hexdocs.pm/mix/Mix.html#install/2):
20 |
21 | ```elixir
22 | Mix.install([:html5ever])
23 | ```
24 |
25 | ## Forcing compilation
26 |
27 | By default **you don't need Rust installed** because the lib will try to download
28 | a precompiled NIF file. In case you want to force compilation set the
29 | `HTML5EVER_BUILD` environment variable to `true` or `1`. Alternatively you can also set the
30 | application env `:build_from_source` to `true` in order to force the build:
31 |
32 | ```elixir
33 | config :html5ever, Html5ever, build_from_source: true
34 | ```
35 |
36 | You also need to add Rustler to your dependencies when you want to force
37 | the compilation:
38 |
39 | ```elixir
40 | def deps do
41 | [
42 | {:html5ever, "~> 0.16.0"},
43 | {:rustler, ">= 0.0.0", optional: true}
44 | ]
45 | end
46 | ```
47 |
48 | ## License
49 |
50 | Licensed under either of
51 |
52 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
53 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
54 |
55 | at your option.
56 |
--------------------------------------------------------------------------------
/RELEASE_CHECKLIST.md:
--------------------------------------------------------------------------------
1 | # Release checklist
2 |
3 | In order to release a new version to Hex.pm we first need to:
4 |
5 | 1. write the changes in the `CHANGELOG.md` file
6 | 2. update the `README.md`, `CHANGELOG.md` and `mix.exs` with the new version
7 | 3. commit and create a tag for that version
8 | 4. push the changes to the repository with: `git push origin master --tags`
9 | 5. wait the CI to build all release files
10 | 6. run `HTML5EVER_BUILD=1 mix rustler_precompiled.download Html5ever.Native --all --print`
11 | 7. copy the output of the mix task and add to the release notes
12 | 8. run `mix hex.publish` and **make sure the checksum file is present**
13 | in the list of files to be published. Also make sure that the `target`
14 | directory of `native/html5ever_elixir` is **NOT** present.
15 |
16 | It's important to ensure that we publish the checksum file with the
17 | package because otherwise the users won't be able to use the lib
18 | with precompiled files. They will need to always enforce compilation.
19 |
--------------------------------------------------------------------------------
/lib/html5ever.ex:
--------------------------------------------------------------------------------
1 | defmodule Html5ever do
2 | @moduledoc """
3 | This is an HTML parser written in Rust.
4 |
5 | The project provides a NIF - Native Implemented Function.
6 | It works on top of [a parser of the same name](https://github.com/servo/html5ever)
7 | from the Servo project.
8 |
9 | By default this lib will try to use a precompiled NIF
10 | from the GitHub releases page. This way you don't need
11 | to have the Rust toolchain installed.
12 | In case no precompiled file is found and the Mix env is
13 | production then an error is raised.
14 |
15 | You can force the compilation to occur by setting the
16 | value of the `HTML5EVER_BUILD` environment variable to
17 | "true" or "1". Alternatively you can also set the application
18 | env `:build_from_source` to `true` in order to force the build:
19 |
20 | config :html5ever, Html5ever, build_from_source: true
21 |
22 | This project is possible thanks to [Rustler](https://hexdocs.pm/rustler).
23 | """
24 |
25 | @doc """
26 | Parses an HTML document from a string.
27 |
28 | This returns a list of tuples representing the HTML tree.
29 |
30 | ## Example
31 |
32 | iex> Html5ever.parse("
Hello world
")
33 | {:ok,
34 | [
35 | {:doctype, "html", "", ""},
36 | {"html", [], [{"head", [], []}, {"body", [], [{"h1", [], ["Hello world"]}]}]}
37 | ]}
38 |
39 | """
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 | ...> "
Hello world
"
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)
64 | end
65 |
66 | @doc """
67 | Parses an HTML document from a string and returns a map.
68 |
69 | The map contains the document structure.
70 |
71 | ## Example
72 |
73 | iex> Html5ever.flat_parse("