diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-04-10 08:33:53 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-04-10 08:33:53 +0300 |
commit | fddc66ed17d4e63e2c08037b7d223c8811b05dfa (patch) | |
tree | c73af224f90e5695382fd0cb3a79298c8cf42682 | |
parent | 847b1874c73fe7da0327c4b5e254937c072d8f55 (diff) | |
download | html-page-fddc66ed17d4e63e2c08037b7d223c8811b05dfa.tar.gz |
docs: add an example to the top level crate documentation
Sponsored-by: author
-rw-r--r-- | src/lib.rs | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -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 { |