Editable

Trait Editable 

Source
pub trait Editable {
    // Required methods
    fn trim(self) -> Self;
    fn insert_to(&mut self, selector: &Selector, target: Node) -> &mut Self;
    fn remove_by(&mut self, selector: &Selector) -> &mut Self;
}
Expand description

Used to insert or remove elements by Selector, and trim the dom.

Required Methods§

Source

fn trim(self) -> Self

Remove all empty text nodes from self.

use html_query_parser::{parse, Editable, Htmlifiable};

let html = r#"
    <!DOCTYPE html>
    <html>
        <head></head>
        <body></body>
    </html>"#;
 
let html = parse(html).trim().html();
assert_eq!(html, r#"<!DOCTYPE html><html><head></head><body></body></html>"#)
Source

fn insert_to(&mut self, selector: &Selector, target: Node) -> &mut Self

Insert node as the last child to all elements that matches the selector.

use html_query_parser::{parse, Node, Selector, Editable, Htmlifiable};

let html = r#"<div><span>Ok</span></div>"#;
 
let selector = Selector::from("div");
let html = parse(html)
    .insert_to(&selector, Node::new_element(
        "span",
        vec![],
        vec![Node::Text("Cancel".to_string())]
    ))
    .html();
assert_eq!(html, r#"<div><span>Ok</span><span>Cancel</span></div>"#)
Source

fn remove_by(&mut self, selector: &Selector) -> &mut Self

Remove all elements that matches the selector.

use html_query_parser::{parse, Selector, Editable, Htmlifiable};

let html = r#"
<div>
    <div class="recommend"></div>
    <div class="results"></div>
    <div class="ad"></div>
</div>"#;
 
let selector = Selector::from(".ad");
let html = parse(html).remove_by(&selector).html();
assert_eq!(html, r#"
<div>
    <div class="recommend"></div>
    <div class="results"></div>
    
</div>"#)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Editable for Vec<Node>

Source§

fn trim(self) -> Self

Source§

fn insert_to(&mut self, selector: &Selector, target: Node) -> &mut Self

Source§

fn remove_by(&mut self, selector: &Selector) -> &mut Self

Implementors§