Skip to main content

serialize_node

Function serialize_node 

Source
pub fn serialize_node(doc: &Document, id: NodeId, buf: &mut String)
Expand description

Serializes a DOM node and its subtree to HTML.

This function recursively serializes an element, its attributes, and all descendant nodes to an HTML string. The output is appended to the provided buffer.

§Serialization Rules

  • Elements: Serialized as <name attrs>children</name> or <name attrs> for void elements
  • Text nodes: Content is HTML-escaped using escape_text
  • Comments: Serialized as <!--content-->
  • Attributes: Values are HTML-escaped using escape_attr

§Examples

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

let soup = Soup::parse("<div class=\"test\"><span>Hello</span></div>");
let doc = soup.document();
let div_id = soup.find("div").unwrap().unwrap().node_id();

let mut html = String::new();
serialize_node(doc, div_id, &mut html);
assert!(html.contains("<div"));
assert!(html.contains("</div>"));