| 1 |
//! Serializes the [`Node`] tree to an HTML string. |
| 2 |
|
| 3 |
use super::tree::is_void_element; |
| 4 |
use super::tree::{Element, Node}; |
| 5 |
use ego_tree::{Tree, iter::Edge}; |
| 6 |
use html5ever::{local_name, ns}; |
| 7 |
use mdbook_core::utils::{escape_html, escape_html_attribute}; |
| 8 |
use std::ops::Deref; |
| 9 |
|
| 10 |
/// Serializes the given tree of [`Node`] elements to an HTML string. |
| 11 |
pub(crate) fn serialize(tree: &Tree<Node>, output: &mut String) { |
| 12 |
for edge in tree.root().traverse() { |
| 13 |
match edge { |
| 14 |
Edge::Open(node) => match node.value() { |
| 15 |
Node::Element(el) => serialize_start(el, output), |
| 16 |
Node::Text(text) => { |
| 17 |
output.push_str(&escape_html(text)); |
| 18 |
} |
| 19 |
Node::Comment(comment) => { |
| 20 |
output.push_str("<!--"); |
| 21 |
output.push_str(comment); |
| 22 |
output.push_str("-->"); |
| 23 |
} |
| 24 |
Node::Fragment => {} |
| 25 |
Node::RawData(html) => { |
| 26 |
output.push_str(html); |
| 27 |
} |
| 28 |
}, |
| 29 |
Edge::Close(node) => { |
| 30 |
if let Node::Element(el) = node.value() { |
| 31 |
serialize_end(el, output); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/// Returns true if this HTML element wants a newline to keep the emitted |
| 39 |
/// output more readable. |
| 40 |
fn wants_pretty_html_newline(name: &str) -> bool { |
| 41 |
matches!(name, |"blockquote"| "dd" |
| 42 |
| "div" |
| 43 |
| "dl" |
| 44 |
| "dt" |
| 45 |
| "h1" |
| 46 |
| "h2" |
| 47 |
| "h3" |
| 48 |
| "h4" |
| 49 |
| "h5" |
| 50 |
| "h6" |
| 51 |
| "hr" |
| 52 |
| "li" |
| 53 |
| "ol" |
| 54 |
| "p" |
| 55 |
| "pre" |
| 56 |
| "table" |
| 57 |
| "tbody" |
| 58 |
| "thead" |
| 59 |
| "tr" |
| 60 |
| "ul") |
| 61 |
} |
| 62 |
|
| 63 |
/// Emit the start tag of an element. |
| 64 |
fn serialize_start(el: &Element, output: &mut String) { |
| 65 |
let el_name = el.name(); |
| 66 |
if wants_pretty_html_newline(el_name) { |
| 67 |
if !output.is_empty() { |
| 68 |
if !output.ends_with('\n') { |
| 69 |
output.push('\n'); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
output.push('<'); |
| 74 |
output.push_str(el_name); |
| 75 |
for (attr_name, value) in &el.attrs { |
| 76 |
output.push(' '); |
| 77 |
match attr_name.ns { |
| 78 |
ns!() => (), |
| 79 |
ns!(xml) => output.push_str("xml:"), |
| 80 |
ns!(xmlns) => { |
| 81 |
if el.name.local != local_name!("xmlns") { |
| 82 |
output.push_str("xmlns:"); |
| 83 |
} |
| 84 |
} |
| 85 |
ns!(xlink) => output.push_str("xlink:"), |
| 86 |
_ => (), // TODO what should it do here? |
| 87 |
} |
| 88 |
output.push_str(attr_name.local.deref()); |
| 89 |
output.push_str("=\""); |
| 90 |
output.push_str(&escape_html_attribute(&value)); |
| 91 |
output.push('"'); |
| 92 |
} |
| 93 |
if el.self_closing { |
| 94 |
output.push_str(" /"); |
| 95 |
} |
| 96 |
output.push('>'); |
| 97 |
} |
| 98 |
|
| 99 |
/// Emit the end tag of an element. |
| 100 |
fn serialize_end(el: &Element, output: &mut String) { |
| 101 |
// Void elements do not have an end tag. |
| 102 |
if el.self_closing || is_void_element(el.name()) { |
| 103 |
return; |
| 104 |
} |
| 105 |
let name = el.name(); |
| 106 |
output.push_str("</"); |
| 107 |
output.push_str(name); |
| 108 |
output.push('>'); |
| 109 |
if wants_pretty_html_newline(name) { |
| 110 |
output.push('\n'); |
| 111 |
} |
| 112 |
} |