summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2023-04-09 11:25:01 +0300
committerLars Wirzenius <liw@liw.fi>2023-04-09 11:25:01 +0300
commit3dc7ba8812a4173a3c7ca7d037d5548b5d778200 (patch)
tree2a1a6a9f156db7b565e62671f4eacf77563ba567
parentacf434dddb300e24350e2ee40c628a5d8283126f (diff)
downloadhtml-page-3dc7ba8812a4173a3c7ca7d037d5548b5d778200.tar.gz
feat: add convenience functions for creating elements with children
Sponsored-by: author
-rw-r--r--src/lib.rs20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index d6a7027..a5e61b7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -227,6 +227,18 @@ impl Element {
self
}
+ /// Append a child element, when constructing.
+ pub fn with_child(mut self, child: Element) -> Self {
+ self.children.push(Content::Element(child));
+ self
+ }
+
+ /// Append a text child, when constructing.
+ pub fn with_text(mut self, child: &str) -> Self {
+ self.children.push(Content::Text(child.into()));
+ self
+ }
+
/// Return the [`Tag`] of the element.
pub fn tag(&self) -> Tag {
self.tag
@@ -517,11 +529,9 @@ mod test {
#[test]
fn visits_all_children() {
- let mut e = Element::new(Tag::P);
- e.push_text("hello ");
- let mut world = Element::new(Tag::B);
- world.push_text("world");
- e.push_child(&world);
+ let e = Element::new(Tag::P)
+ .with_text("hello ")
+ .with_child(Element::new(Tag::B).with_text("world"));
let mut collector = Collector::default();
collector.visit(&e);