diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-04-09 11:25:01 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-04-09 11:25:01 +0300 |
commit | 3dc7ba8812a4173a3c7ca7d037d5548b5d778200 (patch) | |
tree | 2a1a6a9f156db7b565e62671f4eacf77563ba567 | |
parent | acf434dddb300e24350e2ee40c628a5d8283126f (diff) | |
download | html-page-3dc7ba8812a4173a3c7ca7d037d5548b5d778200.tar.gz |
feat: add convenience functions for creating elements with children
Sponsored-by: author
-rw-r--r-- | src/lib.rs | 20 |
1 files changed, 15 insertions, 5 deletions
@@ -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); |