Skip to main content

HtmlSerializer

Trait HtmlSerializer 

Source
pub trait HtmlSerializer {
    // Required methods
    fn serialize_html_into(&self, buf: &mut String);
    fn serialize_inner_into(&self, buf: &mut String);
    fn extract_text_into(&self, buf: &mut String);

    // Provided methods
    fn serialize_html(&self) -> String { ... }
    fn serialize_inner(&self) -> String { ... }
    fn extract_text(&self) -> String { ... }
}
Expand description

Trait for types that can be serialized to HTML.

This trait provides a unified interface for HTML serialization operations. It is implemented for Tag to enable consistent serialization across the library and bindings.

§Design Rationale

The trait uses buffer-based methods (_into suffix) as the primitive operations, with convenience methods that allocate and return String. This enables zero-allocation usage in performance-critical paths while providing ergonomic APIs for common use cases.

§Examples

use scrape_core::{Soup, serialize::HtmlSerializer};

let soup = Soup::parse("<div><span>Hello</span></div>");
let div = soup.find("div").unwrap().unwrap();

// Convenience method (allocates)
let html = div.serialize_html();
assert!(html.contains("<span>"));

// Buffer method (no allocation if buffer has capacity)
let mut buf = String::with_capacity(100);
div.serialize_html_into(&mut buf);
assert_eq!(html, buf);

Required Methods§

Source

fn serialize_html_into(&self, buf: &mut String)

Serializes this node to HTML, appending to the provided buffer.

Source

fn serialize_inner_into(&self, buf: &mut String)

Serializes children to HTML, appending to the provided buffer.

Source

fn extract_text_into(&self, buf: &mut String)

Extracts text content, appending to the provided buffer.

Provided Methods§

Source

fn serialize_html(&self) -> String

Serializes this node and its subtree to HTML.

This is the outer HTML including the node’s own tags.

Source

fn serialize_inner(&self) -> String

Serializes only the children of this node to HTML.

This is the inner HTML excluding the node’s own tags.

Source

fn extract_text(&self) -> String

Extracts text content from this node and its descendants.

HTML tags are stripped; only text node content is included.

Implementors§