Skip to content

Commit

Permalink
add html function
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Oct 31, 2024
1 parent 5942c09 commit 6f683b7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Escape HTML strings
* `id` and `class` attributes
* Nodes
* Html document (`head` and `body`)
* Html document (`html`, `head`, `body`)
* Meta (`title`)
* Text (`h1`, `h2`, `h3`, `h4`, `h5`, `h6` and `text`)
* Container (`div`)
* `Node`, `Attribute` and `Document` types
Expand Down
4 changes: 4 additions & 0 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ pub fn attr(key: &'static str, value: impl Into<Cow<'static, str>>) -> Attribute
Attribute::new(key, value)
}

pub fn lang(lang: impl Into<Cow<'static, str>>) -> Attribute {
attr("lang", lang)
}

pub fn id(id: impl Into<Cow<'static, str>>) -> Attribute {
attr("id", id)
}
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@ impl Attribute {
}
}
}

pub fn html(
attributes: impl IntoIterator<Item = Attribute>,
children: impl IntoIterator<Item = Node>,
) -> Document {
Document(Node::new("html", attributes, children))
}
7 changes: 7 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ pub fn head(
Node::new("head", attributes, children)
}

pub fn title(
attributes: impl IntoIterator<Item = Attribute>,
children: impl IntoIterator<Item = Node>,
) -> Node {
Node::new("title", attributes, children)
}

pub fn body(
attributes: impl IntoIterator<Item = Attribute>,
children: impl IntoIterator<Item = Node>,
Expand Down
15 changes: 14 additions & 1 deletion tests/render_spec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rstest::rstest;

use fun_html::prelude::*;
use fun_html::{html, prelude::*};

#[test]
fn should_render_empty_document() {
Expand All @@ -12,6 +12,18 @@ fn should_render_empty_document() {
)
}

#[test]
fn should_render_html_document() {
let doc = html(
[lang("en")],
[
head([], [title([], [text("greeting")])]),
body([], [h1([], [text("Hello world!")])]),
],
);
assert_eq!(doc.to_string(), "<!DOCTYPE html>\n<html lang=\"en\"><head><title>greeting</title></head><body><h1>Hello world!</h1></body></html>");
}

#[test]
fn should_render_attributes() {
let node = h1(
Expand All @@ -28,6 +40,7 @@ fn should_render_attributes() {
#[case(div([attr("foo", "bar")], [text("hello")]), "<div foo=\"bar\">hello</div>")]
#[case(div([attr("foo", "bar".to_string())], [text("hello".to_string())]), "<div foo=\"bar\">hello</div>")]
#[case(head([id("foo")], [text("hello")]), "<head id=\"foo\">hello</head>")]
#[case(title([attr("foo", "bar")], [text("hello")]), "<title foo=\"bar\">hello</title>")]
#[case(body([id("foo")], [text("hello")]), "<body id=\"foo\">hello</body>")]
#[case(h1([id("foo")], [text("hello")]), "<h1 id=\"foo\">hello</h1>")]
#[case(h2([id("foo")], [text("hello")]), "<h2 id=\"foo\">hello</h2>")]
Expand Down

0 comments on commit 6f683b7

Please sign in to comment.