#html5ever #minify #minify-html #html5 #html #extension #html-css

html5ever_ext

This is a set of unofficial extensions to the html5ever crate’s RcDom and Node structs, including a minifying HTML5 serializer and support for CSS matching

21 releases

Uses old Rust 2015

0.21.20 Dec 4, 2017
0.21.19 Dec 4, 2017
0.21.18 Nov 21, 2017

#7 in #minify-html

Download history 66/week @ 2024-04-01 90/week @ 2024-07-01

90 downloads per month
Used in css-purify

MIT license

120KB
3K SLoC

html5ever_ext

This is a set of unofficial extensions to the html5ever crate's RcDom and Node structs, including a minifying HTML5 serializer and support for CSS matching.

See the documentation.

Licensing

The license for this project is MIT.


lib.rs:

html5_ext

This is a set of unofficial extensions to the html5ever crate's RcDom and Node structs, including a minifying HTML5 serializer and support for CSS matching.

It re-exports the css and html5ever crates, and useful DOM types hidden inside the ::html5ever::markup5ever::rcdom module.

How Tos

To load and minify HTML5

extern crate html5_ext;
use ::html5ever_ext::RcDom;
use ::html5ever_ext::RcDomExt;
use ::html5ever_ext::Minify;

let rc_dom = RcDom::from_file_path_verified_and_stripped_of_comments_and_processing_instructions_and_with_a_sane_doc_type("/path/to/document.html").expect("invalid HTML");
rc_dom.minify_to_file_path();

There are additional methods available on Minify to minify to a byte array or a generic Write-implementing writer.

For more control, eg over serializing multiple node graphs, use the struct UltraMinifyingHtmlSerializer directly.

To match CSS selectors

extern crate html5_ext;
use ::html5ever_ext::RcDom;
use ::html5ever_ext::RcDomExt;
use ::html5ever_ext::parse_css_selector;
use ::html5ever_ext::Selectable;
use ::html5ever_ext::Minify;

let rc_dom = RcDom::from_file_path_verified_and_stripped_of_comments_and_processing_instructions_and_with_a_sane_doc_type("/path/to/document.html").expect("invalid HTML");

let selector = parse_css_selector("p.myclass").unwrap();

assert!(!rc_dom.matches(&selector));

rc_dom.find_all_matching_child_nodes_depth_first_including_this_one(&selector, |node|
{
	//Minify is implemented on node.children as well as node and rc_dom.
	eprintln!("{}", node.children.debug_string());

	const SHORTCUT: bool = false;
	SHORTCUT
})

To work with Nodes

Use the NodeExt, Minify, Selectable and QualNameExt traits.

Dependencies

~5MB
~107K SLoC