Improve element building API
This commit is contained in:
parent
c4b3a279cc
commit
a6fd12510d
5 changed files with 154 additions and 92 deletions
65
src/lib.rs
65
src/lib.rs
|
|
@ -16,20 +16,17 @@ pub use self::{element::*, render::*};
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{html::*, Content, Element, Render};
|
||||
use crate::{html::*, Attr, Content, Element, Render};
|
||||
|
||||
#[test]
|
||||
fn simple_website() {
|
||||
let els = [
|
||||
Content::doctype(),
|
||||
html()
|
||||
.child(head().child(title().child("Hello")))
|
||||
.child(
|
||||
body()
|
||||
.child(h1().child("Hello"))
|
||||
.child(p().child("Hello ").child(em().child("world")).child("!")),
|
||||
)
|
||||
.into(),
|
||||
html((
|
||||
head(title("Hello")),
|
||||
body((h1("Hello"), p(("Hello ", em("world"), "!")))),
|
||||
))
|
||||
.into(),
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -46,35 +43,27 @@ mod tests {
|
|||
#[test]
|
||||
fn void_elements() {
|
||||
// Difference between void and non-void
|
||||
assert_eq!(head().render_to_string().unwrap(), "<head></head>");
|
||||
assert_eq!(input().render_to_string().unwrap(), "<input>");
|
||||
assert_eq!(head(()).render_to_string().unwrap(), "<head></head>");
|
||||
assert_eq!(input(()).render_to_string().unwrap(), "<input>");
|
||||
|
||||
// Void elements must not contain any children
|
||||
assert!(input().child(p()).render_to_string().is_err());
|
||||
assert!(input(p(())).render_to_string().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_text_elements() {
|
||||
assert_eq!(
|
||||
script()
|
||||
.child("foo <script> & </style> bar")
|
||||
script("foo <script> & </style> bar")
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
"<script>foo <script> & </style> bar</script>",
|
||||
);
|
||||
|
||||
println!(
|
||||
"{:?}",
|
||||
script().child("hello </script> world").render_to_string(),
|
||||
);
|
||||
println!("{:?}", script("hello </script> world").render_to_string(),);
|
||||
|
||||
assert!(script()
|
||||
.child("hello </script> world")
|
||||
.render_to_string()
|
||||
.is_err());
|
||||
assert!(script("hello </script> world").render_to_string().is_err());
|
||||
|
||||
assert!(script()
|
||||
.child("hello </ScRiPt ... world")
|
||||
assert!(script("hello </ScRiPt ... world")
|
||||
.render_to_string()
|
||||
.is_err());
|
||||
}
|
||||
|
|
@ -82,33 +71,29 @@ mod tests {
|
|||
#[test]
|
||||
fn escaped_text_elements() {
|
||||
assert_eq!(
|
||||
textarea()
|
||||
.child("foo <p> & bar")
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
textarea("foo <p> & bar").render_to_string().unwrap(),
|
||||
"<textarea>foo <p> & bar</textarea>",
|
||||
);
|
||||
|
||||
assert!(textarea().child(p()).render_to_string().is_err());
|
||||
assert!(textarea(p(())).render_to_string().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attributes() {
|
||||
assert_eq!(
|
||||
input()
|
||||
.attr("name", "tentacles")
|
||||
.attr("type", "number")
|
||||
.attr("min", 10)
|
||||
.attr("max", 100)
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
input((
|
||||
Attr::new("name", "tentacles"),
|
||||
Attr::new("type", "number"),
|
||||
Attr::new("min", 10),
|
||||
Attr::new("max", 100),
|
||||
))
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
r#"<input max="100" min="10" name="tentacles" type="number">"#,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
input()
|
||||
.attr("name", "horns")
|
||||
.attr_true("checked")
|
||||
input((Attr::new("name", "horns"), Attr::yes("checked")))
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
r#"<input checked name="horns">"#,
|
||||
|
|
@ -119,7 +104,7 @@ mod tests {
|
|||
fn always_lowercase() {
|
||||
assert_eq!(
|
||||
Element::normal("HTML")
|
||||
.attr("LANG", "EN")
|
||||
.with(Attr::new("LANG", "EN"))
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
r#"<html lang="EN"></html>"#,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue