Add Element::into_document
It's more convenient in some cases and also makes the example code less "misleading": Now the string represents a full, correct HTML document.
This commit is contained in:
parent
b377ee2936
commit
6f0ae129fa
4 changed files with 20 additions and 10 deletions
|
|
@ -19,6 +19,7 @@ A dependency update to an incompatible version is considered a breaking change.
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- `Element::into_document`
|
||||||
- Eponymous JS helper function in readme
|
- Eponymous JS helper function in readme
|
||||||
|
|
||||||
## v0.1.0 - 2024-12-02
|
## v0.1.0 - 2024-12-02
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ let page: String = html((
|
||||||
p(("This is an example for a ", em("simple"), " web page.")),
|
p(("This is an example for a ", em("simple"), " web page.")),
|
||||||
)),
|
)),
|
||||||
))
|
))
|
||||||
|
.into_document()
|
||||||
.render_to_string()
|
.render_to_string()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -239,6 +239,14 @@ impl Element {
|
||||||
self.add(c);
|
self.add(c);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert this element into a [`Document`].
|
||||||
|
///
|
||||||
|
/// This function is equivalent to calling `self.into()` but may be more
|
||||||
|
/// convenient in some cases.
|
||||||
|
pub fn into_document(self) -> Document {
|
||||||
|
self.into()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A component can add itself to an [`Element`] by modifying it.
|
/// A component can add itself to an [`Element`] by modifying it.
|
||||||
|
|
|
||||||
20
src/lib.rs
20
src/lib.rs
|
|
@ -49,6 +49,7 @@
|
||||||
//! p(("This is an example for a ", em("simple"), " web page.")),
|
//! p(("This is an example for a ", em("simple"), " web page.")),
|
||||||
//! )),
|
//! )),
|
||||||
//! ))
|
//! ))
|
||||||
|
//! .into_document()
|
||||||
//! .render_to_string()
|
//! .render_to_string()
|
||||||
//! .unwrap();
|
//! .unwrap();
|
||||||
//! ```
|
//! ```
|
||||||
|
|
@ -80,21 +81,20 @@ pub use self::{element::*, render::*};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{html::*, Attr, Content, Element, Render};
|
use crate::{html::*, Attr, Element, Render};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_website() {
|
fn simple_website() {
|
||||||
let els = [
|
let page = html((
|
||||||
Content::doctype(),
|
head(title("Hello")),
|
||||||
html((
|
body((h1("Hello"), p(("Hello ", em("world"), "!")))),
|
||||||
head(title("Hello")),
|
))
|
||||||
body((h1("Hello"), p(("Hello ", em("world"), "!")))),
|
.into_document()
|
||||||
))
|
.render_to_string()
|
||||||
.into(),
|
.unwrap();
|
||||||
];
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
els.render_to_string().unwrap(),
|
page,
|
||||||
concat!(
|
concat!(
|
||||||
"<!DOCTYPE html><html>",
|
"<!DOCTYPE html><html>",
|
||||||
"<head><title>Hello</title></head>",
|
"<head><title>Hello</title></head>",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue