### Parsing HTML and Getting Element by ID (Rust) Source: https://github.com/y21/tl/blob/master/README.md Demonstrates parsing an HTML string using `tl::parse` and retrieving an element by its ID using `get_element_by_id`. It then accesses the element's inner text. ```Rust let dom = tl::parse(r#"

Hello

"#, tl::ParserOptions::default()).unwrap(); let parser = dom.parser(); let element = dom.get_element_by_id("text") .expect("Failed to find element") .get(parser) .unwrap(); assert_eq!(element.inner_text(parser), "Hello"); ``` -------------------------------- ### Mutating Anchor Tag href Attribute (Rust) Source: https://github.com/y21/tl/blob/master/README.md Demonstrates how to modify an attribute of a tag in the DOM. It finds an anchor tag using a query selector, gets a mutable reference to it, and updates its `href` attribute value. ```Rust let input = r#"
About
"#; let mut dom = tl::parse(input, tl::ParserOptions::default()) .expect("HTML string too long"); let anchor = dom.query_selector("a[href]") .expect("Failed to parse query selector") .next() .expect("Failed to find anchor tag"); let parser_mut = dom.parser_mut(); let anchor = anchor.get_mut(parser_mut) .expect("Failed to resolve node") .as_tag_mut() .expect("Failed to cast Node to HTMLTag"); let attributes = anchor.attributes_mut(); attributes.get_mut("href") .flatten() .expect("Attribute not found or malformed") .set("http://localhost/about"); assert_eq!(attributes.get("href").flatten(), Some(&"http://localhost/about".into())); ``` -------------------------------- ### Finding Tag Using Query Selector (Rust) Source: https://github.com/y21/tl/blob/master/README.md Shows how to use the CSS query selector API (`query_selector`) to find a specific tag (an `` tag with a `src` attribute) within the parsed DOM. ```Rust let dom = tl::parse(r#"
"#, tl::ParserOptions::default()).unwrap(); let img = dom.query_selector("img[src]").unwrap().next(); assert!(img.is_some()); ``` -------------------------------- ### Adding tl Dependency (Cargo.toml) Source: https://github.com/y21/tl/blob/master/README.md Add the `tl` crate to your project's dependencies in the `Cargo.toml` file. You can specify the version or enable the `simd` feature for SIMD-accelerated parsing (requires a nightly compiler). ```TOML [dependencies] tl = "0.7.8" # or, with explicit SIMD support # (requires a nightly compiler!) # tl = { version = "0.7.8", features = ["simd"] } ``` -------------------------------- ### Iterating Over Subnodes (Rust) Source: https://github.com/y21/tl/blob/master/README.md Illustrates how to iterate over the direct subnodes of the parsed HTML document using `dom.nodes().iter()` and find a specific tag (an `` tag) by checking node type and tag name. ```Rust let dom = tl::parse(r#"
"#, tl::ParserOptions::default()).unwrap(); let img = dom.nodes() .iter() .find(|node| { node.as_tag().map_or(false, |tag| tag.name() == "img") }); assert!(img.is_some()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.