Created
June 29, 2018 19:44
-
-
Save sguzman/55a7172a80e83af3a390e9637101efb8 to your computer and use it in GitHub Desktop.
A simple example using html5ever to modify html data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate html5ever; | |
use html5ever::{ParseOpts, parse_document}; | |
use html5ever::tree_builder::TreeBuilderOpts; | |
use html5ever::rcdom::RcDom; | |
use html5ever::rcdom::NodeEnum::Element; | |
use html5ever::serialize::{SerializeOpts, serialize}; | |
use html5ever::tendril::TendrilSink; | |
fn main() { | |
let opts = ParseOpts { | |
tree_builder: TreeBuilderOpts { | |
drop_doctype: true, | |
..Default::default() | |
}, | |
..Default::default() | |
}; | |
let data = "<!DOCTYPE html><html><body><a href=\"foo\"></a></body></html>".to_string(); | |
let dom = parse_document(RcDom::default(), opts) | |
.from_utf8() | |
.read_from(&mut data.as_bytes()) | |
.unwrap(); | |
let document = dom.document.borrow(); | |
let html = document.children[0].borrow(); | |
let body = html.children[1].borrow(); // Implicit head element at children[0]. | |
{ | |
let mut a = body.children[0].borrow_mut(); | |
if let Element(_, _, ref mut attributes) = a.node { | |
attributes[0].value.push_tendril(&From::from("#anchor")); | |
} | |
} | |
let mut bytes = vec![]; | |
serialize(&mut bytes, &dom.document, SerializeOpts::default()).unwrap(); | |
let result = String::from_utf8(bytes).unwrap(); | |
println!("{}", result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from this stackoverflow answer