summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2023-04-09 17:44:06 +0300
committerLars Wirzenius <liw@liw.fi>2023-04-09 17:44:06 +0300
commitd45c2bc8b797a906cb630d322fb32f4f5a2f0699 (patch)
tree29df4a04e3aaadd5e28506c5c8e3c504a9742725
parent78a98c99f900613f349f9d8be82183ff2bf42e05 (diff)
downloadhtml-page-d45c2bc8b797a906cb630d322fb32f4f5a2f0699.tar.gz
feat: allow raw HTML
Sponsored-by: author
-rw-r--r--src/lib.rs22
1 files changed, 19 insertions, 3 deletions
diff --git a/src/lib.rs b/src/lib.rs
index eacba45..d207b77 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -475,6 +475,12 @@ impl Element {
self.children.push(Content::element(child));
}
+ /// Append HTML to element. It will NOT be escaped, when the
+ /// element is serialized.
+ pub fn push_html(&mut self, html: &str) {
+ self.children.push(Content::html(html));
+ }
+
/// Serialize an element into HTML.
pub fn serialize(&self) -> String {
format!("{}", self)
@@ -503,6 +509,8 @@ enum Content {
Text(String),
/// An HTML element.
Element(Element),
+ /// HTML text.
+ Html(String),
}
impl Content {
@@ -515,6 +523,11 @@ impl Content {
pub fn element(e: &Element) -> Self {
Self::Element(e.clone())
}
+
+ /// Create a new [`Content::Html`].
+ pub fn html(s: &str) -> Self {
+ Self::Html(s.into())
+ }
}
impl Display for Content {
@@ -522,6 +535,7 @@ impl Display for Content {
match self {
Self::Text(s) => write!(f, "{}", encode_safe(s))?,
Self::Element(e) => write!(f, "{}", e)?,
+ Self::Html(s) => write!(f, "{}", s)?,
}
Ok(())
}
@@ -565,18 +579,20 @@ impl Display for Content {
/// ~~~
pub trait Visitor {
/// Visit an element.
- fn visit_element(&mut self, e: &Element);
+ fn visit_element(&mut self, _: &Element) {}
/// Visit non-HTML text content.
- fn visit_text(&mut self, s: &str);
+ fn visit_text(&mut self, _: &str) {}
+ /// Visit literal HTML content.
+ fn visit_html(&mut self, _: &str) {}
/// Visit recursively an element and each of its children.
fn visit(&mut self, root: &Element) {
- eprintln!("root: {:?}", root);
self.visit_element(root);
for child in &root.children {
match child {
Content::Text(s) => self.visit_text(s),
Content::Element(e) => self.visit(e),
+ Content::Html(s) => self.visit_html(s),
}
}
}