summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2023-04-10 08:33:53 +0300
committerLars Wirzenius <liw@liw.fi>2023-04-10 08:33:53 +0300
commitfddc66ed17d4e63e2c08037b7d223c8811b05dfa (patch)
treec73af224f90e5695382fd0cb3a79298c8cf42682
parent847b1874c73fe7da0327c4b5e254937c072d8f55 (diff)
downloadhtml-page-fddc66ed17d4e63e2c08037b7d223c8811b05dfa.tar.gz
docs: add an example to the top level crate documentation
Sponsored-by: author
-rw-r--r--src/lib.rs27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 0a5c405..8df262e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,6 +6,18 @@
//!
//! This crate aims to follow the WhatWG specification at
//! <https://html.spec.whatwg.org/>.
+//!
+//! # Example
+//!
+//! ~~~
+//! use html_page::{Element, Tag};
+//!
+//! let e = Element::new(Tag::P)
+//! .with_text("hello ")
+//! .with_child(Element::new(Tag::Strong).with_text("world"));
+//! assert_eq!(e.serialize(), "<P>hello <STRONG>world</STRONG></P>");
+//! assert_eq!(e.plain_text(), "hello world");
+//! ~~~
#![deny(missing_docs)]
@@ -641,9 +653,22 @@ pub trait Visitor {
}
/// A visitor to extract the text of an element and its children.
+///
+/// This does not include attributes or their values.
+///
+/// Note that you can call [`Element::plain_text`] for simplicity.
+///
+/// ~~~
+/// use html_page::{Element, Tag, TextVisitor, Visitor};
+/// let e = Element::new(Tag::P).with_text("hello, there");
+/// let mut tv = TextVisitor::default();
+/// tv.visit(&e);
+/// assert_eq!(tv.text, "hello, there");
+/// ~~~
#[derive(Debug, Default)]
pub struct TextVisitor {
- text: String,
+ /// The text collected by the visitor.
+ pub text: String,
}
impl Visitor for TextVisitor {