├── .gitattributes ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── bindings ├── Cargo.toml └── src │ ├── lib.rs │ ├── mem.rs │ └── option.rs ├── docs ├── .nojekyll ├── assets │ ├── highlight.css │ ├── icons.css │ ├── icons.png │ ├── icons@2x.png │ ├── main.js │ ├── search.js │ ├── style.css │ ├── widgets.png │ └── widgets@2x.png ├── classes │ ├── Attributes.html │ ├── ChildrenCollection.html │ ├── Collection.html │ ├── CollectionIter.html │ ├── Comment.html │ ├── Dom.html │ ├── GlobalNodeCollection.html │ ├── Node.html │ ├── RawTag.html │ └── Tag.html ├── enums │ └── HTMLVersion.html ├── index.html ├── interfaces │ └── ParserOptions.html └── modules.html ├── node └── lib │ ├── bindings.ts │ └── index.ts ├── package-lock.json ├── package.json ├── test ├── dom.js ├── example.js ├── parse.js ├── runner.js └── test.html ├── tsconfig.json └── typedoc.json /.gitattributes: -------------------------------------------------------------------------------- 1 | test/* linguist-vendored 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bindings/target 2 | bindings/Cargo.lock 3 | node/dist 4 | node/test 5 | node_modules 6 | __test__ 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bindings 2 | node_modules 3 | docs 4 | typedoc.json 5 | __test__ 6 | test 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Timo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tljs 2 | A [high performance](#benchmark) HTML5 parser for JavaScript. 3 | 4 | This library wraps the Rust crate [tl](https://github.com/y21/tl) and exposes its interface to JavaScript. 5 | 6 | ## When To Use 7 | This library can *very quickly* parse *very large* HTML documents. However, this library is not suitable for every use case. 8 | In particular, if you find yourself having to do lots of operations on the nodes, this may not be for you, due to the overhead of calling into WebAssembly. 9 | So, use this library if: 10 | 11 | - Most of the time is likely spent parsing documents. 12 | - Not a lot of operations are done on the nodes. 13 | - You need to parse *large* documents (tens to hundreds of megabytes) 14 | 15 | In any case, you should benchmark this library for your specific use case, and see if you benefit from the fast parsing speeds, or if the WebAssembly overhead is a bottleneck. 16 | 17 | ## How To Use 18 | ```js 19 | const tljs = require('@y21/tljs'); 20 | const dom = await tljs.parse(` 21 | 22 |
Hello World
24 |Hello world
'); 52 | ``` 53 | It doesn't matter *how* you obtain the WebAssembly binary, but you'll need to return an `ArrayBuffer` from the initializer callback (can also be a promise resolving to an `ArrayBuffer`). 54 | 55 | ## Benchmark 56 | ``` 57 | tl : 0.863912 ms/file ± 0.528114 58 | htmlparser2 : 2.02348 ms/file ± 3.05865 59 | html5parser : 2.20736 ms/file ± 2.66850 60 | htmlparser2-dom : 2.70631 ms/file ± 3.40642 61 | html-dom-parser : 2.72998 ms/file ± 3.56091 62 | neutron-html5parser: 2.74419 ms/file ± 1.52848 63 | node-html-parser : 2.89545 ms/file ± 1.80618 64 | libxmljs : 4.20240 ms/file ± 2.99146 65 | zeed-dom : 4.82065 ms/file ± 2.86533 66 | htmljs-parser : 5.97658 ms/file ± 6.65908 67 | parse5 : 6.85238 ms/file ± 7.75122 68 | arijs-stream : 18.7410 ms/file ± 18.6447 69 | arijs-tree : 20.6841 ms/file ± 19.4813 70 | htmlparser : 21.8427 ms/file ± 154.758 71 | html-parser : 27.3543 ms/file ± 20.7064 72 | saxes : 58.8234 ms/file ± 167.164 73 | html5 : 109.685 ms/file ± 146.399 74 | ``` 75 | Benchmarked against real world data using [AndreasMadsen/htmlparser-benchmark](https://github.com/AndreasMadsen/htmlparser-benchmark). 76 | 77 | *Note: This benchmark only measures raw HTML parsing, not DOM interaction.* 78 | -------------------------------------------------------------------------------- /bindings/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bindings" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | 7 | [dependencies] 8 | tl = { version = "0.7.5", features = ["simd"] } 9 | 10 | [lib] 11 | crate-type = ["cdylib"] 12 | -------------------------------------------------------------------------------- /bindings/src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::{ffi::CString, mem::ManuallyDrop}; 2 | 3 | use mem::ExternalString; 4 | use option::FFIOption; 5 | use tl::NodeHandle; 6 | 7 | mod mem; 8 | mod option; 9 | 10 | type Dom = tl::VDom<'static>; 11 | 12 | #[no_mangle] 13 | pub unsafe extern "C" fn tl_parse(ptr: *const u8, len: usize, opts: u8) -> *mut Dom { 14 | let options = tl::ParserOptions::from_raw_checked(opts).unwrap(); 15 | 16 | let slice = std::slice::from_raw_parts(ptr, len); 17 | let input = std::str::from_utf8_unchecked(slice); 18 | let dom = tl::parse(input, options).expect("WASM strings cannot exceed u32::MAX"); 19 | 20 | Box::into_raw(Box::new(dom)) 21 | } 22 | 23 | #[no_mangle] 24 | pub unsafe extern "C" fn tl_dom_nodes_count(ptr: *mut Dom) -> usize { 25 | (*ptr).nodes().len() 26 | } 27 | 28 | #[no_mangle] 29 | pub unsafe extern "C" fn tl_dom_version(ptr: *mut Dom) -> tl::HTMLVersion { 30 | (*ptr) 31 | .version() 32 | .unwrap_or(tl::HTMLVersion::TransitionalHTML401) 33 | } 34 | 35 | #[no_mangle] 36 | pub unsafe extern "C" fn tl_dom_get_element_by_id( 37 | dom_ptr: *mut Dom, 38 | str_ptr: *mut u8, 39 | str_len: usize, 40 | ) -> *mut FFIOptionReturns the number of attributes
5 |Looks up an attribute by key
7 |Inserts a key-value pair into this attributes storage
9 |Removes a key-value pair
11 |Generated using TypeDoc
A collection of subnodes of a particular node, or the DOM.
3 |Returns the node at the given index.
5 |Returns the number of elements in this collection
7 |Copies all elements of this external collection to an array.
9 |Generated using TypeDoc
A base class for collections
3 |Returns the number of elements in this collection
5 |Copies all elements of this external collection to an array.
7 |Generated using TypeDoc
An iterator over elements in an external collection
3 |Generated using TypeDoc
Attempts to downcast this node handle to an HTML comment ().
3 |Attempts to downcast this node handle to a raw HTML node (text).
5 |Attempts to downcast this node handle to a concrete HTML tag. 7 | Some operations are only valid on HTML tags.
8 |Returns the inner HTML of this node.
10 |Returns the inner text of this node.
12 |Generated using TypeDoc
A collection of global nodes
3 |Returns the node at the given index
5 |Returns the number of elements in this collection
7 |Copies all elements of this external collection to an array.
9 |Generated using TypeDoc
A handle to a node in the DOM tree.
3 |Attempts to downcast this node handle to an HTML comment ().
5 |Attempts to downcast this node handle to a raw HTML node (text).
7 |Attempts to downcast this node handle to a concrete HTML tag. 9 | Some operations are only valid on HTML tags.
10 |Returns the inner HTML of this node.
12 |Returns the inner text of this node.
14 |Generated using TypeDoc
Attempts to downcast this node handle to an HTML comment ().
3 |Attempts to downcast this node handle to a raw HTML node (text).
5 |Attempts to downcast this node handle to a concrete HTML tag. 7 | Some operations are only valid on HTML tags.
8 |Returns the inner HTML of this node.
10 |Returns the inner text of this node.
12 |Generated using TypeDoc
Generated using TypeDoc
A high performance HTML5 parser for JavaScript.
6 |This library wraps the Rust crate tl and exposes its interface to JavaScript.
7 | 8 | 9 |This library can very quickly parse very large HTML documents. However, this library is not suitable for every use case. 12 | In particular, if you find yourself having to do lots of operations on the nodes, this may not be for you, due to the overhead of calling into WebAssembly. 13 | So, use this library if:
14 |In any case, you should benchmark this library for your specific use case, and see if you benefit from the fast parsing speeds, or if the WebAssembly overhead is a bottleneck.
20 | 21 | 22 |const tljs = require('@y21/tljs');
const dom = await tljs.parse(`
<!DOCTYPE html>
<div>
<p id="greeting">Hello World</p>
<img id="img" src="image.png" />
</div>
`);
console.log(dom.getElementById('img').asTag().attributes().get('src')); // image.png
console.log(dom.getElementById('greeting').asTag().innerText()); // Hello World
console.log(dom.querySelector('p#greeting').asTag().innerText()); // Hello World
console.log(dom.version() === tljs.HTMLVersion.HTML5); // true
25 |
26 |
27 |
28 | This parser does not fully follow the HTML standard, however it is expected to be able to parse most "sane" HTML. 31 | This greatly impacts performance and should be taken into consideration when comparing performance of different HTML parsers. 32 | Not being bound to a spec enables a lot more optimization opportunities.
33 | 34 | 35 |It's possible to use this library in very "restricted" JavaScript environments (for example no access to the file system or network). By default, this library assumes it's running under Node.js and attempts to load the .wasm
binary needed to call into Rust code using require('fs').readFile
.
If you want to use this library in the browser or other environments, you need to override the default WebAssembly loading mechanism. This depends on your setup, but one way to achieve this would be to host the .wasm
binary elsewhere (maybe serve it from your webserver) and use fetch()
to get the binary.
const tljs = require('@y21/tljs');
// override the wasm loading function
tljs.setInitializerCallback(() => {
return fetch('/tl.wasm').then(x => x.arrayBuffer()); // assuming `/tl.wasm` serves the binary.
});
tljs.parse('<p>Hello world</p>');
40 |
41 | It doesn't matter how you obtain the WebAssembly binary, but you'll need to return an ArrayBuffer
from the initializer callback (can also be a promise resolving to an ArrayBuffer
).
tl : 0.863912 ms/file ± 0.528114
htmlparser2 : 2.02348 ms/file ± 3.05865
html5parser : 2.20736 ms/file ± 2.66850
htmlparser2-dom : 2.70631 ms/file ± 3.40642
html-dom-parser : 2.72998 ms/file ± 3.56091
neutron-html5parser: 2.74419 ms/file ± 1.52848
node-html-parser : 2.89545 ms/file ± 1.80618
libxmljs : 4.20240 ms/file ± 2.99146
zeed-dom : 4.82065 ms/file ± 2.86533
htmljs-parser : 5.97658 ms/file ± 6.65908
parse5 : 6.85238 ms/file ± 7.75122
arijs-stream : 18.7410 ms/file ± 18.6447
arijs-tree : 20.6841 ms/file ± 19.4813
htmlparser : 21.8427 ms/file ± 154.758
html-parser : 27.3543 ms/file ± 20.7064
saxes : 58.8234 ms/file ± 167.164
html5 : 109.685 ms/file ± 146.399
47 |
48 | Benchmarked against real world data using AndreasMadsen/htmlparser-benchmark.
49 |Note: This benchmark only measures raw HTML parsing, not DOM interaction.
50 |Generated using TypeDoc
Options to use for the HTML parser. 3 | The default options are optimized for raw parsing speed.
4 |Enables tracking of HTML Tag class names.
6 |The parser will cache tags during parsing on the fly.
7 | Enabling this makes getElementsByClassName()
lookups ~O(1),
8 | at the cost of a lot of hashing.
9 | Default: false
Enables tracking of HTML Tag IDs.
12 |The parser will cache tags during parsing on the fly.
13 | Enabling this makes getElementById()
lookups ~O(1).
14 | Default: false
Generated using TypeDoc
Hello World
9 |
HTML Tag Attributes
3 |